Line data Source code
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 "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"
33 :
34 : //............................................................................
35 : namespace pcr
36 : {
37 : //............................................................................
38 :
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;
45 :
46 : //========================================================================
47 : // OSelectLabelDialog
48 : //========================================================================
49 : DBG_NAME(OSelectLabelDialog)
50 : //------------------------------------------------------------------------
51 0 : OSelectLabelDialog::OSelectLabelDialog( Window* pParent, Reference< XPropertySet > _xControlModel )
52 : :ModalDialog(pParent, PcrRes(RID_DLG_SELECTLABELCONTROL))
53 : ,m_aMainDesc(this, PcrRes(1))
54 : ,m_aControlTree(this, PcrRes(1))
55 : ,m_aNoAssignment(this, PcrRes(1))
56 : ,m_aSeparator(this, PcrRes(1))
57 : ,m_aOk(this, PcrRes(1))
58 : ,m_aCancel(this, PcrRes(1))
59 : ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER))
60 : ,m_xControlModel(_xControlModel)
61 : ,m_pInitialSelection(NULL)
62 : ,m_pLastSelected(NULL)
63 0 : ,m_bHaveAssignableControl(sal_False)
64 : {
65 : DBG_CTOR(OSelectLabelDialog,NULL);
66 :
67 : // initialize the TreeListBox
68 0 : m_aControlTree.SetSelectionMode( SINGLE_SELECTION );
69 0 : m_aControlTree.SetDragDropMode( 0 );
70 0 : m_aControlTree.EnableInplaceEditing( sal_False );
71 0 : m_aControlTree.SetStyle(m_aControlTree.GetStyle() | WB_BORDER | WB_HASLINES | WB_HASLINESATROOT | WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HSCROLL);
72 :
73 0 : m_aControlTree.SetNodeBitmaps( m_aModelImages.GetImage( RID_SVXIMG_COLLAPSEDNODE ), m_aModelImages.GetImage( RID_SVXIMG_EXPANDEDNODE ) );
74 0 : m_aControlTree.SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
75 0 : m_aControlTree.SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
76 :
77 : // fill the description
78 0 : rtl::OUString sDescription = m_aMainDesc.GetText();
79 0 : sal_Int16 nClassID = FormComponentType::CONTROL;
80 0 : if (::comphelper::hasProperty(PROPERTY_CLASSID, m_xControlModel))
81 0 : nClassID = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID));
82 :
83 : sDescription = sDescription.replaceAll(rtl::OUString("$control_class$"),
84 0 : GetUIHeadlineName(nClassID, makeAny(m_xControlModel)));
85 0 : rtl::OUString sName = ::comphelper::getString(m_xControlModel->getPropertyValue(PROPERTY_NAME));
86 0 : sDescription = sDescription.replaceAll(rtl::OUString("$control_name$"), sName);
87 0 : m_aMainDesc.SetText(sDescription);
88 :
89 : // search for the root of the form hierarchy
90 0 : Reference< XChild > xCont(m_xControlModel, UNO_QUERY);
91 0 : Reference< XInterface > xSearch( xCont.is() ? xCont->getParent() : Reference< XInterface > ());
92 0 : Reference< XResultSet > xParentAsResultSet(xSearch, UNO_QUERY);
93 0 : while (xParentAsResultSet.is())
94 : {
95 0 : xCont = Reference< XChild > (xSearch, UNO_QUERY);
96 0 : xSearch = xCont.is() ? xCont->getParent() : Reference< XInterface > ();
97 0 : xParentAsResultSet = Reference< XResultSet > (xSearch, UNO_QUERY);
98 : }
99 :
100 : // and insert all entries below this root into the listbox
101 0 : if (xSearch.is())
102 : {
103 : // check wich service the allowed components must suppport
104 0 : sal_Int16 nClassId = 0;
105 0 : try { nClassId = ::comphelper::getINT16(m_xControlModel->getPropertyValue(PROPERTY_CLASSID)); } catch(...) { }
106 0 : m_sRequiredService = (FormComponentType::RADIOBUTTON == nClassId) ? SERVICE_COMPONENT_GROUPBOX : SERVICE_COMPONENT_FIXEDTEXT;
107 0 : m_aRequiredControlImage = m_aModelImages.GetImage((FormComponentType::RADIOBUTTON == nClassId) ? RID_SVXIMG_GROUPBOX : RID_SVXIMG_FIXEDTEXT);
108 :
109 : // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
110 0 : Any aCurrentLabelControl( m_xControlModel->getPropertyValue(PROPERTY_CONTROLLABEL) );
111 : DBG_ASSERT((aCurrentLabelControl.getValueTypeClass() == TypeClass_INTERFACE) || !aCurrentLabelControl.hasValue(),
112 :
113 : "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
114 0 : if (aCurrentLabelControl.hasValue())
115 0 : aCurrentLabelControl >>= m_xInitialLabelControl;
116 :
117 : // insert the root
118 0 : Image aRootImage = m_aModelImages.GetImage(RID_SVXIMG_FORMS);
119 0 : SvTreeListEntry* pRoot = m_aControlTree.InsertEntry(PcrRes(RID_STR_FORMS), aRootImage, aRootImage);
120 :
121 : // build the tree
122 0 : m_pInitialSelection = NULL;
123 0 : m_bHaveAssignableControl = sal_False;
124 0 : InsertEntries(xSearch, pRoot);
125 0 : m_aControlTree.Expand(pRoot);
126 : }
127 :
128 0 : if (m_pInitialSelection)
129 : {
130 0 : m_aControlTree.MakeVisible(m_pInitialSelection, sal_True);
131 0 : m_aControlTree.Select(m_pInitialSelection, sal_True);
132 : }
133 : else
134 : {
135 0 : m_aControlTree.MakeVisible(m_aControlTree.First(), sal_True);
136 0 : if (m_aControlTree.FirstSelected())
137 0 : m_aControlTree.Select(m_aControlTree.FirstSelected(), sal_False);
138 0 : m_aNoAssignment.Check(sal_True);
139 : }
140 :
141 0 : if (!m_bHaveAssignableControl)
142 : { // no controls which can be assigned
143 0 : m_aNoAssignment.Check(sal_True);
144 0 : m_aNoAssignment.Enable(sal_False);
145 : }
146 :
147 0 : m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
148 0 : m_aNoAssignment.GetClickHdl().Call(&m_aNoAssignment);
149 :
150 0 : FreeResource();
151 0 : }
152 :
153 : //------------------------------------------------------------------------
154 0 : OSelectLabelDialog::~OSelectLabelDialog()
155 : {
156 : // delete the entry datas of the listbox entries
157 0 : SvTreeListEntry* pLoop = m_aControlTree.First();
158 0 : while (pLoop)
159 : {
160 0 : void* pData = pLoop->GetUserData();
161 0 : if (pData)
162 0 : delete (Reference< XPropertySet > *)pData;
163 0 : pLoop = m_aControlTree.Next(pLoop);
164 : }
165 :
166 : DBG_DTOR(OSelectLabelDialog,NULL);
167 0 : }
168 :
169 : //------------------------------------------------------------------------
170 0 : sal_Int32 OSelectLabelDialog::InsertEntries(const Reference< XInterface > & _xContainer, SvTreeListEntry* pContainerEntry)
171 : {
172 0 : Reference< XIndexAccess > xContainer(_xContainer, UNO_QUERY);
173 0 : if (!xContainer.is())
174 0 : return 0;
175 :
176 0 : sal_Int32 nChildren = 0;
177 0 : rtl::OUString sName;
178 0 : Reference< XPropertySet > xAsSet;
179 0 : for (sal_Int32 i=0; i<xContainer->getCount(); ++i)
180 : {
181 0 : xContainer->getByIndex(i) >>= xAsSet;
182 0 : if (!xAsSet.is())
183 : {
184 : DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
185 0 : continue;
186 : }
187 :
188 0 : if (!::comphelper::hasProperty(PROPERTY_NAME, xAsSet))
189 : // we need at least a name for displaying ...
190 0 : continue;
191 0 : sName = ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_NAME)).getStr();
192 :
193 : // we need to check if the control model supports the required service
194 0 : Reference< XServiceInfo > xInfo(xAsSet, UNO_QUERY);
195 0 : if (!xInfo.is())
196 0 : continue;
197 :
198 0 : if (!xInfo->supportsService(m_sRequiredService))
199 : { // perhaps it is a container
200 0 : Reference< XIndexAccess > xCont(xAsSet, UNO_QUERY);
201 0 : if (xCont.is() && xCont->getCount())
202 : { // yes -> step down
203 0 : Image aFormImage = m_aModelImages.GetImage( RID_SVXIMG_FORM );
204 0 : SvTreeListEntry* pCont = m_aControlTree.InsertEntry(sName, aFormImage, aFormImage, pContainerEntry);
205 0 : sal_Int32 nContChildren = InsertEntries(xCont, pCont);
206 0 : if (nContChildren)
207 : {
208 0 : m_aControlTree.Expand(pCont);
209 0 : ++nChildren;
210 : }
211 : else
212 : { // oops, no valid children -> remove the entry
213 0 : m_aControlTree.ModelIsRemoving(pCont);
214 0 : m_aControlTree.GetModel()->Remove(pCont);
215 0 : m_aControlTree.ModelHasRemoved(pCont);
216 0 : }
217 : }
218 0 : continue;
219 : }
220 :
221 : // get the label
222 0 : if (!::comphelper::hasProperty(PROPERTY_LABEL, xAsSet))
223 0 : continue;
224 :
225 : rtl::OUString sDisplayName = rtl::OUStringBuffer(
226 0 : ::comphelper::getString(xAsSet->getPropertyValue(PROPERTY_LABEL))).
227 0 : appendAscii(RTL_CONSTASCII_STRINGPARAM(" (")).append(sName).append(')').
228 0 : makeStringAndClear();
229 :
230 : // all requirements met -> insert
231 0 : SvTreeListEntry* pCurrent = m_aControlTree.InsertEntry(sDisplayName, m_aRequiredControlImage, m_aRequiredControlImage, pContainerEntry);
232 0 : pCurrent->SetUserData(new Reference< XPropertySet > (xAsSet));
233 0 : ++nChildren;
234 :
235 0 : if (m_xInitialLabelControl == xAsSet)
236 0 : m_pInitialSelection = pCurrent;
237 :
238 0 : m_bHaveAssignableControl = sal_True;
239 0 : }
240 :
241 0 : return nChildren;
242 : }
243 :
244 : //------------------------------------------------------------------------
245 0 : IMPL_LINK(OSelectLabelDialog, OnEntrySelected, SvTreeListBox*, pLB)
246 : {
247 : DBG_ASSERT(pLB == &m_aControlTree, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
248 : (void)pLB;
249 0 : SvTreeListEntry* pSelected = m_aControlTree.FirstSelected();
250 0 : void* pData = pSelected ? pSelected->GetUserData() : NULL;
251 :
252 0 : if (pData)
253 0 : m_xSelectedControl = Reference< XPropertySet > (*(Reference< XPropertySet > *)pData);
254 :
255 0 : m_aNoAssignment.SetClickHdl(Link());
256 0 : m_aNoAssignment.Check(pData == NULL);
257 0 : m_aNoAssignment.SetClickHdl(LINK(this, OSelectLabelDialog, OnNoAssignmentClicked));
258 :
259 0 : return 0L;
260 : }
261 :
262 : //------------------------------------------------------------------------
263 0 : IMPL_LINK(OSelectLabelDialog, OnNoAssignmentClicked, Button*, pButton)
264 : {
265 : DBG_ASSERT(pButton == &m_aNoAssignment, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
266 : (void)pButton;
267 :
268 0 : if (m_aNoAssignment.IsChecked())
269 0 : m_pLastSelected = m_aControlTree.FirstSelected();
270 : else
271 : {
272 : DBG_ASSERT(m_bHaveAssignableControl, "OSelectLabelDialog::OnNoAssignmentClicked");
273 : // search the first assignable entry
274 0 : SvTreeListEntry* pSearch = m_aControlTree.First();
275 0 : while (pSearch)
276 : {
277 0 : if (pSearch->GetUserData())
278 0 : break;
279 0 : pSearch = m_aControlTree.Next(pSearch);
280 : }
281 : // and select it
282 0 : if (pSearch)
283 : {
284 0 : m_aControlTree.Select(pSearch);
285 0 : m_pLastSelected = pSearch;
286 : }
287 : }
288 :
289 0 : if (m_pLastSelected)
290 : {
291 0 : m_aControlTree.SetSelectHdl(Link());
292 0 : m_aControlTree.SetDeselectHdl(Link());
293 0 : m_aControlTree.Select(m_pLastSelected, !m_aNoAssignment.IsChecked());
294 0 : m_aControlTree.SetSelectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
295 0 : m_aControlTree.SetDeselectHdl(LINK(this, OSelectLabelDialog, OnEntrySelected));
296 : }
297 :
298 0 : return 0L;
299 : }
300 :
301 : //............................................................................
302 : } // namespace pcr
303 : //............................................................................
304 :
305 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|