Line data Source code
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 :
21 : #include "uielement/dropdownboxtoolbarcontroller.hxx"
22 :
23 : #include "uielement/toolbar.hxx"
24 :
25 : #include <com/sun/star/util/XURLTransformer.hpp>
26 : #include <com/sun/star/frame/XDispatchProvider.hpp>
27 : #include <com/sun/star/beans/PropertyValue.hpp>
28 : #include <com/sun/star/frame/status/ItemStatus.hpp>
29 : #include <com/sun/star/frame/status/ItemState.hpp>
30 : #include <com/sun/star/frame/status/Visibility.hpp>
31 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
32 :
33 : #include <svtools/toolboxcontroller.hxx>
34 : #include <osl/mutex.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <vcl/mnemonic.hxx>
37 :
38 : using namespace ::com::sun::star;
39 : using namespace ::com::sun::star::awt;
40 : using namespace ::com::sun::star::uno;
41 : using namespace ::com::sun::star::beans;
42 : using namespace ::com::sun::star::lang;
43 : using namespace ::com::sun::star::frame;
44 : using namespace ::com::sun::star::frame::status;
45 : using namespace ::com::sun::star::util;
46 :
47 : namespace framework
48 : {
49 :
50 : // ------------------------------------------------------------------
51 :
52 : // Wrapper class to notify controller about events from ListBox.
53 : // Unfortunaltly the events are notifed through virtual methods instead
54 : // of Listeners.
55 :
56 : class ListBoxControl : public ListBox
57 : {
58 : public:
59 : ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener );
60 : virtual ~ListBoxControl();
61 :
62 : virtual void Select();
63 : virtual void DoubleClick();
64 : virtual void GetFocus();
65 : virtual void LoseFocus();
66 : virtual long PreNotify( NotifyEvent& rNEvt );
67 :
68 : private:
69 : IListBoxListener* m_pListBoxListener;
70 : };
71 :
72 0 : ListBoxControl::ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener ) :
73 : ListBox( pParent, nStyle )
74 0 : , m_pListBoxListener( pListBoxListener )
75 : {
76 0 : }
77 :
78 0 : ListBoxControl::~ListBoxControl()
79 : {
80 0 : m_pListBoxListener = 0;
81 0 : }
82 :
83 0 : void ListBoxControl::Select()
84 : {
85 0 : ListBox::Select();
86 0 : if ( m_pListBoxListener )
87 0 : m_pListBoxListener->Select();
88 0 : }
89 :
90 0 : void ListBoxControl::DoubleClick()
91 : {
92 0 : ListBox::DoubleClick();
93 0 : if ( m_pListBoxListener )
94 0 : m_pListBoxListener->DoubleClick();
95 0 : }
96 :
97 0 : void ListBoxControl::GetFocus()
98 : {
99 0 : ListBox::GetFocus();
100 0 : if ( m_pListBoxListener )
101 0 : m_pListBoxListener->GetFocus();
102 0 : }
103 :
104 0 : void ListBoxControl::LoseFocus()
105 : {
106 0 : ListBox::LoseFocus();
107 0 : if ( m_pListBoxListener )
108 0 : m_pListBoxListener->LoseFocus();
109 0 : }
110 :
111 0 : long ListBoxControl::PreNotify( NotifyEvent& rNEvt )
112 : {
113 0 : long nRet( 0 );
114 0 : if ( m_pListBoxListener )
115 0 : nRet = m_pListBoxListener->PreNotify( rNEvt );
116 0 : if ( nRet == 0 )
117 0 : nRet = ListBox::PreNotify( rNEvt );
118 :
119 0 : return nRet;
120 : }
121 :
122 : // ------------------------------------------------------------------
123 :
124 0 : DropdownToolbarController::DropdownToolbarController(
125 : const Reference< XMultiServiceFactory >& rServiceManager,
126 : const Reference< XFrame >& rFrame,
127 : ToolBox* pToolbar,
128 : sal_uInt16 nID,
129 : sal_Int32 nWidth,
130 : const ::rtl::OUString& aCommand ) :
131 : ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
132 0 : , m_pListBoxControl( 0 )
133 : {
134 0 : m_pListBoxControl = new ListBoxControl( m_pToolbar, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER, this );
135 0 : if ( nWidth == 0 )
136 0 : nWidth = 100;
137 :
138 : // default dropdown size
139 0 : ::Size aLogicalSize( 0, 160 );
140 0 : ::Size aPixelSize = m_pListBoxControl->LogicToPixel( aLogicalSize, MAP_APPFONT );
141 :
142 0 : m_pListBoxControl->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
143 0 : m_pToolbar->SetItemWindow( m_nID, m_pListBoxControl );
144 0 : m_pListBoxControl->SetDropDownLineCount( 5 );
145 0 : }
146 :
147 : // ------------------------------------------------------------------
148 :
149 0 : DropdownToolbarController::~DropdownToolbarController()
150 : {
151 0 : }
152 :
153 : // ------------------------------------------------------------------
154 :
155 0 : void SAL_CALL DropdownToolbarController::dispose()
156 : throw ( RuntimeException )
157 : {
158 0 : SolarMutexGuard aSolarMutexGuard;
159 :
160 0 : m_pToolbar->SetItemWindow( m_nID, 0 );
161 0 : delete m_pListBoxControl;
162 :
163 0 : ComplexToolbarController::dispose();
164 :
165 0 : m_pListBoxControl = 0;
166 0 : }
167 :
168 : // ------------------------------------------------------------------
169 0 : Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
170 : {
171 0 : Sequence<PropertyValue> aArgs( 2 );
172 0 : ::rtl::OUString aSelectedText = m_pListBoxControl->GetSelectEntry();
173 :
174 : // Add key modifier to argument list
175 0 : aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
176 0 : aArgs[0].Value <<= KeyModifier;
177 0 : aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
178 0 : aArgs[1].Value <<= aSelectedText;
179 0 : return aArgs;
180 : }
181 :
182 : // ------------------------------------------------------------------
183 :
184 0 : void DropdownToolbarController::Select()
185 : {
186 0 : if ( m_pListBoxControl->GetEntryCount() > 0 )
187 : {
188 0 : Window::PointerState aState = m_pListBoxControl->GetPointerState();
189 :
190 0 : sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
191 0 : execute( nKeyModifier );
192 : }
193 0 : }
194 :
195 0 : void DropdownToolbarController::DoubleClick()
196 : {
197 0 : }
198 :
199 0 : void DropdownToolbarController::GetFocus()
200 : {
201 0 : notifyFocusGet();
202 0 : }
203 :
204 0 : void DropdownToolbarController::LoseFocus()
205 : {
206 0 : notifyFocusLost();
207 0 : }
208 :
209 0 : long DropdownToolbarController::PreNotify( NotifyEvent& /*rNEvt*/ )
210 : {
211 0 : return 0;
212 : }
213 :
214 : // --------------------------------------------------------
215 :
216 0 : void DropdownToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
217 : {
218 0 : if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
219 : {
220 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
221 : {
222 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
223 : {
224 0 : Sequence< ::rtl::OUString > aList;
225 0 : m_pListBoxControl->Clear();
226 :
227 0 : rControlCommand.Arguments[i].Value >>= aList;
228 0 : for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
229 0 : m_pListBoxControl->InsertEntry( aList[j] );
230 :
231 0 : m_pListBoxControl->SelectEntryPos( 0 );
232 :
233 : // send notification
234 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
235 0 : aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
236 0 : aInfo[0].Value <<= aList;
237 : addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
238 : getDispatchFromCommand( m_aCommandURL ),
239 0 : aInfo );
240 :
241 0 : break;
242 : }
243 : }
244 : }
245 0 : else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
246 : {
247 0 : sal_uInt16 nPos( LISTBOX_APPEND );
248 0 : rtl::OUString aText;
249 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
250 : {
251 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
252 : {
253 0 : if ( rControlCommand.Arguments[i].Value >>= aText )
254 0 : m_pListBoxControl->InsertEntry( aText, nPos );
255 0 : break;
256 : }
257 0 : }
258 : }
259 0 : else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
260 : {
261 0 : sal_uInt16 nPos( LISTBOX_APPEND );
262 0 : rtl::OUString aText;
263 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
264 : {
265 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
266 : {
267 0 : sal_Int32 nTmpPos = 0;
268 0 : if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
269 : {
270 0 : if (( nTmpPos >= 0 ) &&
271 0 : ( nTmpPos < sal_Int32( m_pListBoxControl->GetEntryCount() )))
272 0 : nPos = sal_uInt16( nTmpPos );
273 : }
274 : }
275 0 : else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
276 0 : rControlCommand.Arguments[i].Value >>= aText;
277 : }
278 :
279 0 : m_pListBoxControl->InsertEntry( aText, nPos );
280 : }
281 0 : else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
282 : {
283 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
284 : {
285 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
286 : {
287 0 : sal_Int32 nPos( -1 );
288 0 : if ( rControlCommand.Arguments[i].Value >>= nPos )
289 : {
290 0 : if ( nPos < sal_Int32( m_pListBoxControl->GetEntryCount() ))
291 0 : m_pListBoxControl->RemoveEntry( sal_uInt16( nPos ));
292 : }
293 : break;
294 : }
295 : }
296 : }
297 0 : else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
298 : {
299 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
300 : {
301 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
302 : {
303 0 : rtl::OUString aText;
304 0 : if ( rControlCommand.Arguments[i].Value >>= aText )
305 0 : m_pListBoxControl->RemoveEntry( aText );
306 0 : break;
307 : }
308 : }
309 : }
310 0 : else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
311 : {
312 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
313 : {
314 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
315 : {
316 0 : sal_Int32 nValue( 5 );
317 0 : rControlCommand.Arguments[i].Value >>= nValue;
318 0 : m_pListBoxControl->SetDropDownLineCount( sal_uInt16( nValue ));
319 : break;
320 : }
321 : }
322 : }
323 0 : }
324 :
325 : } // namespace
326 :
327 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|