cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / basctl / source / dlged / dlgedfunc.cxx
blob719814933d6eaa8f9ced907d95b4e7a98c8ee797
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 <svtools/scrolladaptor.hxx>
21 #include <svx/svdview.hxx>
22 #include <dlgedfunc.hxx>
23 #include <dlged.hxx>
24 #include <dlgedview.hxx>
25 #include <vcl/seleng.hxx>
27 namespace basctl
30 IMPL_LINK_NOARG( DlgEdFunc, ScrollTimeout, Timer *, void )
32 vcl::Window& rWindow = rParent.GetWindow();
33 Point aPos = rWindow.ScreenToOutputPixel( rWindow.GetPointerPosPixel() );
34 aPos = rWindow.PixelToLogic( aPos );
35 ForceScroll( aPos );
38 void DlgEdFunc::ForceScroll( const Point& rPos )
40 aScrollTimer.Stop();
42 vcl::Window& rWindow = rParent.GetWindow();
44 static const Point aDefPoint;
45 tools::Rectangle aOutRect( aDefPoint, rWindow.GetOutputSizePixel() );
46 aOutRect = rWindow.PixelToLogic( aOutRect );
48 ScrollAdaptor* pHScroll = rParent.GetHScroll();
49 ScrollAdaptor* pVScroll = rParent.GetVScroll();
50 tools::Long nDeltaX = pHScroll->GetLineSize();
51 tools::Long nDeltaY = pVScroll->GetLineSize();
53 if( !aOutRect.Contains( rPos ) )
55 if( rPos.X() < aOutRect.Left() )
56 nDeltaX = -nDeltaX;
57 else if( rPos.X() <= aOutRect.Right() )
58 nDeltaX = 0;
60 if( rPos.Y() < aOutRect.Top() )
61 nDeltaY = -nDeltaY;
62 else if( rPos.Y() <= aOutRect.Bottom() )
63 nDeltaY = 0;
65 if( nDeltaX )
66 pHScroll->SetThumbPos( pHScroll->GetThumbPos() + nDeltaX );
67 if( nDeltaY )
68 pVScroll->SetThumbPos( pVScroll->GetThumbPos() + nDeltaY );
70 if( nDeltaX )
71 rParent.DoScroll();
72 if( nDeltaY )
73 rParent.DoScroll();
76 aScrollTimer.Start();
79 DlgEdFunc::DlgEdFunc (DlgEditor& rParent_) :
80 rParent(rParent_), aScrollTimer("basctl DlgEdFunc aScrollTimer")
82 aScrollTimer.SetInvokeHandler( LINK( this, DlgEdFunc, ScrollTimeout ) );
83 aScrollTimer.SetTimeout( SELENG_AUTOREPEAT_INTERVAL );
86 DlgEdFunc::~DlgEdFunc()
90 void DlgEdFunc::MouseButtonDown( const MouseEvent& )
94 bool DlgEdFunc::MouseButtonUp( const MouseEvent& )
96 aScrollTimer.Stop();
97 return true;
100 void DlgEdFunc::MouseMove( const MouseEvent& )
104 bool DlgEdFunc::KeyInput( const KeyEvent& rKEvt )
106 bool bReturn = false;
108 SdrView& rView = rParent.GetView();
109 vcl::Window& rWindow = rParent.GetWindow();
111 vcl::KeyCode aCode = rKEvt.GetKeyCode();
112 sal_uInt16 nCode = aCode.GetCode();
114 switch ( nCode )
116 case KEY_ESCAPE:
118 if ( rView.IsAction() )
120 rView.BrkAction();
121 bReturn = true;
123 else if ( rView.GetMarkedObjectList().GetMarkCount() != 0 )
125 const SdrHdlList& rHdlList = rView.GetHdlList();
126 SdrHdl* pHdl = rHdlList.GetFocusHdl();
127 if ( pHdl )
128 const_cast<SdrHdlList&>(rHdlList).ResetFocusHdl();
129 else
130 rView.UnmarkAll();
132 bReturn = true;
135 break;
136 case KEY_TAB:
138 if ( !aCode.IsMod1() && !aCode.IsMod2() )
140 // mark next object
141 if ( !rView.MarkNextObj( !aCode.IsShift() ) )
143 // if no next object, mark first/last
144 rView.UnmarkAllObj();
145 rView.MarkNextObj( !aCode.IsShift() );
148 if ( rView.GetMarkedObjectList().GetMarkCount() != 0 )
149 rView.MakeVisible( rView.GetAllMarkedRect(), rWindow );
151 bReturn = true;
153 else if ( aCode.IsMod1() )
155 // selected handle
156 const SdrHdlList& rHdlList = rView.GetHdlList();
157 const_cast<SdrHdlList&>(rHdlList).TravelFocusHdl( !aCode.IsShift() );
159 // guarantee visibility of focused handle
160 if (SdrHdl* pHdl = rHdlList.GetFocusHdl())
162 Point aHdlPosition( pHdl->GetPos() );
163 tools::Rectangle aVisRect( aHdlPosition - Point( 100, 100 ), Size( 200, 200 ) );
164 rView.MakeVisible( aVisRect, rWindow );
167 bReturn = true;
170 break;
171 case KEY_UP:
172 case KEY_DOWN:
173 case KEY_LEFT:
174 case KEY_RIGHT:
176 tools::Long nX = 0;
177 tools::Long nY = 0;
179 if ( nCode == KEY_UP )
181 // scroll up
182 nX = 0;
183 nY = -1;
185 else if ( nCode == KEY_DOWN )
187 // scroll down
188 nX = 0;
189 nY = 1;
191 else if ( nCode == KEY_LEFT )
193 // scroll left
194 nX = -1;
195 nY = 0;
197 else if ( nCode == KEY_RIGHT )
199 // scroll right
200 nX = 1;
201 nY = 0;
204 if ( rView.GetMarkedObjectList().GetMarkCount() != 0 && !aCode.IsMod1() )
206 if ( aCode.IsMod2() )
208 // move in 1 pixel distance
209 Size aPixelSize = rWindow.PixelToLogic(Size(1, 1));
210 nX *= aPixelSize.Width();
211 nY *= aPixelSize.Height();
213 else
215 // move in 1 mm distance
216 nX *= 100;
217 nY *= 100;
220 const SdrHdlList& rHdlList = rView.GetHdlList();
221 SdrHdl* pHdl = rHdlList.GetFocusHdl();
223 if ( pHdl == nullptr )
225 // no handle selected
226 if ( rView.IsMoveAllowed() )
228 // restrict movement to work area
229 const tools::Rectangle& rWorkArea = rView.GetWorkArea();
231 if ( !rWorkArea.IsEmpty() )
233 tools::Rectangle aMarkRect( rView.GetMarkedObjRect() );
234 aMarkRect.Move( nX, nY );
236 if ( !rWorkArea.Contains( aMarkRect ) )
238 if ( aMarkRect.Left() < rWorkArea.Left() )
239 nX += rWorkArea.Left() - aMarkRect.Left();
241 if ( aMarkRect.Right() > rWorkArea.Right() )
242 nX -= aMarkRect.Right() - rWorkArea.Right();
244 if ( aMarkRect.Top() < rWorkArea.Top() )
245 nY += rWorkArea.Top() - aMarkRect.Top();
247 if ( aMarkRect.Bottom() > rWorkArea.Bottom() )
248 nY -= aMarkRect.Bottom() - rWorkArea.Bottom();
252 if ( nX != 0 || nY != 0 )
254 rView.MoveAllMarked( Size( nX, nY ) );
255 rView.MakeVisible( rView.GetAllMarkedRect(), rWindow );
259 else if (nX || nY)
261 Point aStartPoint(pHdl->GetPos());
262 Point aEndPoint(pHdl->GetPos() + Point(nX, nY));
263 const SdrDragStat& rDragStat = rView.GetDragStat();
265 // start dragging
266 rView.BegDragObj(aStartPoint, nullptr, pHdl, 0);
268 if (rView.IsDragObj())
270 bool const bWasNoSnap = rDragStat.IsNoSnap();
271 bool const bWasSnapEnabled = rView.IsSnapEnabled();
273 // switch snapping off
274 if (!bWasNoSnap)
275 const_cast<SdrDragStat&>(rDragStat).SetNoSnap();
276 if (bWasSnapEnabled)
277 rView.SetSnapEnabled(false);
279 rView.MovAction(aEndPoint);
280 rView.EndDragObj();
282 // restore snap
283 if (!bWasNoSnap)
284 const_cast<SdrDragStat&>(rDragStat).SetNoSnap(bWasNoSnap);
285 if (bWasSnapEnabled)
286 rView.SetSnapEnabled(bWasSnapEnabled);
289 // make moved handle visible
290 tools::Rectangle aVisRect(aEndPoint - Point(100, 100), Size(200, 200));
291 rView.MakeVisible(aVisRect, rWindow);
294 else
296 // scroll page
297 ScrollAdaptor* pScrollBar = ( nX != 0 ) ? rParent.GetHScroll() : rParent.GetVScroll();
298 if ( pScrollBar )
300 tools::Long nRangeMin = pScrollBar->GetRangeMin();
301 tools::Long nRangeMax = pScrollBar->GetRangeMax();
302 tools::Long nThumbPos = pScrollBar->GetThumbPos() + ( ( nX != 0 ) ? nX : nY ) * pScrollBar->GetLineSize();
303 if ( nThumbPos < nRangeMin )
304 nThumbPos = nRangeMin;
305 if ( nThumbPos > nRangeMax )
306 nThumbPos = nRangeMax;
307 pScrollBar->SetThumbPos( nThumbPos );
308 rParent.DoScroll();
312 bReturn = true;
314 break;
315 default:
318 break;
321 if ( bReturn )
322 rWindow.ReleaseMouse();
324 return bReturn;
327 DlgEdFuncInsert::DlgEdFuncInsert (DlgEditor& rParent_) :
328 DlgEdFunc(rParent_)
330 rParent.GetView().SetCreateMode();
333 DlgEdFuncInsert::~DlgEdFuncInsert()
335 rParent.GetView().SetEditMode();
338 void DlgEdFuncInsert::MouseButtonDown( const MouseEvent& rMEvt )
340 if( !rMEvt.IsLeft() )
341 return;
343 SdrView& rView = rParent.GetView();
344 vcl::Window& rWindow = rParent.GetWindow();
345 rView.SetActualWin(rWindow.GetOutDev());
347 Point aPos = rWindow.PixelToLogic( rMEvt.GetPosPixel() );
348 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
349 sal_uInt16 nDrgLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
351 rWindow.CaptureMouse();
353 if ( rMEvt.IsLeft() && rMEvt.GetClicks() == 1 )
355 SdrHdl* pHdl = rView.PickHandle(aPos);
357 // if selected object was hit, drag object
358 if ( pHdl!=nullptr || rView.IsMarkedHit(aPos, nHitLog) )
359 rView.BegDragObj(aPos, nullptr, pHdl, nDrgLog);
360 else if ( rView.GetMarkedObjectList().GetMarkCount() != 0 )
361 rView.UnmarkAll();
363 // if no action, create object
364 if ( !rView.IsAction() )
365 rView.BegCreateObj(aPos);
367 else if ( rMEvt.IsLeft() && rMEvt.GetClicks() == 2 )
369 // if object was hit, show property browser
370 if ( rView.IsMarkedHit(aPos, nHitLog) && rParent.GetMode() != DlgEditor::READONLY )
371 rParent.ShowProperties();
375 bool DlgEdFuncInsert::MouseButtonUp( const MouseEvent& rMEvt )
377 DlgEdFunc::MouseButtonUp( rMEvt );
379 SdrView& rView = rParent.GetView();
380 vcl::Window& rWindow = rParent.GetWindow();
381 rView.SetActualWin(rWindow.GetOutDev());
383 rWindow.ReleaseMouse();
385 // object creation active?
386 if ( rView.IsCreateObj() )
388 rView.EndCreateObj(SdrCreateCmd::ForceEnd);
390 if ( rView.GetMarkedObjectList().GetMarkCount() == 0 )
392 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
393 Point aPos( rWindow.PixelToLogic( rMEvt.GetPosPixel() ) );
394 rView.MarkObj(aPos, nHitLog);
397 return rView.GetMarkedObjectList().GetMarkCount() != 0;
399 else
401 if ( rView.IsDragObj() )
402 rView.EndDragObj( rMEvt.IsMod1() );
403 return true;
407 void DlgEdFuncInsert::MouseMove( const MouseEvent& rMEvt )
409 SdrView& rView = rParent.GetView();
410 vcl::Window& rWindow = rParent.GetWindow();
411 rView.SetActualWin(rWindow.GetOutDev());
413 Point aPos = rWindow.PixelToLogic(rMEvt.GetPosPixel());
414 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
416 if (rView.IsAction())
418 ForceScroll(aPos);
419 rView.MovAction(aPos);
422 rWindow.SetPointer( rView.GetPreferredPointer( aPos, rWindow.GetOutDev(), nHitLog ) );
425 DlgEdFuncSelect::DlgEdFuncSelect (DlgEditor& rParent_) :
426 DlgEdFunc(rParent_)
430 DlgEdFuncSelect::~DlgEdFuncSelect()
434 void DlgEdFuncSelect::MouseButtonDown( const MouseEvent& rMEvt )
436 // get view from parent
437 SdrView& rView = rParent.GetView();
438 vcl::Window& rWindow = rParent.GetWindow();
439 rView.SetActualWin(rWindow.GetOutDev());
441 sal_uInt16 nDrgLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
442 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
443 Point aMDPos = rWindow.PixelToLogic(rMEvt.GetPosPixel());
445 if ( rMEvt.IsLeft() && rMEvt.GetClicks() == 1 )
447 SdrHdl* pHdl = rView.PickHandle(aMDPos);
449 // hit selected object?
450 if ( pHdl!=nullptr || rView.IsMarkedHit(aMDPos, nHitLog) )
452 rView.BegDragObj(aMDPos, nullptr, pHdl, nDrgLog);
454 else
456 // if not multi selection, unmark all
457 if ( !rMEvt.IsShift() )
458 rView.UnmarkAll();
459 else
461 SdrPageView* pPV;
462 SdrObject* pObj = rView.PickObj(aMDPos, nHitLog, pPV);
463 if (pObj)
465 //if (dynamic_cast<DlgEdForm*>(pObj))
466 // rView.UnmarkAll();
467 //else
468 // rParent.UnmarkDialog();
472 if ( rView.MarkObj(aMDPos, nHitLog) )
474 // drag object
475 pHdl = rView.PickHandle(aMDPos);
476 rView.BegDragObj(aMDPos, nullptr, pHdl, nDrgLog);
478 else
480 // select object
481 rView.BegMarkObj(aMDPos);
485 else if ( rMEvt.IsLeft() && rMEvt.GetClicks() == 2 )
487 // if object was hit, show property browser
488 if ( rView.IsMarkedHit(aMDPos, nHitLog) && rParent.GetMode() != DlgEditor::READONLY )
489 rParent.ShowProperties();
493 bool DlgEdFuncSelect::MouseButtonUp( const MouseEvent& rMEvt )
495 DlgEdFunc::MouseButtonUp( rMEvt );
497 // get view from parent
498 SdrView& rView = rParent.GetView();
499 vcl::Window& rWindow = rParent.GetWindow();
500 rView.SetActualWin(rWindow.GetOutDev());
502 Point aPnt = rWindow.PixelToLogic(rMEvt.GetPosPixel());
503 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
505 if ( rMEvt.IsLeft() )
507 if (rView.IsDragObj())
509 // object was dragged
510 rView.EndDragObj( rMEvt.IsMod1() );
511 rView.ForceMarkedToAnotherPage();
513 else if (rView.IsAction())
515 rView.EndAction();
519 rWindow.SetPointer( rView.GetPreferredPointer( aPnt, rWindow.GetOutDev(), nHitLog ) );
520 rWindow.ReleaseMouse();
522 return true;
525 void DlgEdFuncSelect::MouseMove( const MouseEvent& rMEvt )
527 SdrView& rView = rParent.GetView();
528 vcl::Window& rWindow = rParent.GetWindow();
529 rView.SetActualWin(rWindow.GetOutDev());
531 Point aPnt = rWindow.PixelToLogic(rMEvt.GetPosPixel());
532 sal_uInt16 nHitLog = static_cast<sal_uInt16>(rWindow.PixelToLogic(Size(3, 0)).Width());
534 if ( rView.IsAction() )
536 Point aPix = rMEvt.GetPosPixel();
537 Point aPnt_ = rWindow.PixelToLogic(aPix);
539 ForceScroll(aPnt_);
540 rView.MovAction(aPnt_);
543 rWindow.SetPointer( rView.GetPreferredPointer( aPnt, rWindow.GetOutDev(), nHitLog ) );
546 } // namespace basctl
548 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */