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 // Unfortunately the events are notified 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 {
180 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
181
182 m_pToolbar->SetItemWindow( m_nID, 0 );
183 delete m_pListBoxControl;
184
185 ComplexToolbarController::dispose();
186
187 m_pListBoxControl = 0;
188 }
189
190 // ------------------------------------------------------------------
getExecuteArgs(sal_Int16 KeyModifier) const191 Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
192 {
193 Sequence<PropertyValue> aArgs( 2 );
194 ::rtl::OUString aSelectedText = m_pListBoxControl->GetSelectEntry();
195
196 // Add key modifier to argument list
197 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
198 aArgs[0].Value <<= KeyModifier;
199 aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
200 aArgs[1].Value <<= aSelectedText;
201 return aArgs;
202 }
203
204 // ------------------------------------------------------------------
205
Select()206 void DropdownToolbarController::Select()
207 {
208 if ( m_pListBoxControl->GetEntryCount() > 0 )
209 {
210 Window::PointerState aState = m_pListBoxControl->GetPointerState();
211
212 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
213 execute( nKeyModifier );
214 }
215 }
216
DoubleClick()217 void DropdownToolbarController::DoubleClick()
218 {
219 }
220
GetFocus()221 void DropdownToolbarController::GetFocus()
222 {
223 notifyFocusGet();
224 }
225
LoseFocus()226 void DropdownToolbarController::LoseFocus()
227 {
228 notifyFocusLost();
229 }
230
PreNotify(NotifyEvent &)231 long DropdownToolbarController::PreNotify( NotifyEvent& /*rNEvt*/ )
232 {
233 return 0;
234 }
235
236 // --------------------------------------------------------
237
executeControlCommand(const::com::sun::star::frame::ControlCommand & rControlCommand)238 void DropdownToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
239 {
240 if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
241 {
242 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
243 {
244 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
245 {
246 Sequence< ::rtl::OUString > aList;
247 m_pListBoxControl->Clear();
248
249 rControlCommand.Arguments[i].Value >>= aList;
250 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
251 m_pListBoxControl->InsertEntry( aList[j] );
252
253 m_pListBoxControl->SelectEntryPos( 0 );
254
255 // send notification
256 uno::Sequence< beans::NamedValue > aInfo( 1 );
257 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
258 aInfo[0].Value <<= aList;
259 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
260 getDispatchFromCommand( m_aCommandURL ),
261 aInfo );
262
263 break;
264 }
265 }
266 }
267 else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
268 {
269 sal_uInt16 nPos( LISTBOX_APPEND );
270 rtl::OUString aText;
271 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
272 {
273 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
274 {
275 if ( rControlCommand.Arguments[i].Value >>= aText )
276 m_pListBoxControl->InsertEntry( aText, nPos );
277 break;
278 }
279 }
280 }
281 else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
282 {
283 sal_uInt16 nPos( LISTBOX_APPEND );
284 rtl::OUString aText;
285 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
286 {
287 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
288 {
289 sal_Int32 nTmpPos = 0;
290 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
291 {
292 if (( nTmpPos >= 0 ) &&
293 ( nTmpPos < sal_Int32( m_pListBoxControl->GetEntryCount() )))
294 nPos = sal_uInt16( nTmpPos );
295 }
296 }
297 else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
298 rControlCommand.Arguments[i].Value >>= aText;
299 }
300
301 m_pListBoxControl->InsertEntry( aText, nPos );
302 }
303 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
304 {
305 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
306 {
307 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
308 {
309 sal_Int32 nPos( -1 );
310 if ( rControlCommand.Arguments[i].Value >>= nPos )
311 {
312 if ( nPos < sal_Int32( m_pListBoxControl->GetEntryCount() ))
313 m_pListBoxControl->RemoveEntry( sal_uInt16( nPos ));
314 }
315 break;
316 }
317 }
318 }
319 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
320 {
321 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
322 {
323 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
324 {
325 rtl::OUString aText;
326 if ( rControlCommand.Arguments[i].Value >>= aText )
327 m_pListBoxControl->RemoveEntry( aText );
328 break;
329 }
330 }
331 }
332 else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
333 {
334 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
335 {
336 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
337 {
338 sal_Int32 nValue( 5 );
339 rControlCommand.Arguments[i].Value >>= nValue;
340 m_pListBoxControl->SetDropDownLineCount( sal_uInt16( nValue ));
341 break;
342 }
343 }
344 }
345 }
346
347 } // namespace
348