android: Update app-specific/MIME type icons
[LibreOffice.git] / basctl / source / dlged / dlged.cxx
blob1917e3ff4a5656f345970a819d885c5b2c7ef7af
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 <dlged.hxx>
22 #include <dlgedclip.hxx>
23 #include <dlgeddef.hxx>
24 #include <dlgedfac.hxx>
25 #include <dlgedfunc.hxx>
26 #include <dlgedmod.hxx>
27 #include <dlgedobj.hxx>
28 #include <dlgedpage.hxx>
29 #include <dlgedview.hxx>
30 #include <localizationmgr.hxx>
31 #include <baside3.hxx>
33 #include <com/sun/star/awt/Toolkit.hpp>
34 #include <com/sun/star/awt/UnoControlDialog.hpp>
35 #include <com/sun/star/awt/XVclWindowPeer.hpp>
36 #include <com/sun/star/resource/StringResource.hpp>
37 #include <com/sun/star/util/XCloneable.hpp>
38 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
39 #include <comphelper/types.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <tools/debug.hxx>
42 #include <svl/itempool.hxx>
43 #include <svx/sdrpaintwindow.hxx>
44 #include <svx/svdpagv.hxx>
45 #include <toolkit/helper/vclunohelper.hxx>
46 #include <vcl/print.hxx>
47 #include <vcl/svapp.hxx>
48 #include <xmlscript/xml_helper.hxx>
49 #include <xmlscript/xmldlg_imexp.hxx>
50 #include <osl/diagnose.h>
52 namespace basctl
55 using namespace comphelper;
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::beans;
59 using namespace ::com::sun::star::io;
61 constexpr OUStringLiteral aResourceResolverPropName = u"ResourceResolver";
62 constexpr OUStringLiteral aDecorationPropName = u"Decoration";
65 // DlgEdHint
68 DlgEdHint::DlgEdHint(Kind eHint)
69 : eKind(eHint)
70 , pDlgEdObj(nullptr)
74 DlgEdHint::DlgEdHint(Kind eHint, DlgEdObj* pObj)
75 : eKind(eHint)
76 , pDlgEdObj(pObj)
80 DlgEdHint::~DlgEdHint()
85 // DlgEditor
88 void DlgEditor::ShowDialog()
90 uno::Reference< uno::XComponentContext > xContext = getProcessComponentContext();
92 // create a dialog
93 uno::Reference< awt::XUnoControlDialog > xDlg = awt::UnoControlDialog::create( xContext );
95 // clone the dialog model
96 uno::Reference< util::XCloneable > xC( m_xUnoControlDialogModel, uno::UNO_QUERY );
97 uno::Reference< util::XCloneable > xNew = xC->createClone();
98 uno::Reference< awt::XControlModel > xDlgMod( xNew, uno::UNO_QUERY );
100 uno::Reference< beans::XPropertySet > xSrcDlgModPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
101 uno::Reference< beans::XPropertySet > xNewDlgModPropSet( xDlgMod, uno::UNO_QUERY );
102 if( xNewDlgModPropSet.is() )
104 if( xSrcDlgModPropSet.is() )
108 Any aResourceResolver = xSrcDlgModPropSet->getPropertyValue( aResourceResolverPropName );
109 xNewDlgModPropSet->setPropertyValue( aResourceResolverPropName, aResourceResolver );
111 catch(const UnknownPropertyException& )
113 OSL_FAIL( "DlgEditor::ShowDialog(): No ResourceResolver property" );
117 // Disable decoration
120 bool bDecoration = true;
122 Any aDecorationAny = xSrcDlgModPropSet->getPropertyValue( aDecorationPropName );
123 aDecorationAny >>= bDecoration;
124 if( !bDecoration )
126 xNewDlgModPropSet->setPropertyValue( aDecorationPropName, Any( true ) );
127 xNewDlgModPropSet->setPropertyValue( "Title", Any( OUString() ) );
130 catch(const UnknownPropertyException& )
134 // set the model
135 xDlg->setModel( xDlgMod );
137 // create a peer
138 uno::Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
139 xDlg->createPeer( xToolkit, rWindow.GetComponentInterface() );
141 xDlg->execute();
143 // need to cast because of multiple inheritance
144 Reference<awt::XControl>(xDlg)->dispose();
148 bool DlgEditor::UnmarkDialog()
150 SdrObject* pDlgObj = pDlgEdModel->GetPage(0)->GetObj(0);
151 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
153 bool bWasMarked = pDlgEdView->IsObjMarked( pDlgObj );
155 if( bWasMarked )
156 pDlgEdView->MarkObj( pDlgObj, pPgView, true );
158 return bWasMarked;
162 bool DlgEditor::RemarkDialog()
164 SdrObject* pDlgObj = pDlgEdModel->GetPage(0)->GetObj(0);
165 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
167 bool bWasMarked = pDlgEdView->IsObjMarked( pDlgObj );
169 if( !bWasMarked )
170 pDlgEdView->MarkObj( pDlgObj, pPgView );
172 return bWasMarked;
176 DlgEditor::DlgEditor (
177 vcl::Window& rWindow_, DialogWindowLayout& rLayout_,
178 css::uno::Reference<css::frame::XModel> const& xModel,
179 css::uno::Reference<css::container::XNameContainer> const & xDialogModel
181 :pHScroll(nullptr)
182 ,pVScroll(nullptr)
183 ,pDlgEdModel(new DlgEdModel())
184 ,pDlgEdPage(new DlgEdPage(*pDlgEdModel))
185 // set clipboard data flavors
186 ,m_ClipboardDataFlavors{ { /* MimeType */ "application/vnd.sun.xml.dialog",
187 /* HumanPresentableName */ "Dialog 6.0",
188 /* DataType */ cppu::UnoType<Sequence< sal_Int8 >>::get() } }
189 ,m_ClipboardDataFlavorsResource{ m_ClipboardDataFlavors[0],
190 { /* MimeType */ "application/vnd.sun.xml.dialogwithresource",
191 /* HumanPresentableName */ "Dialog 8.0",
192 /* DataType */ cppu::UnoType<Sequence< sal_Int8 >>::get() } }
193 ,pObjFac(new DlgEdFactory(xModel))
194 ,rWindow(rWindow_)
195 ,pFunc(new DlgEdFuncSelect(*this))
196 ,rLayout(rLayout_)
197 ,eMode( DlgEditor::SELECT )
198 ,eActObj( SdrObjKind::BasicDialogPushButton )
199 ,bFirstDraw(false)
200 ,bCreateOK(true)
201 ,bDialogModelChanged(false)
202 ,aMarkIdle("basctl DlgEditor Mark")
203 ,mnPaintGuard(0)
204 ,m_xDocument( xModel )
206 pDlgEdModel->GetItemPool().FreezeIdRanges();
207 pDlgEdView.reset(new DlgEdView(*pDlgEdModel, *rWindow_.GetOutDev(), *this));
208 pDlgEdModel->SetScaleUnit( MapUnit::Map100thMM );
210 SdrLayerAdmin& rAdmin = pDlgEdModel->GetLayerAdmin();
211 rAdmin.NewLayer( rAdmin.GetControlLayerName() );
212 rAdmin.NewLayer( "HiddenLayer" );
214 pDlgEdModel->InsertPage(pDlgEdPage);
216 aMarkIdle.SetInvokeHandler( LINK( this, DlgEditor, MarkTimeout ) );
218 rWindow.SetMapMode( MapMode( MapUnit::Map100thMM ) );
219 pDlgEdPage->SetSize( rWindow.PixelToLogic( Size(DLGED_PAGE_WIDTH_MIN, DLGED_PAGE_HEIGHT_MIN) ) );
221 pDlgEdView->ShowSdrPage(pDlgEdView->GetModel().GetPage(0));
222 pDlgEdView->SetLayerVisible( "HiddenLayer", false );
223 pDlgEdView->SetMoveSnapOnlyTopLeft(true);
224 pDlgEdView->SetWorkArea( tools::Rectangle( Point( 0, 0 ), pDlgEdPage->GetSize() ) );
226 Size aGridSize( 100, 100 ); // 100TH_MM
227 pDlgEdView->SetGridCoarse( aGridSize );
228 pDlgEdView->SetSnapGridWidth(Fraction(aGridSize.Width(), 1), Fraction(aGridSize.Height(), 1));
229 pDlgEdView->SetGridSnap( true );
230 pDlgEdView->SetGridVisible( false );
231 pDlgEdView->SetDragStripes(false);
233 pDlgEdView->SetDesignMode();
235 ::comphelper::disposeComponent( m_xControlContainer );
237 SetDialog(xDialogModel);
240 DlgEditor::~DlgEditor()
242 aMarkIdle.Stop();
244 ::comphelper::disposeComponent( m_xControlContainer );
247 Reference< awt::XControlContainer > const & DlgEditor::GetWindowControlContainer()
249 if (!m_xControlContainer.is())
250 m_xControlContainer = VCLUnoHelper::CreateControlContainer(&rWindow);
251 return m_xControlContainer;
254 void DlgEditor::SetScrollBars(ScrollAdaptor* pHS, ScrollAdaptor* pVS)
256 pHScroll = pHS;
257 pVScroll = pVS;
259 InitScrollBars();
262 void DlgEditor::InitScrollBars()
264 DBG_ASSERT( pHScroll, "DlgEditor::InitScrollBars: no horizontal scroll bar!" );
265 DBG_ASSERT( pVScroll, "DlgEditor::InitScrollBars: no vertical scroll bar!" );
266 if ( !pHScroll || !pVScroll )
267 return;
269 Size aOutSize = rWindow.GetOutDev()->GetOutputSize();
270 Size aPgSize = pDlgEdPage->GetSize();
272 pHScroll->SetRange( Range( 0, aPgSize.Width() ));
273 pVScroll->SetRange( Range( 0, aPgSize.Height() ));
274 pHScroll->SetVisibleSize( aOutSize.Width() );
275 pVScroll->SetVisibleSize( aOutSize.Height() );
277 pHScroll->SetLineSize( aOutSize.Width() / 10 );
278 pVScroll->SetLineSize( aOutSize.Height() / 10 );
279 pHScroll->SetPageSize( aOutSize.Width() / 2 );
280 pVScroll->SetPageSize( aOutSize.Height() / 2 );
282 DoScroll();
286 void DlgEditor::DoScroll()
288 if( !pHScroll || !pVScroll )
289 return;
291 MapMode aMap = rWindow.GetMapMode();
292 Point aOrg = aMap.GetOrigin();
294 Size aScrollPos( pHScroll->GetThumbPos(), pVScroll->GetThumbPos() );
295 aScrollPos = rWindow.LogicToPixel( aScrollPos );
296 aScrollPos = rWindow.PixelToLogic( aScrollPos );
298 tools::Long nX = aScrollPos.Width() + aOrg.X();
299 tools::Long nY = aScrollPos.Height() + aOrg.Y();
301 if( !nX && !nY )
302 return;
304 rWindow.PaintImmediately();
306 // #i31562#
307 // When scrolling, someone was rescuing the Wallpaper and forced the window scroll to
308 // be done without background refresh. I do not know why, but that causes the repaint
309 // problems. Taking that out.
310 // Wallpaper aOldBackground = rWindow.GetBackground();
311 // rWindow.SetBackground();
313 // #i74769# children should be scrolled
314 rWindow.Scroll( -nX, -nY, ScrollFlags::Children);
315 aMap.SetOrigin( Point( -aScrollPos.Width(), -aScrollPos.Height() ) );
316 rWindow.SetMapMode( aMap );
317 rWindow.PaintImmediately();
319 DlgEdHint aHint( DlgEdHint::WINDOWSCROLLED );
320 Broadcast( aHint );
324 void DlgEditor::UpdateScrollBars()
326 MapMode aMap = rWindow.GetMapMode();
327 Point aOrg = aMap.GetOrigin();
329 if ( pHScroll )
330 pHScroll->SetThumbPos( -aOrg.X() );
332 if ( pVScroll )
333 pVScroll->SetThumbPos( -aOrg.Y() );
337 void DlgEditor::SetDialog( const uno::Reference< container::XNameContainer >& xUnoControlDialogModel )
339 // set dialog model
340 m_xUnoControlDialogModel = xUnoControlDialogModel;
342 // create dialog form
343 pDlgEdForm = new DlgEdForm(*pDlgEdModel, *this);
344 uno::Reference< awt::XControlModel > xDlgMod( m_xUnoControlDialogModel , uno::UNO_QUERY );
345 pDlgEdForm->SetUnoControlModel(xDlgMod);
346 static_cast<DlgEdPage*>(pDlgEdModel->GetPage(0))->SetDlgEdForm( pDlgEdForm.get() );
347 pDlgEdModel->GetPage(0)->InsertObject( pDlgEdForm.get() );
348 AdjustPageSize();
349 pDlgEdForm->SetRectFromProps();
350 pDlgEdForm->UpdateTabIndices(); // for backward compatibility
351 pDlgEdForm->StartListening();
353 // create controls
354 if ( m_xUnoControlDialogModel.is() )
356 // get sequence of control names
357 Sequence< OUString > aNames = m_xUnoControlDialogModel->getElementNames();
358 const OUString* pNames = aNames.getConstArray();
359 sal_Int32 nCtrls = aNames.getLength();
361 // create a map of tab indices and control names, sorted by tab index
362 IndexToNameMap aIndexToNameMap;
363 for ( sal_Int32 i = 0; i < nCtrls; ++i )
365 // get name
366 OUString aName( pNames[i] );
368 // get tab index
369 sal_Int16 nTabIndex = -1;
370 Any aCtrl = m_xUnoControlDialogModel->getByName( aName );
371 Reference< css::beans::XPropertySet > xPSet;
372 aCtrl >>= xPSet;
373 if ( xPSet.is() )
374 xPSet->getPropertyValue( DLGED_PROP_TABINDEX ) >>= nTabIndex;
376 // insert into map
377 aIndexToNameMap.emplace( nTabIndex, aName );
380 // create controls and insert them into drawing page
381 for (auto const& indexToName : aIndexToNameMap)
383 Any aCtrl = m_xUnoControlDialogModel->getByName( indexToName.second );
384 Reference< css::awt::XControlModel > xCtrlModel;
385 aCtrl >>= xCtrlModel;
386 rtl::Reference<DlgEdObj> pCtrlObj = new DlgEdObj(*pDlgEdModel);
387 pCtrlObj->SetUnoControlModel( xCtrlModel );
388 pCtrlObj->SetDlgEdForm( pDlgEdForm.get() );
389 pDlgEdForm->AddChild( pCtrlObj.get() );
390 pDlgEdModel->GetPage(0)->InsertObject( pCtrlObj.get() );
391 pCtrlObj->SetRectFromProps();
392 pCtrlObj->UpdateStep();
393 pCtrlObj->StartListening();
397 bFirstDraw = true;
399 pDlgEdModel->SetChanged(false);
402 void DlgEditor::ResetDialog ()
404 DlgEdForm* pOldDlgEdForm = pDlgEdForm.get();
405 DlgEdPage* pPage = static_cast<DlgEdPage*>(pDlgEdModel->GetPage(0));
406 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
407 bool bWasMarked = pDlgEdView->IsObjMarked( pOldDlgEdForm );
408 pDlgEdView->UnmarkAll();
410 // clear SdrObjects with broadcasting
411 pPage->ClearSdrObjList();
413 pPage->SetDlgEdForm( nullptr );
414 SetDialog( m_xUnoControlDialogModel );
415 if( bWasMarked )
416 pDlgEdView->MarkObj( pDlgEdForm.get(), pPgView );
420 Reference< util::XNumberFormatsSupplier > const & DlgEditor::GetNumberFormatsSupplier()
422 if ( !m_xSupplier.is() )
424 Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
425 Reference< util::XNumberFormatsSupplier > xSupplier( util::NumberFormatsSupplier::createWithDefaultLocale(xContext) );
427 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
428 if ( !m_xSupplier.is() )
430 m_xSupplier = xSupplier;
433 return m_xSupplier;
437 void DlgEditor::MouseButtonDown( const MouseEvent& rMEvt )
439 rWindow.GrabFocus();
440 pFunc->MouseButtonDown( rMEvt );
444 void DlgEditor::MouseButtonUp( const MouseEvent& rMEvt )
446 bool bRet = pFunc->MouseButtonUp( rMEvt );
448 if( eMode == DlgEditor::INSERT )
449 bCreateOK = bRet;
453 void DlgEditor::MouseMove( const MouseEvent& rMEvt )
455 pFunc->MouseMove( rMEvt );
459 bool DlgEditor::KeyInput( const KeyEvent& rKEvt )
461 return pFunc->KeyInput( rKEvt );
465 void DlgEditor::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
467 aPaintRect = rRect;
468 mnPaintGuard++;
470 if (bFirstDraw && rWindow.IsVisible() && (rRenderContext.GetOutputSize() != Size()))
472 bFirstDraw = false;
474 // get property set
475 css::uno::Reference<css::beans::XPropertySet> xPSet(pDlgEdForm->GetUnoControlModel(), css::uno::UNO_QUERY);
477 if (xPSet.is())
479 // get dialog size from properties
480 sal_Int32 nWidth = 0, nHeight = 0;
481 xPSet->getPropertyValue( DLGED_PROP_WIDTH ) >>= nWidth;
482 xPSet->getPropertyValue( DLGED_PROP_HEIGHT ) >>= nHeight;
484 if (nWidth == 0 && nHeight == 0)
486 Size aSize = rRenderContext.PixelToLogic( Size( 400, 300 ) );
488 // align with grid
489 Size aGridSize_(tools::Long(pDlgEdView->GetSnapGridWidthX()), tools::Long(pDlgEdView->GetSnapGridWidthY()));
490 aSize.AdjustWidth( -(aSize.Width() % aGridSize_.Width()) );
491 aSize.AdjustHeight( -(aSize.Height() % aGridSize_.Height()) );
493 Point aPos;
494 Size aOutSize = rRenderContext.GetOutputSize();
495 aPos.setX( (aOutSize.Width()>>1) - (aSize.Width()>>1) );
496 aPos.setY( (aOutSize.Height()>>1) - (aSize.Height()>>1) );
498 // align with grid
499 aPos.AdjustX( -(aPos.X() % aGridSize_.Width()) );
500 aPos.AdjustY( -(aPos.Y() % aGridSize_.Height()) );
502 // don't put in the corner
503 Point aMinPos = rRenderContext.PixelToLogic( Point( 30, 20 ) );
504 if( (aPos.X() < aMinPos.X()) || (aPos.Y() < aMinPos.Y()) )
506 aPos = aMinPos;
507 aPos.AdjustX( -(aPos.X() % aGridSize_.Width()) );
508 aPos.AdjustY( -(aPos.Y() % aGridSize_.Height()) );
511 // set dialog position and size
512 pDlgEdForm->SetSnapRect( tools::Rectangle( aPos, aSize ) );
513 pDlgEdForm->EndListening(false);
514 pDlgEdForm->SetPropsFromRect();
515 pDlgEdForm->GetDlgEditor().SetDialogModelChanged();
516 pDlgEdForm->StartListening();
518 // set position and size of controls
519 if (const size_t nObjCount = pDlgEdPage->GetObjCount())
521 for (size_t i = 0 ; i < nObjCount ; ++i)
523 if (DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pDlgEdPage->GetObj(i)))
525 if (!dynamic_cast<DlgEdForm*>(pDlgEdObj))
527 pDlgEdObj->SetRectFromProps();
536 // repaint, get PageView and prepare Region
537 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
538 const vcl::Region aPaintRectRegion(aPaintRect);
540 // #i74769#
541 SdrPaintWindow* pTargetPaintWindow = nullptr;
543 // mark repaint start
544 if (pPgView)
546 pTargetPaintWindow = pPgView->GetView().BeginDrawLayers(&rRenderContext, aPaintRectRegion);
547 OSL_ENSURE(pTargetPaintWindow, "BeginDrawLayers: Got no SdrPaintWindow (!)");
550 // draw background self using wallpaper
551 // #i79128# ...and use correct OutDev for that
552 if (pTargetPaintWindow)
554 Color maBackColor = rRenderContext.GetSettings().GetStyleSettings().GetLightColor();
555 OutputDevice& rTargetOutDev = pTargetPaintWindow->GetTargetOutputDevice();
556 rTargetOutDev.DrawWallpaper(aPaintRect, Wallpaper(maBackColor));
559 // do paint (unbuffered) and mark repaint end
560 if (pPgView)
562 // paint of control layer is done in EndDrawLayers anyway...
563 pPgView->GetView().EndDrawLayers(*pTargetPaintWindow, true);
566 mnPaintGuard--;
570 IMPL_LINK_NOARG(DlgEditor, MarkTimeout, Timer *, void)
572 rLayout.UpdatePropertyBrowser();
576 void DlgEditor::SetMode (Mode eNewMode )
578 if ( eNewMode != eMode )
580 if ( eNewMode == INSERT )
581 pFunc.reset(new DlgEdFuncInsert(*this));
582 else
583 pFunc.reset(new DlgEdFuncSelect(*this));
585 if ( eNewMode == READONLY )
586 pDlgEdModel->SetReadOnly( true );
587 else
588 pDlgEdModel->SetReadOnly( false );
591 if ( eNewMode == TEST )
592 ShowDialog();
594 eMode = eNewMode;
598 void DlgEditor::SetInsertObj(SdrObjKind eObj)
600 eActObj = eObj;
602 pDlgEdView->SetCurrentObj( eActObj, SdrInventor::BasicDialog );
605 void DlgEditor::CreateDefaultObject()
607 // create object by factory
608 rtl::Reference<SdrObject> pObj = SdrObjFactory::MakeNewObject(
609 *pDlgEdModel,
610 pDlgEdView->GetCurrentObjInventor(),
611 pDlgEdView->GetCurrentObjIdentifier());
613 DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj.get());
614 if (!pDlgEdObj)
615 return;
617 // set position and size
618 Size aSize = rWindow.PixelToLogic( Size( 96, 24 ) );
619 Point aPoint = pDlgEdForm->GetSnapRect().Center();
620 aPoint.AdjustX( -(aSize.Width() / 2) );
621 aPoint.AdjustY( -(aSize.Height() / 2) );
622 pDlgEdObj->SetSnapRect( tools::Rectangle( aPoint, aSize ) );
624 // set default property values
625 pDlgEdObj->SetDefaults();
627 // insert object into drawing page
628 SdrPageView* pPageView = pDlgEdView->GetSdrPageView();
629 if (pDlgEdView->InsertObjectAtView(pDlgEdObj, *pPageView))
631 // start listening
632 pDlgEdObj->StartListening();
636 void DlgEditor::Cut()
638 Copy();
639 Delete();
642 static void implCopyStreamToByteSequence( const Reference< XInputStream >& xStream,
643 Sequence< sal_Int8 >& bytes )
645 xStream->readBytes( bytes, xStream->available() );
646 for (;;)
648 Sequence< sal_Int8 > readBytes;
649 sal_Int32 nRead = xStream->readBytes( readBytes, 1024 );
650 if (! nRead)
651 break;
653 sal_Int32 nPos = bytes.getLength();
654 bytes.realloc( nPos + nRead );
655 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), static_cast<sal_uInt32>(nRead) );
659 void DlgEditor::Copy()
661 if( !pDlgEdView->AreObjectsMarked() )
662 return;
664 // stop all drawing actions
665 pDlgEdView->BrkAction();
667 // create an empty clipboard dialog model
668 Reference< util::XCloneable > xClone( m_xUnoControlDialogModel, UNO_QUERY );
669 Reference< util::XCloneable > xNewClone = xClone->createClone();
670 Reference< container::XNameContainer > xClipDialogModel( xNewClone, UNO_QUERY );
672 if ( xClipDialogModel.is() )
674 Sequence< OUString > aNames = xClipDialogModel->getElementNames();
675 const OUString* pNames = aNames.getConstArray();
676 sal_uInt32 nCtrls = aNames.getLength();
678 for ( sal_uInt32 n = 0; n < nCtrls; n++ )
680 xClipDialogModel->removeByName( pNames[n] );
684 // insert control models of marked objects into clipboard dialog model
685 const size_t nMark = pDlgEdView->GetMarkedObjectList().GetMarkCount();
686 for( size_t i = 0; i < nMark; ++i )
688 SdrObject* pObj = pDlgEdView->GetMarkedObjectList().GetMark(i)->GetMarkedSdrObj();
689 DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj);
691 if (pDlgEdObj && !dynamic_cast<DlgEdForm*>(pDlgEdObj))
693 OUString aName;
694 Reference< beans::XPropertySet > xMarkPSet(pDlgEdObj->GetUnoControlModel(), uno::UNO_QUERY);
695 if (xMarkPSet.is())
697 xMarkPSet->getPropertyValue( DLGED_PROP_NAME ) >>= aName;
700 if ( m_xUnoControlDialogModel.is() && m_xUnoControlDialogModel->hasByName(aName) )
702 Any aCtrl = m_xUnoControlDialogModel->getByName( aName );
704 // clone control model
705 Reference< util::XCloneable > xCtrl;
706 aCtrl >>= xCtrl;
707 Reference< util::XCloneable > xNewCtrl = xCtrl->createClone();
708 Any aNewCtrl;
709 aNewCtrl <<= xNewCtrl;
711 if (xClipDialogModel.is())
712 xClipDialogModel->insertByName( aName , aNewCtrl );
717 // export clipboard dialog model to xml
718 Reference< XComponentContext > xContext(
719 comphelper::getProcessComponentContext() );
720 Reference< XInputStreamProvider > xISP = ::xmlscript::exportDialogModel( xClipDialogModel, xContext, m_xDocument );
721 Reference< XInputStream > xStream( xISP->createInputStream() );
722 Sequence< sal_Int8 > DialogModelBytes;
723 implCopyStreamToByteSequence( xStream, DialogModelBytes );
724 xStream->closeInput();
726 // set clipboard content
727 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
728 if ( !xClipboard.is() )
729 return;
731 // With resource?
732 uno::Reference< beans::XPropertySet > xDialogModelPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
733 uno::Reference< resource::XStringResourcePersistence > xStringResourcePersistence;
734 if( xDialogModelPropSet.is() )
738 Any aResourceResolver = xDialogModelPropSet->getPropertyValue( aResourceResolverPropName );
739 aResourceResolver >>= xStringResourcePersistence;
741 catch(const UnknownPropertyException& )
745 rtl::Reference<DlgEdTransferableImpl> pTrans;
746 if( xStringResourcePersistence.is() )
748 // With resource, support old and new format
750 // Export xClipDialogModel another time with ids replaced by current language string
751 LocalizationMgr::resetResourceForDialog( xClipDialogModel, xStringResourcePersistence );
752 Reference< XInputStreamProvider > xISP2 = ::xmlscript::exportDialogModel( xClipDialogModel, xContext, m_xDocument );
753 Reference< XInputStream > xStream2( xISP2->createInputStream() );
754 Sequence< sal_Int8 > NoResourceDialogModelBytes;
755 implCopyStreamToByteSequence( xStream2, NoResourceDialogModelBytes );
756 xStream2->closeInput();
758 // Old format contains dialog with replaced ids
759 Any aNoResourceDialogModelBytesAny;
760 aNoResourceDialogModelBytesAny <<= NoResourceDialogModelBytes;
762 // New format contains dialog and resource
763 Sequence< sal_Int8 > aResData = xStringResourcePersistence->exportBinary();
765 // Create sequence for combined dialog and resource
766 sal_Int32 nDialogDataLen = DialogModelBytes.getLength();
767 sal_Int32 nResDataLen = aResData.getLength();
769 // Combined data = 4 Bytes 32Bit Offset to begin of resource data, lowest byte first
770 // + nDialogDataLen bytes dialog data + nResDataLen resource data
771 sal_Int32 nTotalLen = 4 + nDialogDataLen + nResDataLen;
772 sal_Int32 nResOffset = 4 + nDialogDataLen;
773 Sequence< sal_Int8 > aCombinedData( nTotalLen );
774 sal_Int8* pCombinedData = aCombinedData.getArray();
776 // Write offset
777 sal_Int32 n = nResOffset;
778 for( sal_Int16 i = 0 ; i < 4 ; i++ )
780 pCombinedData[i] = sal_Int8( n & 0xff );
781 n >>= 8;
783 memcpy( pCombinedData + 4, DialogModelBytes.getConstArray(), nDialogDataLen );
784 memcpy( pCombinedData + nResOffset, aResData.getConstArray(), nResDataLen );
786 Sequence< Any > aSeqData
788 aNoResourceDialogModelBytesAny,
789 Any(aCombinedData)
792 pTrans = new DlgEdTransferableImpl( m_ClipboardDataFlavorsResource, aSeqData );
794 else
796 // No resource, support only old format
797 pTrans = new DlgEdTransferableImpl( m_ClipboardDataFlavors , { Any(DialogModelBytes) } );
799 SolarMutexReleaser aReleaser;
800 xClipboard->setContents( pTrans , pTrans );
804 void DlgEditor::Paste()
806 // stop all drawing actions
807 pDlgEdView->BrkAction();
809 // unmark all objects
810 pDlgEdView->UnmarkAll();
812 // get clipboard
813 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
814 if ( !xClipboard.is() )
815 return;
817 Reference< datatransfer::XTransferable > xTransf;
819 SolarMutexReleaser aReleaser;
820 // get clipboard content
821 xTransf = xClipboard->getContents();
823 if ( !xTransf.is() )
824 return;
826 // Is target dialog (library) localized?
827 uno::Reference< beans::XPropertySet > xDialogModelPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
828 uno::Reference< resource::XStringResourceManager > xStringResourceManager;
829 if( xDialogModelPropSet.is() )
833 Any aResourceResolver = xDialogModelPropSet->getPropertyValue( aResourceResolverPropName );
834 aResourceResolver >>= xStringResourceManager;
836 catch(const UnknownPropertyException& )
839 bool bLocalized = false;
840 if( xStringResourceManager.is() )
841 bLocalized = xStringResourceManager->getLocales().hasElements();
843 if ( !xTransf->isDataFlavorSupported( m_ClipboardDataFlavors[0] ) )
844 return;
846 // create clipboard dialog model from xml
847 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
848 Reference< container::XNameContainer > xClipDialogModel( xContext->getServiceManager()->createInstanceWithContext(
849 "com.sun.star.awt.UnoControlDialogModel", xContext ), uno::UNO_QUERY );
851 bool bSourceIsLocalized = false;
852 Sequence< sal_Int8 > DialogModelBytes;
853 Sequence< sal_Int8 > aResData;
854 if( bLocalized && xTransf->isDataFlavorSupported( m_ClipboardDataFlavorsResource[1] ) )
856 bSourceIsLocalized = true;
858 Any aCombinedDataAny = xTransf->getTransferData( m_ClipboardDataFlavorsResource[1] );
859 Sequence< sal_Int8 > aCombinedData;
860 aCombinedDataAny >>= aCombinedData;
861 const sal_Int8* pCombinedData = aCombinedData.getConstArray();
863 sal_Int32 nTotalLen = aCombinedData.getLength();
865 // Reading offset
866 sal_Int32 nResOffset = 0;
867 sal_Int32 nFactor = 1;
868 for( sal_Int16 i = 0; i < 4; i++ )
870 nResOffset += nFactor * sal_uInt8( pCombinedData[i] );
871 nFactor *= 256;
874 sal_Int32 nResDataLen = nTotalLen - nResOffset;
875 sal_Int32 nDialogDataLen = nTotalLen - nResDataLen - 4;
877 DialogModelBytes.realloc( nDialogDataLen );
878 memcpy( DialogModelBytes.getArray(), pCombinedData + 4, nDialogDataLen );
880 aResData.realloc( nResDataLen );
881 memcpy( aResData.getArray(), pCombinedData + nResOffset, nResDataLen );
883 else
885 Any aAny = xTransf->getTransferData( m_ClipboardDataFlavors[0] );
886 aAny >>= DialogModelBytes;
889 if ( xClipDialogModel.is() )
891 Reference<XInputStream> xIn = ::xmlscript::createInputStream( DialogModelBytes.getConstArray(), DialogModelBytes.getLength() );
892 ::xmlscript::importDialogModel( xIn , xClipDialogModel, xContext, m_xDocument );
895 // get control models from clipboard dialog model
896 if ( !xClipDialogModel.is() )
897 return;
899 Sequence< OUString > aNames = xClipDialogModel->getElementNames();
900 const OUString* pNames = aNames.getConstArray();
901 sal_uInt32 nCtrls = aNames.getLength();
903 Reference< resource::XStringResourcePersistence > xStringResourcePersistence;
904 if( nCtrls > 0 && bSourceIsLocalized )
906 xStringResourcePersistence = css::resource::StringResource::create( getProcessComponentContext() );
907 xStringResourcePersistence->importBinary( aResData );
909 for( sal_uInt32 n = 0; n < nCtrls; n++ )
911 Any aA = xClipDialogModel->getByName( pNames[n] );
912 Reference< css::awt::XControlModel > xCM;
913 aA >>= xCM;
915 // clone the control model
916 Reference< util::XCloneable > xClone( xCM, uno::UNO_QUERY );
917 Reference< awt::XControlModel > xCtrlModel( xClone->createClone(), uno::UNO_QUERY );
919 rtl::Reference<DlgEdObj> pCtrlObj = new DlgEdObj(*pDlgEdModel);
920 pCtrlObj->SetDlgEdForm(pDlgEdForm.get()); // set parent form
921 pDlgEdForm->AddChild(pCtrlObj.get()); // add child to parent form
922 pCtrlObj->SetUnoControlModel( xCtrlModel ); // set control model
924 // set new name
925 OUString aOUniqueName( pCtrlObj->GetUniqueName() );
926 Reference< beans::XPropertySet > xPSet( xCtrlModel , UNO_QUERY );
927 xPSet->setPropertyValue( DLGED_PROP_NAME, Any(aOUniqueName) );
929 // set tabindex
930 Sequence< OUString > aNames_ = m_xUnoControlDialogModel->getElementNames();
931 xPSet->setPropertyValue( DLGED_PROP_TABINDEX, Any(static_cast<sal_Int16>(aNames_.getLength())) );
933 if( bLocalized )
935 Any aControlAny;
936 aControlAny <<= xCtrlModel;
937 if( bSourceIsLocalized && xStringResourcePersistence.is() )
939 LocalizationMgr::copyResourcesForPastedEditorObject( this,
940 aControlAny, aOUniqueName, xStringResourcePersistence );
942 else
944 LocalizationMgr::setControlResourceIDsForNewEditorObject
945 ( this, aControlAny, aOUniqueName );
949 // insert control model in editor dialog model
950 Any aCtrlModel;
951 aCtrlModel <<= xCtrlModel;
952 m_xUnoControlDialogModel->insertByName( aOUniqueName , aCtrlModel );
954 // insert object into drawing page
955 pDlgEdModel->GetPage(0)->InsertObject( pCtrlObj.get() );
956 pCtrlObj->SetRectFromProps();
957 pCtrlObj->UpdateStep();
958 pDlgEdForm->UpdateTabOrderAndGroups();
959 pCtrlObj->StartListening(); // start listening
961 // mark object
962 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
963 pDlgEdView->MarkObj( pCtrlObj.get(), pPgView, false, true);
966 // center marked objects in dialog editor form
967 Point aMarkCenter = pDlgEdView->GetMarkedObjRect().Center();
968 Point aFormCenter = pDlgEdForm->GetSnapRect().Center();
969 Point aPoint = aFormCenter - aMarkCenter;
970 Size aSize( aPoint.X() , aPoint.Y() );
971 pDlgEdView->MoveMarkedObj( aSize ); // update of control model properties (position + size) in NbcMove
972 pDlgEdView->MarkListHasChanged();
974 // dialog model changed
975 SetDialogModelChanged();
979 void DlgEditor::Delete()
981 if( !pDlgEdView->AreObjectsMarked() )
982 return;
984 // remove control models of marked objects from dialog model
985 const size_t nMark = pDlgEdView->GetMarkedObjectList().GetMarkCount();
987 for( size_t i = 0; i < nMark; ++i )
989 SdrObject* pObj = pDlgEdView->GetMarkedObjectList().GetMark(i)->GetMarkedSdrObj();
990 DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj);
992 if ( pDlgEdObj && !dynamic_cast<DlgEdForm*>(pDlgEdObj) )
994 // get name from property
995 OUString aName;
996 uno::Reference< beans::XPropertySet > xPSet(pDlgEdObj->GetUnoControlModel(), uno::UNO_QUERY);
997 if (xPSet.is())
999 xPSet->getPropertyValue( DLGED_PROP_NAME ) >>= aName;
1002 // remove control from dialog model
1003 Reference< css::container::XNameAccess > xNameAcc(pDlgEdObj->GetDlgEdForm()->GetUnoControlModel(), UNO_QUERY );
1004 if ( xNameAcc.is() && xNameAcc->hasByName(aName) )
1006 Reference< css::container::XNameContainer > xCont(xNameAcc, UNO_QUERY );
1007 if ( xCont.is() )
1009 if( xCont->hasByName( aName ) )
1011 Any aAny = xCont->getByName( aName );
1012 LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( this, aAny, aName );
1014 xCont->removeByName( aName );
1018 // remove child from parent form
1019 pDlgEdForm->RemoveChild( pDlgEdObj );
1023 // update tab indices
1024 pDlgEdForm->UpdateTabIndices();
1026 pDlgEdView->BrkAction();
1028 bool const bDlgMarked = UnmarkDialog();
1029 pDlgEdView->DeleteMarked();
1030 if( bDlgMarked )
1031 RemarkDialog();
1035 bool DlgEditor::IsPasteAllowed()
1037 // get clipboard
1038 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
1039 if ( xClipboard.is() )
1041 Reference< datatransfer::XTransferable > xTransf;
1043 SolarMutexReleaser aReleaser;
1044 // get clipboard content
1045 xTransf = xClipboard->getContents();
1047 if (xTransf.is())
1048 return xTransf->isDataFlavorSupported(m_ClipboardDataFlavors[0]);
1050 return false;
1054 void DlgEditor::ShowProperties()
1056 rLayout.ShowPropertyBrowser();
1060 void DlgEditor::UpdatePropertyBrowserDelayed()
1062 aMarkIdle.Start();
1066 bool DlgEditor::IsModified() const
1068 return pDlgEdModel->IsChanged() || bDialogModelChanged;
1072 void DlgEditor::ClearModifyFlag()
1074 pDlgEdModel->SetChanged(false);
1075 bDialogModelChanged = false;
1079 namespace Print
1081 tools::Long const nLeftMargin = 1700;
1082 tools::Long const nRightMargin = 900;
1083 tools::Long const nTopMargin = 2000;
1084 tools::Long const nBottomMargin = 1000;
1085 tools::Long const nBorder = 300;
1088 static void lcl_PrintHeader( Printer* pPrinter, const OUString& rTitle ) // not working yet
1091 pPrinter->Push();
1093 Size const aSz = pPrinter->GetOutputSize();
1095 pPrinter->SetLineColor( COL_BLACK );
1096 pPrinter->SetFillColor();
1098 vcl::Font aFont( pPrinter->GetFont() );
1099 aFont.SetWeight( WEIGHT_BOLD );
1100 aFont.SetAlignment( ALIGN_BOTTOM );
1101 pPrinter->SetFont( aFont );
1103 tools::Long const nFontHeight = pPrinter->GetTextHeight();
1105 // 1st border => line, 2+3 border = free space
1106 tools::Long const nYTop = Print::nTopMargin - 3*Print::nBorder - nFontHeight;
1108 tools::Long const nXLeft = Print::nLeftMargin - Print::nBorder;
1109 tools::Long const nXRight = aSz.Width() - Print::nRightMargin + Print::nBorder;
1111 pPrinter->DrawRect(tools::Rectangle(
1112 Point(nXLeft, nYTop),
1113 Size(nXRight - nXLeft, aSz.Height() - nYTop - Print::nBottomMargin + Print::nBorder)
1116 tools::Long nY = Print::nTopMargin - 2*Print::nBorder;
1117 Point aPos(Print::nLeftMargin, nY);
1118 pPrinter->DrawText( aPos, rTitle );
1120 nY = Print::nTopMargin - Print::nBorder;
1121 pPrinter->DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
1123 pPrinter->Pop();
1127 void DlgEditor::printPage( sal_Int32 nPage, Printer* pPrinter, const OUString& rTitle )
1129 if( nPage == 0 )
1130 Print( pPrinter, rTitle );
1134 void DlgEditor::Print( Printer* pPrinter, const OUString& rTitle ) // not working yet
1136 MapMode aOldMap( pPrinter->GetMapMode());
1137 vcl::Font aOldFont( pPrinter->GetFont() );
1139 MapMode aMap( MapUnit::Map100thMM );
1140 pPrinter->SetMapMode( aMap );
1141 vcl::Font aFont;
1142 aFont.SetAlignment( ALIGN_BOTTOM );
1143 aFont.SetFontSize( Size( 0, 360 ));
1144 pPrinter->SetFont( aFont );
1146 Size aPaperSz = pPrinter->GetOutputSize();
1147 aPaperSz.AdjustWidth( -(Print::nLeftMargin + Print::nRightMargin) );
1148 aPaperSz.AdjustHeight( -(Print::nTopMargin + Print::nBottomMargin) );
1150 lcl_PrintHeader( pPrinter, rTitle );
1152 BitmapEx aDlgEx;
1153 Size aBmpSz( pPrinter->PixelToLogic( aDlgEx.GetSizePixel() ) );
1154 double nPaperSzWidth = aPaperSz.Width();
1155 double nPaperSzHeight = aPaperSz.Height();
1156 double nBmpSzWidth = aBmpSz.Width();
1157 double nBmpSzHeight = aBmpSz.Height();
1158 double nScaleX = nPaperSzWidth / nBmpSzWidth;
1159 double nScaleY = nPaperSzHeight / nBmpSzHeight;
1161 Size aOutputSz;
1162 if( nBmpSzHeight * nScaleX <= nPaperSzHeight )
1164 aOutputSz.setWidth( static_cast<tools::Long>(nBmpSzWidth * nScaleX) );
1165 aOutputSz.setHeight( static_cast<tools::Long>(nBmpSzHeight * nScaleX) );
1167 else
1169 aOutputSz.setWidth( static_cast<tools::Long>(nBmpSzWidth * nScaleY) );
1170 aOutputSz.setHeight( static_cast<tools::Long>(nBmpSzHeight * nScaleY) );
1173 Point aPosOffs(
1174 (aPaperSz.Width() / 2) - (aOutputSz.Width() / 2),
1175 (aPaperSz.Height()/ 2) - (aOutputSz.Height() / 2));
1177 aPosOffs.AdjustX(Print::nLeftMargin );
1178 aPosOffs.AdjustY(Print::nTopMargin );
1180 pPrinter->DrawBitmapEx( aPosOffs, aOutputSz, aDlgEx );
1182 pPrinter->SetMapMode( aOldMap );
1183 pPrinter->SetFont( aOldFont );
1187 bool DlgEditor::AdjustPageSize()
1189 bool bAdjustedPageSize = false;
1190 Reference< beans::XPropertySet > xPSet( m_xUnoControlDialogModel, UNO_QUERY );
1191 if ( xPSet.is() )
1193 sal_Int32 nFormXIn = 0, nFormYIn = 0, nFormWidthIn = 0, nFormHeightIn = 0;
1194 xPSet->getPropertyValue( DLGED_PROP_POSITIONX ) >>= nFormXIn;
1195 xPSet->getPropertyValue( DLGED_PROP_POSITIONY ) >>= nFormYIn;
1196 xPSet->getPropertyValue( DLGED_PROP_WIDTH ) >>= nFormWidthIn;
1197 xPSet->getPropertyValue( DLGED_PROP_HEIGHT ) >>= nFormHeightIn;
1199 sal_Int32 nFormX, nFormY, nFormWidth, nFormHeight;
1200 if ( pDlgEdForm && pDlgEdForm->TransformFormToSdrCoordinates( nFormXIn, nFormYIn, nFormWidthIn, nFormHeightIn, nFormX, nFormY, nFormWidth, nFormHeight ) )
1202 Size aPageSizeDelta( 400, 300 );
1203 aPageSizeDelta = rWindow.PixelToLogic( aPageSizeDelta, MapMode( MapUnit::Map100thMM ) );
1205 sal_Int32 nNewPageWidth = nFormX + nFormWidth + aPageSizeDelta.Width();
1206 sal_Int32 nNewPageHeight = nFormY + nFormHeight + aPageSizeDelta.Height();
1208 Size aPageSizeMin( DLGED_PAGE_WIDTH_MIN, DLGED_PAGE_HEIGHT_MIN );
1209 aPageSizeMin = rWindow.PixelToLogic( aPageSizeMin, MapMode( MapUnit::Map100thMM ) );
1210 sal_Int32 nPageWidthMin = aPageSizeMin.Width();
1211 sal_Int32 nPageHeightMin = aPageSizeMin.Height();
1213 if ( nNewPageWidth < nPageWidthMin )
1214 nNewPageWidth = nPageWidthMin;
1216 if ( nNewPageHeight < nPageHeightMin )
1217 nNewPageHeight = nPageHeightMin;
1219 if ( pDlgEdPage )
1221 Size aPageSize = pDlgEdPage->GetSize();
1222 if ( nNewPageWidth != aPageSize.Width() || nNewPageHeight != aPageSize.Height() )
1224 Size aNewPageSize( nNewPageWidth, nNewPageHeight );
1225 pDlgEdPage->SetSize( aNewPageSize );
1226 pDlgEdView->SetWorkArea( tools::Rectangle( Point( 0, 0 ), aNewPageSize ) );
1227 bAdjustedPageSize = true;
1233 return bAdjustedPageSize;
1237 } // namespace basctl
1239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */