1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <strings.hrc>
24 #include <bitmaps.hlst>
25 #include "formstrings.hxx"
26 #include <comphelper/types.hxx>
27 #include <comphelper/property.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <com/sun/star/form/FormComponentType.hpp>
30 #include <com/sun/star/form/runtime/FormController.hpp>
31 #include <osl/diagnose.h>
32 #include <tools/debug.hxx>
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
;
45 OUString
GetImage( const Reference
< XPropertySet
>& _rxSet
)
47 OUString sImageId
= RID_EXTBMP_CONTROL
;
48 // TODO: classify controls also in Basic propbrw
49 if ( _rxSet
.is() && ::comphelper::hasProperty( PROPERTY_CLASSID
, _rxSet
) )
51 switch( ::comphelper::getINT16( _rxSet
->getPropertyValue( PROPERTY_CLASSID
) ) )
53 case FormComponentType::COMMANDBUTTON
: sImageId
= RID_EXTBMP_BUTTON
; break;
54 case FormComponentType::FIXEDTEXT
: sImageId
= RID_EXTBMP_FIXEDTEXT
; break;
55 case FormComponentType::TEXTFIELD
: sImageId
= RID_EXTBMP_EDITBOX
; break;
56 case FormComponentType::RADIOBUTTON
: sImageId
= RID_EXTBMP_RADIOBUTTON
; break;
57 case FormComponentType::CHECKBOX
: sImageId
= RID_EXTBMP_CHECKBOX
; break;
58 case FormComponentType::LISTBOX
: sImageId
= RID_EXTBMP_LISTBOX
; break;
59 case FormComponentType::COMBOBOX
: sImageId
= RID_EXTBMP_COMBOBOX
; break;
60 case FormComponentType::GROUPBOX
: sImageId
= RID_EXTBMP_GROUPBOX
; break;
61 case FormComponentType::IMAGEBUTTON
: sImageId
= RID_EXTBMP_IMAGEBUTTON
; break;
62 case FormComponentType::FILECONTROL
: sImageId
= RID_EXTBMP_FILECONTROL
; break;
63 case FormComponentType::HIDDENCONTROL
: sImageId
= RID_EXTBMP_HIDDEN
; break;
64 case FormComponentType::DATEFIELD
: sImageId
= RID_EXTBMP_DATEFIELD
; break;
65 case FormComponentType::TIMEFIELD
: sImageId
= RID_EXTBMP_TIMEFIELD
; break;
66 case FormComponentType::NUMERICFIELD
: sImageId
= RID_EXTBMP_NUMERICFIELD
; break;
67 case FormComponentType::CURRENCYFIELD
: sImageId
= RID_EXTBMP_CURRENCYFIELD
; break;
68 case FormComponentType::PATTERNFIELD
: sImageId
= RID_EXTBMP_PATTERNFIELD
; break;
69 case FormComponentType::IMAGECONTROL
: sImageId
= RID_EXTBMP_IMAGECONTROL
; break;
70 case FormComponentType::GRIDCONTROL
: sImageId
= RID_EXTBMP_GRID
; break;
71 case FormComponentType::SCROLLBAR
: sImageId
= RID_EXTBMP_SCROLLBAR
; break;
72 case FormComponentType::SPINBUTTON
: sImageId
= RID_EXTBMP_SPINBUTTON
; break;
73 case FormComponentType::NAVIGATIONBAR
: sImageId
= RID_EXTBMP_NAVIGATIONBAR
; break;
75 OSL_FAIL( "TabOrderDialog::GetImage: unknown control type" );
86 class OSimpleTabModel
: public ::cppu::WeakImplHelper
< XTabControllerModel
>
88 Sequence
< Reference
< XControlModel
> > m_aModels
;
91 explicit OSimpleTabModel( const Sequence
< Reference
< XControlModel
> >& _rModels
)
92 :m_aModels( _rModels
)
96 // XTabControllerModel
97 virtual void SAL_CALL
setControlModels(const Sequence
< Reference
< XControlModel
> >& rModels
) override
{m_aModels
= rModels
;}
98 virtual Sequence
< Reference
< XControlModel
> > SAL_CALL
getControlModels() override
{return m_aModels
;}
99 virtual void SAL_CALL
setGroup(const Sequence
< Reference
< XControlModel
> >& /*Group*/, const OUString
& /*GroupName*/) override
{}
100 virtual sal_Int32 SAL_CALL
getGroupCount() override
{return 0;}
101 virtual void SAL_CALL
getGroup(sal_Int32
/*nGroup*/, Sequence
< Reference
< XControlModel
> >& /*Group*/, OUString
& /*Name*/) override
{}
102 virtual void SAL_CALL
getGroupByName(const OUString
& /*Name*/, Sequence
< Reference
< XControlModel
> >& /*Group*/) override
{}
103 virtual sal_Bool SAL_CALL
getGroupControl() override
{return false;} ;
104 virtual void SAL_CALL
setGroupControl(sal_Bool
/*GroupControl*/) override
{};
108 TabOrderDialog::TabOrderDialog(weld::Window
* _pParent
, const Reference
< XTabControllerModel
>& _rxTabModel
,
109 const Reference
< XControlContainer
>& _rxControlCont
, const Reference
< XComponentContext
>& _rxORB
)
110 : GenericDialogController( _pParent
, "modules/spropctrlr/ui/taborder.ui", "TabOrderDialog")
111 , m_xModel( _rxTabModel
)
112 , m_xControlContainer( _rxControlCont
)
114 , m_xLB_Controls(m_xBuilder
->weld_tree_view("CTRLtree"))
115 , m_xPB_OK(m_xBuilder
->weld_button("ok"))
116 , m_xPB_MoveUp(m_xBuilder
->weld_button("upB"))
117 , m_xPB_MoveDown(m_xBuilder
->weld_button("downB"))
118 , m_xPB_AutoOrder(m_xBuilder
->weld_button("autoB"))
120 m_xLB_Controls
->set_size_request(m_xLB_Controls
->get_approximate_digit_width() * 60,
121 m_xLB_Controls
->get_height_rows(10));
122 m_xLB_Controls
->set_selection_mode(SelectionMode::Multiple
);
124 m_xLB_Controls
->connect_model_changed(LINK(this, TabOrderDialog
, ModelHasMoved
));
125 m_xPB_MoveUp
->connect_clicked( LINK( this, TabOrderDialog
, MoveUpClickHdl
) );
126 m_xPB_MoveDown
->connect_clicked( LINK( this, TabOrderDialog
, MoveDownClickHdl
) );
127 m_xPB_AutoOrder
->connect_clicked( LINK( this, TabOrderDialog
, AutoOrderClickHdl
) );
128 m_xPB_OK
->connect_clicked( LINK( this, TabOrderDialog
, OKClickHdl
) );
129 m_xPB_OK
->set_sensitive(false);
132 m_xTempModel
= new OSimpleTabModel( m_xModel
->getControlModels() );
134 if ( m_xTempModel
.is() && m_xControlContainer
.is() )
137 if (m_xLB_Controls
->n_children() < 2)
139 m_xPB_MoveUp
->set_sensitive(false);
140 m_xPB_MoveDown
->set_sensitive(false);
141 m_xPB_AutoOrder
->set_sensitive(false);
146 void TabOrderDialog::SetModified()
148 m_xPB_OK
->set_sensitive(true);
151 TabOrderDialog::~TabOrderDialog()
155 void TabOrderDialog::FillList()
157 DBG_ASSERT( m_xTempModel
.is() && m_xControlContainer
.is(), "TabOrderDialog::FillList: invalid call!" );
158 if ( !m_xTempModel
.is() || !m_xControlContainer
.is() )
161 m_xLB_Controls
->clear();
168 for ( auto const& rControlModel
: m_xTempModel
->getControlModels() )
170 Reference
< XPropertySet
> xControl( rControlModel
, UNO_QUERY
);
171 Reference
< XPropertySetInfo
> xPI
;
173 xPI
= xControl
->getPropertySetInfo();
177 if ( xPI
->hasPropertyByName( PROPERTY_TABSTOP
) )
179 aName
= ::comphelper::getString( xControl
->getPropertyValue( PROPERTY_NAME
) );
180 // TODO: do Basic controls have a name?
181 aImage
= GetImage( xControl
);
182 OUString
sId(OUString::number(reinterpret_cast<sal_Int64
>(xControl
.get())));
183 m_xLB_Controls
->append(sId
, aName
, aImage
);
188 // no property set -> no tab order
189 OSL_FAIL( "TabOrderDialog::FillList: invalid control encountered!" );
190 m_xLB_Controls
->clear();
195 catch( const Exception
& )
197 OSL_FAIL( "TabOrderDialog::FillList: caught an exception!" );
200 // select first entry
201 if (m_xLB_Controls
->n_children())
202 m_xLB_Controls
->select(0);
205 IMPL_LINK_NOARG( TabOrderDialog
, MoveUpClickHdl
, weld::Button
&, void )
210 IMPL_LINK_NOARG( TabOrderDialog
, MoveDownClickHdl
, weld::Button
&, void )
215 IMPL_LINK_NOARG( TabOrderDialog
, AutoOrderClickHdl
, weld::Button
&, void )
219 Reference
< css::form::runtime::XFormController
> xTabController
= css::form::runtime::FormController::create( m_xORB
);
221 xTabController
->setModel( m_xTempModel
);
222 xTabController
->setContainer( m_xControlContainer
);
223 xTabController
->autoTabOrder();
228 ::comphelper::disposeComponent( xTabController
);
230 catch( const Exception
& )
232 OSL_FAIL( "TabOrderDialog::AutoOrderClickHdl: caught an exception!" );
236 IMPL_LINK_NOARG( TabOrderDialog
, OKClickHdl
, weld::Button
&, void )
238 int nEntryCount
= m_xLB_Controls
->n_children();
239 Sequence
< Reference
< XControlModel
> > aSortedControlModelSeq( nEntryCount
);
240 Sequence
< Reference
< XControlModel
> > aControlModels( m_xTempModel
->getControlModels());
241 Reference
< XControlModel
> * pSortedControlModels
= aSortedControlModelSeq
.getArray();
243 for (int i
= 0; i
< nEntryCount
; ++i
)
245 XPropertySet
* pEntry
= reinterpret_cast<XPropertySet
*>(m_xLB_Controls
->get_id(i
).toInt64());
246 for( auto const& rControlModel
: aControlModels
)
248 Reference
< XPropertySet
> xSet(rControlModel
, UNO_QUERY
);
249 if (xSet
.get() == pEntry
)
251 pSortedControlModels
[i
] = rControlModel
;
257 // TODO: UNO action (to bracket all the single actions which are being created)
258 m_xModel
->setControlModels( aSortedControlModelSeq
);
260 m_xDialog
->response(RET_OK
);
263 IMPL_LINK_NOARG(TabOrderDialog
, ModelHasMoved
, weld::TreeView
&, void)
268 void TabOrderDialog::MoveSelection(int nRelPos
)
270 std::vector
<int> aRows(m_xLB_Controls
->get_selected_rows());
274 m_xLB_Controls
->unselect_all();
275 for (int i
= 0; i
< abs(nRelPos
); ++i
)
282 std::sort(aRows
.begin(), aRows
.end());
284 int nFirstSelPos
= aRows
[0];
285 if (nFirstSelPos
== 0) return;
287 for (auto row
: aRows
)
289 int nInsertPos
= row
- 1;
290 m_xLB_Controls
->swap(nInsertPos
, row
);
293 for (auto row
: aRows
)
294 m_xLB_Controls
->select(row
- 1);
296 else if (nRelPos
> 0)
298 std::sort(aRows
.rbegin(), aRows
.rend());
300 int nLastSelPos
= aRows
[0];
301 if( (nLastSelPos
+ nRelPos
- i
) > (m_xLB_Controls
->n_children()-1) ) return;
303 for (auto row
: aRows
)
305 int nInsertPos
= row
+ 1;
306 m_xLB_Controls
->swap(nInsertPos
, row
);
309 for (auto row
: aRows
)
310 m_xLB_Controls
->select(row
+ 1);
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */