Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <vcl/svapp.hxx>
21 :
22 : #include "svx/connctrl.hxx"
23 : #include "svx/dlgutil.hxx"
24 :
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>
34 :
35 : #include <vcl/builder.hxx>
36 : #include <vcl/settings.hxx>
37 : #include <boost/scoped_ptr.hpp>
38 :
39 0 : SvxXConnectionPreview::SvxXConnectionPreview( vcl::Window* pParent, WinBits nStyle)
40 : : Control(pParent, nStyle)
41 : , pEdgeObj(NULL)
42 : , pObjList(NULL)
43 0 : , pView(NULL)
44 : {
45 0 : SetMapMode( MAP_100TH_MM );
46 0 : SetStyles();
47 0 : }
48 :
49 0 : extern "C" SAL_DLLPUBLIC_EXPORT vcl::Window* SAL_CALL makeSvxXConnectionPreview(vcl::Window *pParent, VclBuilder::stringmap &rMap)
50 : {
51 0 : WinBits nWinStyle = 0;
52 0 : OString sBorder = VclBuilder::extractCustomProperty(rMap);
53 0 : if (!sBorder.isEmpty())
54 0 : nWinStyle |= WB_BORDER;
55 0 : return new SvxXConnectionPreview(pParent, nWinStyle);
56 : }
57 :
58 0 : SvxXConnectionPreview::~SvxXConnectionPreview()
59 : {
60 0 : delete pObjList;
61 0 : }
62 :
63 0 : void SvxXConnectionPreview::Resize()
64 : {
65 0 : Control::Resize();
66 :
67 0 : AdaptSize();
68 :
69 0 : Invalidate();
70 0 : }
71 :
72 0 : Size SvxXConnectionPreview::GetOptimalSize() const
73 : {
74 0 : return LogicToPixel(Size(118 , 121), MapMode(MAP_APPFONT));
75 : }
76 :
77 0 : void SvxXConnectionPreview::AdaptSize()
78 : {
79 : // Adapt size
80 0 : if( pObjList )
81 : {
82 0 : SetMapMode( MAP_100TH_MM );
83 :
84 0 : OutputDevice* pOD = pView->GetFirstOutputDevice(); // GetWin( 0 );
85 0 : Rectangle aRect = pObjList->GetAllObjBoundRect();
86 :
87 0 : MapMode aMapMode = GetMapMode();
88 0 : aMapMode.SetMapUnit( pOD->GetMapMode().GetMapUnit() );
89 0 : SetMapMode( aMapMode );
90 :
91 0 : MapMode aDisplayMap( aMapMode );
92 0 : Point aNewPos;
93 0 : Size aNewSize;
94 0 : const Size aWinSize = PixelToLogic( GetOutputSizePixel(), aDisplayMap );
95 0 : const long nWidth = aWinSize.Width();
96 0 : const long nHeight = aWinSize.Height();
97 0 : if (aRect.GetHeight() == 0)
98 0 : return;
99 0 : double fRectWH = (double) aRect.GetWidth() / aRect.GetHeight();
100 0 : if (nHeight == 0)
101 0 : return;
102 0 : double fWinWH = (double) nWidth / nHeight;
103 :
104 : // Adapt bitmap to Thumb size (not here!)
105 0 : if ( fRectWH < fWinWH)
106 : {
107 0 : aNewSize.Width() = (long) ( (double) nHeight * fRectWH );
108 0 : aNewSize.Height()= nHeight;
109 : }
110 : else
111 : {
112 0 : aNewSize.Width() = nWidth;
113 0 : aNewSize.Height()= (long) ( (double) nWidth / fRectWH );
114 : }
115 :
116 0 : Fraction aFrac1( aWinSize.Width(), aRect.GetWidth() );
117 0 : Fraction aFrac2( aWinSize.Height(), aRect.GetHeight() );
118 0 : Fraction aMinFrac( aFrac1 <= aFrac2 ? aFrac1 : aFrac2 );
119 :
120 : // Implement MapMode
121 0 : aDisplayMap.SetScaleX( aMinFrac );
122 0 : aDisplayMap.SetScaleY( aMinFrac );
123 :
124 : // Centering
125 0 : aNewPos.X() = ( nWidth - aNewSize.Width() ) >> 1;
126 0 : aNewPos.Y() = ( nHeight - aNewSize.Height() ) >> 1;
127 :
128 0 : aDisplayMap.SetOrigin( LogicToLogic( aNewPos, aMapMode, aDisplayMap ) );
129 0 : SetMapMode( aDisplayMap );
130 :
131 : // Origin
132 0 : aNewPos = aDisplayMap.GetOrigin();
133 0 : aNewPos -= Point( aRect.TopLeft().X(), aRect.TopLeft().Y() );
134 0 : aDisplayMap.SetOrigin( aNewPos );
135 0 : SetMapMode( aDisplayMap );
136 :
137 0 : Point aPos;
138 0 : MouseEvent aMEvt( aPos, 1, 0, MOUSE_RIGHT );
139 0 : MouseButtonDown( aMEvt );
140 : }
141 : }
142 :
143 0 : void SvxXConnectionPreview::Construct()
144 : {
145 : DBG_ASSERT( pView, "No valid view is passed on! ");
146 :
147 0 : const SdrMarkList& rMarkList = pView->GetMarkedObjectList();
148 0 : const size_t nMarkCount = rMarkList.GetMarkCount();
149 :
150 0 : if( nMarkCount >= 1 )
151 : {
152 0 : bool bFound = false;
153 :
154 0 : for( size_t i = 0; i < nMarkCount && !bFound; ++i )
155 : {
156 0 : const SdrObject* pObj = rMarkList.GetMark( i )->GetMarkedSdrObj();
157 0 : sal_uInt32 nInv = pObj->GetObjInventor();
158 0 : sal_uInt16 nId = pObj->GetObjIdentifier();
159 0 : if( nInv == SdrInventor && nId == OBJ_EDGE )
160 : {
161 0 : bFound = true;
162 0 : const SdrEdgeObj* pTmpEdgeObj = static_cast<const SdrEdgeObj*>(pObj);
163 0 : pEdgeObj = (SdrEdgeObj*) pTmpEdgeObj->Clone();
164 :
165 0 : SdrObjConnection& rConn1 = (SdrObjConnection&)pEdgeObj->GetConnection( true );
166 0 : SdrObjConnection& rConn2 = (SdrObjConnection&)pEdgeObj->GetConnection( false );
167 :
168 0 : rConn1 = pTmpEdgeObj->GetConnection( true );
169 0 : rConn2 = pTmpEdgeObj->GetConnection( false );
170 :
171 0 : SdrObject* pTmpObj1 = pTmpEdgeObj->GetConnectedNode( true );
172 0 : SdrObject* pTmpObj2 = pTmpEdgeObj->GetConnectedNode( false );
173 :
174 : // potential memory leak here (!). Create SdrObjList only when there is
175 : // not yet one.
176 0 : if(!pObjList)
177 : {
178 0 : pObjList = new SdrObjList( pView->GetModel(), NULL );
179 : }
180 :
181 0 : if( pTmpObj1 )
182 : {
183 0 : SdrObject* pObj1 = pTmpObj1->Clone();
184 0 : pObjList->InsertObject( pObj1 );
185 0 : pEdgeObj->ConnectToNode( true, pObj1 );
186 : }
187 0 : if( pTmpObj2 )
188 : {
189 0 : SdrObject* pObj2 = pTmpObj2->Clone();
190 0 : pObjList->InsertObject( pObj2 );
191 0 : pEdgeObj->ConnectToNode( false, pObj2 );
192 : }
193 0 : pObjList->InsertObject( pEdgeObj );
194 : }
195 : }
196 : }
197 :
198 0 : if( !pEdgeObj )
199 0 : pEdgeObj = new SdrEdgeObj();
200 :
201 0 : AdaptSize();
202 0 : }
203 :
204 0 : void SvxXConnectionPreview::Paint( const Rectangle& )
205 : {
206 0 : if( pObjList )
207 : {
208 : // #110094#
209 : // This will not work anymore. To not start at Adam and Eve, i will
210 : // ATM not try to change all this stuff to really using an own model
211 : // and a view. I will just try to provide a mechanism to paint such
212 : // objects without own model and without a page/view with the new
213 : // mechanism.
214 :
215 : // New stuff: Use a ObjectContactOfObjListPainter.
216 0 : sdr::contact::SdrObjectVector aObjectVector;
217 :
218 0 : for(size_t a = 0; a < pObjList->GetObjCount(); ++a)
219 : {
220 0 : SdrObject* pObject = pObjList->GetObj(a);
221 : DBG_ASSERT(pObject,
222 : "SvxXConnectionPreview::Paint: Corrupt ObjectList (!)");
223 0 : aObjectVector.push_back(pObject);
224 : }
225 :
226 0 : sdr::contact::ObjectContactOfObjListPainter aPainter(*this, aObjectVector, 0);
227 0 : sdr::contact::DisplayInfo aDisplayInfo;
228 :
229 : // do processing
230 0 : aPainter.ProcessDisplay(aDisplayInfo);
231 : }
232 0 : }
233 :
234 0 : void SvxXConnectionPreview::SetAttributes( const SfxItemSet& rInAttrs )
235 : {
236 0 : pEdgeObj->SetMergedItemSetAndBroadcast(rInAttrs);
237 :
238 0 : Invalidate();
239 0 : }
240 :
241 :
242 : // Get number of lines which are offset based on the preview object
243 :
244 0 : sal_uInt16 SvxXConnectionPreview::GetLineDeltaAnz()
245 : {
246 0 : const SfxItemSet& rSet = pEdgeObj->GetMergedItemSet();
247 0 : sal_uInt16 nCount(0);
248 :
249 0 : if(SfxItemState::DONTCARE != rSet.GetItemState(SDRATTR_EDGELINEDELTAANZ))
250 0 : nCount = static_cast<const SdrEdgeLineDeltaAnzItem&>(rSet.Get(SDRATTR_EDGELINEDELTAANZ)).GetValue();
251 :
252 0 : return nCount;
253 : }
254 :
255 0 : void SvxXConnectionPreview::MouseButtonDown( const MouseEvent& rMEvt )
256 : {
257 0 : bool bZoomIn = rMEvt.IsLeft() && !rMEvt.IsShift();
258 0 : bool bZoomOut = rMEvt.IsRight() || rMEvt.IsShift();
259 0 : bool bCtrl = rMEvt.IsMod1();
260 :
261 0 : if( bZoomIn || bZoomOut )
262 : {
263 0 : MapMode aMapMode = GetMapMode();
264 0 : Fraction aXFrac = aMapMode.GetScaleX();
265 0 : Fraction aYFrac = aMapMode.GetScaleY();
266 0 : boost::scoped_ptr<Fraction> pMultFrac;
267 :
268 0 : if( bZoomIn )
269 : {
270 0 : if( bCtrl )
271 0 : pMultFrac.reset(new Fraction( 3, 2 ));
272 : else
273 0 : pMultFrac.reset(new Fraction( 11, 10 ));
274 : }
275 : else
276 : {
277 0 : if( bCtrl )
278 0 : pMultFrac.reset(new Fraction( 2, 3 ));
279 : else
280 0 : pMultFrac.reset(new Fraction( 10, 11 ));
281 : }
282 :
283 0 : aXFrac *= *pMultFrac;
284 0 : aYFrac *= *pMultFrac;
285 0 : if( (double)aXFrac > 0.001 && (double)aXFrac < 1000.0 &&
286 0 : (double)aYFrac > 0.001 && (double)aYFrac < 1000.0 )
287 : {
288 0 : aMapMode.SetScaleX( aXFrac );
289 0 : aMapMode.SetScaleY( aYFrac );
290 0 : SetMapMode( aMapMode );
291 :
292 0 : Size aOutSize( GetOutputSize() );
293 :
294 0 : Point aPt( aMapMode.GetOrigin() );
295 0 : long nX = (long)( ( (double)aOutSize.Width() - ( (double)aOutSize.Width() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
296 0 : long nY = (long)( ( (double)aOutSize.Height() - ( (double)aOutSize.Height() * (double)*pMultFrac ) ) / 2.0 + 0.5 );
297 0 : aPt.X() += nX;
298 0 : aPt.Y() += nY;
299 :
300 0 : aMapMode.SetOrigin( aPt );
301 0 : SetMapMode( aMapMode );
302 :
303 0 : Invalidate();
304 0 : }
305 : }
306 0 : }
307 :
308 0 : void SvxXConnectionPreview::SetStyles()
309 : {
310 0 : const StyleSettings& rStyles = Application::GetSettings().GetStyleSettings();
311 0 : SetDrawMode( GetSettings().GetStyleSettings().GetHighContrastMode() ? OUTPUT_DRAWMODE_CONTRAST : OUTPUT_DRAWMODE_COLOR );
312 0 : SetBackground( Wallpaper( Color( rStyles.GetFieldColor() ) ) );
313 0 : }
314 :
315 0 : void SvxXConnectionPreview::DataChanged( const DataChangedEvent& rDCEvt )
316 : {
317 0 : Control::DataChanged( rDCEvt );
318 :
319 0 : if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
320 : {
321 0 : SetStyles();
322 : }
323 594 : }
324 :
325 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|