cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / extensions / source / propctrlr / taborder.cxx
blob42a375c70238bc0dee84ab5a6900b38d57053167
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 <bitmaps.hlst>
23 #include "formstrings.hxx"
24 #include <comphelper/types.hxx>
25 #include <comphelper/property.hxx>
26 #include <cppuhelper/implbase.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>
32 #include <comphelper/diagnose_ex.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;
43 namespace {
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;
74 default:
75 OSL_FAIL( "TabOrderDialog::GetImage: unknown control type" );
79 return sImageId;
84 //= OSimpleTabModel
86 class OSimpleTabModel : public ::cppu::WeakImplHelper< XTabControllerModel>
88 Sequence< Reference< XControlModel > > m_aModels;
90 public:
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 {};
107 //= TabOrderDialog
108 TabOrderDialog::TabOrderDialog(weld::Window* _pParent, const Reference< XTabControllerModel >& _rxTabModel,
109 const Reference< XControlContainer >& _rxControlCont, const Reference< XComponentContext >& _rxORB)
110 : GenericDialogController( _pParent, u"modules/spropctrlr/ui/taborder.ui"_ustr, u"TabOrderDialog"_ustr)
111 , m_xModel( _rxTabModel )
112 , m_xControlContainer( _rxControlCont )
113 , m_xORB( _rxORB )
114 , m_xLB_Controls(m_xBuilder->weld_tree_view(u"CTRLtree"_ustr))
115 , m_xPB_OK(m_xBuilder->weld_button(u"ok"_ustr))
116 , m_xPB_MoveUp(m_xBuilder->weld_button(u"upB"_ustr))
117 , m_xPB_MoveDown(m_xBuilder->weld_button(u"downB"_ustr))
118 , m_xPB_AutoOrder(m_xBuilder->weld_button(u"autoB"_ustr))
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);
131 if ( m_xModel.is() )
132 m_xTempModel = new OSimpleTabModel( m_xModel->getControlModels() );
134 if ( m_xTempModel.is() && m_xControlContainer.is() )
135 FillList();
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() )
159 return;
161 m_xLB_Controls->clear();
165 OUString aName;
166 OUString aImage;
168 const Sequence<Reference<css::awt::XControlModel>> aControlModels = m_xTempModel->getControlModels();
169 for ( auto const& rControlModel : aControlModels )
171 Reference< XPropertySet > xControl( rControlModel, UNO_QUERY );
172 Reference< XPropertySetInfo > xPI;
173 if ( xControl.is() )
174 xPI = xControl->getPropertySetInfo();
176 if ( xPI.is() )
178 if ( xPI->hasPropertyByName( PROPERTY_TABSTOP ) )
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);
187 else
189 // no property set -> no tab order
190 OSL_FAIL( "TabOrderDialog::FillList: invalid control encountered!" );
191 m_xLB_Controls->clear();
192 break;
196 catch( const Exception& )
198 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "TabOrderDialog::FillList" );
201 // select first entry
202 if (m_xLB_Controls->n_children())
203 m_xLB_Controls->select(0);
206 IMPL_LINK_NOARG( TabOrderDialog, MoveUpClickHdl, weld::Button&, void )
208 MoveSelection(-1);
211 IMPL_LINK_NOARG( TabOrderDialog, MoveDownClickHdl, weld::Button&, void )
213 MoveSelection(1);
216 IMPL_LINK_NOARG( TabOrderDialog, AutoOrderClickHdl, weld::Button&, void )
220 Reference< css::form::runtime::XFormController > xTabController = css::form::runtime::FormController::create( m_xORB );
222 xTabController->setModel( m_xTempModel );
223 xTabController->setContainer( m_xControlContainer );
224 xTabController->autoTabOrder();
226 SetModified();
227 FillList();
229 ::comphelper::disposeComponent( xTabController );
231 catch( const Exception& )
233 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "TabOrderDialog::AutoOrderClickHdl" );
237 IMPL_LINK_NOARG( TabOrderDialog, OKClickHdl, weld::Button&, void )
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();
244 for (int i = 0; i < nEntryCount; ++i)
246 XPropertySet* pEntry = weld::fromId<XPropertySet*>(m_xLB_Controls->get_id(i));
247 for( auto const& rControlModel : aControlModels )
249 Reference< XPropertySet > xSet(rControlModel, UNO_QUERY);
250 if (xSet.get() == pEntry)
252 pSortedControlModels[i] = rControlModel;
253 break;
258 // TODO: UNO action (to bracket all the single actions which are being created)
259 m_xModel->setControlModels( aSortedControlModelSeq );
261 m_xDialog->response(RET_OK);
264 IMPL_LINK_NOARG(TabOrderDialog, ModelHasMoved, weld::TreeView&, void)
266 SetModified();
269 void TabOrderDialog::MoveSelection(int nRelPos)
271 std::vector<int> aRows(m_xLB_Controls->get_selected_rows());
272 if (aRows.empty())
273 return;
275 m_xLB_Controls->unselect_all();
276 for (int i = 0; i < abs(nRelPos); ++i)
278 SetModified();
280 // move entries
281 if (nRelPos < 0)
283 std::sort(aRows.begin(), aRows.end());
285 int nFirstSelPos = aRows[0];
286 if (nFirstSelPos == 0) return;
288 for (auto row : aRows)
290 int nInsertPos = row - 1;
291 m_xLB_Controls->swap(nInsertPos, row);
294 for (auto row : aRows)
295 m_xLB_Controls->select(row - 1);
297 else if (nRelPos > 0)
299 std::sort(aRows.rbegin(), aRows.rend());
301 int nLastSelPos = aRows[0];
302 if( (nLastSelPos + nRelPos - i) > (m_xLB_Controls->n_children()-1) ) return;
304 for (auto row : aRows)
306 int nInsertPos = row + 1;
307 m_xLB_Controls->swap(nInsertPos, row);
310 for (auto row : aRows)
311 m_xLB_Controls->select(row + 1);
317 } // namespace pcr
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */