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 "selectlabeldialog.hxx"
21 #include "formresid.hrc"
22 #include "bitmaps.hlst"
23 #include "formbrowsertools.hxx"
24 #include "formstrings.hxx"
25 #include <com/sun/star/form/FormComponentType.hpp>
26 #include <com/sun/star/container/XChild.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <com/sun/star/sdbc/XResultSet.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <comphelper/property.hxx>
32 #include <comphelper/types.hxx>
33 #include "svtools/treelistentry.hxx"
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::container
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::form
;
44 using namespace ::com::sun::star::sdbc
;
45 using namespace ::com::sun::star::lang
;
51 OSelectLabelDialog::OSelectLabelDialog( vcl::Window
* pParent
, Reference
< XPropertySet
> const & _xControlModel
)
52 :ModalDialog(pParent
, "LabelSelectionDialog", "modules/spropctrlr/ui/labelselectiondialog.ui")
53 ,m_xControlModel(_xControlModel
)
54 ,m_pInitialSelection(nullptr)
55 ,m_pLastSelected(nullptr)
56 ,m_bHaveAssignableControl(false)
58 get(m_pMainDesc
, "label");
59 get(m_pControlTree
, "control");
60 get(m_pNoAssignment
, "noassignment");
62 // initialize the TreeListBox
63 m_pControlTree
->SetSelectionMode( SelectionMode::Single
);
64 m_pControlTree
->SetDragDropMode( DragDropMode::NONE
);
65 m_pControlTree
->EnableInplaceEditing( false );
66 m_pControlTree
->SetStyle(m_pControlTree
->GetStyle() | WB_BORDER
| WB_HASLINES
| WB_HASLINESATROOT
| WB_HASBUTTONS
| WB_HASBUTTONSATROOT
| WB_HSCROLL
);
68 m_pControlTree
->SetNodeBitmaps(Image(BitmapEx(RID_EXTBMP_COLLAPSEDNODE
)),
69 Image(BitmapEx(RID_EXTBMP_EXPANDEDNODE
)));
70 m_pControlTree
->SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
71 m_pControlTree
->SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
73 // fill the description
74 OUString sDescription
= m_pMainDesc
->GetText();
75 sal_Int16 nClassID
= FormComponentType::CONTROL
;
76 if (::comphelper::hasProperty(PROPERTY_CLASSID
, m_xControlModel
))
77 nClassID
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
));
79 sDescription
= sDescription
.replaceAll("$controlclass$",
80 GetUIHeadlineName(nClassID
, makeAny(m_xControlModel
)));
81 OUString sName
= ::comphelper::getString(m_xControlModel
->getPropertyValue(PROPERTY_NAME
));
82 sDescription
= sDescription
.replaceAll("$controlname$", sName
);
83 m_pMainDesc
->SetText(sDescription
);
85 // search for the root of the form hierarchy
86 Reference
< XChild
> xCont(m_xControlModel
, UNO_QUERY
);
87 Reference
< XInterface
> xSearch( xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ());
88 Reference
< XResultSet
> xParentAsResultSet(xSearch
, UNO_QUERY
);
89 while (xParentAsResultSet
.is())
91 xCont
.set(xSearch
, UNO_QUERY
);
92 xSearch
= xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ();
93 xParentAsResultSet
.set(xSearch
, UNO_QUERY
);
96 // and insert all entries below this root into the listbox
99 // check which service the allowed components must support
100 sal_Int16 nClassId
= 0;
101 try { nClassId
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
)); } catch(...) { }
102 m_sRequiredService
= (FormComponentType::RADIOBUTTON
== nClassId
) ? OUString(SERVICE_COMPONENT_GROUPBOX
) : OUString(SERVICE_COMPONENT_FIXEDTEXT
);
103 m_aRequiredControlImage
= Image(BitmapEx(FormComponentType::RADIOBUTTON
== nClassId
? OUStringLiteral(RID_EXTBMP_GROUPBOX
) : OUStringLiteral(RID_EXTBMP_FIXEDTEXT
)));
105 // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
106 Any
aCurrentLabelControl( m_xControlModel
->getPropertyValue(PROPERTY_CONTROLLABEL
) );
107 DBG_ASSERT((aCurrentLabelControl
.getValueTypeClass() == TypeClass_INTERFACE
) || !aCurrentLabelControl
.hasValue(),
109 "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
110 if (aCurrentLabelControl
.hasValue())
111 aCurrentLabelControl
>>= m_xInitialLabelControl
;
114 Image
aRootImage(BitmapEx(RID_EXTBMP_FORMS
));
115 SvTreeListEntry
* pRoot
= m_pControlTree
->InsertEntry(PcrRes(RID_STR_FORMS
), aRootImage
, aRootImage
);
118 m_pInitialSelection
= nullptr;
119 m_bHaveAssignableControl
= false;
120 InsertEntries(xSearch
, pRoot
);
121 m_pControlTree
->Expand(pRoot
);
124 if (m_pInitialSelection
)
126 m_pControlTree
->MakeVisible(m_pInitialSelection
, true);
127 m_pControlTree
->Select(m_pInitialSelection
);
131 m_pControlTree
->MakeVisible(m_pControlTree
->First(), true);
132 if (m_pControlTree
->FirstSelected())
133 m_pControlTree
->Select(m_pControlTree
->FirstSelected(), false);
134 m_pNoAssignment
->Check();
137 if (!m_bHaveAssignableControl
)
138 { // no controls which can be assigned
139 m_pNoAssignment
->Check();
140 m_pNoAssignment
->Enable(false);
143 m_pNoAssignment
->SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
144 m_pNoAssignment
->GetClickHdl().Call(m_pNoAssignment
);
147 OSelectLabelDialog::~OSelectLabelDialog()
152 void OSelectLabelDialog::dispose()
154 // delete the entry datas of the listbox entries
155 SvTreeListEntry
* pLoop
= m_pControlTree
->First();
158 void* pData
= pLoop
->GetUserData();
160 delete static_cast<Reference
< XPropertySet
> *>(pData
);
161 pLoop
= m_pControlTree
->Next(pLoop
);
164 m_pControlTree
.clear();
165 m_pNoAssignment
.clear();
166 ModalDialog::dispose();
169 sal_Int32
OSelectLabelDialog::InsertEntries(const Reference
< XInterface
> & _xContainer
, SvTreeListEntry
* pContainerEntry
)
171 Reference
< XIndexAccess
> xContainer(_xContainer
, UNO_QUERY
);
172 if (!xContainer
.is())
175 sal_Int32 nChildren
= 0;
177 Reference
< XPropertySet
> xAsSet
;
178 for (sal_Int32 i
=0; i
<xContainer
->getCount(); ++i
)
180 xContainer
->getByIndex(i
) >>= xAsSet
;
183 SAL_INFO("extensions.propctrlr", "OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
187 if (!::comphelper::hasProperty(PROPERTY_NAME
, xAsSet
))
188 // we need at least a name for displaying ...
190 sName
= ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_NAME
)).getStr();
192 // we need to check if the control model supports the required service
193 Reference
< XServiceInfo
> xInfo(xAsSet
, UNO_QUERY
);
197 if (!xInfo
->supportsService(m_sRequiredService
))
198 { // perhaps it is a container
199 Reference
< XIndexAccess
> xCont(xAsSet
, UNO_QUERY
);
200 if (xCont
.is() && xCont
->getCount())
201 { // yes -> step down
202 Image
aFormImage(BitmapEx(RID_EXTBMP_FORM
));
203 SvTreeListEntry
* pCont
= m_pControlTree
->InsertEntry(sName
, aFormImage
, aFormImage
, pContainerEntry
);
204 sal_Int32 nContChildren
= InsertEntries(xCont
, pCont
);
207 m_pControlTree
->Expand(pCont
);
211 { // oops, no valid children -> remove the entry
212 m_pControlTree
->ModelIsRemoving(pCont
);
213 m_pControlTree
->GetModel()->Remove(pCont
);
214 m_pControlTree
->ModelHasRemoved(pCont
);
221 if (!::comphelper::hasProperty(PROPERTY_LABEL
, xAsSet
))
224 OUString sDisplayName
= OUStringBuffer(
225 ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_LABEL
))).
226 append(" (").append(sName
).append(')').
227 makeStringAndClear();
229 // all requirements met -> insert
230 SvTreeListEntry
* pCurrent
= m_pControlTree
->InsertEntry(sDisplayName
, m_aRequiredControlImage
, m_aRequiredControlImage
, pContainerEntry
);
231 pCurrent
->SetUserData(new Reference
< XPropertySet
> (xAsSet
));
234 if (m_xInitialLabelControl
== xAsSet
)
235 m_pInitialSelection
= pCurrent
;
237 m_bHaveAssignableControl
= true;
244 IMPL_LINK(OSelectLabelDialog
, OnEntrySelected
, SvTreeListBox
*, pLB
, void)
246 DBG_ASSERT(pLB
== m_pControlTree
, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
248 SvTreeListEntry
* pSelected
= m_pControlTree
->FirstSelected();
249 void* pData
= pSelected
? pSelected
->GetUserData() : nullptr;
252 m_xSelectedControl
.set(*static_cast<Reference
< XPropertySet
> *>(pData
));
254 m_pNoAssignment
->SetClickHdl(Link
<Button
*,void>());
255 m_pNoAssignment
->Check(pData
== nullptr);
256 m_pNoAssignment
->SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
260 IMPL_LINK(OSelectLabelDialog
, OnNoAssignmentClicked
, Button
*, pButton
, void)
262 DBG_ASSERT(pButton
== m_pNoAssignment
, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
265 if (m_pNoAssignment
->IsChecked())
266 m_pLastSelected
= m_pControlTree
->FirstSelected();
269 DBG_ASSERT(m_bHaveAssignableControl
, "OSelectLabelDialog::OnNoAssignmentClicked");
270 // search the first assignable entry
271 SvTreeListEntry
* pSearch
= m_pControlTree
->First();
274 if (pSearch
->GetUserData())
276 pSearch
= m_pControlTree
->Next(pSearch
);
281 m_pControlTree
->Select(pSearch
);
282 m_pLastSelected
= pSearch
;
288 m_pControlTree
->SetSelectHdl(Link
<SvTreeListBox
*,void>());
289 m_pControlTree
->SetDeselectHdl(Link
<SvTreeListBox
*,void>());
290 m_pControlTree
->Select(m_pLastSelected
, !m_pNoAssignment
->IsChecked());
291 m_pControlTree
->SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
292 m_pControlTree
->SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */