LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / basctl / source / dlged / propbrw.cxx
blob4a848124964080e3a57376b28e17d9ad9f37f286
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 .
21 #include <propbrw.hxx>
22 #include <basidesh.hxx>
23 #include <dlgedobj.hxx>
24 #include <iderid.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 <tools/diagnose_ex.h>
41 #include <vcl/layout.hxx>
42 #include <vcl/stdtext.hxx>
43 #include <vcl/weld.hxx>
45 #include <memory>
47 namespace basctl
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!" );
63 if (pIdeShell)
64 ImplUpdate(pIdeShell->GetCurrentDocument(), pIdeShell->GetCurDlgView());
65 else if (pShell)
66 ImplUpdate(nullptr, pShell->GetDrawView());
67 else
68 ImplUpdate(nullptr, nullptr);
72 namespace
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;
83 } // namespace
85 PropBrw::PropBrw (DialogWindowLayout& rLayout_):
86 DockingWindow(&rLayout_),
87 m_xContentArea(VclPtr<VclVBox>::Create(this)),
88 m_bInitialStateChange(true),
89 m_xContextDocument(SfxViewShell::Current() ? SfxViewShell::Current()->GetCurrentDocument() : Reference<XModel>()),
90 pView(nullptr)
92 Size aPropWinSize(STD_WIN_SIZE_X,STD_WIN_SIZE_Y);
93 SetMinOutputSizePixel(Size(STD_MIN_SIZE_X,STD_MIN_SIZE_Y));
94 SetOutputSizePixel(aPropWinSize);
96 // turn off WB_CLIPCHILDREN otherwise the bg won't extend "under"
97 // transparent children of the widget
98 m_xContentArea->SetControlBackground(m_xContentArea->GetSettings().GetStyleSettings().GetWindowColor());
99 m_xContentArea->SetBackground(m_xContentArea->GetControlBackground());
100 m_xContentArea->SetStyle(m_xContentArea->GetStyle() & ~WB_CLIPCHILDREN);
101 m_xContentArea->Show();
105 // create a frame wrapper for myself
106 m_xMeAsFrame = frame::Frame::create( comphelper::getProcessComponentContext() );
107 m_xMeAsFrame->initialize(VCLUnoHelper::GetInterface(m_xContentArea));
108 m_xMeAsFrame->setName( "form property browser" ); // change name!
110 catch (const Exception&)
112 OSL_FAIL("PropBrw::PropBrw: could not create/initialize my frame!");
113 m_xMeAsFrame.clear();
116 ImplReCreateController();
120 void PropBrw::ImplReCreateController()
122 OSL_PRECOND( m_xMeAsFrame.is(), "PropBrw::ImplCreateController: no frame for myself!" );
123 if ( !m_xMeAsFrame.is() )
124 return;
126 if ( m_xBrowserController.is() )
127 ImplDestroyController();
131 Reference< XComponentContext > xOwnContext = comphelper::getProcessComponentContext();
133 // a ComponentContext for the
134 ::cppu::ContextEntry_Init aHandlerContextInfo[] =
136 ::cppu::ContextEntry_Init( "DialogParentWindow", Any(VCLUnoHelper::GetInterface(this))),
137 ::cppu::ContextEntry_Init( "ContextDocument", Any( m_xContextDocument ) )
139 Reference< XComponentContext > xInspectorContext(
140 ::cppu::createComponentContext( aHandlerContextInfo, SAL_N_ELEMENTS( aHandlerContextInfo ), xOwnContext ) );
142 // create a property browser controller
143 Reference< XMultiComponentFactory > xFactory( xInspectorContext->getServiceManager(), UNO_SET_THROW );
144 static constexpr OUStringLiteral s_sControllerServiceName = u"com.sun.star.awt.PropertyBrowserController";
145 m_xBrowserController.set( xFactory->createInstanceWithContext( s_sControllerServiceName, xInspectorContext ), UNO_QUERY );
146 if ( !m_xBrowserController.is() )
148 vcl::Window* pWin = GetParent();
149 ShowServiceNotAvailableError(pWin ? pWin->GetFrameWeld() : nullptr, s_sControllerServiceName, true);
151 else
153 Reference< XController > xAsXController( m_xBrowserController, UNO_QUERY );
154 DBG_ASSERT(xAsXController.is(), "PropBrw::PropBrw: invalid controller object!");
155 if (!xAsXController.is())
157 ::comphelper::disposeComponent(m_xBrowserController);
158 m_xBrowserController.clear();
160 else
162 xAsXController->attachFrame( Reference<XFrame>(m_xMeAsFrame,UNO_QUERY_THROW) );
166 Point aPropWinPos( WIN_BORDER, WIN_BORDER );
167 Size aPropWinSize(STD_WIN_SIZE_X,STD_WIN_SIZE_Y);
168 aPropWinSize.AdjustWidth( -(2*WIN_BORDER) );
169 aPropWinSize.AdjustHeight( -(2*WIN_BORDER) );
171 VclContainer::setLayoutAllocation(*m_xContentArea, aPropWinPos, aPropWinSize);
172 m_xContentArea->Show();
174 catch (const Exception&)
176 OSL_FAIL("PropBrw::PropBrw: could not create/initialize the browser controller!");
179 ::comphelper::disposeComponent(m_xBrowserController);
181 catch(const Exception&)
185 m_xBrowserController.clear();
187 Resize();
190 PropBrw::~PropBrw()
192 disposeOnce();
195 void PropBrw::dispose()
197 if ( m_xBrowserController.is() )
198 ImplDestroyController();
199 m_xContentArea.disposeAndClear();
200 DockingWindow::dispose();
204 void PropBrw::ImplDestroyController()
206 implSetNewObject( Reference< XPropertySet >() );
208 if ( m_xMeAsFrame.is() )
209 m_xMeAsFrame->setComponent( nullptr, nullptr );
211 Reference< XController > xAsXController( m_xBrowserController, UNO_QUERY );
212 if ( xAsXController.is() )
213 xAsXController->attachFrame( nullptr );
217 ::comphelper::disposeComponent( m_xBrowserController );
219 catch( const Exception& )
221 DBG_UNHANDLED_EXCEPTION("basctl");
224 m_xBrowserController.clear();
227 bool PropBrw::Close()
229 ImplDestroyController();
231 return DockingWindow::Close();
234 Sequence< Reference< XInterface > >
235 PropBrw::CreateMultiSelectionSequence( const SdrMarkList& _rMarkList )
237 Sequence< Reference< XInterface > > aSeq;
238 InterfaceArray aInterfaces;
240 const size_t nMarkCount = _rMarkList.GetMarkCount();
241 for( size_t i = 0 ; i < nMarkCount ; ++i )
243 SdrObject* pCurrent = _rMarkList.GetMark(i)->GetMarkedSdrObj();
245 std::unique_ptr<SdrObjListIter> pGroupIterator;
246 if (pCurrent->IsGroupObject())
248 pGroupIterator.reset(new SdrObjListIter(pCurrent->GetSubList()));
249 pCurrent = pGroupIterator->IsMore() ? pGroupIterator->Next() : nullptr;
252 while (pCurrent)
254 if (DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pCurrent))
256 Reference< XInterface > xControlInterface(pDlgEdObj->GetUnoControlModel(), UNO_QUERY);
257 if (xControlInterface.is())
258 aInterfaces.push_back(xControlInterface);
261 // next element
262 pCurrent = pGroupIterator && pGroupIterator->IsMore() ? pGroupIterator->Next() : nullptr;
266 sal_Int32 nCount = aInterfaces.size();
267 aSeq.realloc( nCount );
268 Reference< XInterface >* pInterfaces = aSeq.getArray();
269 for( sal_Int32 i = 0 ; i < nCount ; i++ )
270 pInterfaces[i] = aInterfaces[i];
272 return aSeq;
276 void PropBrw::implSetNewObjectSequence
277 ( const Sequence< Reference< XInterface > >& _rObjectSeq )
279 Reference< inspection::XObjectInspector > xObjectInspector(m_xBrowserController, UNO_QUERY);
280 if ( xObjectInspector.is() )
282 xObjectInspector->inspect( _rObjectSeq );
284 OUString aText = IDEResId(RID_STR_BRWTITLE_PROPERTIES)
285 + IDEResId(RID_STR_BRWTITLE_MULTISELECT);
286 SetText( aText );
291 void PropBrw::implSetNewObject( const Reference< XPropertySet >& _rxObject )
293 if ( m_xBrowserController.is() )
295 m_xBrowserController->setPropertyValue( "IntrospectedObject",
296 Any( _rxObject )
299 // set the new title according to the selected object
300 SetText( GetHeadlineName( _rxObject ) );
305 OUString PropBrw::GetHeadlineName( const Reference< XPropertySet >& _rxObject )
307 OUString aName;
308 Reference< lang::XServiceInfo > xServiceInfo( _rxObject, UNO_QUERY );
310 if (xServiceInfo.is()) // single selection
312 OUString sResId;
313 aName = IDEResId(RID_STR_BRWTITLE_PROPERTIES);
315 if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlDialogModel" ) )
317 sResId = RID_STR_CLASS_DIALOG;
319 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlButtonModel" ) )
321 sResId = RID_STR_CLASS_BUTTON;
323 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlRadioButtonModel" ) )
325 sResId = RID_STR_CLASS_RADIOBUTTON;
327 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCheckBoxModel" ) )
329 sResId = RID_STR_CLASS_CHECKBOX;
331 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlListBoxModel" ) )
333 sResId = RID_STR_CLASS_LISTBOX;
335 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlComboBoxModel" ) )
337 sResId = RID_STR_CLASS_COMBOBOX;
339 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlGroupBoxModel" ) )
341 sResId = RID_STR_CLASS_GROUPBOX;
343 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlEditModel" ) )
345 sResId = RID_STR_CLASS_EDIT;
347 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedTextModel" ) )
349 sResId = RID_STR_CLASS_FIXEDTEXT;
351 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlImageControlModel" ) )
353 sResId = RID_STR_CLASS_IMAGECONTROL;
355 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlProgressBarModel" ) )
357 sResId = RID_STR_CLASS_PROGRESSBAR;
359 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlScrollBarModel" ) )
361 sResId = RID_STR_CLASS_SCROLLBAR;
363 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedLineModel" ) )
365 sResId = RID_STR_CLASS_FIXEDLINE;
367 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlDateFieldModel" ) )
369 sResId = RID_STR_CLASS_DATEFIELD;
371 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlTimeFieldModel" ) )
373 sResId = RID_STR_CLASS_TIMEFIELD;
375 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlNumericFieldModel" ) )
377 sResId = RID_STR_CLASS_NUMERICFIELD;
379 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCurrencyFieldModel" ) )
381 sResId = RID_STR_CLASS_CURRENCYFIELD;
383 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFormattedFieldModel" ) )
385 sResId = RID_STR_CLASS_FORMATTEDFIELD;
387 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlPatternFieldModel" ) )
389 sResId = RID_STR_CLASS_PATTERNFIELD;
391 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFileControlModel" ) )
393 sResId = RID_STR_CLASS_FILECONTROL;
395 else if ( xServiceInfo->supportsService( "com.sun.star.awt.tree.TreeControlModel" ) )
397 sResId = RID_STR_CLASS_TREECONTROL;
399 else if ( xServiceInfo->supportsService( "com.sun.star.awt.grid.UnoControlGridModel" ) )
401 sResId = RID_STR_CLASS_GRIDCONTROL;
403 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedHyperlinkModel" ) )
405 sResId = RID_STR_CLASS_HYPERLINKCONTROL;
407 else
409 sResId = RID_STR_CLASS_CONTROL;
412 if (!sResId.isEmpty())
414 aName += sResId;
417 else if (!_rxObject.is()) // no properties
419 aName = IDEResId(RID_STR_BRWTITLE_NO_PROPERTIES);
422 return aName;
425 void PropBrw::ImplUpdate( const Reference< XModel >& _rxContextDocument, SdrView* pNewView )
427 Reference< XModel > xContextDocument( _rxContextDocument );
429 // if we should simply "empty" ourself, assume the context document didn't change
430 if ( !pNewView )
432 OSL_ENSURE( !_rxContextDocument.is(), "PropBrw::ImplUpdate: no view, but a document?!" );
433 xContextDocument = m_xContextDocument;
436 if ( xContextDocument != m_xContextDocument )
438 m_xContextDocument = xContextDocument;
439 ImplReCreateController();
444 if ( pView )
446 EndListening( *(pView->GetModel()) );
447 pView = nullptr;
450 if ( !pNewView )
451 return;
453 pView = pNewView;
455 // set focus on initialization
456 if ( m_bInitialStateChange )
458 m_xContentArea->GrabFocus();
459 m_bInitialStateChange = false;
462 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
463 const size_t nMarkCount = rMarkList.GetMarkCount();
465 if ( nMarkCount == 0 )
467 EndListening( *(pView->GetModel()) );
468 pView = nullptr;
469 implSetNewObject( nullptr );
470 return;
473 Reference< XPropertySet > xNewObject;
474 Sequence< Reference< XInterface > > aNewObjects;
475 if ( nMarkCount == 1 )
477 if (DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(rMarkList.GetMark(0)->GetMarkedSdrObj()))
479 if ( pDlgEdObj->IsGroupObject() ) // group object
480 aNewObjects = CreateMultiSelectionSequence( rMarkList );
481 else // single selection
482 xNewObject.set(pDlgEdObj->GetUnoControlModel(), css::uno::UNO_QUERY);
485 else if ( nMarkCount > 1 ) // multiple selection
487 aNewObjects = CreateMultiSelectionSequence( rMarkList );
490 if ( aNewObjects.hasElements() )
491 implSetNewObjectSequence( aNewObjects );
492 else
493 implSetNewObject( xNewObject );
495 StartListening( *(pView->GetModel()) );
497 catch ( const PropertyVetoException& ) { /* silence */ }
498 catch ( const Exception& )
500 DBG_UNHANDLED_EXCEPTION("basctl");
504 } // namespace basctl
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */