Update ooo320-m1
[ooovba.git] / svx / source / dialog / connctrl.cxx
blob2005cfb04325f8fd59e41aa17aeecd0332170a3b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: connctrl.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 // include ---------------------------------------------------------------
36 #include <svx/svdoedge.hxx>
37 #include <svx/svdattrx.hxx>
38 #include <svx/svdmark.hxx>
39 #include <svx/svdview.hxx>
41 #include <svx/svdpage.hxx> // SdrObjList
43 #include "connctrl.hxx"
44 #include <svx/dialmgr.hxx>
45 #include "dlgutil.hxx"
47 // #110094#
48 #include <svx/sdr/contact/objectcontactofobjlistpainter.hxx>
50 // #110094#
51 #include <svx/sdr/contact/displayinfo.hxx>
52 #include <vcl/svapp.hxx>
54 /*************************************************************************
56 |* Ctor SvxXConnectionPreview
58 *************************************************************************/
60 SvxXConnectionPreview::SvxXConnectionPreview( Window* pParent, const ResId& rResId,
61 const SfxItemSet& rInAttrs ) :
62 Control ( pParent, rResId ),
63 rAttrs ( rInAttrs ),
64 pEdgeObj( NULL ),
65 pObjList( NULL ),
66 pView ( NULL )
68 SetMapMode( MAP_100TH_MM );
69 SetStyles();
72 /*************************************************************************
74 |* Dtor SvxXConnectionPreview
76 *************************************************************************/
78 SvxXConnectionPreview::~SvxXConnectionPreview()
80 delete pObjList;
83 /*************************************************************************
85 |* Dtor SvxXConnectionPreview
87 *************************************************************************/
89 void SvxXConnectionPreview::Construct()
91 DBG_ASSERT( pView, "Keine gueltige View Uebergeben!" );
93 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
94 ULONG nMarkCount = rMarkList.GetMarkCount();
96 if( nMarkCount >= 1 )
98 BOOL bFound = FALSE;
99 const SdrObject* pObj = rMarkList.GetMark(0)->GetMarkedSdrObj();
102 for( USHORT i = 0; i < nMarkCount && !bFound; i++ )
104 pObj = rMarkList.GetMark( i )->GetMarkedSdrObj();
105 UINT32 nInv = pObj->GetObjInventor();
106 UINT16 nId = pObj->GetObjIdentifier();
107 if( nInv == SdrInventor && nId == OBJ_EDGE )
109 bFound = TRUE;
110 SdrEdgeObj* pTmpEdgeObj = (SdrEdgeObj*) pObj;
111 pEdgeObj = (SdrEdgeObj*) pTmpEdgeObj->Clone();
113 SdrObjConnection& rConn1 = (SdrObjConnection&)pEdgeObj->GetConnection( TRUE );
114 SdrObjConnection& rConn2 = (SdrObjConnection&)pEdgeObj->GetConnection( FALSE );
116 rConn1 = pTmpEdgeObj->GetConnection( TRUE );
117 rConn2 = pTmpEdgeObj->GetConnection( FALSE );
119 SdrObject* pTmpObj1 = pTmpEdgeObj->GetConnectedNode( TRUE );
120 SdrObject* pTmpObj2 = pTmpEdgeObj->GetConnectedNode( FALSE );
122 // #110094#
123 // potential memory leak here (!). Create SdrObjList only when there is
124 // not yet one.
125 if(!pObjList)
127 pObjList = new SdrObjList( pView->GetModel(), NULL );
130 if( pTmpObj1 )
132 SdrObject* pObj1 = pTmpObj1->Clone();
133 pObjList->InsertObject( pObj1 );
134 pEdgeObj->ConnectToNode( TRUE, pObj1 );
136 if( pTmpObj2 )
138 SdrObject* pObj2 = pTmpObj2->Clone();
139 pObjList->InsertObject( pObj2 );
140 pEdgeObj->ConnectToNode( FALSE, pObj2 );
142 pObjList->InsertObject( pEdgeObj );
147 if( !pEdgeObj )
148 pEdgeObj = new SdrEdgeObj();
150 // Groesse anpassen
151 if( pObjList )
153 OutputDevice* pOD = pView->GetFirstOutputDevice(); // GetWin( 0 );
154 Rectangle aRect = pObjList->GetAllObjBoundRect();
156 MapMode aMapMode = GetMapMode();
157 aMapMode.SetMapUnit( pOD->GetMapMode().GetMapUnit() );
158 SetMapMode( aMapMode );
160 MapMode aDisplayMap( aMapMode );
161 Point aNewPos;
162 Size aNewSize;
163 const Size aWinSize = PixelToLogic( GetOutputSizePixel(), aDisplayMap );
164 const long nWidth = aWinSize.Width();
165 const long nHeight = aWinSize.Height();
166 double fRectWH = (double) aRect.GetWidth() / aRect.GetHeight();
167 double fWinWH = (double) nWidth / nHeight;
169 // Bitmap an Thumbgroesse anpassen (hier nicht!)
170 if ( fRectWH < fWinWH)
172 aNewSize.Width() = (long) ( (double) nHeight * fRectWH );
173 aNewSize.Height()= nHeight;
175 else
177 aNewSize.Width() = nWidth;
178 aNewSize.Height()= (long) ( (double) nWidth / fRectWH );
181 Fraction aFrac1( aWinSize.Width(), aRect.GetWidth() );
182 Fraction aFrac2( aWinSize.Height(), aRect.GetHeight() );
183 Fraction aMinFrac( aFrac1 <= aFrac2 ? aFrac1 : aFrac2 );
185 // MapMode umsetzen
186 aDisplayMap.SetScaleX( aMinFrac );
187 aDisplayMap.SetScaleY( aMinFrac );
189 // Zentrierung
190 aNewPos.X() = ( nWidth - aNewSize.Width() ) >> 1;
191 aNewPos.Y() = ( nHeight - aNewSize.Height() ) >> 1;
193 aDisplayMap.SetOrigin( LogicToLogic( aNewPos, aMapMode, aDisplayMap ) );
194 SetMapMode( aDisplayMap );
196 // Ursprung
197 aNewPos = aDisplayMap.GetOrigin();
198 aNewPos -= Point( aRect.TopLeft().X(), aRect.TopLeft().Y() );
199 aDisplayMap.SetOrigin( aNewPos );
200 SetMapMode( aDisplayMap );
203 Point aPos;
204 MouseEvent aMEvt( aPos, 1, 0, MOUSE_RIGHT );
205 MouseButtonDown( aMEvt );
207 Point aPt( -aRect.TopLeft().X(), -aRect.TopLeft().Y() );
208 aMapMode.SetOrigin( aPt );
210 // Skalierung
211 Size aSize = GetOutputSize();
212 Fraction aFrac1( aSize.Width(), aRect.GetWidth() );
213 Fraction aFrac2( aSize.Height(), aRect.GetHeight() );
214 Fraction aMaxFrac( aFrac1 > aFrac2 ? aFrac1 : aFrac2 );
215 Fraction aMinFrac( aFrac1 <= aFrac2 ? aFrac1 : aFrac2 );
216 BOOL bChange = (BOOL) ( (double)aMinFrac > 1.0 );
217 aMapMode.SetScaleX( aMinFrac );
218 aMapMode.SetScaleY( aMinFrac );
220 // zentrieren
221 long nXXL = aSize.Width() > aRect.GetWidth() ? aSize.Width() : aRect.GetWidth();
222 long nXS = aSize.Width() <= aRect.GetWidth() ? aSize.Width() : aRect.GetWidth();
223 if( bChange )
225 long nTmp = nXXL; nXXL = nXS; nXS = nTmp;
227 long nX = (long) ( (double)aMinFrac * (double)nXXL );
228 nX = (long) ( (double)labs( nXS - nX ) / (double)aMinFrac / 2.0 );
230 long nYXL = aSize.Height() > aRect.GetHeight() ? aSize.Height() : aRect.GetHeight();
231 long nYS = aSize.Height() <= aRect.GetHeight() ? aSize.Height() : aRect.GetHeight();
232 if( bChange )
234 long nTmp = nXXL; nXXL = nXS; nXS = nTmp;
236 long nY = (long) ( (double)aMinFrac * (double)nYXL );
237 nY = (long) ( (double)labs( nYS - nY ) / (double)aMinFrac / 2.0 );
239 aPt += Point( nX, nY );
240 aMapMode.SetOrigin( aPt );
242 SetMapMode( aMapMode );
247 /*************************************************************************
249 |* SvxXConnectionPreview: Paint()
251 *************************************************************************/
253 void SvxXConnectionPreview::Paint( const Rectangle& )
255 if( pObjList )
257 // #110094#
258 // This will not work anymore. To not start at Adam and Eve, i will
259 // ATM not try to change all this stuff to really using an own model
260 // and a view. I will just try to provide a mechanism to paint such
261 // objects without own model and without a page/view with the new
262 // mechanism.
264 // New stuff: Use a ObjectContactOfObjListPainter.
265 sdr::contact::SdrObjectVector aObjectVector;
267 for(sal_uInt32 a(0L); a < pObjList->GetObjCount(); a++)
269 SdrObject* pObject = pObjList->GetObj(a);
270 DBG_ASSERT(pObject,
271 "SvxXConnectionPreview::Paint: Corrupt ObjectList (!)");
272 aObjectVector.push_back(pObject);
275 sdr::contact::ObjectContactOfObjListPainter aPainter(*this, aObjectVector, 0);
276 sdr::contact::DisplayInfo aDisplayInfo;
278 // do processing
279 aPainter.ProcessDisplay(aDisplayInfo);
283 /*************************************************************************
285 |* SvxXConnectionPreview: SetAttributes()
287 *************************************************************************/
289 void SvxXConnectionPreview::SetAttributes( const SfxItemSet& rInAttrs )
291 //pEdgeObj->SetItemSetAndBroadcast(rInAttrs);
292 pEdgeObj->SetMergedItemSetAndBroadcast(rInAttrs);
294 Invalidate();
297 /*************************************************************************
299 |* Ermittelt die Anzahl der Linienversaetze anhand des Preview-Objektes
301 *************************************************************************/
303 USHORT SvxXConnectionPreview::GetLineDeltaAnz()
305 const SfxItemSet& rSet = pEdgeObj->GetMergedItemSet();
306 sal_uInt16 nCount(0);
308 if(SFX_ITEM_DONTCARE != rSet.GetItemState(SDRATTR_EDGELINEDELTAANZ))
309 nCount = ((const SdrEdgeLineDeltaAnzItem&)rSet.Get(SDRATTR_EDGELINEDELTAANZ)).GetValue();
311 return nCount;
314 /*************************************************************************
316 |* SvxXConnectionPreview: MouseButtonDown()
318 *************************************************************************/
320 void SvxXConnectionPreview::MouseButtonDown( const MouseEvent& rMEvt )
322 BOOL bZoomIn = rMEvt.IsLeft() && !rMEvt.IsShift();
323 BOOL bZoomOut = rMEvt.IsRight() || rMEvt.IsShift();
324 BOOL bCtrl = rMEvt.IsMod1();
326 if( bZoomIn || bZoomOut )
328 MapMode aMapMode = GetMapMode();
329 Fraction aXFrac = aMapMode.GetScaleX();
330 Fraction aYFrac = aMapMode.GetScaleY();
331 Fraction* pMultFrac;
333 if( bZoomIn )
335 if( bCtrl )
336 pMultFrac = new Fraction( 3, 2 );
337 else
338 pMultFrac = new Fraction( 11, 10 );
340 else
342 if( bCtrl )
343 pMultFrac = new Fraction( 2, 3 );
344 else
345 pMultFrac = new Fraction( 10, 11 );
348 aXFrac *= *pMultFrac;
349 aYFrac *= *pMultFrac;
350 if( (double)aXFrac > 0.001 && (double)aXFrac < 1000.0 &&
351 (double)aYFrac > 0.001 && (double)aYFrac < 1000.0 )
353 aMapMode.SetScaleX( aXFrac );
354 aMapMode.SetScaleY( aYFrac );
355 SetMapMode( aMapMode );
357 Size aOutSize( GetOutputSize() );
359 Point aPt( aMapMode.GetOrigin() );
360 long nX = (long)( ( (double)aOutSize.Width() - ( (double)aOutSize.Width() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
361 long nY = (long)( ( (double)aOutSize.Height() - ( (double)aOutSize.Height() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
362 aPt.X() += nX;
363 aPt.Y() += nY;
365 aMapMode.SetOrigin( aPt );
366 SetMapMode( aMapMode );
368 Invalidate();
370 delete pMultFrac;
374 void SvxXConnectionPreview::SetStyles()
376 const StyleSettings& rStyles = Application::GetSettings().GetStyleSettings();
377 SetDrawMode( GetDisplayBackground().GetColor().IsDark() ? OUTPUT_DRAWMODE_CONTRAST : OUTPUT_DRAWMODE_COLOR );
378 SetBackground( Wallpaper( Color( rStyles.GetFieldColor() ) ) );
381 void SvxXConnectionPreview::DataChanged( const DataChangedEvent& rDCEvt )
383 Control::DataChanged( rDCEvt );
385 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
387 SetStyles();