xref: /core/svx/source/stbctrls/selctrl.cxx (revision aafc733e)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <vcl/builder.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/menu.hxx>
23 #include <vcl/status.hxx>
24 #include <svl/intitem.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <tools/urlobj.hxx>
27 
28 #include <svx/selctrl.hxx>
29 
30 #include "stbctrls.h"
31 #include <bitmaps.hlst>
32 
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 
35 SFX_IMPL_STATUSBAR_CONTROL(SvxSelectionModeControl, SfxUInt16Item);
36 
37 /// Popup menu to select the selection type
38 class SelectionTypePopup
39 {
40     VclBuilder        m_aBuilder;
41     VclPtr<PopupMenu> m_xMenu;
42     static sal_uInt16 id_to_state(const OString& rIdent);
43     sal_uInt16 state_to_id(sal_uInt16 nState) const;
44 public:
45     explicit SelectionTypePopup(sal_uInt16 nCurrent);
46     OUString GetItemTextForState(sal_uInt16 nState) { return m_xMenu->GetItemText(state_to_id(nState)); }
47     sal_uInt16 GetState() const { return id_to_state(m_xMenu->GetCurItemIdent()); }
48     sal_uInt16 Execute(vcl::Window* pWindow, const Point& rPopupPos) { return m_xMenu->Execute(pWindow, rPopupPos); }
49 };
50 
51 sal_uInt16 SelectionTypePopup::id_to_state(const OString& rIdent)
52 {
53     if (rIdent == "block")
54         return 3;
55     else if (rIdent == "adding")
56         return 2;
57     else if (rIdent == "extending")
58         return 1;
59     else // fall through
60         return 0;
61 }
62 
63 sal_uInt16 SelectionTypePopup::state_to_id(sal_uInt16 nState) const
64 {
65     switch (nState)
66     {
67         default: // fall through
68         case 0: return m_xMenu->GetItemId("standard");
69         case 1: return m_xMenu->GetItemId("extending");
70         case 2: return m_xMenu->GetItemId("adding");
71         case 3: return m_xMenu->GetItemId("block");
72     }
73 }
74 
75 SelectionTypePopup::SelectionTypePopup(sal_uInt16 nCurrent)
76     : m_aBuilder(nullptr, VclBuilderContainer::getUIRootDir(), "svx/ui/selectionmenu.ui", "")
77     , m_xMenu(m_aBuilder.get_menu("menu"))
78 {
79     m_xMenu->CheckItem(state_to_id(nCurrent));
80 }
81 
82 SvxSelectionModeControl::SvxSelectionModeControl( sal_uInt16 _nSlotId,
83                                                   sal_uInt16 _nId,
84                                                   StatusBar& rStb ) :
85     SfxStatusBarControl( _nSlotId, _nId, rStb ),
86     mnState( 0 ),
87     maImage(StockImage::Yes, RID_SVXBMP_SELECTION)
88 {
89     GetStatusBar().SetItemText( GetId(), "" );
90 }
91 
92 void SvxSelectionModeControl::StateChanged( sal_uInt16, SfxItemState eState,
93                                             const SfxPoolItem* pState )
94 {
95     if ( SfxItemState::DEFAULT == eState )
96     {
97         DBG_ASSERT( dynamic_cast< const SfxUInt16Item* >(pState) !=  nullptr, "invalid item type" );
98         const SfxUInt16Item* pItem = static_cast<const SfxUInt16Item*>(pState);
99         mnState = pItem->GetValue();
100 
101         SelectionTypePopup aPop(mnState);
102         GetStatusBar().SetQuickHelpText(GetId(), aPop.GetItemTextForState(mnState));
103     }
104 }
105 
106 bool SvxSelectionModeControl::MouseButtonDown( const MouseEvent& rEvt )
107 {
108     SelectionTypePopup aPop(mnState);
109     StatusBar& rStatusbar = GetStatusBar();
110 
111     if (rEvt.IsMiddle() && aPop.Execute(&rStatusbar, rEvt.GetPosPixel()))
112     {
113         sal_uInt16 nNewState = aPop.GetState();
114         if ( nNewState != mnState )
115         {
116             mnState = nNewState;
117 
118             css::uno::Any a;
119             SfxUInt16Item aState( GetSlotId(), mnState );
120             INetURLObject aObj( m_aCommandURL );
121 
122             css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
123             aArgs[0].Name  = aObj.GetURLPath();
124             aState.QueryValue( a );
125             aArgs[0].Value = a;
126 
127             execute( aArgs );
128         }
129     }
130 
131     return true;
132 }
133 
134 
135 void SvxSelectionModeControl::Paint( const UserDrawEvent& rUsrEvt )
136 {
137     const tools::Rectangle aControlRect = getControlRect();
138     vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
139     tools::Rectangle aRect = rUsrEvt.GetRect();
140 
141     Size aImgSize( maImage.GetSizePixel() );
142 
143     Point aPos( aRect.Left() + ( aControlRect.GetWidth() - aImgSize.Width() ) / 2,
144             aRect.Top() + ( aControlRect.GetHeight() - aImgSize.Height() ) / 2 );
145 
146     pDev->DrawImage( aPos, maImage );
147 }
148 
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
150