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 "groupboxwiz.hxx"
21 #include "commonpagesdbp.hxx"
22 #include <tools/debug.hxx>
23 #include <vcl/svapp.hxx>
24 #include "optiongrouplayouter.hxx"
26 #include <comphelper/processfactory.hxx>
27 #include <osl/diagnose.h>
29 #define GBW_STATE_OPTIONLIST 0
30 #define GBW_STATE_DEFAULTOPTION 1
31 #define GBW_STATE_OPTIONVALUES 2
32 #define GBW_STATE_DBFIELD 3
33 #define GBW_STATE_FINALIZE 4
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::form
;
42 OGroupBoxWizard::OGroupBoxWizard(weld::Window
* _pParent
,
43 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
44 : OControlWizard(_pParent
, _rxObjectModel
, _rxContext
)
45 , m_bVisitedDefault(false)
48 initControlSettings(&m_aSettings
);
50 m_xPrevPage
->set_help_id(HID_GROUPWIZARD_PREVIOUS
);
51 m_xNextPage
->set_help_id(HID_GROUPWIZARD_NEXT
);
52 m_xCancel
->set_help_id(HID_GROUPWIZARD_CANCEL
);
53 m_xFinish
->set_help_id(HID_GROUPWIZARD_FINISH
);
54 setTitleBase(compmodule::ModuleRes(RID_STR_GROUPWIZARD_TITLE
));
57 bool OGroupBoxWizard::approveControl(sal_Int16 _nClassId
)
59 return FormComponentType::GROUPBOX
== _nClassId
;
62 std::unique_ptr
<BuilderPage
> OGroupBoxWizard::createPage(::vcl::WizardTypes::WizardState _nState
)
64 OString
sIdent(OString::number(_nState
));
65 weld::Container
* pPageContainer
= m_xAssistant
->append_page(sIdent
);
69 case GBW_STATE_OPTIONLIST
:
70 return std::make_unique
<ORadioSelectionPage
>(pPageContainer
, this);
72 case GBW_STATE_DEFAULTOPTION
:
73 return std::make_unique
<ODefaultFieldSelectionPage
>(pPageContainer
, this);
75 case GBW_STATE_OPTIONVALUES
:
76 return std::make_unique
<OOptionValuesPage
>(pPageContainer
, this);
78 case GBW_STATE_DBFIELD
:
79 return std::make_unique
<OOptionDBFieldPage
>(pPageContainer
, this);
81 case GBW_STATE_FINALIZE
:
82 return std::make_unique
<OFinalizeGBWPage
>(pPageContainer
, this);
88 vcl::WizardTypes::WizardState
OGroupBoxWizard::determineNextState( ::vcl::WizardTypes::WizardState _nCurrentState
) const
90 switch (_nCurrentState
)
92 case GBW_STATE_OPTIONLIST
:
93 return GBW_STATE_DEFAULTOPTION
;
95 case GBW_STATE_DEFAULTOPTION
:
96 return GBW_STATE_OPTIONVALUES
;
98 case GBW_STATE_OPTIONVALUES
:
99 if (getContext().aFieldNames
.hasElements())
100 return GBW_STATE_DBFIELD
;
102 return GBW_STATE_FINALIZE
;
104 case GBW_STATE_DBFIELD
:
105 return GBW_STATE_FINALIZE
;
108 return WZS_INVALID_STATE
;
111 void OGroupBoxWizard::enterState(::vcl::WizardTypes::WizardState _nState
)
113 // some stuff to do before calling the base class (modifying our settings)
116 case GBW_STATE_DEFAULTOPTION
:
117 if (!m_bVisitedDefault
)
118 { // assume that the first of the radio buttons should be selected
119 DBG_ASSERT(m_aSettings
.aLabels
.size(), "OGroupBoxWizard::enterState: should never have reached this state!");
120 m_aSettings
.sDefaultField
= m_aSettings
.aLabels
[0];
122 m_bVisitedDefault
= true;
125 case GBW_STATE_DBFIELD
:
127 { // try to generate a default for the DB field
128 // (simply use the first field in the DB names collection)
129 if (getContext().aFieldNames
.hasElements())
130 m_aSettings
.sDBField
= getContext().aFieldNames
[0];
136 // setting the def button... to be done before the base class is called, too, 'cause the base class
137 // calls the pages, which are allowed to override our def button behaviour
138 defaultButton(GBW_STATE_FINALIZE
== _nState
? WizardButtonFlags::FINISH
: WizardButtonFlags::NEXT
);
140 // allow "finish" on the last page only
141 enableButtons(WizardButtonFlags::FINISH
, GBW_STATE_FINALIZE
== _nState
);
142 // allow previous on all pages but the first one
143 enableButtons(WizardButtonFlags::PREVIOUS
, GBW_STATE_OPTIONLIST
!= _nState
);
144 // allow next on all pages but the last one
145 enableButtons(WizardButtonFlags::NEXT
, GBW_STATE_FINALIZE
!= _nState
);
147 OControlWizard::enterState(_nState
);
150 bool OGroupBoxWizard::onFinish()
152 // commit the basic control settings
153 commitControlSettings(&m_aSettings
);
155 // create the radio buttons
158 OOptionGroupLayouter
aLayouter( getComponentContext() );
159 aLayouter
.doLayout(getContext(), getSettings());
161 catch(const Exception
&)
163 OSL_FAIL("OGroupBoxWizard::createRadios: caught an exception while creating the radio shapes!");
166 return OControlWizard::onFinish();
169 ORadioSelectionPage::ORadioSelectionPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
170 : OGBWPage(pPage
, pWizard
, "modules/sabpilot/ui/groupradioselectionpage.ui", "GroupRadioSelectionPage")
171 , m_xRadioName(m_xBuilder
->weld_entry("radiolabels"))
172 , m_xMoveRight(m_xBuilder
->weld_button("toright"))
173 , m_xMoveLeft(m_xBuilder
->weld_button("toleft"))
174 , m_xExistingRadios(m_xBuilder
->weld_tree_view("radiobuttons"))
176 if (getContext().aFieldNames
.hasElements())
178 enableFormDatasourceDisplay();
181 m_xMoveLeft
->connect_clicked(LINK(this, ORadioSelectionPage
, OnMoveEntry
));
182 m_xMoveRight
->connect_clicked(LINK(this, ORadioSelectionPage
, OnMoveEntry
));
183 m_xRadioName
->connect_changed(LINK(this, ORadioSelectionPage
, OnNameModified
));
184 m_xExistingRadios
->connect_changed(LINK(this, ORadioSelectionPage
, OnEntrySelected
));
186 implCheckMoveButtons();
187 m_xExistingRadios
->set_selection_mode(SelectionMode::Multiple
);
189 getDialog()->defaultButton(m_xMoveRight
.get());
192 ORadioSelectionPage::~ORadioSelectionPage()
196 void ORadioSelectionPage::Activate()
198 OGBWPage::Activate();
199 m_xRadioName
->grab_focus();
202 void ORadioSelectionPage::initializePage()
204 OGBWPage::initializePage();
206 m_xRadioName
->set_text("");
208 // no need to initialize the list of radios here
209 // (we're the only one affecting this special setting, so it will be in the same state as last time this
210 // page was committed)
212 implCheckMoveButtons();
215 bool ORadioSelectionPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
217 if (!OGBWPage::commitPage(_eReason
))
220 // copy the names of the radio buttons to be inserted
221 // and initialize the values
222 OOptionGroupSettings
& rSettings
= getSettings();
223 rSettings
.aLabels
.clear();
224 rSettings
.aValues
.clear();
225 rSettings
.aLabels
.reserve(m_xExistingRadios
->n_children());
226 rSettings
.aValues
.reserve(m_xExistingRadios
->n_children());
227 for (sal_Int32 i
=0; i
<m_xExistingRadios
->n_children(); ++i
)
229 rSettings
.aLabels
.push_back(m_xExistingRadios
->get_text(i
));
230 rSettings
.aValues
.push_back(OUString::number((i
+ 1)));
236 IMPL_LINK( ORadioSelectionPage
, OnMoveEntry
, weld::Button
&, rButton
, void )
238 bool bMoveLeft
= (m_xMoveLeft
.get() == &rButton
);
241 while (m_xExistingRadios
->count_selected_rows())
242 m_xExistingRadios
->remove(m_xExistingRadios
->get_selected_index());
246 m_xExistingRadios
->append_text(m_xRadioName
->get_text());
247 m_xRadioName
->set_text("");
250 implCheckMoveButtons();
254 m_xExistingRadios
->grab_focus();
256 m_xRadioName
->grab_focus();
259 IMPL_LINK_NOARG( ORadioSelectionPage
, OnEntrySelected
, weld::TreeView
&, void )
261 implCheckMoveButtons();
264 IMPL_LINK_NOARG( ORadioSelectionPage
, OnNameModified
, weld::Entry
&, void )
266 implCheckMoveButtons();
269 bool ORadioSelectionPage::canAdvance() const
271 return 0 != m_xExistingRadios
->n_children();
274 void ORadioSelectionPage::implCheckMoveButtons()
276 bool bHaveSome
= (0 != m_xExistingRadios
->n_children());
277 bool bSelectedSome
= (0 != m_xExistingRadios
->count_selected_rows());
278 bool bUnfinishedInput
= !m_xRadioName
->get_text().isEmpty();
280 m_xMoveLeft
->set_sensitive(bSelectedSome
);
281 m_xMoveRight
->set_sensitive(bUnfinishedInput
);
283 getDialog()->enableButtons(WizardButtonFlags::NEXT
, bHaveSome
);
285 if (bUnfinishedInput
)
287 if (!m_xMoveRight
->get_has_default())
288 getDialog()->defaultButton(m_xMoveRight
.get());
292 if (m_xMoveRight
->get_has_default())
293 getDialog()->defaultButton(WizardButtonFlags::NEXT
);
297 ODefaultFieldSelectionPage::ODefaultFieldSelectionPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
298 : OMaybeListSelectionPage(pPage
, pWizard
, "modules/sabpilot/ui/defaultfieldselectionpage.ui", "DefaultFieldSelectionPage")
299 , m_xDefSelYes(m_xBuilder
->weld_radio_button("defaultselectionyes"))
300 , m_xDefSelNo(m_xBuilder
->weld_radio_button("defaultselectionno"))
301 , m_xDefSelection(m_xBuilder
->weld_combo_box("defselectionfield"))
303 announceControls(*m_xDefSelYes
, *m_xDefSelNo
, *m_xDefSelection
);
306 ODefaultFieldSelectionPage::~ODefaultFieldSelectionPage()
310 void ODefaultFieldSelectionPage::initializePage()
312 OMaybeListSelectionPage::initializePage();
314 const OOptionGroupSettings
& rSettings
= getSettings();
317 m_xDefSelection
->clear();
318 for (auto const& label
: rSettings
.aLabels
)
319 m_xDefSelection
->append_text(label
);
321 implInitialize(rSettings
.sDefaultField
);
324 bool ODefaultFieldSelectionPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
326 if (!OMaybeListSelectionPage::commitPage(_eReason
))
329 OOptionGroupSettings
& rSettings
= getSettings();
330 implCommit(rSettings
.sDefaultField
);
335 OOptionValuesPage::OOptionValuesPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
336 : OGBWPage(pPage
, pWizard
, "modules/sabpilot/ui/optionvaluespage.ui", "OptionValuesPage")
337 , m_xValue(m_xBuilder
->weld_entry("optionvalue"))
338 , m_xOptions(m_xBuilder
->weld_tree_view("radiobuttons"))
339 , m_nLastSelection(::vcl::WizardTypes::WizardState(-1))
341 m_xOptions
->connect_changed(LINK(this, OOptionValuesPage
, OnOptionSelected
));
344 OOptionValuesPage::~OOptionValuesPage()
348 IMPL_LINK_NOARG( OOptionValuesPage
, OnOptionSelected
, weld::TreeView
&, void )
350 implTraveledOptions();
353 void OOptionValuesPage::Activate()
355 OGBWPage::Activate();
356 m_xValue
->grab_focus();
359 void OOptionValuesPage::implTraveledOptions()
361 if (::vcl::WizardTypes::WizardState(-1) != m_nLastSelection
)
363 // save the value for the last option
364 DBG_ASSERT(static_cast<size_t>(m_nLastSelection
) < m_aUncommittedValues
.size(), "OOptionValuesPage::implTraveledOptions: invalid previous selection index!");
365 m_aUncommittedValues
[m_nLastSelection
] = m_xValue
->get_text();
368 m_nLastSelection
= m_xOptions
->get_selected_index();
369 DBG_ASSERT(static_cast<size_t>(m_nLastSelection
) < m_aUncommittedValues
.size(), "OOptionValuesPage::implTraveledOptions: invalid new selection index!");
370 m_xValue
->set_text(m_aUncommittedValues
[m_nLastSelection
]);
373 void OOptionValuesPage::initializePage()
375 OGBWPage::initializePage();
377 const OOptionGroupSettings
& rSettings
= getSettings();
378 DBG_ASSERT(rSettings
.aLabels
.size(), "OOptionValuesPage::initializePage: no options!!");
379 DBG_ASSERT(rSettings
.aLabels
.size() == rSettings
.aValues
.size(), "OOptionValuesPage::initializePage: inconsistent data!");
381 // fill the list with all available options
383 m_nLastSelection
= -1;
384 for (auto const& label
: rSettings
.aLabels
)
385 m_xOptions
->append_text(label
);
387 // remember the values ... can't set them directly in the settings without the explicit commit call
388 // so we need have a copy of the values
389 m_aUncommittedValues
= rSettings
.aValues
;
391 // select the first entry
392 m_xOptions
->select(0);
393 implTraveledOptions();
396 bool OOptionValuesPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
398 if (!OGBWPage::commitPage(_eReason
))
401 OOptionGroupSettings
& rSettings
= getSettings();
403 // commit the current value
404 implTraveledOptions();
405 // copy the uncommitted values
406 rSettings
.aValues
= m_aUncommittedValues
;
411 OOptionDBFieldPage::OOptionDBFieldPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
412 : ODBFieldPage(pPage
, pWizard
)
414 setDescriptionText(compmodule::ModuleRes(RID_STR_GROUPWIZ_DBFIELD
));
417 OUString
& OOptionDBFieldPage::getDBFieldSetting()
419 return static_cast<OGroupBoxWizard
*>(getDialog())->getSettings().sDBField
;
422 OFinalizeGBWPage::OFinalizeGBWPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
423 : OGBWPage(pPage
, pWizard
, "modules/sabpilot/ui/optionsfinalpage.ui", "OptionsFinalPage")
424 , m_xName(m_xBuilder
->weld_entry("nameit"))
428 OFinalizeGBWPage::~OFinalizeGBWPage()
432 void OFinalizeGBWPage::Activate()
434 OGBWPage::Activate();
435 m_xName
->grab_focus();
438 bool OFinalizeGBWPage::canAdvance() const
443 void OFinalizeGBWPage::initializePage()
445 OGBWPage::initializePage();
447 const OOptionGroupSettings
& rSettings
= getSettings();
448 m_xName
->set_text(rSettings
.sControlLabel
);
451 bool OFinalizeGBWPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
453 if (!OGBWPage::commitPage(_eReason
))
456 getSettings().sControlLabel
= m_xName
->get_text();
464 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */