bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / taborder.cxx
blob08c6670a69a43dcb1fd7a4c3dcbf506ade13b69f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "taborder.hxx"
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>
34 namespace pcr
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;
44 //= OSimpleTabModel
46 class OSimpleTabModel : public ::cppu::WeakImplHelper1< XTabControllerModel>
48 Sequence< Reference< XControlModel > > m_aModels;
50 public:
51 OSimpleTabModel( const Sequence< Reference< XControlModel > >& _rModels )
52 :m_aModels( _rModels )
56 // XTabControllerModel
57 virtual void SAL_CALL setControlModels(const Sequence< Reference< XControlModel > >& rModels) throw( RuntimeException, std::exception ) SAL_OVERRIDE {m_aModels = rModels;}
58 virtual Sequence< Reference< XControlModel > > SAL_CALL getControlModels() throw( RuntimeException, std::exception ) SAL_OVERRIDE {return m_aModels;}
59 virtual void SAL_CALL setGroup(const Sequence< Reference< XControlModel > >& /*Group*/, const OUString& /*GroupName*/) throw( RuntimeException, std::exception ) SAL_OVERRIDE {}
60 virtual sal_Int32 SAL_CALL getGroupCount() throw( RuntimeException, std::exception ) SAL_OVERRIDE {return 0;}
61 virtual void SAL_CALL getGroup(sal_Int32 /*nGroup*/, Sequence< Reference< XControlModel > >& /*Group*/, OUString& /*Name*/) throw( RuntimeException, std::exception ) SAL_OVERRIDE {}
62 virtual void SAL_CALL getGroupByName(const OUString& /*Name*/, Sequence< Reference< XControlModel > >& /*Group*/) throw( RuntimeException, std::exception ) SAL_OVERRIDE {}
63 virtual sal_Bool SAL_CALL getGroupControl() throw( RuntimeException, std::exception ) SAL_OVERRIDE {return sal_False;} ;
64 virtual void SAL_CALL setGroupControl(sal_Bool /*GroupControl*/) throw( RuntimeException, std::exception ) SAL_OVERRIDE {};
68 //= TabOrderDialog
71 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 ,pImageList( NULL )
79 get(m_pLB_Controls, "CTRLtree");
80 get(m_pPB_OK, "ok");
81 get(m_pPB_MoveUp, "upB");
82 get(m_pPB_MoveDown, "downB");
83 get(m_pPB_AutoOrder, "autoB");
86 m_pPB_MoveUp->SetClickHdl( LINK( this, TabOrderDialog, MoveUpClickHdl ) );
87 m_pPB_MoveDown->SetClickHdl( LINK( this, TabOrderDialog, MoveDownClickHdl ) );
88 m_pPB_AutoOrder->SetClickHdl( LINK( this, TabOrderDialog, AutoOrderClickHdl ) );
89 m_pPB_OK->SetClickHdl( LINK( this, TabOrderDialog, OKClickHdl ) );
90 m_pPB_OK->Disable();
92 pImageList = new ImageList( PcrRes( RID_IL_FORMEXPLORER ) );
94 if ( m_xModel.is() )
95 m_xTempModel = new OSimpleTabModel( m_xModel->getControlModels() );
97 if ( m_xTempModel.is() && m_xControlContainer.is() )
98 FillList();
100 if ( m_pLB_Controls->GetEntryCount() < 2 )
102 m_pPB_MoveUp->Disable();
103 m_pPB_MoveDown->Disable();
104 m_pPB_AutoOrder->Disable();
110 void TabOrderDialog::SetModified()
112 m_pPB_OK->Enable();
116 TabOrderDialog::~TabOrderDialog()
118 disposeOnce();
121 void TabOrderDialog::dispose()
123 m_pLB_Controls->Hide();
124 // delete pLB_Controls;
125 delete pImageList;
126 m_pLB_Controls.clear();
127 m_pPB_OK.clear();
128 m_pPB_MoveUp.clear();
129 m_pPB_MoveDown.clear();
130 m_pPB_AutoOrder.clear();
131 ModalDialog::dispose();
135 Image TabOrderDialog::GetImage( const Reference< XPropertySet >& _rxSet ) const
137 sal_uInt16 nImageId = RID_SVXIMG_CONTROL;
138 // TODO: classify controls also in Basic propbrw
139 if ( _rxSet.is() && ::comphelper::hasProperty( PROPERTY_CLASSID, _rxSet ) )
141 switch( ::comphelper::getINT16( _rxSet->getPropertyValue( PROPERTY_CLASSID ) ) )
143 case FormComponentType::COMMANDBUTTON: nImageId = RID_SVXIMG_BUTTON; break;
144 case FormComponentType::FIXEDTEXT: nImageId = RID_SVXIMG_FIXEDTEXT; break;
145 case FormComponentType::TEXTFIELD: nImageId = RID_SVXIMG_EDIT; break;
146 case FormComponentType::RADIOBUTTON: nImageId = RID_SVXIMG_RADIOBUTTON; break;
147 case FormComponentType::CHECKBOX: nImageId = RID_SVXIMG_CHECKBOX; break;
148 case FormComponentType::LISTBOX: nImageId = RID_SVXIMG_LISTBOX; break;
149 case FormComponentType::COMBOBOX: nImageId = RID_SVXIMG_COMBOBOX; break;
150 case FormComponentType::GROUPBOX: nImageId = RID_SVXIMG_GROUPBOX; break;
151 case FormComponentType::IMAGEBUTTON: nImageId = RID_SVXIMG_IMAGEBUTTON; break;
152 case FormComponentType::FILECONTROL: nImageId = RID_SVXIMG_FILECONTROL; break;
153 case FormComponentType::HIDDENCONTROL: nImageId = RID_SVXIMG_HIDDEN; break;
154 case FormComponentType::DATEFIELD: nImageId = RID_SVXIMG_DATEFIELD; break;
155 case FormComponentType::TIMEFIELD: nImageId = RID_SVXIMG_TIMEFIELD; break;
156 case FormComponentType::NUMERICFIELD: nImageId = RID_SVXIMG_NUMERICFIELD; break;
157 case FormComponentType::CURRENCYFIELD: nImageId = RID_SVXIMG_CURRENCYFIELD; break;
158 case FormComponentType::PATTERNFIELD: nImageId = RID_SVXIMG_PATTERNFIELD; break;
159 case FormComponentType::IMAGECONTROL: nImageId = RID_SVXIMG_IMAGECONTROL; break;
160 case FormComponentType::GRIDCONTROL: nImageId = RID_SVXIMG_GRID; break;
161 case FormComponentType::SCROLLBAR: nImageId = RID_SVXIMG_SCROLLBAR; break;
162 case FormComponentType::SPINBUTTON: nImageId = RID_SVXIMG_SPINBUTTON; break;
163 case FormComponentType::NAVIGATIONBAR: nImageId = RID_SVXIMG_NAVIGATIONBAR; break;
164 default:
165 OSL_FAIL( "TabOrderDialog::GetImage: unknown control type" );
169 return pImageList->GetImage( nImageId );
173 void TabOrderDialog::FillList()
175 DBG_ASSERT( m_xTempModel.is() && m_xControlContainer.is(), "TabOrderDialog::FillList: invalid call!" );
176 if ( !m_xTempModel.is() || !m_xControlContainer.is() )
177 return;
179 m_pLB_Controls->Clear();
183 Sequence< Reference< XControlModel > > aControlModels( m_xTempModel->getControlModels() );
184 const Reference< XControlModel >* pControlModels = aControlModels.getConstArray();
186 OUString aName;
187 Image aImage;
189 for ( sal_Int32 i=0; i < aControlModels.getLength(); ++i, ++pControlModels )
191 Reference< XPropertySet > xControl( *pControlModels, UNO_QUERY );
192 Reference< XPropertySetInfo > xPI;
193 if ( xControl.is() )
194 xPI = xControl->getPropertySetInfo();
196 if ( xPI.is() )
198 if ( xPI->hasPropertyByName( PROPERTY_TABSTOP ) )
200 aName = ::comphelper::getString( xControl->getPropertyValue( PROPERTY_NAME ) );
201 // TODO: do Basic controls have a name?
202 aImage = GetImage( xControl );
203 m_pLB_Controls->InsertEntry( aName, aImage, aImage, 0, false, TREELIST_APPEND, xControl.get() );
206 else
208 // no property set -> no tab order
209 OSL_FAIL( "TabOrderDialog::FillList: invalid control encountered!" );
210 m_pLB_Controls->Clear();
211 break;
215 catch( const Exception& )
217 OSL_FAIL( "TabOrderDialog::FillList: caught an exception!" );
220 // select first entry
221 SvTreeListEntry* pFirstEntry = m_pLB_Controls->GetEntry( 0 );
222 if ( pFirstEntry )
223 m_pLB_Controls->Select( pFirstEntry );
227 IMPL_LINK( TabOrderDialog, MoveUpClickHdl, Button*, /*pButton*/ )
229 m_pLB_Controls->MoveSelection( -1 );
230 return 0;
234 IMPL_LINK( TabOrderDialog, MoveDownClickHdl, Button*, /*pButton*/ )
236 m_pLB_Controls->MoveSelection( 1 );
237 return 0;
241 IMPL_LINK( TabOrderDialog, AutoOrderClickHdl, Button*, /*pButton*/ )
245 Reference< css::form::runtime::XFormController > xTabController = css::form::runtime::FormController::create( m_xORB );
247 xTabController->setModel( m_xTempModel );
248 xTabController->setContainer( m_xControlContainer );
249 xTabController->autoTabOrder();
251 SetModified();
252 FillList();
254 ::comphelper::disposeComponent( xTabController );
256 catch( const Exception& )
258 OSL_FAIL( "TabOrderDialog::AutoOrderClickHdl: caught an exception!" );
261 return 0;
265 IMPL_LINK( TabOrderDialog, OKClickHdl, Button*, /*pButton*/ )
267 sal_uLong nEntryCount = m_pLB_Controls->GetEntryCount();
268 Sequence< Reference< XControlModel > > aSortedControlModelSeq( nEntryCount );
269 Sequence< Reference< XControlModel > > aControlModels( m_xTempModel->getControlModels());
270 Reference< XControlModel > * pSortedControlModels = aSortedControlModelSeq.getArray();
271 const Reference< XControlModel > * pControlModels = aControlModels.getConstArray();
273 for (sal_uLong i=0; i < nEntryCount; i++)
275 SvTreeListEntry* pEntry = m_pLB_Controls->GetEntry(i);
277 for( sal_Int32 j=0; j<aControlModels.getLength(); j++ )
279 Reference< XPropertySet > xSet(pControlModels[j], UNO_QUERY);
280 if ((XPropertySet*)xSet.get() == static_cast<XPropertySet*>(pEntry->GetUserData()))
282 pSortedControlModels[i] = pControlModels[j];
283 break;
288 // TODO: UNO action (to bracket all the single actions which are being created)
289 m_xModel->setControlModels( aSortedControlModelSeq );
291 EndDialog( RET_OK );
292 return 0;
296 //= TabOrderListBox
299 TabOrderListBox::TabOrderListBox( vcl::Window* pParent, WinBits nBits )
300 :SvTreeListBox( pParent, nBits )
302 SetDragDropMode(DragDropMode::ALL/*DragDropMode::CTRL_MOVE*/);
303 // Hmm. The flag alone is not enough, so to be on the safe side ...
305 SetSelectionMode( MULTIPLE_SELECTION );
308 VCL_BUILDER_DECL_FACTORY(TabOrderListBox)
310 WinBits nWinStyle = WB_TABSTOP;
311 OString sBorder = VclBuilder::extractCustomProperty(rMap);
312 if (!sBorder.isEmpty())
313 nWinStyle |= WB_BORDER;
314 rRet = VclPtr<TabOrderListBox>::Create(pParent, nWinStyle);
317 TabOrderListBox::~TabOrderListBox()
322 void TabOrderListBox::ModelHasMoved( SvTreeListEntry* _pSource )
324 SvTreeListBox::ModelHasMoved( _pSource );
326 static_cast<TabOrderDialog*>(GetParentDialog())->SetModified();
330 void TabOrderListBox::MoveSelection( long nRelPos )
332 OUString aSelEntryPrevText, aSelEntryNextText;
333 Image aImage;
334 for (long i=0; i<labs(nRelPos); i++)
336 static_cast<TabOrderDialog*>(GetParentDialog())->SetModified();
339 // move entries
340 if( nRelPos < 0 )
342 SvTreeListEntry* pFirstSelected = FirstSelected();
343 if( !pFirstSelected ) return;
344 sal_uLong nFirstSelPos = GetModel()->GetAbsPos( pFirstSelected );
345 if( nFirstSelPos == 0 ) return;
347 SvTreeListEntry* pSelEntry = pFirstSelected;
348 while( pSelEntry )
350 sal_uLong nSelEntryPos = GetModel()->GetAbsPos( pSelEntry );
351 SvTreeListEntry* pSelEntryPrev = GetEntry( nSelEntryPos-1 );
352 aSelEntryPrevText = GetEntryText( pSelEntryPrev );
353 aImage = GetExpandedEntryBmp(pSelEntryPrev);
354 void* pData = pSelEntryPrev->GetUserData();
356 GetModel()->Remove( pSelEntryPrev );
357 InsertEntry( aSelEntryPrevText, aImage, aImage, 0, false, nSelEntryPos, pData );
359 pSelEntry = NextSelected( pSelEntry );
363 else if( nRelPos > 0 )
365 SvTreeListEntry* pLastSelected = LastSelected();
366 if( !pLastSelected ) return;
367 sal_uLong nLastSelPos = GetModel()->GetAbsPos( pLastSelected );
369 if( (nLastSelPos + nRelPos - i) > (GetEntryCount()-1) ) return;
371 #if OSL_DEBUG_LEVEL > 0
372 sal_uLong nSelCount = GetSelectionCount();
373 (void)nSelCount;
374 #endif
377 SvTreeListEntry* pSelEntry = pLastSelected;
378 while( pSelEntry )
380 sal_uLong nSelEntryPos = GetModel()->GetAbsPos( pSelEntry );
381 SvTreeListEntry* pSelEntryNext = GetEntry( nSelEntryPos+1 );
382 void* pData = pSelEntryNext->GetUserData();
384 aSelEntryNextText = GetEntryText( pSelEntryNext );
385 aImage = GetExpandedEntryBmp(pSelEntryNext);
387 GetModel()->Remove( pSelEntryNext );
388 InsertEntry( aSelEntryNextText, aImage, aImage, 0, false, nSelEntryPos, pData );
390 pSelEntry = PrevSelected( pSelEntry );
392 long nThumbPos = GetVScroll()->GetThumbPos();
393 long nVisibleSize = GetVScroll()->GetVisibleSize();
394 long nFirstVisible = GetModel()->GetAbsPos( FirstVisible());
396 if ( ( nThumbPos + nVisibleSize + 1 ) < (long)( nLastSelPos + 3 ) )
397 GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
398 else if((nThumbPos+nVisibleSize+1) >= (nFirstVisible))
399 GetVScroll()->DoScrollAction(SCROLL_LINEUP);
405 } // namespace pcr
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */