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 .
21 #include <sal/macros.h>
24 #include <fmPropBrw.hxx>
25 #include <svx/strings.hrc>
26 #include <fmservs.hxx>
27 #include <fmshimp.hxx>
28 #include <fmpgeimp.hxx>
30 #include <svx/dialmgr.hxx>
31 #include <svx/fmpage.hxx>
32 #include <svx/fmshell.hxx>
33 #include <svx/fmview.hxx>
34 #include <svx/sdrpagewindow.hxx>
35 #include <svx/svdpagv.hxx>
36 #include <svx/svxids.hrc>
38 #include <com/sun/star/awt/XControlContainer.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/form/XForm.hpp>
41 #include <com/sun/star/form/FormComponentType.hpp>
42 #include <com/sun/star/form/inspection/DefaultFormComponentInspectorModel.hpp>
43 #include <com/sun/star/frame/Frame.hpp>
44 #include <com/sun/star/inspection/ObjectInspector.hpp>
45 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
46 #include <com/sun/star/inspection/DefaultHelpProvider.hpp>
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #include <com/sun/star/util/VetoException.hpp>
50 #include <comphelper/processfactory.hxx>
51 #include <comphelper/property.hxx>
52 #include <comphelper/sequence.hxx>
53 #include <comphelper/types.hxx>
54 #include <cppuhelper/component_context.hxx>
55 #include <o3tl/deleter.hxx>
56 #include <sfx2/bindings.hxx>
57 #include <sfx2/childwin.hxx>
58 #include <sfx2/objitem.hxx>
59 #include <sfx2/objsh.hxx>
60 #include <tools/diagnose_ex.h>
61 #include <unotools/confignode.hxx>
62 #include <vcl/stdtext.hxx>
63 #include <vcl/svapp.hxx>
64 #include <vcl/weld.hxx>
65 #include <vcl/weldutils.hxx>
67 using namespace ::com::sun::star
;
68 using namespace ::com::sun::star::beans
;
69 using namespace ::com::sun::star::container
;
70 using namespace ::com::sun::star::form
;
71 using namespace ::com::sun::star::form::inspection
;
72 using namespace ::com::sun::star::frame
;
73 using namespace ::com::sun::star::inspection
;
74 using namespace ::com::sun::star::lang
;
75 using namespace ::com::sun::star::uno
;
76 using namespace ::com::sun::star::util
;
77 using namespace ::svxform
;
78 using ::com::sun::star::awt::XWindow
;
81 SFX_IMPL_MODELESSDIALOGCONTOLLER(FmPropBrwMgr
, SID_FM_SHOW_PROPERTIES
)
83 FmPropBrwMgr::FmPropBrwMgr( vcl::Window
* _pParent
, sal_uInt16 _nId
,
84 SfxBindings
* _pBindings
, const SfxChildWinInfo
* _pInfo
)
85 :SfxChildWindow(_pParent
, _nId
)
87 std::shared_ptr
<FmPropBrw
> xControl(new FmPropBrw(::comphelper::getProcessComponentContext(), _pBindings
,
88 this, _pParent
->GetFrameWeld(), _pInfo
), o3tl::default_delete
<FmPropBrw
>());
89 SetController(std::move(xControl
));
90 static_cast<FmPropBrw
*>(GetController().get())->Initialize( _pInfo
);
93 static OUString
GetUIHeadlineName(sal_Int16 nClassId
, const Any
& aUnoObj
)
95 const char* pClassNameResourceId
= nullptr;
99 case FormComponentType::TEXTFIELD
:
101 Reference
< XInterface
> xIFace
;
103 pClassNameResourceId
= RID_STR_PROPTITLE_EDIT
;
105 { // we have a chance to check if it's a formatted field model
106 Reference
< XServiceInfo
> xInfo(xIFace
, UNO_QUERY
);
107 if (xInfo
.is() && (xInfo
->supportsService(FM_SUN_COMPONENT_FORMATTEDFIELD
)))
108 pClassNameResourceId
= RID_STR_PROPTITLE_FORMATTED
;
109 else if (!xInfo
.is())
111 // couldn't distinguish between formatted and edit with the service name, so try with the properties
112 Reference
< XPropertySet
> xProps(xIFace
, UNO_QUERY
);
115 Reference
< XPropertySetInfo
> xPropsInfo
= xProps
->getPropertySetInfo();
116 if (xPropsInfo
.is() && xPropsInfo
->hasPropertyByName(FM_PROP_FORMATSSUPPLIER
))
117 pClassNameResourceId
= RID_STR_PROPTITLE_FORMATTED
;
124 case FormComponentType::COMMANDBUTTON
:
125 pClassNameResourceId
= RID_STR_PROPTITLE_PUSHBUTTON
; break;
126 case FormComponentType::RADIOBUTTON
:
127 pClassNameResourceId
= RID_STR_PROPTITLE_RADIOBUTTON
; break;
128 case FormComponentType::CHECKBOX
:
129 pClassNameResourceId
= RID_STR_PROPTITLE_CHECKBOX
; break;
130 case FormComponentType::LISTBOX
:
131 pClassNameResourceId
= RID_STR_PROPTITLE_LISTBOX
; break;
132 case FormComponentType::COMBOBOX
:
133 pClassNameResourceId
= RID_STR_PROPTITLE_COMBOBOX
; break;
134 case FormComponentType::GROUPBOX
:
135 pClassNameResourceId
= RID_STR_PROPTITLE_GROUPBOX
; break;
136 case FormComponentType::IMAGEBUTTON
:
137 pClassNameResourceId
= RID_STR_PROPTITLE_IMAGEBUTTON
; break;
138 case FormComponentType::FIXEDTEXT
:
139 pClassNameResourceId
= RID_STR_PROPTITLE_FIXEDTEXT
; break;
140 case FormComponentType::GRIDCONTROL
:
141 pClassNameResourceId
= RID_STR_PROPTITLE_DBGRID
; break;
142 case FormComponentType::FILECONTROL
:
143 pClassNameResourceId
= RID_STR_PROPTITLE_FILECONTROL
; break;
144 case FormComponentType::DATEFIELD
:
145 pClassNameResourceId
= RID_STR_PROPTITLE_DATEFIELD
; break;
146 case FormComponentType::TIMEFIELD
:
147 pClassNameResourceId
= RID_STR_PROPTITLE_TIMEFIELD
; break;
148 case FormComponentType::NUMERICFIELD
:
149 pClassNameResourceId
= RID_STR_PROPTITLE_NUMERICFIELD
; break;
150 case FormComponentType::CURRENCYFIELD
:
151 pClassNameResourceId
= RID_STR_PROPTITLE_CURRENCYFIELD
; break;
152 case FormComponentType::PATTERNFIELD
:
153 pClassNameResourceId
= RID_STR_PROPTITLE_PATTERNFIELD
; break;
154 case FormComponentType::IMAGECONTROL
:
155 pClassNameResourceId
= RID_STR_PROPTITLE_IMAGECONTROL
; break;
156 case FormComponentType::HIDDENCONTROL
:
157 pClassNameResourceId
= RID_STR_PROPTITLE_HIDDEN
; break;
158 case FormComponentType::SCROLLBAR
:
159 pClassNameResourceId
= RID_STR_PROPTITLE_SCROLLBAR
; break;
160 case FormComponentType::SPINBUTTON
:
161 pClassNameResourceId
= RID_STR_PROPTITLE_SPINBUTTON
; break;
162 case FormComponentType::NAVIGATIONBAR
:
163 pClassNameResourceId
= RID_STR_PROPTITLE_NAVBAR
; break;
164 case FormComponentType::CONTROL
:
166 pClassNameResourceId
= RID_STR_CONTROL
; break;
169 return SvxResId(pClassNameResourceId
);
172 FmPropBrw::FmPropBrw(const Reference
< XComponentContext
>& _xORB
, SfxBindings
* _pBindings
,
173 SfxChildWindow
* _pMgr
, weld::Window
* _pParent
, const SfxChildWinInfo
* _pInfo
)
174 : SfxModelessDialogController(_pBindings
, _pMgr
, _pParent
, "svx/ui/formpropertydialog.ui", "FormPropertyDialog")
175 , SfxControllerItem(SID_FM_PROPERTY_CONTROL
, *_pBindings
)
176 , m_bInitialStateChange(true)
177 , m_pParent(_pParent
)
178 , m_nAsyncGetFocusId(nullptr)
179 , m_xContainer(m_xBuilder
->weld_container("container"))
182 m_xContainer
->set_size_request(m_xContainer
->get_approximate_digit_width() * 72, m_xContainer
->get_text_height() * 20);
186 // create a frame wrapper for myself
187 m_xMeAsFrame
= Frame::create(m_xORB
);
189 // transport the container area of this dialog to be the container window of the frame
190 css::uno::Reference
<css::awt::XWindow
> xFrameContainerWindow(new weld::TransportAsXWindow(m_xContainer
.get()));
191 m_xMeAsFrame
->initialize(xFrameContainerWindow
);
192 m_xMeAsFrame
->setName("form property browser");
194 catch (const Exception
&)
196 OSL_FAIL("FmPropBrw::FmPropBrw: could not create/initialize my frame!");
197 m_xMeAsFrame
.clear();
201 m_sLastActivePage
= _pInfo
->aExtraString
;
204 FmPropBrw::~FmPropBrw()
206 if (m_nAsyncGetFocusId
)
208 Application::RemoveUserEvent(m_nAsyncGetFocusId
);
209 m_nAsyncGetFocusId
= nullptr;
212 if (m_xBrowserController
.is())
213 implDetachController();
216 // remove our own properties from the component context. We cannot ensure that the component context
217 // is freed (there might be refcount problems :-\), so at least ensure the context itself
218 // does hold the objects anymore
219 Reference
<XNameContainer
> xName(m_xInspectorContext
,uno::UNO_QUERY
);
222 const OUString pProps
[] = { OUString( "ContextDocument" )
223 , OUString( "DialogParentWindow" )
224 , OUString( "ControlContext" )
225 , OUString( "ControlShapeAccess" ) };
226 for (const auto & i
: pProps
)
227 xName
->removeByName( i
);
230 catch (const Exception
& )
232 DBG_UNHANDLED_EXCEPTION("svx");
234 ::SfxControllerItem::dispose();
237 OUString
FmPropBrw::getCurrentPage() const
239 OUString sCurrentPage
;
242 if ( m_xBrowserController
.is() )
244 OSL_VERIFY( m_xBrowserController
->getViewData() >>= sCurrentPage
);
247 if ( sCurrentPage
.isEmpty() )
248 sCurrentPage
= m_sLastActivePage
;
250 catch( const Exception
& )
252 TOOLS_WARN_EXCEPTION("svx.form", "caught an exception while retrieving the current page!");
257 void FmPropBrw::implDetachController()
259 m_sLastActivePage
= getCurrentPage();
261 implSetNewSelection( InterfaceBag() );
263 if ( m_xMeAsFrame
.is() )
267 m_xMeAsFrame
->setComponent(nullptr, nullptr);
269 catch( const Exception
& )
271 TOOLS_WARN_EXCEPTION("svx.form", "caught an exception while resetting the component!");
275 // we attached a frame to the controller manually, so we need to manually tell it that it's detached, too
276 if ( m_xBrowserController
.is() )
278 m_xBrowserController
->attachFrame( nullptr );
281 m_xBrowserController
.clear();
282 m_xInspectorModel
.clear();
283 m_xMeAsFrame
.clear();
286 void FmPropBrw::Close()
288 // suspend the controller (it is allowed to veto)
289 if ( m_xMeAsFrame
.is() )
293 Reference
< XController
> xController( m_xMeAsFrame
->getController() );
294 if ( xController
.is() && !xController
->suspend( true ) )
297 catch( const Exception
& )
299 TOOLS_WARN_EXCEPTION("svx.form", "caught an exception while asking the controller!");
303 implDetachController();
305 // remember our bindings: while we're closed, we're deleted, too, so accessing the bindings after this
307 // 10/19/00 - 79321 - FS
308 SfxBindings
& rBindings
= SfxControllerItem::GetBindings();
310 SfxModelessDialogController::Close();
312 rBindings
.Invalidate(SID_FM_CTL_PROPERTIES
);
313 rBindings
.Invalidate(SID_FM_PROPERTIES
);
316 bool FmPropBrw::implIsReadOnlyModel() const
320 if ( m_xInspectorModel
.is() )
321 return m_xInspectorModel
->getIsReadOnly();
324 catch( const Exception
& )
326 DBG_UNHANDLED_EXCEPTION("svx");
332 void FmPropBrw::implSetNewSelection( const InterfaceBag
& _rSelection
)
334 if ( !m_xBrowserController
.is() )
339 Reference
< XObjectInspector
> xInspector( m_xBrowserController
, UNO_QUERY_THROW
);
341 // tell it the objects to inspect
342 xInspector
->inspect( comphelper::containerToSequence(_rSelection
) );
344 catch( const VetoException
& )
348 catch( const Exception
& )
350 TOOLS_WARN_EXCEPTION("svx.form", "");
354 // set the new title according to the selected object
357 if ( _rSelection
.empty() )
359 sTitle
= SvxResId(RID_STR_NO_PROPERTIES
);
361 else if ( _rSelection
.size() > 1 )
363 // no form component and (no form or no name) -> Multiselection
364 sTitle
= SvxResId(RID_STR_PROPERTIES_CONTROL
) +
365 SvxResId(RID_STR_PROPTITLE_MULTISELECT
);
369 Reference
< XPropertySet
> xSingleSelection( *_rSelection
.begin(), UNO_QUERY
);
370 if ( ::comphelper::hasProperty( FM_PROP_CLASSID
, xSingleSelection
) )
372 sal_Int16 nClassID
= FormComponentType::CONTROL
;
373 xSingleSelection
->getPropertyValue( FM_PROP_CLASSID
) >>= nClassID
;
375 sTitle
= SvxResId(RID_STR_PROPERTIES_CONTROL
) +
376 GetUIHeadlineName(nClassID
, makeAny(xSingleSelection
));
378 else if ( Reference
< XForm
>( xSingleSelection
, UNO_QUERY
).is() )
379 sTitle
= SvxResId(RID_STR_PROPERTIES_FORM
);
382 if ( implIsReadOnlyModel() )
383 sTitle
+= SvxResId(RID_STR_READONLY_VIEW
);
385 m_xDialog
->set_title(sTitle
);
388 void FmPropBrw::FillInfo( SfxChildWinInfo
& rInfo
) const
390 rInfo
.bVisible
= false;
391 rInfo
.aExtraString
= getCurrentPage();
394 IMPL_LINK_NOARG( FmPropBrw
, OnAsyncGetFocus
, void*, void )
396 m_nAsyncGetFocusId
= nullptr;
401 bool lcl_shouldEnableHelpSection( const Reference
< XComponentContext
>& _rxContext
)
403 ::utl::OConfigurationTreeRoot
aConfiguration(
404 ::utl::OConfigurationTreeRoot::createWithComponentContext(
406 "/org.openoffice.Office.Common/Forms/PropertyBrowser/" ) );
408 bool bEnabled
= false;
409 OSL_VERIFY( aConfiguration
.getNodeValue( "DirectHelp" ) >>= bEnabled
);
414 void FmPropBrw::impl_createPropertyBrowser_throw( FmFormShell
* _pFormShell
)
416 // the document in which we live
417 Reference
< XInterface
> xDocument
;
418 if ( _pFormShell
&& _pFormShell
->GetObjectShell() )
419 xDocument
= _pFormShell
->GetObjectShell()->GetModel();
421 // the context of the controls in our document
422 Reference
< awt::XControlContainer
> xControlContext
;
423 if ( _pFormShell
&& _pFormShell
->GetFormView() )
425 SdrPageView
* pPageView
= _pFormShell
->GetFormView()->GetSdrPageView();
429 SdrPageWindow
* pPageWindow
= pPageView
->GetPageWindow(0);
433 xControlContext
= pPageWindow
->GetControlContainer();
438 // the default parent window for message boxes
439 Reference
< XWindow
> xParentWindow(m_xDialog
->GetXWindow());
441 // the mapping from control models to control shapes
442 Reference
< XMap
> xControlMap
;
443 FmFormPage
* pFormPage
= _pFormShell
? _pFormShell
->GetCurPage() : nullptr;
445 xControlMap
= pFormPage
->GetImpl().getControlToShapeMap();
447 // our own component context
449 // a ComponentContext for the
450 ::cppu::ContextEntry_Init aHandlerContextInfo
[] =
452 ::cppu::ContextEntry_Init( "ContextDocument", makeAny( xDocument
) ),
453 ::cppu::ContextEntry_Init( "DialogParentWindow", makeAny( xParentWindow
) ),
454 ::cppu::ContextEntry_Init( "ControlContext", makeAny( xControlContext
) ),
455 ::cppu::ContextEntry_Init( "ControlShapeAccess", makeAny( xControlMap
) )
457 m_xInspectorContext
.set(
458 ::cppu::createComponentContext( aHandlerContextInfo
, SAL_N_ELEMENTS( aHandlerContextInfo
),
461 bool bEnableHelpSection
= lcl_shouldEnableHelpSection( m_xORB
);
463 // an object inspector model
466 ? DefaultFormComponentInspectorModel::createWithHelpSection( m_xInspectorContext
, 3, 5 )
467 : DefaultFormComponentInspectorModel::createDefault( m_xInspectorContext
);
469 // an object inspector
470 m_xBrowserController
=
471 ObjectInspector::createWithModel(
472 m_xInspectorContext
, m_xInspectorModel
475 if ( !m_xBrowserController
.is() )
477 ShowServiceNotAvailableError(m_pParent
, u
"com.sun.star.inspection.ObjectInspector", true);
481 m_xBrowserController
->attachFrame( Reference
<XFrame
>(m_xMeAsFrame
,UNO_QUERY_THROW
) );
484 if ( bEnableHelpSection
)
486 Reference
< XObjectInspector
> xInspector( m_xBrowserController
, UNO_QUERY_THROW
);
487 Reference
< XObjectInspectorUI
> xInspectorUI( xInspector
->getInspectorUI() );
488 DefaultHelpProvider::create( m_xInspectorContext
, xInspectorUI
);
493 void FmPropBrw::impl_ensurePropertyBrowser_nothrow( FmFormShell
* _pFormShell
)
495 // the document in which we live
496 Reference
< XInterface
> xDocument
;
497 SfxObjectShell
* pObjectShell
= _pFormShell
? _pFormShell
->GetObjectShell() : nullptr;
499 xDocument
= pObjectShell
->GetModel();
500 if ( ( xDocument
== m_xLastKnownDocument
) && m_xBrowserController
.is() )
506 // clean up any previous instances of the object inspector
507 if ( m_xMeAsFrame
.is() )
508 m_xMeAsFrame
->setComponent( nullptr, nullptr );
510 ::comphelper::disposeComponent( m_xBrowserController
);
511 m_xBrowserController
.clear();
512 m_xInspectorModel
.clear();
514 // and create a new one
515 impl_createPropertyBrowser_throw( _pFormShell
);
517 catch( const Exception
& )
519 DBG_UNHANDLED_EXCEPTION("svx");
521 m_xLastKnownDocument
= xDocument
;
525 void FmPropBrw::StateChanged(sal_uInt16 nSID
, SfxItemState eState
, const SfxPoolItem
* pState
)
527 if (!pState
|| SID_FM_PROPERTY_CONTROL
!= nSID
)
532 if (eState
>= SfxItemState::DEFAULT
)
534 FmFormShell
* pShell
= dynamic_cast<FmFormShell
*>( static_cast<const SfxObjectItem
*>(pState
)->GetShell() );
535 InterfaceBag aSelection
;
537 pShell
->GetImpl()->getCurrentSelection_Lock(aSelection
);
539 impl_ensurePropertyBrowser_nothrow( pShell
);
541 // set the new object to inspect
542 implSetNewSelection( aSelection
);
544 // if this is the first time we're here, some additional things need to be done ...
545 if ( m_bInitialStateChange
)
547 // if we're just newly created, we want to have the focus
548 m_nAsyncGetFocusId
= Application::PostUserEvent(LINK(this, FmPropBrw
, OnAsyncGetFocus
));
550 // and additionally, we want to show the page which was active during
551 // our previous incarnation
552 if ( !m_sLastActivePage
.isEmpty() )
556 if ( m_xBrowserController
.is() )
557 m_xBrowserController
->restoreViewData( makeAny( m_sLastActivePage
) );
559 catch( const Exception
& )
561 TOOLS_WARN_EXCEPTION("svx.form",
562 "caught an exception while setting the initial page!");
566 m_bInitialStateChange
= false;
572 implSetNewSelection( InterfaceBag() );
577 TOOLS_WARN_EXCEPTION("svx.form", "");
581 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */