xref: /AOO41X/main/framework/source/uielement/comboboxtoolbarcontroller.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 #ifndef __FRAMEWORK_UIELEMENT_COMBOBOXTOOLBARCONTROLLER_HXX
32 #include "uielement/comboboxtoolbarcontroller.hxx"
33 #endif
34 
35 //_________________________________________________________________________________________________________________
36 //	my own includes
37 //_________________________________________________________________________________________________________________
38 
39 #ifndef __FRAMEWORK_TOOLBAR_HXX_
40 #include "uielement/toolbar.hxx"
41 #endif
42 
43 //_________________________________________________________________________________________________________________
44 //	interface includes
45 //_________________________________________________________________________________________________________________
46 #include <com/sun/star/util/XURLTransformer.hpp>
47 #include <com/sun/star/frame/XDispatchProvider.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/frame/status/ItemStatus.hpp>
50 #include <com/sun/star/frame/status/ItemState.hpp>
51 #include <com/sun/star/frame/status/Visibility.hpp>
52 #include <com/sun/star/frame/XControlNotificationListener.hpp>
53 #include <com/sun/star/util/Color.hpp>
54 
55 //_________________________________________________________________________________________________________________
56 //	other includes
57 //_________________________________________________________________________________________________________________
58 #include <svtools/toolboxcontroller.hxx>
59 #include <vos/mutex.hxx>
60 #include <vcl/svapp.hxx>
61 #ifndef _VCL_MNEMONIC_HXX_
62 #include <vcl/mnemonic.hxx>
63 #endif
64 #include <vcl/toolbox.hxx>
65 #include <vcl/combobox.hxx>
66 #include <tools/urlobj.hxx>
67 
68 using namespace ::com::sun::star;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::beans;
71 using namespace ::com::sun::star::lang;
72 using namespace ::com::sun::star::frame;
73 using namespace ::com::sun::star::frame::status;
74 using namespace ::com::sun::star::util;
75 
76 namespace framework
77 {
78 
79 // ------------------------------------------------------------------
80 
81 // Wrapper class to notify controller about events from combobox.
82 // Unfortunaltly the events are notifed through virtual methods instead
83 // of Listeners.
84 
85 class ComboBoxControl : public ComboBox
86 {
87     public:
88         ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener );
89         virtual ~ComboBoxControl();
90 
91         virtual void Select();
92         virtual void DoubleClick();
93         virtual void Modify();
94         virtual void KeyInput( const ::KeyEvent& rKEvt );
95 	    virtual void GetFocus();
96 	    virtual void LoseFocus();
97         virtual long PreNotify( NotifyEvent& rNEvt );
98 
99     private:
100         IComboBoxListener* m_pComboBoxListener;
101 };
102 
103 ComboBoxControl::ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener ) :
104     ComboBox( pParent, nStyle )
105     , m_pComboBoxListener( pComboBoxListener )
106 {
107 }
108 
109 ComboBoxControl::~ComboBoxControl()
110 {
111     m_pComboBoxListener = 0;
112 }
113 
114 void ComboBoxControl::Select()
115 {
116     ComboBox::Select();
117     if ( m_pComboBoxListener )
118         m_pComboBoxListener->Select();
119 }
120 
121 void ComboBoxControl::DoubleClick()
122 {
123     ComboBox::DoubleClick();
124     if ( m_pComboBoxListener )
125         m_pComboBoxListener->DoubleClick();
126 }
127 
128 void ComboBoxControl::Modify()
129 {
130     ComboBox::Modify();
131     if ( m_pComboBoxListener )
132         m_pComboBoxListener->Modify();
133 }
134 
135 void ComboBoxControl::KeyInput( const ::KeyEvent& rKEvt )
136 {
137     ComboBox::KeyInput( rKEvt );
138     if ( m_pComboBoxListener )
139         m_pComboBoxListener->KeyInput( rKEvt );
140 }
141 
142 void ComboBoxControl::GetFocus()
143 {
144     ComboBox::GetFocus();
145     if ( m_pComboBoxListener )
146         m_pComboBoxListener->GetFocus();
147 }
148 
149 void ComboBoxControl::LoseFocus()
150 {
151     ComboBox::LoseFocus();
152     if ( m_pComboBoxListener )
153         m_pComboBoxListener->LoseFocus();
154 }
155 
156 long ComboBoxControl::PreNotify( NotifyEvent& rNEvt )
157 {
158     long nRet( 0 );
159     if ( m_pComboBoxListener )
160         nRet = m_pComboBoxListener->PreNotify( rNEvt );
161     if ( nRet == 0 )
162         nRet = ComboBox::PreNotify( rNEvt );
163 
164     return nRet;
165 }
166 
167 // ------------------------------------------------------------------
168 
169 ComboboxToolbarController::ComboboxToolbarController(
170     const Reference< XMultiServiceFactory >& rServiceManager,
171     const Reference< XFrame >&               rFrame,
172     ToolBox*                                 pToolbar,
173     sal_uInt16                                   nID,
174     sal_Int32                                nWidth,
175     const ::rtl::OUString&                          aCommand ) :
176     ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
177     ,   m_pComboBox( 0 )
178 {
179     m_pComboBox = new ComboBoxControl( m_pToolbar, WB_DROPDOWN, this );
180     if ( nWidth == 0 )
181         nWidth = 100;
182 
183     // default dropdown size
184     ::Size aLogicalSize( 8, 160 );
185     ::Size aPixelSize = m_pComboBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
186 
187     m_pComboBox->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
188     m_pToolbar->SetItemWindow( m_nID, m_pComboBox );
189 }
190 
191 // ------------------------------------------------------------------
192 
193 ComboboxToolbarController::~ComboboxToolbarController()
194 {
195 }
196 
197 // ------------------------------------------------------------------
198 
199 void SAL_CALL ComboboxToolbarController::dispose()
200 throw ( RuntimeException )
201 {
202     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
203 
204     m_pToolbar->SetItemWindow( m_nID, 0 );
205     delete m_pComboBox;
206 
207     ComplexToolbarController::dispose();
208 
209     m_pComboBox = 0;
210 }
211 
212 // ------------------------------------------------------------------
213 Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
214 {
215     Sequence<PropertyValue> aArgs( 2 );
216     ::rtl::OUString aSelectedText = m_pComboBox->GetText();
217 
218     // Add key modifier to argument list
219     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
220     aArgs[0].Value <<= KeyModifier;
221     aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
222     aArgs[1].Value <<= aSelectedText;
223     return aArgs;
224 }
225 
226 // ------------------------------------------------------------------
227 
228 void ComboboxToolbarController::Select()
229 {
230     if ( m_pComboBox->GetEntryCount() > 0 )
231     {
232         Window::PointerState aState = m_pComboBox->GetPointerState();
233 
234         sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
235         execute( nKeyModifier );
236     }
237 }
238 
239 void ComboboxToolbarController::DoubleClick()
240 {
241 }
242 
243 void ComboboxToolbarController::Modify()
244 {
245     notifyTextChanged( m_pComboBox->GetText() );
246 }
247 
248 void ComboboxToolbarController::KeyInput( const ::KeyEvent& )
249 {
250 }
251 
252 void ComboboxToolbarController::GetFocus()
253 {
254     notifyFocusGet();
255 }
256 
257 void ComboboxToolbarController::LoseFocus()
258 {
259     notifyFocusLost();
260 }
261 
262 long ComboboxToolbarController::PreNotify( NotifyEvent& rNEvt )
263 {
264     switch ( rNEvt.GetType() )
265     {
266         case EVENT_KEYINPUT :
267             {
268                 const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
269                 const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
270                 if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
271                 {
272                     // Call execute only with non-empty text
273                     if ( m_pComboBox->GetText().Len() > 0 )
274                         execute( rKeyCode.GetModifier() );
275                     return 1;
276                 }
277             }
278             break;
279         case EVENT_GETFOCUS :
280             notifyFocusGet();
281             break;
282         case EVENT_LOSEFOCUS :
283             notifyFocusLost();
284             break;
285         default :
286             break;
287     }
288     return 0;
289 }
290 
291 // --------------------------------------------------------
292 
293 void ComboboxToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
294 {
295     if ( rControlCommand.Command.equalsAsciiL( "SetText", 7 ))
296     {
297         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
298         {
299             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
300             {
301                 rtl::OUString aText;
302                 rControlCommand.Arguments[i].Value >>= aText;
303                 m_pComboBox->SetText( aText );
304 
305                 // send notification
306                 notifyTextChanged( aText );
307                 break;
308             }
309         }
310     }
311     else if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
312     {
313         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
314         {
315             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
316             {
317                 Sequence< ::rtl::OUString > aList;
318                 m_pComboBox->Clear();
319 
320                 rControlCommand.Arguments[i].Value >>= aList;
321                 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
322                     m_pComboBox->InsertEntry( aList[j] );
323 
324                 // send notification
325                 uno::Sequence< beans::NamedValue > aInfo( 1 );
326                 aInfo[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
327                 aInfo[0].Value <<= aList;
328                 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
329                                getDispatchFromCommand( m_aCommandURL ),
330                                aInfo );
331 
332                 break;
333             }
334         }
335     }
336     else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
337     {
338         sal_uInt16      nPos( COMBOBOX_APPEND );
339         rtl::OUString   aText;
340         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
341         {
342             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
343             {
344                 if ( rControlCommand.Arguments[i].Value >>= aText )
345                     m_pComboBox->InsertEntry( aText, nPos );
346                 break;
347             }
348         }
349     }
350     else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
351     {
352         sal_uInt16      nPos( COMBOBOX_APPEND );
353         rtl::OUString   aText;
354         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
355         {
356             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
357             {
358                 sal_Int32 nTmpPos = 0;
359                 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
360                 {
361                     if (( nTmpPos >= 0 ) &&
362                         ( nTmpPos < sal_Int32( m_pComboBox->GetEntryCount() )))
363                         nPos = sal_uInt16( nTmpPos );
364                 }
365             }
366             else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
367                 rControlCommand.Arguments[i].Value >>= aText;
368         }
369 
370         m_pComboBox->InsertEntry( aText, nPos );
371     }
372     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
373     {
374         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
375         {
376             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
377             {
378                 sal_Int32 nPos( -1 );
379                 if ( rControlCommand.Arguments[i].Value >>= nPos )
380                 {
381                     if ( nPos < sal_Int32( m_pComboBox->GetEntryCount() ))
382                         m_pComboBox->RemoveEntry( sal_uInt16( nPos ));
383                 }
384                 break;
385             }
386         }
387     }
388     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
389     {
390         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
391         {
392             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
393             {
394                 rtl::OUString aText;
395                 if ( rControlCommand.Arguments[i].Value >>= aText )
396                     m_pComboBox->RemoveEntry( aText );
397                 break;
398             }
399         }
400     }
401     else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
402     {
403         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
404         {
405             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
406             {
407                 sal_Int32 nValue( 5 );
408                 rControlCommand.Arguments[i].Value >>= nValue;
409                 m_pComboBox->SetDropDownLineCount( sal_uInt16( nValue ));
410                 break;
411             }
412         }
413     }
414     else if ( rControlCommand.Command.equalsAsciiL( "SetBackgroundColor", 18 ))
415     {
416         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
417         {
418             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
419             {
420                 com::sun::star::util::Color aColor(0);
421                 if ( rControlCommand.Arguments[i].Value >>= aColor )
422                 {
423                     ::Color aBackColor( static_cast< sal_uInt32 >( aColor ));
424                     m_pComboBox->SetControlBackground( aBackColor );
425                 }
426                 break;
427             }
428         }
429     }
430     else if ( rControlCommand.Command.equalsAsciiL( "SetTextColor", 12 ))
431     {
432         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
433         {
434             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
435             {
436                 com::sun::star::util::Color aColor(0);
437                 if ( rControlCommand.Arguments[i].Value >>= aColor )
438                 {
439                     ::Color aForeColor( static_cast< sal_uInt32 >( aColor ));
440                     m_pComboBox->SetControlForeground( aForeColor );
441                 }
442                 break;
443             }
444         }
445     }
446 }
447 
448 } // namespace
449 
450