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 <propbrw.hxx>
22 #include <basidesh.hxx>
23 #include <dlgedobj.hxx>
25 #include <baside3.hxx>
26 #include <strings.hrc>
28 #include <strings.hxx>
30 #include <com/sun/star/frame/Frame.hpp>
31 #include <com/sun/star/inspection/XObjectInspector.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <comphelper/types.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <cppuhelper/component_context.hxx>
36 #include <tools/debug.hxx>
37 #include <svx/svditer.hxx>
38 #include <svx/svdview.hxx>
39 #include <toolkit/helper/vclunohelper.hxx>
40 #include <comphelper/diagnose_ex.hxx>
41 #include <vcl/layout.hxx>
42 #include <vcl/stdtext.hxx>
43 #include <vcl/weld.hxx>
50 using namespace ::com::sun::star
;
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::lang
;
53 using namespace ::com::sun::star::frame
;
54 using namespace ::com::sun::star::beans
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::comphelper
;
59 void PropBrw::Update( const SfxViewShell
* pShell
)
61 Shell
const* pIdeShell
= dynamic_cast<Shell
const*>(pShell
);
62 OSL_ENSURE( pIdeShell
|| !pShell
, "PropBrw::Update: invalid shell!" );
64 ImplUpdate(pIdeShell
->GetCurrentDocument(), pIdeShell
->GetCurDlgView());
66 ImplUpdate(nullptr, pShell
->GetDrawView());
68 ImplUpdate(nullptr, nullptr);
75 const tools::Long STD_WIN_SIZE_X
= 300;
76 const tools::Long STD_WIN_SIZE_Y
= 350;
78 const tools::Long STD_MIN_SIZE_X
= 250;
79 const tools::Long STD_MIN_SIZE_Y
= 250;
81 const tools::Long WIN_BORDER
= 2;
85 static Reference
<XModel
> lclGetModel()
87 if (SfxViewShell
* pCurrent
= SfxViewShell::Current())
88 return pCurrent
->GetCurrentDocument();
89 return Reference
<XModel
>();
92 PropBrw::PropBrw (DialogWindowLayout
& rLayout_
):
93 DockingWindow(&rLayout_
),
94 m_xContentArea(VclPtr
<VclVBox
>::Create(this)),
95 m_bInitialStateChange(true),
96 m_xContextDocument(lclGetModel()),
99 Size
aPropWinSize(STD_WIN_SIZE_X
,STD_WIN_SIZE_Y
);
100 SetMinOutputSizePixel(Size(STD_MIN_SIZE_X
,STD_MIN_SIZE_Y
));
101 SetOutputSizePixel(aPropWinSize
);
103 // turn off WB_CLIPCHILDREN otherwise the bg won't extend "under"
104 // transparent children of the widget
105 m_xContentArea
->SetControlBackground(m_xContentArea
->GetSettings().GetStyleSettings().GetWindowColor());
106 m_xContentArea
->SetBackground(m_xContentArea
->GetControlBackground());
107 m_xContentArea
->SetStyle(m_xContentArea
->GetStyle() & ~WB_CLIPCHILDREN
);
108 m_xContentArea
->Show();
112 // create a frame wrapper for myself
113 m_xMeAsFrame
= frame::Frame::create( comphelper::getProcessComponentContext() );
114 m_xMeAsFrame
->initialize(VCLUnoHelper::GetInterface(m_xContentArea
));
115 m_xMeAsFrame
->setName( u
"form property browser"_ustr
); // change name!
117 catch (const Exception
&)
119 OSL_FAIL("PropBrw::PropBrw: could not create/initialize my frame!");
120 m_xMeAsFrame
.clear();
123 ImplReCreateController();
127 void PropBrw::ImplReCreateController()
129 OSL_PRECOND( m_xMeAsFrame
.is(), "PropBrw::ImplCreateController: no frame for myself!" );
130 if ( !m_xMeAsFrame
.is() )
133 if ( m_xBrowserController
.is() )
134 ImplDestroyController();
138 const Reference
< XComponentContext
>& xOwnContext
= comphelper::getProcessComponentContext();
140 // a ComponentContext for the
141 ::cppu::ContextEntry_Init aHandlerContextInfo
[] =
143 ::cppu::ContextEntry_Init( u
"DialogParentWindow"_ustr
, Any(VCLUnoHelper::GetInterface(this))),
144 ::cppu::ContextEntry_Init( u
"ContextDocument"_ustr
, Any( m_xContextDocument
) )
146 Reference
< XComponentContext
> xInspectorContext(
147 ::cppu::createComponentContext( aHandlerContextInfo
, std::size( aHandlerContextInfo
), xOwnContext
) );
149 // create a property browser controller
150 Reference
< XMultiComponentFactory
> xFactory( xInspectorContext
->getServiceManager(), UNO_SET_THROW
);
151 static constexpr OUString s_sControllerServiceName
= u
"com.sun.star.awt.PropertyBrowserController"_ustr
;
152 m_xBrowserController
.set( xFactory
->createInstanceWithContext( s_sControllerServiceName
, xInspectorContext
), UNO_QUERY
);
153 if ( !m_xBrowserController
.is() )
155 vcl::Window
* pWin
= GetParent();
156 ShowServiceNotAvailableError(pWin
? pWin
->GetFrameWeld() : nullptr, s_sControllerServiceName
, true);
160 Reference
< XController
> xAsXController( m_xBrowserController
, UNO_QUERY
);
161 DBG_ASSERT(xAsXController
.is(), "PropBrw::PropBrw: invalid controller object!");
162 if (!xAsXController
.is())
164 ::comphelper::disposeComponent(m_xBrowserController
);
165 m_xBrowserController
.clear();
169 xAsXController
->attachFrame( Reference
<XFrame
>(m_xMeAsFrame
,UNO_QUERY_THROW
) );
173 Point
aPropWinPos( WIN_BORDER
, WIN_BORDER
);
174 Size
aPropWinSize(STD_WIN_SIZE_X
,STD_WIN_SIZE_Y
);
175 aPropWinSize
.AdjustWidth( -(2*WIN_BORDER
) );
176 aPropWinSize
.AdjustHeight( -(2*WIN_BORDER
) );
178 VclContainer::setLayoutAllocation(*m_xContentArea
, aPropWinPos
, aPropWinSize
);
179 m_xContentArea
->Show();
181 catch (const Exception
&)
183 OSL_FAIL("PropBrw::PropBrw: could not create/initialize the browser controller!");
186 ::comphelper::disposeComponent(m_xBrowserController
);
188 catch(const Exception
&)
192 m_xBrowserController
.clear();
202 void PropBrw::dispose()
204 if ( m_xBrowserController
.is() )
205 ImplDestroyController();
206 m_xContentArea
.disposeAndClear();
207 DockingWindow::dispose();
211 void PropBrw::ImplDestroyController()
213 implSetNewObject( Reference
< XPropertySet
>() );
215 if ( m_xMeAsFrame
.is() )
216 m_xMeAsFrame
->setComponent( nullptr, nullptr );
218 Reference
< XController
> xAsXController( m_xBrowserController
, UNO_QUERY
);
219 if ( xAsXController
.is() )
220 xAsXController
->attachFrame( nullptr );
224 ::comphelper::disposeComponent( m_xBrowserController
);
226 catch( const Exception
& )
228 DBG_UNHANDLED_EXCEPTION("basctl");
231 m_xBrowserController
.clear();
234 bool PropBrw::Close()
236 ImplDestroyController();
238 return DockingWindow::Close();
241 Sequence
< Reference
< XInterface
> >
242 PropBrw::CreateMultiSelectionSequence( const SdrMarkList
& _rMarkList
)
244 Sequence
< Reference
< XInterface
> > aSeq
;
245 InterfaceArray aInterfaces
;
247 const size_t nMarkCount
= _rMarkList
.GetMarkCount();
248 for( size_t i
= 0 ; i
< nMarkCount
; ++i
)
250 SdrObject
* pCurrent
= _rMarkList
.GetMark(i
)->GetMarkedSdrObj();
251 assert(pCurrent
&& "GetMarkedSdrObj will succeed");
253 std::optional
<SdrObjListIter
> oGroupIterator
;
254 if (pCurrent
->IsGroupObject())
256 oGroupIterator
.emplace(pCurrent
->GetSubList());
257 pCurrent
= oGroupIterator
->IsMore() ? oGroupIterator
->Next() : nullptr;
262 if (DlgEdObj
* pDlgEdObj
= dynamic_cast<DlgEdObj
*>(pCurrent
))
264 Reference
< XInterface
> xControlInterface(pDlgEdObj
->GetUnoControlModel(), UNO_QUERY
);
265 if (xControlInterface
.is())
266 aInterfaces
.push_back(xControlInterface
);
270 pCurrent
= oGroupIterator
&& oGroupIterator
->IsMore() ? oGroupIterator
->Next() : nullptr;
274 sal_Int32 nCount
= aInterfaces
.size();
275 aSeq
.realloc( nCount
);
276 Reference
< XInterface
>* pInterfaces
= aSeq
.getArray();
277 for( sal_Int32 i
= 0 ; i
< nCount
; i
++ )
278 pInterfaces
[i
] = aInterfaces
[i
];
284 void PropBrw::implSetNewObjectSequence
285 ( const Sequence
< Reference
< XInterface
> >& _rObjectSeq
)
287 Reference
< inspection::XObjectInspector
> xObjectInspector(m_xBrowserController
, UNO_QUERY
);
288 if ( xObjectInspector
.is() )
290 xObjectInspector
->inspect( _rObjectSeq
);
292 OUString aText
= IDEResId(RID_STR_BRWTITLE_PROPERTIES
)
293 + IDEResId(RID_STR_BRWTITLE_MULTISELECT
);
299 void PropBrw::implSetNewObject( const Reference
< XPropertySet
>& _rxObject
)
301 if ( m_xBrowserController
.is() )
303 m_xBrowserController
->setPropertyValue( u
"IntrospectedObject"_ustr
,
307 // set the new title according to the selected object
308 SetText( GetHeadlineName( _rxObject
) );
313 OUString
PropBrw::GetHeadlineName( const Reference
< XPropertySet
>& _rxObject
)
316 Reference
< lang::XServiceInfo
> xServiceInfo( _rxObject
, UNO_QUERY
);
318 if (xServiceInfo
.is()) // single selection
321 aName
= IDEResId(RID_STR_BRWTITLE_PROPERTIES
);
323 if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlDialogModel"_ustr
) )
325 sResId
= RID_STR_CLASS_DIALOG
;
327 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlButtonModel"_ustr
) )
329 sResId
= RID_STR_CLASS_BUTTON
;
331 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlRadioButtonModel"_ustr
) )
333 sResId
= RID_STR_CLASS_RADIOBUTTON
;
335 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlCheckBoxModel"_ustr
) )
337 sResId
= RID_STR_CLASS_CHECKBOX
;
339 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlListBoxModel"_ustr
) )
341 sResId
= RID_STR_CLASS_LISTBOX
;
343 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlComboBoxModel"_ustr
) )
345 sResId
= RID_STR_CLASS_COMBOBOX
;
347 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlGroupBoxModel"_ustr
) )
349 sResId
= RID_STR_CLASS_GROUPBOX
;
351 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlEditModel"_ustr
) )
353 sResId
= RID_STR_CLASS_EDIT
;
355 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlFixedTextModel"_ustr
) )
357 sResId
= RID_STR_CLASS_FIXEDTEXT
;
359 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlImageControlModel"_ustr
) )
361 sResId
= RID_STR_CLASS_IMAGECONTROL
;
363 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlProgressBarModel"_ustr
) )
365 sResId
= RID_STR_CLASS_PROGRESSBAR
;
367 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlScrollBarModel"_ustr
) )
369 sResId
= RID_STR_CLASS_SCROLLBAR
;
371 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlFixedLineModel"_ustr
) )
373 sResId
= RID_STR_CLASS_FIXEDLINE
;
375 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlDateFieldModel"_ustr
) )
377 sResId
= RID_STR_CLASS_DATEFIELD
;
379 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlTimeFieldModel"_ustr
) )
381 sResId
= RID_STR_CLASS_TIMEFIELD
;
383 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlNumericFieldModel"_ustr
) )
385 sResId
= RID_STR_CLASS_NUMERICFIELD
;
387 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlCurrencyFieldModel"_ustr
) )
389 sResId
= RID_STR_CLASS_CURRENCYFIELD
;
391 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlFormattedFieldModel"_ustr
) )
393 sResId
= RID_STR_CLASS_FORMATTEDFIELD
;
395 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlPatternFieldModel"_ustr
) )
397 sResId
= RID_STR_CLASS_PATTERNFIELD
;
399 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlFileControlModel"_ustr
) )
401 sResId
= RID_STR_CLASS_FILECONTROL
;
403 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.tree.TreeControlModel"_ustr
) )
405 sResId
= RID_STR_CLASS_TREECONTROL
;
407 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.grid.UnoControlGridModel"_ustr
) )
409 sResId
= RID_STR_CLASS_GRIDCONTROL
;
411 else if ( xServiceInfo
->supportsService( u
"com.sun.star.awt.UnoControlFixedHyperlinkModel"_ustr
) )
413 sResId
= RID_STR_CLASS_HYPERLINKCONTROL
;
417 sResId
= RID_STR_CLASS_CONTROL
;
420 if (!sResId
.isEmpty())
425 else if (!_rxObject
.is()) // no properties
427 aName
= IDEResId(RID_STR_BRWTITLE_NO_PROPERTIES
);
433 void PropBrw::ImplUpdate( const Reference
< XModel
>& _rxContextDocument
, SdrView
* pNewView
)
435 Reference
< XModel
> xContextDocument( _rxContextDocument
);
437 // if we should simply "empty" ourself, assume the context document didn't change
440 OSL_ENSURE( !_rxContextDocument
.is(), "PropBrw::ImplUpdate: no view, but a document?!" );
441 xContextDocument
= m_xContextDocument
;
444 if ( xContextDocument
!= m_xContextDocument
)
446 m_xContextDocument
= std::move(xContextDocument
);
447 ImplReCreateController();
454 EndListening(pView
->GetModel());
463 // set focus on initialization
464 if ( m_bInitialStateChange
)
466 m_xContentArea
->GrabFocus();
467 m_bInitialStateChange
= false;
470 const SdrMarkList
& rMarkList
= pView
->GetMarkedObjectList();
471 const size_t nMarkCount
= rMarkList
.GetMarkCount();
473 if ( nMarkCount
== 0 )
475 EndListening(pView
->GetModel());
477 implSetNewObject( nullptr );
481 Reference
< XPropertySet
> xNewObject
;
482 Sequence
< Reference
< XInterface
> > aNewObjects
;
483 if ( nMarkCount
== 1 )
485 if (DlgEdObj
* pDlgEdObj
= dynamic_cast<DlgEdObj
*>(rMarkList
.GetMark(0)->GetMarkedSdrObj()))
487 if ( pDlgEdObj
->IsGroupObject() ) // group object
488 aNewObjects
= CreateMultiSelectionSequence( rMarkList
);
489 else // single selection
490 xNewObject
.set(pDlgEdObj
->GetUnoControlModel(), css::uno::UNO_QUERY
);
493 else if ( nMarkCount
> 1 ) // multiple selection
495 aNewObjects
= CreateMultiSelectionSequence( rMarkList
);
498 if ( aNewObjects
.hasElements() )
499 implSetNewObjectSequence( aNewObjects
);
501 implSetNewObject( xNewObject
);
503 StartListening(pView
->GetModel());
505 catch ( const PropertyVetoException
& ) { /* silence */ }
506 catch ( const Exception
& )
508 DBG_UNHANDLED_EXCEPTION("basctl");
512 } // namespace basctl
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */