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 : #include "taborder.hxx"
21 : #include "taborder.hrc"
22 :
23 : #include "modulepcr.hxx"
24 : #include "formresid.hrc"
25 : #include "formstrings.hxx"
26 : #include <comphelper/types.hxx>
27 : #include <comphelper/property.hxx>
28 : #include <cppuhelper/implbase1.hxx>
29 : #include <com/sun/star/form/FormComponentType.hpp>
30 : #include <com/sun/star/awt/XTabController.hpp>
31 : #include <vcl/scrbar.hxx>
32 : #include "svtools/treelistentry.hxx"
33 :
34 : //............................................................................
35 : namespace pcr
36 : {
37 : //............................................................................
38 :
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::com::sun::star::awt;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::com::sun::star::form;
43 : using namespace ::com::sun::star::beans;
44 : using namespace ::com::sun::star::datatransfer;
45 :
46 : //========================================================================
47 : //= OSimpleTabModel
48 : //========================================================================
49 0 : class OSimpleTabModel : public ::cppu::WeakImplHelper1< XTabControllerModel>
50 : {
51 : Sequence< Reference< XControlModel > > m_aModels;
52 :
53 : public:
54 0 : OSimpleTabModel( const Sequence< Reference< XControlModel > >& _rModels )
55 0 : :m_aModels( _rModels )
56 : {
57 0 : }
58 :
59 : // XTabControllerModel
60 0 : virtual void SAL_CALL setControlModels(const Sequence< Reference< XControlModel > >& rModels) throw( RuntimeException ) {m_aModels = rModels;}
61 0 : virtual Sequence< Reference< XControlModel > > SAL_CALL getControlModels(void) throw( RuntimeException ) {return m_aModels;}
62 0 : virtual void SAL_CALL setGroup(const Sequence< Reference< XControlModel > >& /*Group*/, const ::rtl::OUString& /*GroupName*/) throw( RuntimeException ) {}
63 0 : virtual sal_Int32 SAL_CALL getGroupCount(void) throw( RuntimeException ) {return 0;}
64 0 : virtual void SAL_CALL getGroup(sal_Int32 /*nGroup*/, Sequence< Reference< XControlModel > >& /*Group*/, ::rtl::OUString& /*Name*/) throw( RuntimeException ) {}
65 0 : virtual void SAL_CALL getGroupByName(const ::rtl::OUString& /*Name*/, Sequence< Reference< XControlModel > >& /*Group*/) throw( RuntimeException ) {}
66 0 : virtual sal_Bool SAL_CALL getGroupControl(void) throw( RuntimeException ){return sal_False;} ;
67 0 : virtual void SAL_CALL setGroupControl(sal_Bool /*GroupControl*/) throw( RuntimeException ){};
68 : };
69 :
70 : //========================================================================
71 : //= TabOrderDialog
72 : //========================================================================
73 : DBG_NAME(TabOrderDialog)
74 : //------------------------------------------------------------------------
75 0 : TabOrderDialog::TabOrderDialog( Window* _pParent, const Reference< XTabControllerModel >& _rxTabModel,
76 : const Reference< XControlContainer >& _rxControlCont, const Reference< XMultiServiceFactory >& _rxORB )
77 : :ModalDialog( _pParent, PcrRes( RID_DLG_TABORDER ) )
78 : ,m_xModel( _rxTabModel )
79 : ,m_xControlContainer( _rxControlCont )
80 : ,m_xORB( _rxORB )
81 : ,aFT_Controls( this, PcrRes( FT_CONTROLS ) )
82 : ,aLB_Controls( this, PcrRes( CTRL_TREE ) )
83 : ,aPB_OK( this, PcrRes( PB_OK ) )
84 : ,aPB_CANCEL( this, PcrRes( PB_CANCEL ) )
85 : ,aPB_HELP( this, PcrRes( PB_HELP ) )
86 : ,aPB_MoveUp( this, PcrRes( PB_MOVE_UP ) )
87 : ,aPB_MoveDown( this, PcrRes( PB_MOVE_DOWN ) )
88 : ,aPB_AutoOrder( this, PcrRes( PB_AUTO_ORDER ) )
89 0 : ,pImageList( NULL )
90 : {
91 : DBG_CTOR(TabOrderDialog,NULL);
92 :
93 0 : aPB_MoveUp.SetClickHdl( LINK( this, TabOrderDialog, MoveUpClickHdl ) );
94 0 : aPB_MoveDown.SetClickHdl( LINK( this, TabOrderDialog, MoveDownClickHdl ) );
95 0 : aPB_AutoOrder.SetClickHdl( LINK( this, TabOrderDialog, AutoOrderClickHdl ) );
96 0 : aPB_OK.SetClickHdl( LINK( this, TabOrderDialog, OKClickHdl ) );
97 0 : aPB_OK.Disable();
98 :
99 0 : pImageList = new ImageList( PcrRes( RID_IL_FORMEXPLORER ) );
100 :
101 0 : if ( m_xModel.is() )
102 0 : m_xTempModel = new OSimpleTabModel( m_xModel->getControlModels() );
103 :
104 0 : if ( m_xTempModel.is() && m_xControlContainer.is() )
105 0 : FillList();
106 :
107 0 : if ( aLB_Controls.GetEntryCount() < 2 )
108 : {
109 0 : aPB_MoveUp.Disable();
110 0 : aPB_MoveDown.Disable();
111 0 : aPB_AutoOrder.Disable();
112 : }
113 :
114 0 : FreeResource();
115 0 : }
116 :
117 : //------------------------------------------------------------------------
118 0 : void TabOrderDialog::SetModified()
119 : {
120 0 : aPB_OK.Enable();
121 0 : }
122 :
123 : //------------------------------------------------------------------------
124 0 : TabOrderDialog::~TabOrderDialog()
125 : {
126 0 : aLB_Controls.Hide();
127 : // delete pLB_Controls;
128 0 : delete pImageList;
129 :
130 : DBG_DTOR(TabOrderDialog,NULL);
131 0 : }
132 :
133 : //------------------------------------------------------------------------
134 0 : Image TabOrderDialog::GetImage( const Reference< XPropertySet >& _rxSet ) const
135 : {
136 0 : sal_uInt16 nImageId = RID_SVXIMG_CONTROL;
137 : // TODO: classify controls also in Basic propbrw
138 0 : if ( _rxSet.is() && ::comphelper::hasProperty( PROPERTY_CLASSID, _rxSet ) )
139 : {
140 0 : switch( ::comphelper::getINT16( _rxSet->getPropertyValue( PROPERTY_CLASSID ) ) )
141 : {
142 0 : case FormComponentType::COMMANDBUTTON: nImageId = RID_SVXIMG_BUTTON; break;
143 0 : case FormComponentType::FIXEDTEXT: nImageId = RID_SVXIMG_FIXEDTEXT; break;
144 0 : case FormComponentType::TEXTFIELD: nImageId = RID_SVXIMG_EDIT; break;
145 0 : case FormComponentType::RADIOBUTTON: nImageId = RID_SVXIMG_RADIOBUTTON; break;
146 0 : case FormComponentType::CHECKBOX: nImageId = RID_SVXIMG_CHECKBOX; break;
147 0 : case FormComponentType::LISTBOX: nImageId = RID_SVXIMG_LISTBOX; break;
148 0 : case FormComponentType::COMBOBOX: nImageId = RID_SVXIMG_COMBOBOX; break;
149 0 : case FormComponentType::GROUPBOX: nImageId = RID_SVXIMG_GROUPBOX; break;
150 0 : case FormComponentType::IMAGEBUTTON: nImageId = RID_SVXIMG_IMAGEBUTTON; break;
151 0 : case FormComponentType::FILECONTROL: nImageId = RID_SVXIMG_FILECONTROL; break;
152 0 : case FormComponentType::HIDDENCONTROL: nImageId = RID_SVXIMG_HIDDEN; break;
153 0 : case FormComponentType::DATEFIELD: nImageId = RID_SVXIMG_DATEFIELD; break;
154 0 : case FormComponentType::TIMEFIELD: nImageId = RID_SVXIMG_TIMEFIELD; break;
155 0 : case FormComponentType::NUMERICFIELD: nImageId = RID_SVXIMG_NUMERICFIELD; break;
156 0 : case FormComponentType::CURRENCYFIELD: nImageId = RID_SVXIMG_CURRENCYFIELD; break;
157 0 : case FormComponentType::PATTERNFIELD: nImageId = RID_SVXIMG_PATTERNFIELD; break;
158 0 : case FormComponentType::IMAGECONTROL: nImageId = RID_SVXIMG_IMAGECONTROL; break;
159 0 : case FormComponentType::GRIDCONTROL: nImageId = RID_SVXIMG_GRID; break;
160 0 : case FormComponentType::SCROLLBAR: nImageId = RID_SVXIMG_SCROLLBAR; break;
161 0 : case FormComponentType::SPINBUTTON: nImageId = RID_SVXIMG_SPINBUTTON; break;
162 0 : case FormComponentType::NAVIGATIONBAR: nImageId = RID_SVXIMG_NAVIGATIONBAR; break;
163 : default:
164 : OSL_FAIL( "TabOrderDialog::GetImage: unknown control type" );
165 : }
166 : }
167 :
168 0 : return pImageList->GetImage( nImageId );
169 : }
170 :
171 : //------------------------------------------------------------------------
172 0 : void TabOrderDialog::FillList()
173 : {
174 : DBG_ASSERT( m_xTempModel.is() && m_xControlContainer.is(), "TabOrderDialog::FillList: invalid call!" );
175 0 : if ( !m_xTempModel.is() || !m_xControlContainer.is() )
176 0 : return;
177 :
178 0 : aLB_Controls.Clear();
179 :
180 : try
181 : {
182 0 : Sequence< Reference< XControlModel > > aControlModels( m_xTempModel->getControlModels() );
183 0 : const Reference< XControlModel >* pControlModels = aControlModels.getConstArray();
184 :
185 0 : ::rtl::OUString aName;
186 0 : Image aImage;
187 :
188 0 : for ( sal_Int32 i=0; i < aControlModels.getLength(); ++i, ++pControlModels )
189 : {
190 0 : Reference< XPropertySet > xControl( *pControlModels, UNO_QUERY );
191 0 : Reference< XPropertySetInfo > xPI;
192 0 : if ( xControl.is() )
193 0 : xPI = xControl->getPropertySetInfo();
194 :
195 0 : if ( xPI.is() )
196 : {
197 0 : if ( xPI->hasPropertyByName( PROPERTY_TABSTOP ) )
198 : {
199 0 : aName = ::comphelper::getString( xControl->getPropertyValue( PROPERTY_NAME ) );
200 : // TODO: do Basic controls have a name?
201 0 : aImage = GetImage( xControl );
202 0 : aLB_Controls.InsertEntry( aName, aImage, aImage, 0, sal_False, LIST_APPEND, xControl.get() );
203 : }
204 : }
205 : else
206 : {
207 : // no property set -> no tab order
208 : OSL_FAIL( "TabOrderDialog::FillList: invalid control encountered!" );
209 0 : aLB_Controls.Clear();
210 : break;
211 : }
212 0 : }
213 : }
214 0 : catch( const Exception& )
215 : {
216 : OSL_FAIL( "TabOrderDialog::FillList: caught an exception!" );
217 : }
218 :
219 : // select first entry
220 0 : SvTreeListEntry* pFirstEntry = aLB_Controls.GetEntry( 0 );
221 0 : if ( pFirstEntry )
222 0 : aLB_Controls.Select( pFirstEntry );
223 : }
224 :
225 : //------------------------------------------------------------------------
226 0 : IMPL_LINK( TabOrderDialog, MoveUpClickHdl, Button*, /*pButton*/ )
227 : {
228 0 : aLB_Controls.MoveSelection( -1 );
229 0 : return 0;
230 : }
231 :
232 : //------------------------------------------------------------------------
233 0 : IMPL_LINK( TabOrderDialog, MoveDownClickHdl, Button*, /*pButton*/ )
234 : {
235 0 : aLB_Controls.MoveSelection( 1 );
236 0 : return 0;
237 : }
238 :
239 : //------------------------------------------------------------------------
240 0 : IMPL_LINK( TabOrderDialog, AutoOrderClickHdl, Button*, /*pButton*/ )
241 : {
242 : try
243 : {
244 0 : Reference< XTabController > xTabController;
245 0 : if ( m_xORB.is() )
246 0 : xTabController = xTabController.query( m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.FormController" ) ) ) );
247 : DBG_ASSERT( xTabController.is(), "TabOrderDialog::AutoOrderClickHdl: could not instantiate a tab controller!" );
248 0 : if ( !xTabController.is() )
249 0 : return 0;
250 :
251 0 : xTabController->setModel( m_xTempModel );
252 0 : xTabController->setContainer( m_xControlContainer );
253 0 : xTabController->autoTabOrder();
254 :
255 0 : SetModified();
256 0 : FillList();
257 :
258 0 : ::comphelper::disposeComponent( xTabController );
259 : }
260 0 : catch( const Exception& )
261 : {
262 : OSL_FAIL( "TabOrderDialog::AutoOrderClickHdl: caught an exception!" );
263 : }
264 :
265 0 : return 0;
266 : }
267 :
268 : //------------------------------------------------------------------------
269 0 : IMPL_LINK( TabOrderDialog, OKClickHdl, Button*, /*pButton*/ )
270 : {
271 0 : sal_uLong nEntryCount = aLB_Controls.GetEntryCount();
272 0 : Sequence< Reference< XControlModel > > aSortedControlModelSeq( nEntryCount );
273 0 : Sequence< Reference< XControlModel > > aControlModels( m_xTempModel->getControlModels());
274 0 : Reference< XControlModel > * pSortedControlModels = aSortedControlModelSeq.getArray();
275 0 : const Reference< XControlModel > * pControlModels = aControlModels.getConstArray();
276 :
277 0 : for (sal_uLong i=0; i < nEntryCount; i++)
278 : {
279 0 : SvTreeListEntry* pEntry = aLB_Controls.GetEntry(i);
280 :
281 0 : for( sal_Int32 j=0; j<aControlModels.getLength(); j++ )
282 : {
283 0 : Reference< XPropertySet > xSet(pControlModels[j], UNO_QUERY);
284 0 : if ((XPropertySet*)xSet.get() == ((XPropertySet*)pEntry->GetUserData()))
285 : {
286 0 : pSortedControlModels[i] = pControlModels[j];
287 : break;
288 : }
289 0 : }
290 : }
291 :
292 : // TODO: UNO action (to bracket all the single actions which are being created)
293 0 : m_xModel->setControlModels( aSortedControlModelSeq );
294 :
295 0 : EndDialog( sal_True );
296 0 : return 0;
297 : }
298 :
299 : //========================================================================
300 : //= TabOrderListBox
301 : //========================================================================
302 : DBG_NAME(TabOrderListBox);
303 : //------------------------------------------------------------------------
304 0 : TabOrderListBox::TabOrderListBox( Window* pParent, const ResId& rResId )
305 0 : :SvTreeListBox( pParent, rResId )
306 : {
307 : DBG_CTOR(TabOrderListBox,NULL);
308 0 : SetDragDropMode(0xFFFF/*SV_DRAGDROP_CTRL_MOVE*/);
309 : // Hmm. The flag alone is not enough, so to be on the safe side ...
310 :
311 0 : SetSelectionMode( MULTIPLE_SELECTION );
312 0 : }
313 :
314 : //------------------------------------------------------------------------
315 0 : TabOrderListBox::~TabOrderListBox()
316 : {
317 : DBG_DTOR(TabOrderListBox,NULL);
318 0 : }
319 :
320 : //------------------------------------------------------------------------
321 0 : void TabOrderListBox::ModelHasMoved( SvTreeListEntry* _pSource )
322 : {
323 0 : SvTreeListBox::ModelHasMoved( _pSource );
324 :
325 0 : ((TabOrderDialog*)Window::GetParent())->SetModified();
326 0 : }
327 :
328 : //------------------------------------------------------------------------
329 0 : void TabOrderListBox::MoveSelection( long nRelPos )
330 : {
331 0 : rtl::OUString aSelEntryPrevText, aSelEntryNextText;
332 0 : Image aImage;
333 0 : for (long i=0; i<labs(nRelPos); i++)
334 : {
335 0 : ((TabOrderDialog*)Window::GetParent())->SetModified();
336 :
337 : //////////////////////////////////////////////////////////////////////
338 : // move entries
339 0 : if( nRelPos < 0 )
340 : {
341 0 : SvTreeListEntry* pFirstSelected = FirstSelected();
342 0 : if( !pFirstSelected ) return;
343 0 : sal_uLong nFirstSelPos = GetModel()->GetAbsPos( pFirstSelected );
344 0 : if( nFirstSelPos == 0 ) return;
345 :
346 0 : SvTreeListEntry* pSelEntry = pFirstSelected;
347 0 : while( pSelEntry )
348 : {
349 0 : sal_uLong nSelEntryPos = GetModel()->GetAbsPos( pSelEntry );
350 0 : SvTreeListEntry* pSelEntryPrev = GetEntry( nSelEntryPos-1 );
351 0 : aSelEntryPrevText = GetEntryText( pSelEntryPrev );
352 0 : aImage = GetExpandedEntryBmp(pSelEntryPrev);
353 0 : void* pData = pSelEntryPrev->GetUserData();
354 :
355 0 : GetModel()->Remove( pSelEntryPrev );
356 0 : InsertEntry( aSelEntryPrevText, aImage, aImage, 0, sal_False, nSelEntryPos, pData );
357 :
358 0 : pSelEntry = NextSelected( pSelEntry );
359 : }
360 : }
361 :
362 0 : else if( nRelPos > 0 )
363 : {
364 0 : SvTreeListEntry* pLastSelected = LastSelected();
365 0 : if( !pLastSelected ) return;
366 0 : sal_uLong nLastSelPos = GetModel()->GetAbsPos( pLastSelected );
367 :
368 0 : if( (nLastSelPos + nRelPos - i) > (GetEntryCount()-1) ) return;
369 :
370 : #if OSL_DEBUG_LEVEL > 0
371 : sal_uLong nSelCount = GetSelectionCount();
372 : (void)nSelCount;
373 : #endif
374 :
375 :
376 0 : SvTreeListEntry* pSelEntry = pLastSelected;
377 0 : while( pSelEntry )
378 : {
379 0 : sal_uLong nSelEntryPos = GetModel()->GetAbsPos( pSelEntry );
380 0 : SvTreeListEntry* pSelEntryNext = GetEntry( nSelEntryPos+1 );
381 0 : void* pData = pSelEntryNext->GetUserData();
382 :
383 0 : aSelEntryNextText = GetEntryText( pSelEntryNext );
384 0 : aImage = GetExpandedEntryBmp(pSelEntryNext);
385 :
386 0 : GetModel()->Remove( pSelEntryNext );
387 0 : InsertEntry( aSelEntryNextText, aImage, aImage, 0, sal_False, nSelEntryPos, pData );
388 :
389 0 : pSelEntry = PrevSelected( pSelEntry );
390 : }
391 0 : long nThumbPos = GetVScroll()->GetThumbPos();
392 0 : long nVisibleSize = GetVScroll()->GetVisibleSize();
393 0 : long nFirstVisible = GetModel()->GetAbsPos( FirstVisible());
394 :
395 0 : if ( ( nThumbPos + nVisibleSize + 1 ) < (long)( nLastSelPos + 3 ) )
396 0 : GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
397 0 : else if((nThumbPos+nVisibleSize+1) >= (nFirstVisible))
398 0 : GetVScroll()->DoScrollAction(SCROLL_LINEUP);
399 : }
400 0 : }
401 : }
402 :
403 : //............................................................................
404 : } // namespace pcr
405 : //............................................................................
406 :
407 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|