bump product version to 5.0.4.1
[LibreOffice.git] / basctl / source / dlged / dlged.cxx
blobf9cb691efc2a107b15975f7dc4c7f084d17629cc
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 "basidesh.hxx"
22 #include "dlged.hxx"
23 #include "dlgedclip.hxx"
24 #include "dlgeddef.hxx"
25 #include "dlgedfac.hxx"
26 #include "dlgedfunc.hxx"
27 #include "dlgedmod.hxx"
28 #include "dlgedobj.hxx"
29 #include "dlgedpage.hxx"
30 #include "dlgedview.hxx"
31 #include "iderdll.hxx"
32 #include "localizationmgr.hxx"
33 #include "baside3.hxx"
35 #include <com/sun/star/awt/Toolkit.hpp>
36 #include <com/sun/star/awt/UnoControlDialog.hpp>
37 #include <com/sun/star/awt/XDialog.hpp>
38 #include <com/sun/star/resource/StringResource.hpp>
39 #include <com/sun/star/util/XCloneable.hpp>
40 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
41 #include <comphelper/types.hxx>
42 #include <sfx2/viewfrm.hxx>
43 #include <svl/itempool.hxx>
44 #include <svx/sdrpaintwindow.hxx>
45 #include <svx/svxids.hrc>
46 #include <toolkit/helper/vclunohelper.hxx>
47 #include <vcl/svapp.hxx>
48 #include <xmlscript/xml_helper.hxx>
49 #include <xmlscript/xmldlg_imexp.hxx>
51 namespace basctl
54 using namespace comphelper;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::io;
60 static const char aResourceResolverPropName[] = "ResourceResolver";
61 static const char aDecorationPropName[] = "Decoration";
62 static const char aTitlePropName[] = "Title";
66 // DlgEdHint
69 DlgEdHint::DlgEdHint(Kind eHint)
70 : eKind(eHint)
71 , pDlgEdObj(0)
75 DlgEdHint::DlgEdHint(Kind eHint, DlgEdObj* pObj)
76 : eKind(eHint)
77 , pDlgEdObj(pObj)
81 DlgEdHint::~DlgEdHint()
86 // DlgEditor
89 void DlgEditor::ShowDialog()
91 uno::Reference< uno::XComponentContext > xContext = getProcessComponentContext();
93 // create a dialog
94 uno::Reference< awt::XUnoControlDialog > xDlg = awt::UnoControlDialog::create( xContext );
96 // clone the dialog model
97 uno::Reference< util::XCloneable > xC( m_xUnoControlDialogModel, uno::UNO_QUERY );
98 uno::Reference< util::XCloneable > xNew = xC->createClone();
99 uno::Reference< awt::XControlModel > xDlgMod( xNew, uno::UNO_QUERY );
101 uno::Reference< beans::XPropertySet > xSrcDlgModPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
102 uno::Reference< beans::XPropertySet > xNewDlgModPropSet( xDlgMod, uno::UNO_QUERY );
103 if( xNewDlgModPropSet.is() )
105 if( xSrcDlgModPropSet.is() )
109 Any aResourceResolver = xSrcDlgModPropSet->getPropertyValue( aResourceResolverPropName );
110 xNewDlgModPropSet->setPropertyValue( aResourceResolverPropName, aResourceResolver );
112 catch(const UnknownPropertyException& )
114 OSL_FAIL( "DlgEditor::ShowDialog(): No ResourceResolver property" );
118 // Disable decoration
121 bool bDecoration = true;
123 Any aDecorationAny = xSrcDlgModPropSet->getPropertyValue( aDecorationPropName );
124 aDecorationAny >>= bDecoration;
125 if( !bDecoration )
127 xNewDlgModPropSet->setPropertyValue( aDecorationPropName, makeAny( true ) );
128 xNewDlgModPropSet->setPropertyValue( aTitlePropName, makeAny( OUString() ) );
131 catch(const UnknownPropertyException& )
135 // set the model
136 xDlg->setModel( xDlgMod );
138 // create a peer
139 uno::Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
140 xDlg->createPeer( xToolkit, rWindow.GetComponentInterface() );
142 xDlg->execute();
144 // need to cast because of multiple inheritance
145 Reference<awt::XControl>(xDlg)->dispose();
149 bool DlgEditor::UnmarkDialog()
151 SdrObject* pDlgObj = pDlgEdModel->GetPage(0)->GetObj(0);
152 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
154 bool bWasMarked = pDlgEdView->IsObjMarked( pDlgObj );
156 if( bWasMarked )
157 pDlgEdView->MarkObj( pDlgObj, pPgView, true );
159 return bWasMarked;
163 bool DlgEditor::RemarkDialog()
165 SdrObject* pDlgObj = pDlgEdModel->GetPage(0)->GetObj(0);
166 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
168 bool bWasMarked = pDlgEdView->IsObjMarked( pDlgObj );
170 if( !bWasMarked )
171 pDlgEdView->MarkObj( pDlgObj, pPgView, false );
173 return bWasMarked;
177 DlgEditor::DlgEditor (
178 vcl::Window& rWindow_, DialogWindowLayout& rLayout_,
179 com::sun::star::uno::Reference<com::sun::star::frame::XModel> const& xModel,
180 com::sun::star::uno::Reference<com::sun::star::container::XNameContainer> xDialogModel
182 :pHScroll(NULL)
183 ,pVScroll(NULL)
184 ,pDlgEdModel(new DlgEdModel())
185 ,pDlgEdPage(new DlgEdPage(*pDlgEdModel))
186 ,m_ClipboardDataFlavors(1)
187 ,m_ClipboardDataFlavorsResource(2)
188 ,pObjFac(new DlgEdFactory(xModel))
189 ,rWindow(rWindow_)
190 ,pFunc(new DlgEdFuncSelect(*this))
191 ,rLayout(rLayout_)
192 ,eMode( DlgEditor::SELECT )
193 ,eActObj( OBJ_DLG_PUSHBUTTON )
194 ,bFirstDraw(false)
195 ,aGridSize( 100, 100 ) // 100TH_MM
196 ,bGridVisible(false)
197 ,bGridSnap(true)
198 ,bCreateOK(true)
199 ,bDialogModelChanged(false)
200 ,mnPaintGuard(0)
201 ,m_xDocument( xModel )
203 pDlgEdModel->GetItemPool().FreezeIdRanges();
204 pDlgEdView.reset(new DlgEdView(*pDlgEdModel, rWindow_, *this));
205 pDlgEdModel->SetScaleUnit( MAP_100TH_MM );
207 SdrLayerAdmin& rAdmin = pDlgEdModel->GetLayerAdmin();
208 rAdmin.NewLayer( rAdmin.GetControlLayerName() );
209 rAdmin.NewLayer( OUString( "HiddenLayer" ) );
211 pDlgEdModel->InsertPage(pDlgEdPage);
213 // set clipboard data flavors
214 m_ClipboardDataFlavors[0].MimeType = "application/vnd.sun.xml.dialog" ;
215 m_ClipboardDataFlavors[0].HumanPresentableName = "Dialog 6.0" ;
216 m_ClipboardDataFlavors[0].DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
218 m_ClipboardDataFlavorsResource[0] = m_ClipboardDataFlavors[0];
219 m_ClipboardDataFlavorsResource[1].MimeType = "application/vnd.sun.xml.dialogwithresource" ;
220 m_ClipboardDataFlavorsResource[1].HumanPresentableName = "Dialog 8.0" ;
221 m_ClipboardDataFlavorsResource[1].DataType = cppu::UnoType<Sequence< sal_Int8 >>::get();
223 aMarkIdle.SetPriority(SchedulerPriority::LOW);
224 aMarkIdle.SetIdleHdl( LINK( this, DlgEditor, MarkTimeout ) );
226 rWindow.SetMapMode( MapMode( MAP_100TH_MM ) );
227 pDlgEdPage->SetSize( rWindow.PixelToLogic( Size(DLGED_PAGE_WIDTH_MIN, DLGED_PAGE_HEIGHT_MIN) ) );
229 pDlgEdView->ShowSdrPage(pDlgEdView->GetModel()->GetPage(0));
230 pDlgEdView->SetLayerVisible( OUString( "HiddenLayer" ), false );
231 pDlgEdView->SetMoveSnapOnlyTopLeft(true);
232 pDlgEdView->SetWorkArea( Rectangle( Point( 0, 0 ), pDlgEdPage->GetSize() ) );
234 pDlgEdView->SetGridCoarse( aGridSize );
235 pDlgEdView->SetSnapGridWidth(Fraction(aGridSize.Width(), 1), Fraction(aGridSize.Height(), 1));
236 pDlgEdView->SetGridSnap( bGridSnap );
237 pDlgEdView->SetGridVisible( bGridVisible );
238 pDlgEdView->SetDragStripes(false);
240 pDlgEdView->SetDesignMode(true);
242 ::comphelper::disposeComponent( m_xControlContainer );
244 SetDialog(xDialogModel);
248 DlgEditor::~DlgEditor()
250 aMarkIdle.Stop();
252 ::comphelper::disposeComponent( m_xControlContainer );
256 Reference< awt::XControlContainer > DlgEditor::GetWindowControlContainer()
258 if (!m_xControlContainer.is())
259 m_xControlContainer = VCLUnoHelper::CreateControlContainer(&rWindow);
260 return m_xControlContainer;
264 void DlgEditor::SetScrollBars( ScrollBar* pHS, ScrollBar* pVS )
266 pHScroll = pHS;
267 pVScroll = pVS;
269 InitScrollBars();
273 void DlgEditor::InitScrollBars()
275 DBG_ASSERT( pHScroll, "DlgEditor::InitScrollBars: no horizontal scroll bar!" );
276 DBG_ASSERT( pVScroll, "DlgEditor::InitScrollBars: no vertical scroll bar!" );
277 if ( !pHScroll || !pVScroll )
278 return;
280 Size aOutSize = rWindow.GetOutputSize();
281 Size aPgSize = pDlgEdPage->GetSize();
283 pHScroll->SetRange( Range( 0, aPgSize.Width() ));
284 pVScroll->SetRange( Range( 0, aPgSize.Height() ));
285 pHScroll->SetVisibleSize( (sal_uLong)aOutSize.Width() );
286 pVScroll->SetVisibleSize( (sal_uLong)aOutSize.Height() );
288 pHScroll->SetLineSize( aOutSize.Width() / 10 );
289 pVScroll->SetLineSize( aOutSize.Height() / 10 );
290 pHScroll->SetPageSize( aOutSize.Width() / 2 );
291 pVScroll->SetPageSize( aOutSize.Height() / 2 );
293 DoScroll( pHScroll );
294 DoScroll( pVScroll );
298 void DlgEditor::DoScroll( ScrollBar* )
300 if( !pHScroll || !pVScroll )
301 return;
303 MapMode aMap = rWindow.GetMapMode();
304 Point aOrg = aMap.GetOrigin();
306 Size aScrollPos( pHScroll->GetThumbPos(), pVScroll->GetThumbPos() );
307 aScrollPos = rWindow.LogicToPixel( aScrollPos );
308 aScrollPos = rWindow.PixelToLogic( aScrollPos );
310 long nX = aScrollPos.Width() + aOrg.X();
311 long nY = aScrollPos.Height() + aOrg.Y();
313 if( !nX && !nY )
314 return;
316 rWindow.Update();
318 // #i31562#
319 // When scrolling, someone was rescuing the Wallpaper and forced the window scroll to
320 // be done without background refresh. I do not know why, but that causes the repaint
321 // problems. Taking that out.
322 // Wallpaper aOldBackground = rWindow.GetBackground();
323 // rWindow.SetBackground();
325 // #i74769# children should be scrolled
326 rWindow.Scroll( -nX, -nY, SCROLL_CHILDREN);
327 aMap.SetOrigin( Point( -aScrollPos.Width(), -aScrollPos.Height() ) );
328 rWindow.SetMapMode( aMap );
329 rWindow.Update();
331 DlgEdHint aHint( DlgEdHint::WINDOWSCROLLED );
332 Broadcast( aHint );
336 void DlgEditor::UpdateScrollBars()
338 MapMode aMap = rWindow.GetMapMode();
339 Point aOrg = aMap.GetOrigin();
341 if ( pHScroll )
342 pHScroll->SetThumbPos( -aOrg.X() );
344 if ( pVScroll )
345 pVScroll->SetThumbPos( -aOrg.Y() );
349 void DlgEditor::SetDialog( uno::Reference< container::XNameContainer > xUnoControlDialogModel )
351 // set dialog model
352 m_xUnoControlDialogModel = xUnoControlDialogModel;
354 // create dialog form
355 pDlgEdForm = new DlgEdForm(*this);
356 uno::Reference< awt::XControlModel > xDlgMod( m_xUnoControlDialogModel , uno::UNO_QUERY );
357 pDlgEdForm->SetUnoControlModel(xDlgMod);
358 static_cast<DlgEdPage*>(pDlgEdModel->GetPage(0))->SetDlgEdForm( pDlgEdForm );
359 pDlgEdModel->GetPage(0)->InsertObject( pDlgEdForm );
360 AdjustPageSize();
361 pDlgEdForm->SetRectFromProps();
362 pDlgEdForm->UpdateTabIndices(); // for backward compatibility
363 pDlgEdForm->StartListening();
365 // create controls
366 Reference< ::com::sun::star::container::XNameAccess > xNameAcc( m_xUnoControlDialogModel, UNO_QUERY );
367 if ( xNameAcc.is() )
369 // get sequence of control names
370 Sequence< OUString > aNames = xNameAcc->getElementNames();
371 const OUString* pNames = aNames.getConstArray();
372 sal_Int32 nCtrls = aNames.getLength();
374 // create a map of tab indices and control names, sorted by tab index
375 IndexToNameMap aIndexToNameMap;
376 for ( sal_Int32 i = 0; i < nCtrls; ++i )
378 // get name
379 OUString aName( pNames[i] );
381 // get tab index
382 sal_Int16 nTabIndex = -1;
383 Any aCtrl = xNameAcc->getByName( aName );
384 Reference< ::com::sun::star::beans::XPropertySet > xPSet;
385 aCtrl >>= xPSet;
386 if ( xPSet.is() )
387 xPSet->getPropertyValue( DLGED_PROP_TABINDEX ) >>= nTabIndex;
389 // insert into map
390 aIndexToNameMap.insert( IndexToNameMap::value_type( nTabIndex, aName ) );
393 // create controls and insert them into drawing page
394 for ( IndexToNameMap::iterator aIt = aIndexToNameMap.begin(); aIt != aIndexToNameMap.end(); ++aIt )
396 Any aCtrl = xNameAcc->getByName( aIt->second );
397 Reference< ::com::sun::star::awt::XControlModel > xCtrlModel;
398 aCtrl >>= xCtrlModel;
399 DlgEdObj* pCtrlObj = new DlgEdObj();
400 pCtrlObj->SetUnoControlModel( xCtrlModel );
401 pCtrlObj->SetDlgEdForm( pDlgEdForm );
402 pDlgEdForm->AddChild( pCtrlObj );
403 pDlgEdModel->GetPage(0)->InsertObject( pCtrlObj );
404 pCtrlObj->SetRectFromProps();
405 pCtrlObj->UpdateStep();
406 pCtrlObj->StartListening();
410 bFirstDraw = true;
412 pDlgEdModel->SetChanged(false);
415 void DlgEditor::ResetDialog ()
417 DlgEdForm* pOldDlgEdForm = pDlgEdForm;
418 DlgEdPage* pPage = static_cast<DlgEdPage*>(pDlgEdModel->GetPage(0));
419 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
420 bool bWasMarked = pDlgEdView->IsObjMarked( pOldDlgEdForm );
421 pDlgEdView->UnmarkAll();
422 pPage->Clear();
423 pPage->SetDlgEdForm( NULL );
424 SetDialog( m_xUnoControlDialogModel );
425 if( bWasMarked )
426 pDlgEdView->MarkObj( pDlgEdForm, pPgView, false );
430 Reference< util::XNumberFormatsSupplier > const & DlgEditor::GetNumberFormatsSupplier()
432 if ( !m_xSupplier.is() )
434 Reference< uno::XComponentContext > xContext = ::comphelper::getProcessComponentContext();
435 Reference< util::XNumberFormatsSupplier > xSupplier( util::NumberFormatsSupplier::createWithDefaultLocale(xContext) );
437 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
438 if ( !m_xSupplier.is() )
440 m_xSupplier = xSupplier;
443 return m_xSupplier;
447 void DlgEditor::MouseButtonDown( const MouseEvent& rMEvt )
449 rWindow.GrabFocus();
450 pFunc->MouseButtonDown( rMEvt );
454 void DlgEditor::MouseButtonUp( const MouseEvent& rMEvt )
456 bool bRet = pFunc->MouseButtonUp( rMEvt );
458 if( eMode == DlgEditor::INSERT )
459 bCreateOK = bRet;
463 void DlgEditor::MouseMove( const MouseEvent& rMEvt )
465 pFunc->MouseMove( rMEvt );
469 bool DlgEditor::KeyInput( const KeyEvent& rKEvt )
471 return pFunc->KeyInput( rKEvt );
475 void DlgEditor::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
477 aPaintRect = rRect;
478 mnPaintGuard++;
480 Size aMacSize;
481 if (bFirstDraw && rWindow.IsVisible() && (rRenderContext.GetOutputSize() != aMacSize))
483 bFirstDraw = false;
485 // get property set
486 css::uno::Reference<css::beans::XPropertySet> xPSet(pDlgEdForm->GetUnoControlModel(), css::uno::UNO_QUERY);
488 if (xPSet.is())
490 // get dialog size from properties
491 sal_Int32 nWidth = 0, nHeight = 0;
492 xPSet->getPropertyValue( DLGED_PROP_WIDTH ) >>= nWidth;
493 xPSet->getPropertyValue( DLGED_PROP_HEIGHT ) >>= nHeight;
495 if (nWidth == 0 && nHeight == 0)
497 Size aSize = rRenderContext.PixelToLogic( Size( 400, 300 ) );
499 // align with grid
500 Size aGridSize_(long(pDlgEdView->GetSnapGridWidthX()), long(pDlgEdView->GetSnapGridWidthY()));
501 aSize.Width() -= aSize.Width() % aGridSize_.Width();
502 aSize.Height() -= aSize.Height() % aGridSize_.Height();
504 Point aPos;
505 Size aOutSize = rRenderContext.GetOutputSize();
506 aPos.X() = (aOutSize.Width()>>1) - (aSize.Width()>>1);
507 aPos.Y() = (aOutSize.Height()>>1) - (aSize.Height()>>1);
509 // align with grid
510 aPos.X() -= aPos.X() % aGridSize_.Width();
511 aPos.Y() -= aPos.Y() % aGridSize_.Height();
513 // don't put in the corner
514 Point aMinPos = rRenderContext.PixelToLogic( Point( 30, 20 ) );
515 if( (aPos.X() < aMinPos.X()) || (aPos.Y() < aMinPos.Y()) )
517 aPos = aMinPos;
518 aPos.X() -= aPos.X() % aGridSize_.Width();
519 aPos.Y() -= aPos.Y() % aGridSize_.Height();
522 // set dialog position and size
523 pDlgEdForm->SetSnapRect( Rectangle( aPos, aSize ) );
524 pDlgEdForm->EndListening(false);
525 pDlgEdForm->SetPropsFromRect();
526 pDlgEdForm->GetDlgEditor().SetDialogModelChanged(true);
527 pDlgEdForm->StartListening();
529 // set position and size of controls
530 if (const size_t nObjCount = pDlgEdPage->GetObjCount())
532 for (size_t i = 0 ; i < nObjCount ; ++i)
534 if (DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pDlgEdPage->GetObj(i)))
536 if (!dynamic_cast<DlgEdForm*>(pDlgEdObj))
538 pDlgEdObj->SetRectFromProps();
547 // repaint, get PageView and prepare Region
548 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
549 const vcl::Region aPaintRectRegion(aPaintRect);
551 // #i74769#
552 SdrPaintWindow* pTargetPaintWindow = 0;
554 // mark repaint start
555 if (pPgView)
557 pTargetPaintWindow = pPgView->GetView().BeginDrawLayers(&rRenderContext, aPaintRectRegion);
558 OSL_ENSURE(pTargetPaintWindow, "BeginDrawLayers: Got no SdrPaintWindow (!)");
561 // draw background self using wallpaper
562 // #i79128# ...and use correct OutDev for that
563 if (pTargetPaintWindow)
565 OutputDevice& rTargetOutDev = pTargetPaintWindow->GetTargetOutputDevice();
566 rTargetOutDev.DrawWallpaper(aPaintRect, Wallpaper(Color(COL_WHITE)));
569 // do paint (unbuffered) and mark repaint end
570 if (pPgView)
572 // paint of control layer is done in EndDrawLayers anyway...
573 pPgView->GetView().EndDrawLayers(*pTargetPaintWindow, true);
576 mnPaintGuard--;
580 IMPL_LINK_NOARG_TYPED(DlgEditor, MarkTimeout, Idle *, void)
582 rLayout.UpdatePropertyBrowser();
586 void DlgEditor::SetMode (Mode eNewMode )
588 if ( eNewMode != eMode )
590 if ( eNewMode == INSERT )
591 pFunc.reset(new DlgEdFuncInsert(*this));
592 else
593 pFunc.reset(new DlgEdFuncSelect(*this));
595 if ( eNewMode == READONLY )
596 pDlgEdModel->SetReadOnly( true );
597 else
598 pDlgEdModel->SetReadOnly( false );
601 if ( eNewMode == TEST )
602 ShowDialog();
604 eMode = eNewMode;
608 void DlgEditor::SetInsertObj( sal_uInt16 eObj )
610 eActObj = eObj;
612 pDlgEdView->SetCurrentObj( eActObj, DlgInventor );
615 void DlgEditor::CreateDefaultObject()
617 // create object by factory
618 SdrObject* pObj = SdrObjFactory::MakeNewObject( pDlgEdView->GetCurrentObjInventor(), pDlgEdView->GetCurrentObjIdentifier(), pDlgEdPage );
620 if (DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj))
622 // set position and size
623 Size aSize = rWindow.PixelToLogic( Size( 96, 24 ) );
624 Point aPoint = (pDlgEdForm->GetSnapRect()).Center();
625 aPoint.X() -= aSize.Width() / 2;
626 aPoint.Y() -= aSize.Height() / 2;
627 pDlgEdObj->SetSnapRect( Rectangle( aPoint, aSize ) );
629 // set default property values
630 pDlgEdObj->SetDefaults();
632 // insert object into drawing page
633 SdrPageView* pPageView = pDlgEdView->GetSdrPageView();
634 pDlgEdView->InsertObjectAtView( pDlgEdObj, *pPageView);
636 // start listening
637 pDlgEdObj->StartListening();
642 void DlgEditor::Cut()
644 Copy();
645 Delete();
649 void implCopyStreamToByteSequence( Reference< XInputStream > xStream,
650 Sequence< sal_Int8 >& bytes )
652 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
653 for (;;)
655 Sequence< sal_Int8 > readBytes;
656 nRead = xStream->readBytes( readBytes, 1024 );
657 if (! nRead)
658 break;
660 sal_Int32 nPos = bytes.getLength();
661 bytes.realloc( nPos + nRead );
662 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
666 void DlgEditor::Copy()
668 if( !pDlgEdView->AreObjectsMarked() )
669 return;
671 // stop all drawing actions
672 pDlgEdView->BrkAction();
674 // create an empty clipboard dialog model
675 Reference< util::XCloneable > xClone( m_xUnoControlDialogModel, UNO_QUERY );
676 Reference< util::XCloneable > xNewClone = xClone->createClone();
677 Reference< container::XNameContainer > xClipDialogModel( xNewClone, UNO_QUERY );
679 Reference< container::XNameAccess > xNAcc( xClipDialogModel, UNO_QUERY );
680 if ( xNAcc.is() )
682 Sequence< OUString > aNames = xNAcc->getElementNames();
683 const OUString* pNames = aNames.getConstArray();
684 sal_uInt32 nCtrls = aNames.getLength();
686 for ( sal_uInt32 n = 0; n < nCtrls; n++ )
688 xClipDialogModel->removeByName( pNames[n] );
692 // insert control models of marked objects into clipboard dialog model
693 const size_t nMark = pDlgEdView->GetMarkedObjectList().GetMarkCount();
694 for( size_t i = 0; i < nMark; ++i )
696 SdrObject* pObj = pDlgEdView->GetMarkedObjectList().GetMark(i)->GetMarkedSdrObj();
697 DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj);
699 if (pDlgEdObj && !dynamic_cast<DlgEdForm*>(pDlgEdObj))
701 OUString aName;
702 Reference< beans::XPropertySet > xMarkPSet(pDlgEdObj->GetUnoControlModel(), uno::UNO_QUERY);
703 if (xMarkPSet.is())
705 xMarkPSet->getPropertyValue( DLGED_PROP_NAME ) >>= aName;
708 Reference< container::XNameAccess > xNameAcc(m_xUnoControlDialogModel, UNO_QUERY );
709 if ( xNameAcc.is() && xNameAcc->hasByName(aName) )
711 Any aCtrl = xNameAcc->getByName( aName );
713 // clone control model
714 Reference< util::XCloneable > xCtrl;
715 aCtrl >>= xCtrl;
716 Reference< util::XCloneable > xNewCtrl = xCtrl->createClone();
717 Any aNewCtrl;
718 aNewCtrl <<= xNewCtrl;
720 if (xClipDialogModel.is())
721 xClipDialogModel->insertByName( aName , aNewCtrl );
726 // export clipboard dialog model to xml
727 Reference< XComponentContext > xContext(
728 comphelper::getProcessComponentContext() );
729 Reference< XInputStreamProvider > xISP = ::xmlscript::exportDialogModel( xClipDialogModel, xContext, m_xDocument );
730 Reference< XInputStream > xStream( xISP->createInputStream() );
731 Sequence< sal_Int8 > DialogModelBytes;
732 implCopyStreamToByteSequence( xStream, DialogModelBytes );
733 xStream->closeInput();
735 // set clipboard content
736 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
737 if ( xClipboard.is() )
739 // With resource?
740 uno::Reference< beans::XPropertySet > xDialogModelPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
741 uno::Reference< resource::XStringResourcePersistence > xStringResourcePersistence;
742 if( xDialogModelPropSet.is() )
746 Any aResourceResolver = xDialogModelPropSet->getPropertyValue( aResourceResolverPropName );
747 aResourceResolver >>= xStringResourcePersistence;
749 catch(const UnknownPropertyException& )
753 DlgEdTransferableImpl* pTrans = NULL;
754 if( xStringResourcePersistence.is() )
756 // With resource, support old and new format
758 // Export xClipDialogModel another time with ids replaced by current language string
759 uno::Reference< resource::XStringResourceManager >
760 xStringResourceManager( xStringResourcePersistence, uno::UNO_QUERY );
761 LocalizationMgr::resetResourceForDialog( xClipDialogModel, xStringResourceManager );
762 Reference< XInputStreamProvider > xISP2 = ::xmlscript::exportDialogModel( xClipDialogModel, xContext, m_xDocument );
763 Reference< XInputStream > xStream2( xISP2->createInputStream() );
764 Sequence< sal_Int8 > NoResourceDialogModelBytes;
765 implCopyStreamToByteSequence( xStream2, NoResourceDialogModelBytes );
766 xStream2->closeInput();
768 // Old format contains dialog with replaced ids
769 Sequence< Any > aSeqData(2);
770 Any aNoResourceDialogModelBytesAny;
771 aNoResourceDialogModelBytesAny <<= NoResourceDialogModelBytes;
772 aSeqData[0] = aNoResourceDialogModelBytesAny;
774 // New format contains dialog and resource
775 Sequence< sal_Int8 > aResData = xStringResourcePersistence->exportBinary();
777 // Create sequence for combined dialog and resource
778 sal_Int32 nDialogDataLen = DialogModelBytes.getLength();
779 sal_Int32 nResDataLen = aResData.getLength();
781 // Combined data = 4 Bytes 32Bit Offset to begin of resource data, lowest byte first
782 // + nDialogDataLen bytes dialog data + nResDataLen resource data
783 sal_Int32 nTotalLen = 4 + nDialogDataLen + nResDataLen;
784 sal_Int32 nResOffset = 4 + nDialogDataLen;
785 Sequence< sal_Int8 > aCombinedData( nTotalLen );
786 sal_Int8* pCombinedData = aCombinedData.getArray();
788 // Write offset
789 sal_Int32 n = nResOffset;
790 for( sal_Int16 i = 0 ; i < 4 ; i++ )
792 pCombinedData[i] = sal_Int8( n & 0xff );
793 n >>= 8;
795 memcpy( pCombinedData + 4, DialogModelBytes.getConstArray(), nDialogDataLen );
796 memcpy( pCombinedData + nResOffset, aResData.getConstArray(), nResDataLen );
798 Any aCombinedDataAny;
799 aCombinedDataAny <<= aCombinedData;
800 aSeqData[1] = aCombinedDataAny;
802 pTrans = new DlgEdTransferableImpl( m_ClipboardDataFlavorsResource, aSeqData );
804 else
806 // No resource, support only old format
807 Sequence< Any > aSeqData(1);
808 Any aDialogModelBytesAny;
809 aDialogModelBytesAny <<= DialogModelBytes;
810 aSeqData[0] = aDialogModelBytesAny;
811 pTrans = new DlgEdTransferableImpl( m_ClipboardDataFlavors , aSeqData );
813 SolarMutexReleaser aReleaser;
814 xClipboard->setContents( pTrans , pTrans );
819 void DlgEditor::Paste()
821 // stop all drawing actions
822 pDlgEdView->BrkAction();
824 // unmark all objects
825 pDlgEdView->UnmarkAll();
827 // get clipboard
828 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
829 if ( xClipboard.is() )
831 Reference< datatransfer::XTransferable > xTransf;
834 SolarMutexReleaser aReleaser;
835 // get clipboard content
836 xTransf = xClipboard->getContents();
838 if ( xTransf.is() )
840 // Is target dialog (library) localized?
841 uno::Reference< beans::XPropertySet > xDialogModelPropSet( m_xUnoControlDialogModel, uno::UNO_QUERY );
842 uno::Reference< resource::XStringResourceManager > xStringResourceManager;
843 if( xDialogModelPropSet.is() )
847 Any aResourceResolver = xDialogModelPropSet->getPropertyValue( aResourceResolverPropName );
848 aResourceResolver >>= xStringResourceManager;
850 catch(const UnknownPropertyException& )
853 bool bLocalized = false;
854 if( xStringResourceManager.is() )
855 bLocalized = ( xStringResourceManager->getLocales().getLength() > 0 );
857 if ( xTransf->isDataFlavorSupported( m_ClipboardDataFlavors[0] ) )
859 // create clipboard dialog model from xml
860 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
861 Reference< container::XNameContainer > xClipDialogModel( xContext->getServiceManager()->createInstanceWithContext(
862 "com.sun.star.awt.UnoControlDialogModel", xContext ), uno::UNO_QUERY );
864 bool bSourceIsLocalized = false;
865 Sequence< sal_Int8 > DialogModelBytes;
866 Sequence< sal_Int8 > aResData;
867 if( bLocalized && xTransf->isDataFlavorSupported( m_ClipboardDataFlavorsResource[1] ) )
869 bSourceIsLocalized = true;
871 Any aCombinedDataAny = xTransf->getTransferData( m_ClipboardDataFlavorsResource[1] );
872 Sequence< sal_Int8 > aCombinedData;
873 aCombinedDataAny >>= aCombinedData;
874 const sal_Int8* pCombinedData = aCombinedData.getConstArray();
876 sal_Int32 nTotalLen = aCombinedData.getLength();
878 // Reading offset
879 sal_Int32 nResOffset = 0;
880 sal_Int32 nFactor = 1;
881 for( sal_Int16 i = 0; i < 4; i++ )
883 nResOffset += nFactor * sal_uInt8( pCombinedData[i] );
884 nFactor *= 256;
887 sal_Int32 nResDataLen = nTotalLen - nResOffset;
888 sal_Int32 nDialogDataLen = nTotalLen - nResDataLen - 4;
890 DialogModelBytes.realloc( nDialogDataLen );
891 memcpy( DialogModelBytes.getArray(), pCombinedData + 4, nDialogDataLen );
893 aResData.realloc( nResDataLen );
894 memcpy( aResData.getArray(), pCombinedData + nResOffset, nResDataLen );
896 else
898 Any aAny = xTransf->getTransferData( m_ClipboardDataFlavors[0] );
899 aAny >>= DialogModelBytes;
902 if ( xClipDialogModel.is() )
904 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( rtl::ByteSequence(DialogModelBytes.getArray(), DialogModelBytes.getLength()) ) , xClipDialogModel, xContext, m_xDocument );
907 // get control models from clipboard dialog model
908 Reference< ::com::sun::star::container::XNameAccess > xNameAcc( xClipDialogModel, UNO_QUERY );
909 if ( xNameAcc.is() )
911 Sequence< OUString > aNames = xNameAcc->getElementNames();
912 const OUString* pNames = aNames.getConstArray();
913 sal_uInt32 nCtrls = aNames.getLength();
915 Reference< resource::XStringResourcePersistence > xStringResourcePersistence;
916 if( nCtrls > 0 && bSourceIsLocalized )
918 xStringResourcePersistence = css::resource::StringResource::create( getProcessComponentContext() );
919 xStringResourcePersistence->importBinary( aResData );
921 for( sal_uInt32 n = 0; n < nCtrls; n++ )
923 Any aA = xNameAcc->getByName( pNames[n] );
924 Reference< ::com::sun::star::awt::XControlModel > xCM;
925 aA >>= xCM;
927 // clone the control model
928 Reference< util::XCloneable > xClone( xCM, uno::UNO_QUERY );
929 Reference< awt::XControlModel > xCtrlModel( xClone->createClone(), uno::UNO_QUERY );
931 DlgEdObj* pCtrlObj = new DlgEdObj();
932 pCtrlObj->SetDlgEdForm(pDlgEdForm); // set parent form
933 pDlgEdForm->AddChild(pCtrlObj); // add child to parent form
934 pCtrlObj->SetUnoControlModel( xCtrlModel ); // set control model
936 // set new name
937 OUString aOUniqueName( pCtrlObj->GetUniqueName() );
938 Reference< beans::XPropertySet > xPSet( xCtrlModel , UNO_QUERY );
939 Any aUniqueName;
940 aUniqueName <<= aOUniqueName;
941 xPSet->setPropertyValue( DLGED_PROP_NAME, aUniqueName );
943 // set tabindex
944 Reference< container::XNameAccess > xNA( m_xUnoControlDialogModel , UNO_QUERY );
945 Sequence< OUString > aNames_ = xNA->getElementNames();
946 Any aTabIndex;
947 aTabIndex <<= (sal_Int16) aNames_.getLength();
948 xPSet->setPropertyValue( DLGED_PROP_TABINDEX, aTabIndex );
950 if( bLocalized )
952 Any aControlAny;
953 aControlAny <<= xCtrlModel;
954 if( bSourceIsLocalized && xStringResourcePersistence.is() )
956 Reference< resource::XStringResourceResolver >
957 xSourceStringResolver( xStringResourcePersistence, UNO_QUERY );
958 LocalizationMgr::copyResourcesForPastedEditorObject( this,
959 aControlAny, aOUniqueName, xSourceStringResolver );
961 else
963 LocalizationMgr::setControlResourceIDsForNewEditorObject
964 ( this, aControlAny, aOUniqueName );
968 // insert control model in editor dialog model
969 Any aCtrlModel;
970 aCtrlModel <<= xCtrlModel;
971 m_xUnoControlDialogModel->insertByName( aOUniqueName , aCtrlModel );
973 // insert object into drawing page
974 pDlgEdModel->GetPage(0)->InsertObject( pCtrlObj );
975 pCtrlObj->SetRectFromProps();
976 pCtrlObj->UpdateStep();
977 pDlgEdForm->UpdateTabOrderAndGroups();
978 pCtrlObj->StartListening(); // start listening
980 // mark object
981 SdrPageView* pPgView = pDlgEdView->GetSdrPageView();
982 pDlgEdView->MarkObj( pCtrlObj, pPgView, false, true);
985 // center marked objects in dialog editor form
986 Point aMarkCenter = (pDlgEdView->GetMarkedObjRect()).Center();
987 Point aFormCenter = (pDlgEdForm->GetSnapRect()).Center();
988 Point aPoint = aFormCenter - aMarkCenter;
989 Size aSize( aPoint.X() , aPoint.Y() );
990 pDlgEdView->MoveMarkedObj( aSize ); // update of control model properties (position + size) in NbcMove
991 pDlgEdView->MarkListHasChanged();
993 // dialog model changed
994 SetDialogModelChanged(true);
1002 void DlgEditor::Delete()
1004 if( !pDlgEdView->AreObjectsMarked() )
1005 return;
1007 // remove control models of marked objects from dialog model
1008 const size_t nMark = pDlgEdView->GetMarkedObjectList().GetMarkCount();
1010 for( size_t i = 0; i < nMark; ++i )
1012 SdrObject* pObj = pDlgEdView->GetMarkedObjectList().GetMark(i)->GetMarkedSdrObj();
1013 DlgEdObj* pDlgEdObj = dynamic_cast<DlgEdObj*>(pObj);
1015 if ( pDlgEdObj && !dynamic_cast<DlgEdForm*>(pDlgEdObj) )
1017 // get name from property
1018 OUString aName;
1019 uno::Reference< beans::XPropertySet > xPSet(pDlgEdObj->GetUnoControlModel(), uno::UNO_QUERY);
1020 if (xPSet.is())
1022 xPSet->getPropertyValue( DLGED_PROP_NAME ) >>= aName;
1025 // remove control from dialog model
1026 Reference< ::com::sun::star::container::XNameAccess > xNameAcc(pDlgEdObj->GetDlgEdForm()->GetUnoControlModel(), UNO_QUERY );
1027 if ( xNameAcc.is() && xNameAcc->hasByName(aName) )
1029 Reference< ::com::sun::star::container::XNameContainer > xCont(xNameAcc, UNO_QUERY );
1030 if ( xCont.is() )
1032 if( xCont->hasByName( aName ) )
1034 Any aAny = xCont->getByName( aName );
1035 LocalizationMgr::deleteControlResourceIDsForDeletedEditorObject( this, aAny, aName );
1037 xCont->removeByName( aName );
1041 // remove child from parent form
1042 pDlgEdForm->RemoveChild( pDlgEdObj );
1046 // update tab indices
1047 pDlgEdForm->UpdateTabIndices();
1049 pDlgEdView->BrkAction();
1051 bool const bDlgMarked = UnmarkDialog();
1052 pDlgEdView->DeleteMarked();
1053 if( bDlgMarked )
1054 RemarkDialog();
1058 bool DlgEditor::IsPasteAllowed()
1060 // get clipboard
1061 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow().GetClipboard();
1062 if ( xClipboard.is() )
1064 // get clipboard content
1065 SolarMutexReleaser aReleaser;
1066 Reference< datatransfer::XTransferable > xTransf = xClipboard->getContents();
1068 return xTransf.is() && xTransf->isDataFlavorSupported( m_ClipboardDataFlavors[0] );
1070 return false;
1074 void DlgEditor::ShowProperties()
1076 rLayout.ShowPropertyBrowser();
1080 void DlgEditor::UpdatePropertyBrowserDelayed()
1082 aMarkIdle.Start();
1086 bool DlgEditor::IsModified() const
1088 return pDlgEdModel->IsChanged() || bDialogModelChanged;
1092 void DlgEditor::ClearModifyFlag()
1094 pDlgEdModel->SetChanged(false);
1095 bDialogModelChanged = false;
1099 namespace Print
1101 long const nLeftMargin = 1700;
1102 long const nRightMargin = 900;
1103 long const nTopMargin = 2000;
1104 long const nBottomMargin = 1000;
1105 long const nBorder = 300;
1108 void lcl_PrintHeader( Printer* pPrinter, const OUString& rTitle ) // not working yet
1111 pPrinter->Push();
1113 Size const aSz = pPrinter->GetOutputSize();
1115 pPrinter->SetLineColor( COL_BLACK );
1116 pPrinter->SetFillColor();
1118 vcl::Font aFont( pPrinter->GetFont() );
1119 aFont.SetWeight( WEIGHT_BOLD );
1120 aFont.SetAlign( ALIGN_BOTTOM );
1121 pPrinter->SetFont( aFont );
1123 long const nFontHeight = pPrinter->GetTextHeight();
1125 // 1st border => line, 2+3 border = free space
1126 long const nYTop = Print::nTopMargin - 3*Print::nBorder - nFontHeight;
1128 long const nXLeft = Print::nLeftMargin - Print::nBorder;
1129 long const nXRight = aSz.Width() - Print::nRightMargin + Print::nBorder;
1131 pPrinter->DrawRect(Rectangle(
1132 Point(nXLeft, nYTop),
1133 Size(nXRight - nXLeft, aSz.Height() - nYTop - Print::nBottomMargin + Print::nBorder)
1136 long nY = Print::nTopMargin - 2*Print::nBorder;
1137 Point aPos(Print::nLeftMargin, nY);
1138 pPrinter->DrawText( aPos, rTitle );
1140 nY = Print::nTopMargin - Print::nBorder;
1141 pPrinter->DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
1143 pPrinter->Pop();
1147 void DlgEditor::printPage( sal_Int32 nPage, Printer* pPrinter, const OUString& rTitle )
1149 if( nPage == 0 )
1150 Print( pPrinter, rTitle );
1154 void DlgEditor::Print( Printer* pPrinter, const OUString& rTitle ) // not working yet
1157 MapMode aOldMap( pPrinter->GetMapMode());
1158 vcl::Font aOldFont( pPrinter->GetFont() );
1160 MapMode aMap( MAP_100TH_MM );
1161 pPrinter->SetMapMode( aMap );
1162 vcl::Font aFont;
1163 aFont.SetAlign( ALIGN_BOTTOM );
1164 aFont.SetSize( Size( 0, 360 ));
1165 pPrinter->SetFont( aFont );
1167 Size aPaperSz = pPrinter->GetOutputSize();
1168 aPaperSz.Width() -= (Print::nLeftMargin + Print::nRightMargin);
1169 aPaperSz.Height() -= (Print::nTopMargin + Print::nBottomMargin);
1171 lcl_PrintHeader( pPrinter, rTitle );
1173 Bitmap aDlg;
1174 Size aBmpSz( pPrinter->PixelToLogic( aDlg.GetSizePixel() ) );
1175 double nPaperSzWidth = aPaperSz.Width();
1176 double nPaperSzHeight = aPaperSz.Height();
1177 double nBmpSzWidth = aBmpSz.Width();
1178 double nBmpSzHeight = aBmpSz.Height();
1179 double nScaleX = (nPaperSzWidth / nBmpSzWidth );
1180 double nScaleY = (nPaperSzHeight / nBmpSzHeight );
1182 Size aOutputSz;
1183 if( nBmpSzHeight * nScaleX <= nPaperSzHeight )
1185 aOutputSz.Width() = (long)(((double)nBmpSzWidth) * nScaleX);
1186 aOutputSz.Height() = (long)(((double)nBmpSzHeight) * nScaleX);
1188 else
1190 aOutputSz.Width() = (long)(((double)nBmpSzWidth) * nScaleY);
1191 aOutputSz.Height() = (long)(((double)nBmpSzHeight) * nScaleY);
1194 Point aPosOffs(
1195 (aPaperSz.Width() / 2) - (aOutputSz.Width() / 2),
1196 (aPaperSz.Height()/ 2) - (aOutputSz.Height() / 2));
1198 aPosOffs.X() += Print::nLeftMargin;
1199 aPosOffs.Y() += Print::nTopMargin;
1201 pPrinter->DrawBitmap( aPosOffs, aOutputSz, aDlg );
1203 pPrinter->SetMapMode( aOldMap );
1204 pPrinter->SetFont( aOldFont );
1209 bool DlgEditor::AdjustPageSize()
1211 bool bAdjustedPageSize = false;
1212 Reference< beans::XPropertySet > xPSet( m_xUnoControlDialogModel, UNO_QUERY );
1213 if ( xPSet.is() )
1215 sal_Int32 nFormXIn = 0, nFormYIn = 0, nFormWidthIn = 0, nFormHeightIn = 0;
1216 xPSet->getPropertyValue( DLGED_PROP_POSITIONX ) >>= nFormXIn;
1217 xPSet->getPropertyValue( DLGED_PROP_POSITIONY ) >>= nFormYIn;
1218 xPSet->getPropertyValue( DLGED_PROP_WIDTH ) >>= nFormWidthIn;
1219 xPSet->getPropertyValue( DLGED_PROP_HEIGHT ) >>= nFormHeightIn;
1221 sal_Int32 nFormX, nFormY, nFormWidth, nFormHeight;
1222 if ( pDlgEdForm && pDlgEdForm->TransformFormToSdrCoordinates( nFormXIn, nFormYIn, nFormWidthIn, nFormHeightIn, nFormX, nFormY, nFormWidth, nFormHeight ) )
1224 Size aPageSizeDelta( 400, 300 );
1225 aPageSizeDelta = rWindow.PixelToLogic( aPageSizeDelta, MapMode( MAP_100TH_MM ) );
1227 sal_Int32 nNewPageWidth = nFormX + nFormWidth + aPageSizeDelta.Width();
1228 sal_Int32 nNewPageHeight = nFormY + nFormHeight + aPageSizeDelta.Height();
1230 Size aPageSizeMin( DLGED_PAGE_WIDTH_MIN, DLGED_PAGE_HEIGHT_MIN );
1231 aPageSizeMin = rWindow.PixelToLogic( aPageSizeMin, MapMode( MAP_100TH_MM ) );
1232 sal_Int32 nPageWidthMin = aPageSizeMin.Width();
1233 sal_Int32 nPageHeightMin = aPageSizeMin.Height();
1235 if ( nNewPageWidth < nPageWidthMin )
1236 nNewPageWidth = nPageWidthMin;
1238 if ( nNewPageHeight < nPageHeightMin )
1239 nNewPageHeight = nPageHeightMin;
1241 if ( pDlgEdPage )
1243 Size aPageSize = pDlgEdPage->GetSize();
1244 if ( nNewPageWidth != aPageSize.Width() || nNewPageHeight != aPageSize.Height() )
1246 Size aNewPageSize( nNewPageWidth, nNewPageHeight );
1247 pDlgEdPage->SetSize( aNewPageSize );
1248 pDlgEdView->SetWorkArea( Rectangle( Point( 0, 0 ), aNewPageSize ) );
1249 bAdjustedPageSize = true;
1255 return bAdjustedPageSize;
1259 } // namespace basctl
1261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */