1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #ifndef __FRAMEWORK_UIELEMENT_DROPDOWNBOXTOOLBARCONTROLLER_HXX
28 #include "uielement/dropdownboxtoolbarcontroller.hxx"
29 #endif
30 
31 //_________________________________________________________________________________________________________________
32 //	my own includes
33 //_________________________________________________________________________________________________________________
34 
35 #ifndef __FRAMEWORK_TOOLBAR_HXX_
36 #include "uielement/toolbar.hxx"
37 #endif
38 
39 //_________________________________________________________________________________________________________________
40 //	interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <com/sun/star/frame/XDispatchProvider.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/frame/status/ItemStatus.hpp>
46 #include <com/sun/star/frame/status/ItemState.hpp>
47 #include <com/sun/star/frame/status/Visibility.hpp>
48 #include <com/sun/star/frame/XControlNotificationListener.hpp>
49 
50 //_________________________________________________________________________________________________________________
51 //	other includes
52 //_________________________________________________________________________________________________________________
53 #include <svtools/toolboxcontroller.hxx>
54 #include <vos/mutex.hxx>
55 #include <vcl/svapp.hxx>
56 #ifndef _VCL_MNEMONIC_HXX_
57 #include <vcl/mnemonic.hxx>
58 #endif
59 #include <tools/urlobj.hxx>
60 
61 using namespace ::com::sun::star;
62 using namespace ::com::sun::star::awt;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::frame;
67 using namespace ::com::sun::star::frame::status;
68 using namespace ::com::sun::star::util;
69 
70 namespace framework
71 {
72 
73 // ------------------------------------------------------------------
74 
75 // Wrapper class to notify controller about events from ListBox.
76 // Unfortunaltly the events are notifed through virtual methods instead
77 // of Listeners.
78 
79 class ListBoxControl : public ListBox
80 {
81     public:
82         ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener );
83         virtual ~ListBoxControl();
84 
85         virtual void Select();
86         virtual void DoubleClick();
87 	    virtual void GetFocus();
88 	    virtual void LoseFocus();
89         virtual long PreNotify( NotifyEvent& rNEvt );
90 
91     private:
92         IListBoxListener* m_pListBoxListener;
93 };
94 
ListBoxControl(Window * pParent,WinBits nStyle,IListBoxListener * pListBoxListener)95 ListBoxControl::ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener ) :
96     ListBox( pParent, nStyle )
97     , m_pListBoxListener( pListBoxListener )
98 {
99 }
100 
~ListBoxControl()101 ListBoxControl::~ListBoxControl()
102 {
103     m_pListBoxListener = 0;
104 }
105 
Select()106 void ListBoxControl::Select()
107 {
108     ListBox::Select();
109     if ( m_pListBoxListener )
110         m_pListBoxListener->Select();
111 }
112 
DoubleClick()113 void ListBoxControl::DoubleClick()
114 {
115     ListBox::DoubleClick();
116     if ( m_pListBoxListener )
117         m_pListBoxListener->DoubleClick();
118 }
119 
GetFocus()120 void ListBoxControl::GetFocus()
121 {
122     ListBox::GetFocus();
123     if ( m_pListBoxListener )
124         m_pListBoxListener->GetFocus();
125 }
126 
LoseFocus()127 void ListBoxControl::LoseFocus()
128 {
129     ListBox::LoseFocus();
130     if ( m_pListBoxListener )
131         m_pListBoxListener->LoseFocus();
132 }
133 
PreNotify(NotifyEvent & rNEvt)134 long ListBoxControl::PreNotify( NotifyEvent& rNEvt )
135 {
136     long nRet( 0 );
137     if ( m_pListBoxListener )
138         nRet = m_pListBoxListener->PreNotify( rNEvt );
139     if ( nRet == 0 )
140         nRet = ListBox::PreNotify( rNEvt );
141 
142     return nRet;
143 }
144 
145 // ------------------------------------------------------------------
146 
DropdownToolbarController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & rFrame,ToolBox * pToolbar,sal_uInt16 nID,sal_Int32 nWidth,const::rtl::OUString & aCommand)147 DropdownToolbarController::DropdownToolbarController(
148     const Reference< XMultiServiceFactory >& rServiceManager,
149     const Reference< XFrame >&               rFrame,
150     ToolBox*                                 pToolbar,
151     sal_uInt16                                   nID,
152     sal_Int32                                nWidth,
153     const ::rtl::OUString&                          aCommand ) :
154     ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
155     ,   m_pListBoxControl( 0 )
156 {
157     m_pListBoxControl = new ListBoxControl( m_pToolbar, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER, this );
158     if ( nWidth == 0 )
159         nWidth = 100;
160 
161     // default dropdown size
162     ::Size aLogicalSize( 0, 160 );
163     ::Size aPixelSize = m_pListBoxControl->LogicToPixel( aLogicalSize, MAP_APPFONT );
164 
165     m_pListBoxControl->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
166     m_pToolbar->SetItemWindow( m_nID, m_pListBoxControl );
167     m_pListBoxControl->SetDropDownLineCount( 5 );
168 }
169 
170 // ------------------------------------------------------------------
171 
~DropdownToolbarController()172 DropdownToolbarController::~DropdownToolbarController()
173 {
174 }
175 
176 // ------------------------------------------------------------------
177 
dispose()178 void SAL_CALL DropdownToolbarController::dispose()
179 throw ( RuntimeException )
180 {
181     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
182 
183     m_pToolbar->SetItemWindow( m_nID, 0 );
184     delete m_pListBoxControl;
185 
186     ComplexToolbarController::dispose();
187 
188     m_pListBoxControl = 0;
189 }
190 
191 // ------------------------------------------------------------------
getExecuteArgs(sal_Int16 KeyModifier) const192 Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
193 {
194     Sequence<PropertyValue> aArgs( 2 );
195     ::rtl::OUString aSelectedText = m_pListBoxControl->GetSelectEntry();
196 
197     // Add key modifier to argument list
198     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
199     aArgs[0].Value <<= KeyModifier;
200     aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
201     aArgs[1].Value <<= aSelectedText;
202     return aArgs;
203 }
204 
205 // ------------------------------------------------------------------
206 
Select()207 void DropdownToolbarController::Select()
208 {
209     if ( m_pListBoxControl->GetEntryCount() > 0 )
210     {
211         Window::PointerState aState = m_pListBoxControl->GetPointerState();
212 
213         sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
214         execute( nKeyModifier );
215     }
216 }
217 
DoubleClick()218 void DropdownToolbarController::DoubleClick()
219 {
220 }
221 
GetFocus()222 void DropdownToolbarController::GetFocus()
223 {
224     notifyFocusGet();
225 }
226 
LoseFocus()227 void DropdownToolbarController::LoseFocus()
228 {
229     notifyFocusLost();
230 }
231 
PreNotify(NotifyEvent &)232 long DropdownToolbarController::PreNotify( NotifyEvent& /*rNEvt*/ )
233 {
234     return 0;
235 }
236 
237 // --------------------------------------------------------
238 
executeControlCommand(const::com::sun::star::frame::ControlCommand & rControlCommand)239 void DropdownToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
240 {
241     if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
242     {
243         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
244         {
245             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
246             {
247                 Sequence< ::rtl::OUString > aList;
248                 m_pListBoxControl->Clear();
249 
250                 rControlCommand.Arguments[i].Value >>= aList;
251                 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
252                     m_pListBoxControl->InsertEntry( aList[j] );
253 
254                 m_pListBoxControl->SelectEntryPos( 0 );
255 
256                 // send notification
257                 uno::Sequence< beans::NamedValue > aInfo( 1 );
258                 aInfo[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
259                 aInfo[0].Value <<= aList;
260                 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
261                                getDispatchFromCommand( m_aCommandURL ),
262                                aInfo );
263 
264                 break;
265             }
266         }
267     }
268     else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
269     {
270         sal_uInt16      nPos( LISTBOX_APPEND );
271         rtl::OUString   aText;
272         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
273         {
274             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
275             {
276                 if ( rControlCommand.Arguments[i].Value >>= aText )
277                     m_pListBoxControl->InsertEntry( aText, nPos );
278                 break;
279             }
280         }
281     }
282     else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
283     {
284         sal_uInt16      nPos( LISTBOX_APPEND );
285         rtl::OUString   aText;
286         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
287         {
288             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
289             {
290                 sal_Int32 nTmpPos = 0;
291                 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
292                 {
293                     if (( nTmpPos >= 0 ) &&
294                         ( nTmpPos < sal_Int32( m_pListBoxControl->GetEntryCount() )))
295                         nPos = sal_uInt16( nTmpPos );
296                 }
297             }
298             else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
299                 rControlCommand.Arguments[i].Value >>= aText;
300         }
301 
302         m_pListBoxControl->InsertEntry( aText, nPos );
303     }
304     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
305     {
306         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
307         {
308             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
309             {
310                 sal_Int32 nPos( -1 );
311                 if ( rControlCommand.Arguments[i].Value >>= nPos )
312                 {
313                     if ( nPos < sal_Int32( m_pListBoxControl->GetEntryCount() ))
314                         m_pListBoxControl->RemoveEntry( sal_uInt16( nPos ));
315                 }
316                 break;
317             }
318         }
319     }
320     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
321     {
322         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
323         {
324             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
325             {
326                 rtl::OUString aText;
327                 if ( rControlCommand.Arguments[i].Value >>= aText )
328                     m_pListBoxControl->RemoveEntry( aText );
329                 break;
330             }
331         }
332     }
333     else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
334     {
335         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
336         {
337             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
338             {
339                 sal_Int32 nValue( 5 );
340                 rControlCommand.Arguments[i].Value >>= nValue;
341                 m_pListBoxControl->SetDropDownLineCount( sal_uInt16( nValue ));
342                 break;
343             }
344         }
345     }
346 }
347 
348 } // namespace
349 
350