LibreOffice Module extensions (master) 1
taborder.cxx
Go to the documentation of this file.
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 <bitmaps.hlst>
23#include "formstrings.hxx"
24#include <comphelper/types.hxx>
27#include <com/sun/star/beans/XPropertySet.hpp>
28#include <com/sun/star/form/FormComponentType.hpp>
29#include <com/sun/star/form/runtime/FormController.hpp>
30#include <osl/diagnose.h>
31#include <tools/debug.hxx>
33
34namespace 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 namespace {
44
45 OUString GetImage( const Reference< XPropertySet >& _rxSet )
46 {
47 OUString sImageId = RID_EXTBMP_CONTROL;
48 // TODO: classify controls also in Basic propbrw
49 if ( _rxSet.is() && ::comphelper::hasProperty( PROPERTY_CLASSID, _rxSet ) )
50 {
51 switch( ::comphelper::getINT16( _rxSet->getPropertyValue( PROPERTY_CLASSID ) ) )
52 {
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;
74 default:
75 OSL_FAIL( "TabOrderDialog::GetImage: unknown control type" );
76 }
77 }
78
79 return sImageId;
80 }
81
82 //= OSimpleTabModel
83
84 class OSimpleTabModel : public ::cppu::WeakImplHelper< XTabControllerModel>
85 {
86 Sequence< Reference< XControlModel > > m_aModels;
87
88 public:
89 explicit OSimpleTabModel( const Sequence< Reference< XControlModel > >& _rModels )
90 :m_aModels( _rModels )
91 {
92 }
93
94 // XTabControllerModel
95 virtual void SAL_CALL setControlModels(const Sequence< Reference< XControlModel > >& rModels) override {m_aModels = rModels;}
96 virtual Sequence< Reference< XControlModel > > SAL_CALL getControlModels() override {return m_aModels;}
97 virtual void SAL_CALL setGroup(const Sequence< Reference< XControlModel > >& /*Group*/, const OUString& /*GroupName*/) override {}
98 virtual sal_Int32 SAL_CALL getGroupCount() override {return 0;}
99 virtual void SAL_CALL getGroup(sal_Int32 /*nGroup*/, Sequence< Reference< XControlModel > >& /*Group*/, OUString& /*Name*/) override {}
100 virtual void SAL_CALL getGroupByName(const OUString& /*Name*/, Sequence< Reference< XControlModel > >& /*Group*/) override {}
101 virtual sal_Bool SAL_CALL getGroupControl() override {return false;} ;
102 virtual void SAL_CALL setGroupControl(sal_Bool /*GroupControl*/) override {};
103 };
104
105 }
106
107 //= TabOrderDialog
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 )
113 , m_xORB( _rxORB )
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"))
119 {
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);
123
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);
130
131 if ( m_xModel.is() )
132 m_xTempModel = new OSimpleTabModel( m_xModel->getControlModels() );
133
134 if ( m_xTempModel.is() && m_xControlContainer.is() )
135 FillList();
136
137 if (m_xLB_Controls->n_children() < 2)
138 {
139 m_xPB_MoveUp->set_sensitive(false);
140 m_xPB_MoveDown->set_sensitive(false);
141 m_xPB_AutoOrder->set_sensitive(false);
142 }
143
144 }
145
147 {
148 m_xPB_OK->set_sensitive(true);
149 }
150
152 {
153 }
154
156 {
157 DBG_ASSERT( m_xTempModel.is() && m_xControlContainer.is(), "TabOrderDialog::FillList: invalid call!" );
158 if ( !m_xTempModel.is() || !m_xControlContainer.is() )
159 return;
160
161 m_xLB_Controls->clear();
162
163 try
164 {
165 OUString aName;
166 OUString aImage;
167
168 const Sequence<Reference<css::awt::XControlModel>> aControlModels = m_xTempModel->getControlModels();
169 for ( auto const& rControlModel : aControlModels )
170 {
171 Reference< XPropertySet > xControl( rControlModel, UNO_QUERY );
172 Reference< XPropertySetInfo > xPI;
173 if ( xControl.is() )
174 xPI = xControl->getPropertySetInfo();
175
176 if ( xPI.is() )
177 {
178 if ( xPI->hasPropertyByName( PROPERTY_TABSTOP ) )
179 {
180 aName = ::comphelper::getString( xControl->getPropertyValue( PROPERTY_NAME ) );
181 // TODO: do Basic controls have a name?
182 aImage = GetImage( xControl );
183 OUString sId(weld::toId(xControl.get()));
184 m_xLB_Controls->append(sId, aName, aImage);
185 }
186 }
187 else
188 {
189 // no property set -> no tab order
190 OSL_FAIL( "TabOrderDialog::FillList: invalid control encountered!" );
191 m_xLB_Controls->clear();
192 break;
193 }
194 }
195 }
196 catch( const Exception& )
197 {
198 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "TabOrderDialog::FillList" );
199 }
200
201 // select first entry
202 if (m_xLB_Controls->n_children())
203 m_xLB_Controls->select(0);
204 }
205
206 IMPL_LINK_NOARG( TabOrderDialog, MoveUpClickHdl, weld::Button&, void )
207 {
208 MoveSelection(-1);
209 }
210
211 IMPL_LINK_NOARG( TabOrderDialog, MoveDownClickHdl, weld::Button&, void )
212 {
213 MoveSelection(1);
214 }
215
216 IMPL_LINK_NOARG( TabOrderDialog, AutoOrderClickHdl, weld::Button&, void )
217 {
218 try
219 {
220 Reference< css::form::runtime::XFormController > xTabController = css::form::runtime::FormController::create( m_xORB );
221
222 xTabController->setModel( m_xTempModel );
223 xTabController->setContainer( m_xControlContainer );
224 xTabController->autoTabOrder();
225
226 SetModified();
227 FillList();
228
229 ::comphelper::disposeComponent( xTabController );
230 }
231 catch( const Exception& )
232 {
233 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "TabOrderDialog::AutoOrderClickHdl" );
234 }
235 }
236
238 {
239 int nEntryCount = m_xLB_Controls->n_children();
240 Sequence< Reference< XControlModel > > aSortedControlModelSeq( nEntryCount );
241 const Sequence< Reference< XControlModel > > aControlModels( m_xTempModel->getControlModels());
242 Reference< XControlModel > * pSortedControlModels = aSortedControlModelSeq.getArray();
243
244 for (int i = 0; i < nEntryCount; ++i)
245 {
246 XPropertySet* pEntry = weld::fromId<XPropertySet*>(m_xLB_Controls->get_id(i));
247 for( auto const& rControlModel : aControlModels )
248 {
249 Reference< XPropertySet > xSet(rControlModel, UNO_QUERY);
250 if (xSet.get() == pEntry)
251 {
252 pSortedControlModels[i] = rControlModel;
253 break;
254 }
255 }
256 }
257
258 // TODO: UNO action (to bracket all the single actions which are being created)
259 m_xModel->setControlModels( aSortedControlModelSeq );
260
261 m_xDialog->response(RET_OK);
262 }
263
265 {
266 SetModified();
267 }
268
270 {
271 std::vector<int> aRows(m_xLB_Controls->get_selected_rows());
272 if (aRows.empty())
273 return;
274
275 m_xLB_Controls->unselect_all();
276 for (int i = 0; i < abs(nRelPos); ++i)
277 {
278 SetModified();
279
280 // move entries
281 if (nRelPos < 0)
282 {
283 std::sort(aRows.begin(), aRows.end());
284
285 int nFirstSelPos = aRows[0];
286 if (nFirstSelPos == 0) return;
287
288 for (auto row : aRows)
289 {
290 int nInsertPos = row - 1;
291 m_xLB_Controls->swap(nInsertPos, row);
292 }
293
294 for (auto row : aRows)
295 m_xLB_Controls->select(row - 1);
296 }
297 else if (nRelPos > 0)
298 {
299 std::sort(aRows.rbegin(), aRows.rend());
300
301 int nLastSelPos = aRows[0];
302 if( (nLastSelPos + nRelPos - i) > (m_xLB_Controls->n_children()-1) ) return;
303
304 for (auto row : aRows)
305 {
306 int nInsertPos = row + 1;
307 m_xLB_Controls->swap(nInsertPos, row);
308 }
309
310 for (auto row : aRows)
311 m_xLB_Controls->select(row + 1);
312 }
313 }
314 }
315
316
317} // namespace pcr
318
319
320/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
void MoveSelection(int nRelPos)
Definition: taborder.cxx:269
css::uno::Reference< css::awt::XControlContainer > m_xControlContainer
Definition: taborder.hxx:38
std::unique_ptr< weld::Button > m_xPB_MoveUp
Definition: taborder.hxx:44
std::unique_ptr< weld::Button > m_xPB_OK
Definition: taborder.hxx:43
virtual ~TabOrderDialog() override
Definition: taborder.cxx:151
TabOrderDialog(weld::Window *pParent, const css::uno::Reference< css::awt::XTabControllerModel > &_rxTabModel, const css::uno::Reference< css::awt::XControlContainer > &_rxControlCont, const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
Definition: taborder.cxx:108
std::unique_ptr< weld::Button > m_xPB_MoveDown
Definition: taborder.hxx:45
std::unique_ptr< weld::TreeView > m_xLB_Controls
Definition: taborder.hxx:42
css::uno::Reference< css::awt::XTabControllerModel > m_xTempModel
Definition: taborder.hxx:34
css::uno::Reference< css::awt::XTabControllerModel > m_xModel
Definition: taborder.hxx:36
std::unique_ptr< weld::Button > m_xPB_AutoOrder
Definition: taborder.hxx:46
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< frame::XModel > m_xModel
constexpr OUStringLiteral PROPERTY_NAME
Definition: formstrings.hxx:36
constexpr OUStringLiteral PROPERTY_CLASSID
Definition: formstrings.hxx:30
constexpr OUStringLiteral PROPERTY_TABSTOP
OUString aName
@ Exception
class SAL_NO_VTABLE XPropertySet
int i
a property handler for any virtual string properties
Definition: browserline.cxx:39
IMPL_LINK_NOARG(OBrowserLine, OnButtonFocus, weld::Widget &, void)
OUString toId(const void *pValue)
SwNodeOffset abs(const SwNodeOffset &a)
unsigned char sal_Bool
OUString sId
RET_OK