Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / dialog / connctrl.cxx
blobfb8f85e2ff84175c4bd585188cf558889ea2ae9d
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 .
20 #include <vcl/svapp.hxx>
22 #include "svx/connctrl.hxx"
23 #include "svx/dlgutil.hxx"
25 #include <svx/dialmgr.hxx>
26 #include <svx/sdr/contact/displayinfo.hxx>
27 #include <svx/sdr/contact/objectcontactofobjlistpainter.hxx>
28 #include <svx/svdmark.hxx>
29 #include <svx/svdoedge.hxx>
30 #include <svx/svdpage.hxx>
31 #include <svx/svdview.hxx>
32 #include <svx/sxelditm.hxx>
33 #include <sxmkitm.hxx>
35 #include <vcl/builder.hxx>
36 #include <vcl/settings.hxx>
38 SvxXConnectionPreview::SvxXConnectionPreview( Window* pParent, WinBits nStyle)
39 : Control(pParent, nStyle)
40 , pEdgeObj(NULL)
41 , pObjList(NULL)
42 , pView(NULL)
44 SetMapMode( MAP_100TH_MM );
45 SetStyles();
48 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxXConnectionPreview(Window *pParent, VclBuilder::stringmap &rMap)
50 WinBits nWinStyle = 0;
51 OString sBorder = VclBuilder::extractCustomProperty(rMap);
52 if (!sBorder.isEmpty())
53 nWinStyle |= WB_BORDER;
54 return new SvxXConnectionPreview(pParent, nWinStyle);
57 SvxXConnectionPreview::~SvxXConnectionPreview()
59 delete pObjList;
62 void SvxXConnectionPreview::Resize()
64 Control::Resize();
66 AdaptSize();
68 Invalidate();
71 Size SvxXConnectionPreview::GetOptimalSize() const
73 return LogicToPixel(Size(118 , 121), MapMode(MAP_APPFONT));
76 void SvxXConnectionPreview::AdaptSize()
78 // Adapt size
79 if( pObjList )
81 SetMapMode( MAP_100TH_MM );
83 OutputDevice* pOD = pView->GetFirstOutputDevice(); // GetWin( 0 );
84 Rectangle aRect = pObjList->GetAllObjBoundRect();
86 MapMode aMapMode = GetMapMode();
87 aMapMode.SetMapUnit( pOD->GetMapMode().GetMapUnit() );
88 SetMapMode( aMapMode );
90 MapMode aDisplayMap( aMapMode );
91 Point aNewPos;
92 Size aNewSize;
93 const Size aWinSize = PixelToLogic( GetOutputSizePixel(), aDisplayMap );
94 const long nWidth = aWinSize.Width();
95 const long nHeight = aWinSize.Height();
96 if (aRect.GetHeight() == 0)
97 return;
98 double fRectWH = (double) aRect.GetWidth() / aRect.GetHeight();
99 if (nHeight == 0)
100 return;
101 double fWinWH = (double) nWidth / nHeight;
103 // Adapt bitmap to Thumb size (not here!)
104 if ( fRectWH < fWinWH)
106 aNewSize.Width() = (long) ( (double) nHeight * fRectWH );
107 aNewSize.Height()= nHeight;
109 else
111 aNewSize.Width() = nWidth;
112 aNewSize.Height()= (long) ( (double) nWidth / fRectWH );
115 Fraction aFrac1( aWinSize.Width(), aRect.GetWidth() );
116 Fraction aFrac2( aWinSize.Height(), aRect.GetHeight() );
117 Fraction aMinFrac( aFrac1 <= aFrac2 ? aFrac1 : aFrac2 );
119 // Implement MapMode
120 aDisplayMap.SetScaleX( aMinFrac );
121 aDisplayMap.SetScaleY( aMinFrac );
123 // Centering
124 aNewPos.X() = ( nWidth - aNewSize.Width() ) >> 1;
125 aNewPos.Y() = ( nHeight - aNewSize.Height() ) >> 1;
127 aDisplayMap.SetOrigin( LogicToLogic( aNewPos, aMapMode, aDisplayMap ) );
128 SetMapMode( aDisplayMap );
130 // Origin
131 aNewPos = aDisplayMap.GetOrigin();
132 aNewPos -= Point( aRect.TopLeft().X(), aRect.TopLeft().Y() );
133 aDisplayMap.SetOrigin( aNewPos );
134 SetMapMode( aDisplayMap );
136 Point aPos;
137 MouseEvent aMEvt( aPos, 1, 0, MOUSE_RIGHT );
138 MouseButtonDown( aMEvt );
142 void SvxXConnectionPreview::Construct()
144 DBG_ASSERT( pView, "No valid view is passed on! ");
146 const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
147 sal_uIntPtr nMarkCount = rMarkList.GetMarkCount();
149 if( nMarkCount >= 1 )
151 bool bFound = false;
153 for( sal_uInt16 i = 0; i < nMarkCount && !bFound; i++ )
155 const SdrObject* pObj = rMarkList.GetMark( i )->GetMarkedSdrObj();
156 sal_uInt32 nInv = pObj->GetObjInventor();
157 sal_uInt16 nId = pObj->GetObjIdentifier();
158 if( nInv == SdrInventor && nId == OBJ_EDGE )
160 bFound = true;
161 SdrEdgeObj* pTmpEdgeObj = (SdrEdgeObj*) pObj;
162 pEdgeObj = (SdrEdgeObj*) pTmpEdgeObj->Clone();
164 SdrObjConnection& rConn1 = (SdrObjConnection&)pEdgeObj->GetConnection( true );
165 SdrObjConnection& rConn2 = (SdrObjConnection&)pEdgeObj->GetConnection( false );
167 rConn1 = pTmpEdgeObj->GetConnection( true );
168 rConn2 = pTmpEdgeObj->GetConnection( false );
170 SdrObject* pTmpObj1 = pTmpEdgeObj->GetConnectedNode( true );
171 SdrObject* pTmpObj2 = pTmpEdgeObj->GetConnectedNode( false );
173 // potential memory leak here (!). Create SdrObjList only when there is
174 // not yet one.
175 if(!pObjList)
177 pObjList = new SdrObjList( pView->GetModel(), NULL );
180 if( pTmpObj1 )
182 SdrObject* pObj1 = pTmpObj1->Clone();
183 pObjList->InsertObject( pObj1 );
184 pEdgeObj->ConnectToNode( true, pObj1 );
186 if( pTmpObj2 )
188 SdrObject* pObj2 = pTmpObj2->Clone();
189 pObjList->InsertObject( pObj2 );
190 pEdgeObj->ConnectToNode( false, pObj2 );
192 pObjList->InsertObject( pEdgeObj );
197 if( !pEdgeObj )
198 pEdgeObj = new SdrEdgeObj();
200 AdaptSize();
203 void SvxXConnectionPreview::Paint( const Rectangle& )
205 if( pObjList )
207 // #110094#
208 // This will not work anymore. To not start at Adam and Eve, i will
209 // ATM not try to change all this stuff to really using an own model
210 // and a view. I will just try to provide a mechanism to paint such
211 // objects without own model and without a page/view with the new
212 // mechanism.
214 // New stuff: Use a ObjectContactOfObjListPainter.
215 sdr::contact::SdrObjectVector aObjectVector;
217 for(sal_uInt32 a(0L); a < pObjList->GetObjCount(); a++)
219 SdrObject* pObject = pObjList->GetObj(a);
220 DBG_ASSERT(pObject,
221 "SvxXConnectionPreview::Paint: Corrupt ObjectList (!)");
222 aObjectVector.push_back(pObject);
225 sdr::contact::ObjectContactOfObjListPainter aPainter(*this, aObjectVector, 0);
226 sdr::contact::DisplayInfo aDisplayInfo;
228 // do processing
229 aPainter.ProcessDisplay(aDisplayInfo);
233 void SvxXConnectionPreview::SetAttributes( const SfxItemSet& rInAttrs )
235 pEdgeObj->SetMergedItemSetAndBroadcast(rInAttrs);
237 Invalidate();
241 // Get number of lines which are offset based on the preview object
243 sal_uInt16 SvxXConnectionPreview::GetLineDeltaAnz()
245 const SfxItemSet& rSet = pEdgeObj->GetMergedItemSet();
246 sal_uInt16 nCount(0);
248 if(SFX_ITEM_DONTCARE != rSet.GetItemState(SDRATTR_EDGELINEDELTAANZ))
249 nCount = ((const SdrEdgeLineDeltaAnzItem&)rSet.Get(SDRATTR_EDGELINEDELTAANZ)).GetValue();
251 return nCount;
254 void SvxXConnectionPreview::MouseButtonDown( const MouseEvent& rMEvt )
256 bool bZoomIn = rMEvt.IsLeft() && !rMEvt.IsShift();
257 bool bZoomOut = rMEvt.IsRight() || rMEvt.IsShift();
258 bool bCtrl = rMEvt.IsMod1();
260 if( bZoomIn || bZoomOut )
262 MapMode aMapMode = GetMapMode();
263 Fraction aXFrac = aMapMode.GetScaleX();
264 Fraction aYFrac = aMapMode.GetScaleY();
265 Fraction* pMultFrac;
267 if( bZoomIn )
269 if( bCtrl )
270 pMultFrac = new Fraction( 3, 2 );
271 else
272 pMultFrac = new Fraction( 11, 10 );
274 else
276 if( bCtrl )
277 pMultFrac = new Fraction( 2, 3 );
278 else
279 pMultFrac = new Fraction( 10, 11 );
282 aXFrac *= *pMultFrac;
283 aYFrac *= *pMultFrac;
284 if( (double)aXFrac > 0.001 && (double)aXFrac < 1000.0 &&
285 (double)aYFrac > 0.001 && (double)aYFrac < 1000.0 )
287 aMapMode.SetScaleX( aXFrac );
288 aMapMode.SetScaleY( aYFrac );
289 SetMapMode( aMapMode );
291 Size aOutSize( GetOutputSize() );
293 Point aPt( aMapMode.GetOrigin() );
294 long nX = (long)( ( (double)aOutSize.Width() - ( (double)aOutSize.Width() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
295 long nY = (long)( ( (double)aOutSize.Height() - ( (double)aOutSize.Height() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
296 aPt.X() += nX;
297 aPt.Y() += nY;
299 aMapMode.SetOrigin( aPt );
300 SetMapMode( aMapMode );
302 Invalidate();
304 delete pMultFrac;
308 void SvxXConnectionPreview::SetStyles()
310 const StyleSettings& rStyles = Application::GetSettings().GetStyleSettings();
311 SetDrawMode( GetSettings().GetStyleSettings().GetHighContrastMode() ? OUTPUT_DRAWMODE_CONTRAST : OUTPUT_DRAWMODE_COLOR );
312 SetBackground( Wallpaper( Color( rStyles.GetFieldColor() ) ) );
315 void SvxXConnectionPreview::DataChanged( const DataChangedEvent& rDCEvt )
317 Control::DataChanged( rDCEvt );
319 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
321 SetStyles();
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */