1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: selectlabeldialog.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "selectlabeldialog.hxx"
34 #ifndef _EXTENSIONS_PROPCTRLR_FORMRESID_HRC_
35 #include "formresid.hrc"
37 #include "formbrowsertools.hxx"
38 #include "formstrings.hxx"
39 #include <com/sun/star/form/FormComponentType.hpp>
40 #include <com/sun/star/container/XChild.hpp>
41 #include <com/sun/star/container/XIndexAccess.hpp>
42 #include <com/sun/star/sdbc/XResultSet.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/lang/XServiceInfo.hpp>
45 #include <comphelper/property.hxx>
46 #include <comphelper/types.hxx>
48 //............................................................................
51 //............................................................................
53 using namespace ::com::sun::star::uno
;
54 using namespace ::com::sun::star::container
;
55 using namespace ::com::sun::star::beans
;
56 using namespace ::com::sun::star::form
;
57 using namespace ::com::sun::star::sdbc
;
58 using namespace ::com::sun::star::lang
;
60 //========================================================================
62 //========================================================================
63 DBG_NAME(OSelectLabelDialog
)
64 //------------------------------------------------------------------------
65 OSelectLabelDialog::OSelectLabelDialog( Window
* pParent
, Reference
< XPropertySet
> _xControlModel
)
66 :ModalDialog(pParent
, PcrRes(RID_DLG_SELECTLABELCONTROL
))
67 ,m_aMainDesc(this, PcrRes(1))
68 ,m_aControlTree(this, PcrRes(1))
69 ,m_aNoAssignment(this, PcrRes(1))
70 ,m_aSeparator(this, PcrRes(1))
71 ,m_aOk(this, PcrRes(1))
72 ,m_aCancel(this, PcrRes(1))
73 ,m_aModelImages(PcrRes(RID_IL_FORMEXPLORER
))
74 ,m_xControlModel(_xControlModel
)
75 ,m_pInitialSelection(NULL
)
76 ,m_pLastSelected(NULL
)
77 ,m_bHaveAssignableControl(sal_False
)
79 DBG_CTOR(OSelectLabelDialog
,NULL
);
81 // initialize the TreeListBox
82 m_aControlTree
.SetSelectionMode( SINGLE_SELECTION
);
83 m_aControlTree
.SetDragDropMode( 0 );
84 m_aControlTree
.EnableInplaceEditing( sal_False
);
85 m_aControlTree
.SetWindowBits(WB_BORDER
| WB_HASLINES
| WB_HASLINESATROOT
| WB_HASBUTTONS
| WB_HASBUTTONSATROOT
| WB_HSCROLL
);
87 m_aControlTree
.SetNodeBitmaps( m_aModelImages
.GetImage( RID_SVXIMG_COLLAPSEDNODE
), m_aModelImages
.GetImage( RID_SVXIMG_EXPANDEDNODE
) );
88 m_aControlTree
.SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
89 m_aControlTree
.SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
91 // fill the description
92 UniString sDescription
= m_aMainDesc
.GetText();
93 sal_Int16 nClassID
= FormComponentType::CONTROL
;
94 if (::comphelper::hasProperty(PROPERTY_CLASSID
, m_xControlModel
))
95 nClassID
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
));
97 sDescription
.SearchAndReplace(String::CreateFromAscii("$control_class$"), GetUIHeadlineName(nClassID
, makeAny(m_xControlModel
)));
98 UniString sName
= ::comphelper::getString(m_xControlModel
->getPropertyValue(PROPERTY_NAME
)).getStr();
99 sDescription
.SearchAndReplace(String::CreateFromAscii("$control_name$"), sName
);
100 m_aMainDesc
.SetText(sDescription
);
102 // search for the root of the form hierarchy
103 Reference
< XChild
> xCont(m_xControlModel
, UNO_QUERY
);
104 Reference
< XInterface
> xSearch( xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ());
105 Reference
< XResultSet
> xParentAsResultSet(xSearch
, UNO_QUERY
);
106 while (xParentAsResultSet
.is())
108 xCont
= Reference
< XChild
> (xSearch
, UNO_QUERY
);
109 xSearch
= xCont
.is() ? xCont
->getParent() : Reference
< XInterface
> ();
110 xParentAsResultSet
= Reference
< XResultSet
> (xSearch
, UNO_QUERY
);
113 // and insert all entries below this root into the listbox
116 // check wich service the allowed components must suppport
117 sal_Int16 nClassId
= 0;
118 try { nClassId
= ::comphelper::getINT16(m_xControlModel
->getPropertyValue(PROPERTY_CLASSID
)); } catch(...) { }
119 m_sRequiredService
= (FormComponentType::RADIOBUTTON
== nClassId
) ? SERVICE_COMPONENT_GROUPBOX
: SERVICE_COMPONENT_FIXEDTEXT
;
120 m_aRequiredControlImage
= m_aModelImages
.GetImage((FormComponentType::RADIOBUTTON
== nClassId
) ? RID_SVXIMG_GROUPBOX
: RID_SVXIMG_FIXEDTEXT
);
122 // calc the currently set label control (so InsertEntries can calc m_pInitialSelection)
123 Any
aCurrentLabelControl( m_xControlModel
->getPropertyValue(PROPERTY_CONTROLLABEL
) );
124 DBG_ASSERT((aCurrentLabelControl
.getValueTypeClass() == TypeClass_INTERFACE
) || !aCurrentLabelControl
.hasValue(),
126 "OSelectLabelDialog::OSelectLabelDialog : invalid ControlLabel property !");
127 if (aCurrentLabelControl
.hasValue())
128 aCurrentLabelControl
>>= m_xInitialLabelControl
;
131 Image aRootImage
= m_aModelImages
.GetImage(RID_SVXIMG_FORMS
);
132 SvLBoxEntry
* pRoot
= m_aControlTree
.InsertEntry(PcrRes(RID_STR_FORMS
), aRootImage
, aRootImage
);
135 m_pInitialSelection
= NULL
;
136 m_bHaveAssignableControl
= sal_False
;
137 InsertEntries(xSearch
, pRoot
);
138 m_aControlTree
.Expand(pRoot
);
141 if (m_pInitialSelection
)
143 m_aControlTree
.MakeVisible(m_pInitialSelection
, sal_True
);
144 m_aControlTree
.Select(m_pInitialSelection
, sal_True
);
148 m_aControlTree
.MakeVisible(m_aControlTree
.First(), sal_True
);
149 if (m_aControlTree
.FirstSelected())
150 m_aControlTree
.Select(m_aControlTree
.FirstSelected(), sal_False
);
151 m_aNoAssignment
.Check(sal_True
);
154 if (!m_bHaveAssignableControl
)
155 { // no controls which can be assigned
156 m_aNoAssignment
.Check(sal_True
);
157 m_aNoAssignment
.Enable(sal_False
);
160 m_aNoAssignment
.SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
161 m_aNoAssignment
.GetClickHdl().Call(&m_aNoAssignment
);
166 //------------------------------------------------------------------------
167 OSelectLabelDialog::~OSelectLabelDialog()
169 // delete the entry datas of the listbox entries
170 SvLBoxEntry
* pLoop
= m_aControlTree
.First();
173 void* pData
= pLoop
->GetUserData();
175 delete (Reference
< XPropertySet
> *)pData
;
176 pLoop
= m_aControlTree
.Next(pLoop
);
179 DBG_DTOR(OSelectLabelDialog
,NULL
);
182 //------------------------------------------------------------------------
183 sal_Int32
OSelectLabelDialog::InsertEntries(const Reference
< XInterface
> & _xContainer
, SvLBoxEntry
* pContainerEntry
)
185 Reference
< XIndexAccess
> xContainer(_xContainer
, UNO_QUERY
);
186 if (!xContainer
.is())
189 sal_Int32 nChildren
= 0;
190 UniString sName
,sDisplayName
;
191 Reference
< XPropertySet
> xAsSet
;
192 for (sal_Int32 i
=0; i
<xContainer
->getCount(); ++i
)
194 xContainer
->getByIndex(i
) >>= xAsSet
;
197 DBG_WARNING("OSelectLabelDialog::InsertEntries : strange : a form component which isn't a property set !");
201 if (!::comphelper::hasProperty(PROPERTY_NAME
, xAsSet
))
202 // we need at least a name for displaying ...
204 sName
= ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_NAME
)).getStr();
206 // we need to check if the control model supports the required service
207 Reference
< XServiceInfo
> xInfo(xAsSet
, UNO_QUERY
);
211 if (!xInfo
->supportsService(m_sRequiredService
))
212 { // perhaps it is a container
213 Reference
< XIndexAccess
> xCont(xAsSet
, UNO_QUERY
);
214 if (xCont
.is() && xCont
->getCount())
215 { // yes -> step down
216 Image aFormImage
= m_aModelImages
.GetImage( RID_SVXIMG_FORM
);
217 SvLBoxEntry
* pCont
= m_aControlTree
.InsertEntry(sName
, aFormImage
, aFormImage
, pContainerEntry
);
218 sal_Int32 nContChildren
= InsertEntries(xCont
, pCont
);
221 m_aControlTree
.Expand(pCont
);
225 { // oops, no valid childs -> remove the entry
226 m_aControlTree
.ModelIsRemoving(pCont
);
227 m_aControlTree
.GetModel()->Remove(pCont
);
228 m_aControlTree
.ModelHasRemoved(pCont
);
235 if (!::comphelper::hasProperty(PROPERTY_LABEL
, xAsSet
))
237 sDisplayName
= ::comphelper::getString(xAsSet
->getPropertyValue(PROPERTY_LABEL
)).getStr();
238 sDisplayName
+= String::CreateFromAscii(" (");
239 sDisplayName
+= sName
;
242 // all requirements met -> insert
243 SvLBoxEntry
* pCurrent
= m_aControlTree
.InsertEntry(sDisplayName
, m_aRequiredControlImage
, m_aRequiredControlImage
, pContainerEntry
);
244 pCurrent
->SetUserData(new Reference
< XPropertySet
> (xAsSet
));
247 if (m_xInitialLabelControl
== xAsSet
)
248 m_pInitialSelection
= pCurrent
;
250 m_bHaveAssignableControl
= sal_True
;
256 //------------------------------------------------------------------------
257 IMPL_LINK(OSelectLabelDialog
, OnEntrySelected
, SvTreeListBox
*, pLB
)
259 DBG_ASSERT(pLB
== &m_aControlTree
, "OSelectLabelDialog::OnEntrySelected : where did this come from ?");
261 SvLBoxEntry
* pSelected
= m_aControlTree
.FirstSelected();
262 void* pData
= pSelected
? pSelected
->GetUserData() : NULL
;
265 m_xSelectedControl
= Reference
< XPropertySet
> (*(Reference
< XPropertySet
> *)pData
);
267 m_aNoAssignment
.SetClickHdl(Link());
268 m_aNoAssignment
.Check(pData
== NULL
);
269 m_aNoAssignment
.SetClickHdl(LINK(this, OSelectLabelDialog
, OnNoAssignmentClicked
));
274 //------------------------------------------------------------------------
275 IMPL_LINK(OSelectLabelDialog
, OnNoAssignmentClicked
, Button
*, pButton
)
277 DBG_ASSERT(pButton
== &m_aNoAssignment
, "OSelectLabelDialog::OnNoAssignmentClicked : where did this come from ?");
280 if (m_aNoAssignment
.IsChecked())
281 m_pLastSelected
= m_aControlTree
.FirstSelected();
284 DBG_ASSERT(m_bHaveAssignableControl
, "OSelectLabelDialog::OnNoAssignmentClicked");
285 // search the first assignable entry
286 SvLBoxEntry
* pSearch
= m_aControlTree
.First();
289 if (pSearch
->GetUserData())
291 pSearch
= m_aControlTree
.Next(pSearch
);
296 m_aControlTree
.Select(pSearch
);
297 m_pLastSelected
= pSearch
;
303 m_aControlTree
.SetSelectHdl(Link());
304 m_aControlTree
.SetDeselectHdl(Link());
305 m_aControlTree
.Select(m_pLastSelected
, !m_aNoAssignment
.IsChecked());
306 m_aControlTree
.SetSelectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
307 m_aControlTree
.SetDeselectHdl(LINK(this, OSelectLabelDialog
, OnEntrySelected
));
313 //............................................................................
315 //............................................................................