bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / selectlabeldialog.cxx
blob0d5b30dee61d5f5abac053593c29236cb3aad00e
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 "selectlabeldialog.hxx"
21 #include "formresid.hrc"
22 #include "formbrowsertools.hxx"
23 #include "formstrings.hxx"
24 #include <com/sun/star/form/FormComponentType.hpp>
25 #include <com/sun/star/container/XChild.hpp>
26 #include <com/sun/star/container/XIndexAccess.hpp>
27 #include <com/sun/star/sdbc/XResultSet.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <comphelper/property.hxx>
31 #include <comphelper/types.hxx>
32 #include "svtools/treelistentry.hxx"
35 namespace pcr
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::form;
43 using namespace ::com::sun::star::sdbc;
44 using namespace ::com::sun::star::lang;
47 // OSelectLabelDialog
50 OSelectLabelDialog::OSelectLabelDialog( vcl::Window* pParent, Reference< XPropertySet > _xControlModel )
51 :ModalDialog(pParent, "LabelSelectionDialog", "modules/spropctrlr/ui/labelselectiondialog.ui")
52 ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER))
53 ,m_xControlModel(_xControlModel)
54 ,m_pInitialSelection(NULL)
55 ,m_pLastSelected(NULL)
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( SINGLE_SELECTION );
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( m_aModelImages.GetImage( RID_SVXIMG_COLLAPSEDNODE ), m_aModelImages.GetImage( RID_SVXIMG_EXPANDEDNODE ) );
69 m_pControlTree->SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
70 m_pControlTree->SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
72 // fill the description
73 OUString sDescription = m_pMainDesc->GetText();
74 sal_Int16 nClassID = FormComponentType::CONTROL;
75 if (::comphelper::hasProperty(PROPERTY_CLASSID, m_xControlModel))
76 nClassID = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID));
78 sDescription = sDescription.replaceAll(OUString("$controlclass$"),
79 GetUIHeadlineName(nClassID, makeAny(m_xControlModel)));
80 OUString sName = ::comphelper::getString(m_xControlModel->getPropertyValue(PROPERTY_NAME));
81 sDescription = sDescription.replaceAll(OUString("$controlname$"), sName);
82 m_pMainDesc->SetText(sDescription);
84 // search for the root of the form hierarchy
85 Reference< XChild > xCont(m_xControlModel, UNO_QUERY);
86 Reference< XInterface > xSearch( xCont.is() ? xCont->getParent() : Reference< XInterface > ());
87 Reference< XResultSet > xParentAsResultSet(xSearch, UNO_QUERY);
88 while (xParentAsResultSet.is())
90 xCont = Reference< XChild > (xSearch, UNO_QUERY);
91 xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
92 xParentAsResultSet = Reference< XResultSet > (xSearch, UNO_QUERY);
95 // and insert all entries below this root into the listbox
96 if (xSearch.is())
98 // check which service the allowed components must suppport
99 sal_Int16 nClassId = 0;
100 try { nClassId = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID)); } catch(...) { }
101 m_sRequiredService = (FormComponentType::RADIOBUTTON == nClassId) ? OUString(SERVICE_COMPONENT_GROUPBOX) : OUString(SERVICE_COMPONENT_FIXEDTEXT);
102 m_aRequiredControlImage = m_aModelImages.GetImage((FormComponentType::RADIOBUTTON == nClassId) ? RID_SVXIMG_GROUPBOX : RID_SVXIMG_FIXEDTEXT);
104 // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
105 Any aCurrentLabelControl( m_xControlModel->getPropertyValue(PROPERTY_CONTROLLABEL) );
106 DBG_ASSERT((aCurrentLabelControl.getValueTypeClass() == TypeClass_INTERFACE) || !aCurrentLabelControl.hasValue(),
108 "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
109 if (aCurrentLabelControl.hasValue())
110 aCurrentLabelControl >>= m_xInitialLabelControl;
112 // insert the root
113 Image aRootImage = m_aModelImages.GetImage(RID_SVXIMG_FORMS);
114 SvTreeListEntry* pRoot = m_pControlTree->InsertEntry(PcrRes(RID_STR_FORMS).toString(), aRootImage, aRootImage);
116 // build the tree
117 m_pInitialSelection = NULL;
118 m_bHaveAssignableControl = false;
119 InsertEntries(xSearch, pRoot);
120 m_pControlTree->Expand(pRoot);
123 if (m_pInitialSelection)
125 m_pControlTree->MakeVisible(m_pInitialSelection, true);
126 m_pControlTree->Select(m_pInitialSelection, true);
128 else
130 m_pControlTree->MakeVisible(m_pControlTree->First(), true);
131 if (m_pControlTree->FirstSelected())
132 m_pControlTree->Select(m_pControlTree->FirstSelected(), false);
133 m_pNoAssignment->Check(true);
136 if (!m_bHaveAssignableControl)
137 { // no controls which can be assigned
138 m_pNoAssignment->Check(true);
139 m_pNoAssignment->Enable(false);
142 m_pNoAssignment->SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
143 m_pNoAssignment->GetClickHdl().Call(m_pNoAssignment);
146 OSelectLabelDialog::~OSelectLabelDialog()
148 disposeOnce();
151 void OSelectLabelDialog::dispose()
153 // delete the entry datas of the listbox entries
154 SvTreeListEntry* pLoop = m_pControlTree->First();
155 while (pLoop)
157 void* pData = pLoop->GetUserData();
158 if (pData)
159 delete static_cast<Reference< XPropertySet > *>(pData);
160 pLoop = m_pControlTree->Next(pLoop);
162 m_pMainDesc.clear();
163 m_pControlTree.clear();
164 m_pNoAssignment.clear();
165 ModalDialog::dispose();
168 sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvTreeListEntry* pContainerEntry)
170 Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
171 if (!xContainer.is())
172 return 0;
174 sal_Int32 nChildren = 0;
175 OUString sName;
176 Reference< XPropertySet > xAsSet;
177 for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
179 xContainer->getByIndex(i) >>= xAsSet;
180 if (!xAsSet.is())
182 DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
183 continue;
186 if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
187 // we need at least a name for displaying ...
188 continue;
189 sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
191 // we need to check if the control model supports the required service
192 Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
193 if (!xInfo.is())
194 continue;
196 if (!xInfo->supportsService(m_sRequiredService))
197 { // perhaps it is a container
198 Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
199 if (xCont.is() && xCont->getCount())
200 { // yes -> step down
201 Image aFormImage = m_aModelImages.GetImage( RID_SVXIMG_FORM );
202 SvTreeListEntry* pCont = m_pControlTree->InsertEntry(sName, aFormImage, aFormImage, pContainerEntry);
203 sal_Int32 nContChildren = InsertEntries(xCont, pCont);
204 if (nContChildren)
206 m_pControlTree->Expand(pCont);
207 ++nChildren;
209 else
210 { // oops, no valid children -> remove the entry
211 m_pControlTree->ModelIsRemoving(pCont);
212 m_pControlTree->GetModel()->Remove(pCont);
213 m_pControlTree->ModelHasRemoved(pCont);
216 continue;
219 // get the label
220 if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
221 continue;
223 OUString sDisplayName = OUStringBuffer(
224 ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
225 append(" (").append(sName).append(')').
226 makeStringAndClear();
228 // all requirements met -> insert
229 SvTreeListEntry* pCurrent = m_pControlTree->InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
230 pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
231 ++nChildren;
233 if (m_xInitialLabelControl == xAsSet)
234 m_pInitialSelection = pCurrent;
236 m_bHaveAssignableControl = true;
239 return nChildren;
243 IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
245 DBG_ASSERT(pLB == m_pControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
246 (void)pLB;
247 SvTreeListEntry* pSelected = m_pControlTree->FirstSelected();
248 void* pData = pSelected ? pSelected->GetUserData() : NULL;
250 if (pData)
251 m_xSelectedControl = Reference< XPropertySet > (*static_cast<Reference< XPropertySet > *>(pData));
253 m_pNoAssignment->SetClickHdl(Link<>());
254 m_pNoAssignment->Check(pData == NULL);
255 m_pNoAssignment->SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
257 return 0L;
261 IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
263 DBG_ASSERT(pButton == m_pNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
264 (void)pButton;
266 if (m_pNoAssignment->IsChecked())
267 m_pLastSelected = m_pControlTree->FirstSelected();
268 else
270 DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
271 // search the first assignable entry
272 SvTreeListEntry* pSearch = m_pControlTree->First();
273 while (pSearch)
275 if (pSearch->GetUserData())
276 break;
277 pSearch = m_pControlTree->Next(pSearch);
279 // and select it
280 if (pSearch)
282 m_pControlTree->Select(pSearch);
283 m_pLastSelected = pSearch;
287 if (m_pLastSelected)
289 m_pControlTree->SetSelectHdl(Link<>());
290 m_pControlTree->SetDeselectHdl(Link<>());
291 m_pControlTree->Select(m_pLastSelected, !m_pNoAssignment->IsChecked());
292 m_pControlTree->SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
293 m_pControlTree->SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
296 return 0L;
300 } // namespace pcr
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */