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