1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/accessibility/AccessibleRole.hpp>
22 #include <vcl/svapp.hxx>
25 #include <hatchwindow.hxx>
27 /************************************************************************/
28 /*************************************************************************
29 |* SvResizeHelper::SvResizeHelper()
32 *************************************************************************/
33 SvResizeHelper::SvResizeHelper()
36 , bResizeable( sal_True
)
40 /*************************************************************************
41 |* SvResizeHelper::FillHandleRects()
43 |* Beschreibung: Die acht Handles zum vergroessern
44 *************************************************************************/
45 void SvResizeHelper::FillHandleRectsPixel( Rectangle aRects
[ 8 ] ) const
47 // nur wegen EMPTY_RECT
48 Point aBottomRight
= aOuter
.BottomRight();
51 aRects
[ 0 ] = Rectangle( aOuter
.TopLeft(), aBorder
);
53 aRects
[ 1 ] = Rectangle( Point( aOuter
.Center().X() - aBorder
.Width() / 2,
57 aRects
[ 2 ] = Rectangle( Point( aBottomRight
.X() - aBorder
.Width() +1,
61 aRects
[ 3 ] = Rectangle( Point( aBottomRight
.X() - aBorder
.Width() +1,
62 aOuter
.Center().Y() - aBorder
.Height() / 2 ),
65 aRects
[ 4 ] = Rectangle( Point( aBottomRight
.X() - aBorder
.Width() +1,
66 aBottomRight
.Y() - aBorder
.Height() +1 ),
69 aRects
[ 5 ] = Rectangle( Point( aOuter
.Center().X() - aBorder
.Width() / 2,
70 aBottomRight
.Y() - aBorder
.Height() +1),
73 aRects
[ 6 ] = Rectangle( Point( aOuter
.Left(),
74 aBottomRight
.Y() - aBorder
.Height() +1),
77 aRects
[ 7 ] = Rectangle( Point( aOuter
.Left(),
78 aOuter
.Center().Y() - aBorder
.Height() / 2 ),
82 /*************************************************************************
83 |* SvResizeHelper::FillMoveRectsPixel()
85 |* Beschreibung: Die vier Kanten werden berechnet
86 *************************************************************************/
87 void SvResizeHelper::FillMoveRectsPixel( Rectangle aRects
[ 4 ] ) const
91 aRects
[ 0 ].Bottom() = aRects
[ 0 ].Top() + aBorder
.Height() -1;
94 aRects
[ 1 ].Left() = aRects
[ 1 ].Right() - aBorder
.Width() -1;
97 aRects
[ 2 ].Top() = aRects
[ 2 ].Bottom() - aBorder
.Height() -1;
100 aRects
[ 3 ].Right() = aRects
[ 3 ].Left() + aBorder
.Width() -1;
103 /*************************************************************************
104 |* SvResizeHelper::Draw()
107 *************************************************************************/
108 void SvResizeHelper::Draw( OutputDevice
* pDev
)
111 pDev
->SetMapMode( MapMode() );
113 Color
aFillColor( COL_LIGHTGRAY
);
115 pDev
->SetFillColor( aFillColor
);
116 pDev
->SetLineColor();
118 Rectangle aMoveRects
[ 4 ];
119 FillMoveRectsPixel( aMoveRects
);
121 for( i
= 0; i
< 4; i
++ )
122 pDev
->DrawRect( aMoveRects
[ i
] );
126 pDev
->SetFillColor( aColBlack
);
127 Rectangle aRects
[ 8 ];
128 FillHandleRectsPixel( aRects
);
129 for( i
= 0; i
< 8; i
++ )
130 pDev
->DrawRect( aRects
[ i
] );
135 /*************************************************************************
136 |* SvResizeHelper::InvalidateBorder()
139 *************************************************************************/
140 void SvResizeHelper::InvalidateBorder( Window
* pWin
)
142 Rectangle aMoveRects
[ 4 ];
143 FillMoveRectsPixel( aMoveRects
);
144 for( sal_uInt16 i
= 0; i
< 4; i
++ )
145 pWin
->Invalidate( aMoveRects
[ i
] );
148 /*************************************************************************
149 |* SvResizeHelper::SelectBegin()
152 *************************************************************************/
153 sal_Bool
SvResizeHelper::SelectBegin( Window
* pWin
, const Point
& rPos
)
157 nGrab
= SelectMove( pWin
, rPos
);
160 aSelPos
= rPos
; // Start-Position merken
161 pWin
->CaptureMouse();
168 /*************************************************************************
169 |* SvResizeHelper::SelectMove()
172 *************************************************************************/
173 short SvResizeHelper::SelectMove( Window
* pWin
, const Point
& rPos
)
179 Rectangle aRects
[ 8 ];
180 FillHandleRectsPixel( aRects
);
181 for( sal_uInt16 i
= 0; i
< 8; i
++ )
182 if( aRects
[ i
].IsInside( rPos
) )
185 // Move-Rect ueberlappen Handles
186 Rectangle aMoveRects
[ 4 ];
187 FillMoveRectsPixel( aMoveRects
);
188 for( sal_uInt16 i
= 0; i
< 4; i
++ )
189 if( aMoveRects
[ i
].IsInside( rPos
) )
194 Rectangle
aRect( GetTrackRectPixel( rPos
) );
195 aRect
.SetSize( pWin
->PixelToLogic( aRect
.GetSize() ) );
196 aRect
.SetPos( pWin
->PixelToLogic( aRect
.TopLeft() ) );
197 pWin
->ShowTracking( aRect
);
202 Point
SvResizeHelper::GetTrackPosPixel( const Rectangle
& rRect
) const
204 // wie das Rechteck zurueckkommt ist egal, es zaehlt welches Handle
205 // initial angefasst wurde
207 Rectangle
aRect( rRect
);
209 // nur wegen EMPTY_RECT
210 Point aBR
= aOuter
.BottomRight();
211 Point aTR
= aOuter
.TopRight();
212 Point aBL
= aOuter
.BottomLeft();
216 aPos
= aRect
.TopLeft() - aOuter
.TopLeft();
219 aPos
.Y() = aRect
.Top() - aOuter
.Top();
222 aPos
= aRect
.TopRight() - aTR
;
225 aPos
.X() = aRect
.Right() - aTR
.X();
228 aPos
= aRect
.BottomRight() - aBR
;
231 aPos
.Y() = aRect
.Bottom() - aBR
.Y();
234 aPos
= aRect
.BottomLeft() - aBL
;
237 aPos
.X() = aRect
.Left() - aOuter
.Left();
240 aPos
= aRect
.TopLeft() - aOuter
.TopLeft();
243 return aPos
+= aSelPos
;
246 /*************************************************************************
247 |* SvResizeHelper::GetTrackRectPixel()
250 *************************************************************************/
251 Rectangle
SvResizeHelper::GetTrackRectPixel( const Point
& rTrackPos
) const
253 Rectangle aTrackRect
;
256 Point aDiff
= rTrackPos
- aSelPos
;
258 Point aBR
= aOuter
.BottomRight();
262 aTrackRect
.Top() += aDiff
.Y();
263 aTrackRect
.Left() += aDiff
.X();
266 aTrackRect
.Top() += aDiff
.Y();
269 aTrackRect
.Top() += aDiff
.Y();
270 aTrackRect
.Right() = aBR
.X() + aDiff
.X();
273 aTrackRect
.Right() = aBR
.X() + aDiff
.X();
276 aTrackRect
.Bottom() = aBR
.Y() + aDiff
.Y();
277 aTrackRect
.Right() = aBR
.X() + aDiff
.X();
280 aTrackRect
.Bottom() = aBR
.Y() + aDiff
.Y();
283 aTrackRect
.Bottom() = aBR
.Y() + aDiff
.Y();
284 aTrackRect
.Left() += aDiff
.X();
287 aTrackRect
.Left() += aDiff
.X();
290 if( Application::GetSettings().GetLayoutRTL() )
291 aDiff
.X() = -aDiff
.X(); // workaround for move in RTL mode
292 aTrackRect
.SetPos( aTrackRect
.TopLeft() + aDiff
);
299 void SvResizeHelper::ValidateRect( Rectangle
& rValidate
) const
304 if( rValidate
.Top() > rValidate
.Bottom() )
306 rValidate
.Top() = rValidate
.Bottom();
307 rValidate
.Bottom() = RECT_EMPTY
;
309 if( rValidate
.Left() > rValidate
.Right() )
311 rValidate
.Left() = rValidate
.Right();
312 rValidate
.Right() = RECT_EMPTY
;
316 if( rValidate
.Top() > rValidate
.Bottom() )
318 rValidate
.Top() = rValidate
.Bottom();
319 rValidate
.Bottom() = RECT_EMPTY
;
323 if( rValidate
.Top() > rValidate
.Bottom() )
325 rValidate
.Top() = rValidate
.Bottom();
326 rValidate
.Bottom() = RECT_EMPTY
;
328 if( rValidate
.Left() > rValidate
.Right() )
329 rValidate
.Right() = RECT_EMPTY
;
332 if( rValidate
.Left() > rValidate
.Right() )
333 rValidate
.Right() = RECT_EMPTY
;
336 if( rValidate
.Top() > rValidate
.Bottom() )
337 rValidate
.Bottom() = RECT_EMPTY
;
338 if( rValidate
.Left() > rValidate
.Right() )
339 rValidate
.Right() = RECT_EMPTY
;
342 if( rValidate
.Top() > rValidate
.Bottom() )
343 rValidate
.Bottom() = RECT_EMPTY
;
346 if( rValidate
.Top() > rValidate
.Bottom() )
347 rValidate
.Bottom() = RECT_EMPTY
;
348 if( rValidate
.Left() > rValidate
.Right() )
350 rValidate
.Left() = rValidate
.Right();
351 rValidate
.Right() = RECT_EMPTY
;
355 if( rValidate
.Left() > rValidate
.Right() )
357 rValidate
.Left() = rValidate
.Right();
358 rValidate
.Right() = RECT_EMPTY
;
362 if( rValidate
.Right() == RECT_EMPTY
)
363 rValidate
.Right() = rValidate
.Left();
364 if( rValidate
.Bottom() == RECT_EMPTY
)
365 rValidate
.Bottom() = rValidate
.Top();
367 // Mindestgr"osse 5 x 5
368 if( rValidate
.Left() + 5 > rValidate
.Right() )
369 rValidate
.Right() = rValidate
.Left() +5;
370 if( rValidate
.Top() + 5 > rValidate
.Bottom() )
371 rValidate
.Bottom() = rValidate
.Top() +5;
374 /*************************************************************************
375 |* SvResizeHelper::SelectRelease()
378 *************************************************************************/
379 sal_Bool
SvResizeHelper::SelectRelease( Window
* pWin
, const Point
& rPos
,
380 Rectangle
& rOutPosSize
)
384 rOutPosSize
= GetTrackRectPixel( rPos
);
385 rOutPosSize
.Justify();
387 pWin
->ReleaseMouse();
388 pWin
->HideTracking();
394 /*************************************************************************
395 |* SvResizeHelper::Release()
398 *************************************************************************/
399 void SvResizeHelper::Release( Window
* pWin
)
403 pWin
->ReleaseMouse();
404 pWin
->HideTracking();
409 /*************************************************************************
410 |* SvResizeWindow::SvResizeWindow()
413 *************************************************************************/
414 SvResizeWindow::SvResizeWindow
417 VCLXHatchWindow
* pWrapper
419 : Window( pParent
, WB_CLIPCHILDREN
)
421 , m_bActive( sal_False
)
422 , m_pWrapper( pWrapper
)
424 OSL_ENSURE( pParent
!= NULL
&& pWrapper
!= NULL
, "Wrong initialization of hatch window!\n" );
426 SetAccessibleRole( ::com::sun::star::accessibility::AccessibleRole::EMBEDDED_OBJECT
);
427 m_aResizer
.SetOuterRectPixel( Rectangle( Point(), GetOutputSizePixel() ) );
430 /*************************************************************************
431 |* SvResizeWindow::SetHatchBorderPixel()
434 *************************************************************************/
435 void SvResizeWindow::SetHatchBorderPixel( const Size
& rSize
)
437 m_aResizer
.SetBorderPixel( rSize
);
440 /*************************************************************************
441 |* SvResizeWindow::SelectMouse()
444 *************************************************************************/
445 void SvResizeWindow::SelectMouse( const Point
& rPos
)
447 short nGrab
= m_aResizer
.SelectMove( this, rPos
);
450 if( m_nMoveGrab
!= nGrab
)
451 { // Pointer hat sich geaendert
453 SetPointer( m_aOldPointer
);
456 PointerStyle aStyle
= POINTER_MOVE
;
458 aStyle
= POINTER_ESIZE
;
459 else if( nGrab
== 2 )
460 aStyle
= POINTER_NESIZE
;
461 else if( nGrab
== 1 )
462 aStyle
= POINTER_SSIZE
;
463 else if( nGrab
== 0 )
464 aStyle
= POINTER_SESIZE
;
465 if( m_nMoveGrab
== -1 ) // das erste mal
467 m_aOldPointer
= GetPointer();
468 SetPointer( Pointer( aStyle
) );
471 SetPointer( Pointer( aStyle
) );
477 /*************************************************************************
478 |* SvResizeWindow::MouseButtonDown()
481 *************************************************************************/
482 void SvResizeWindow::MouseButtonDown( const MouseEvent
& rEvt
)
484 if( m_aResizer
.SelectBegin( this, rEvt
.GetPosPixel() ) )
485 SelectMouse( rEvt
.GetPosPixel() );
488 /*************************************************************************
489 |* SvResizeWindow::MouseMove()
492 *************************************************************************/
493 void SvResizeWindow::MouseMove( const MouseEvent
& rEvt
)
495 if( m_aResizer
.GetGrab() == -1 )
496 SelectMouse( rEvt
.GetPosPixel() );
499 Rectangle
aRect( m_aResizer
.GetTrackRectPixel( rEvt
.GetPosPixel() ) );
500 Point aDiff
= GetPosPixel();
501 aRect
.SetPos( aRect
.TopLeft() + aDiff
);
502 m_aResizer
.ValidateRect( aRect
);
504 m_pWrapper
->QueryObjAreaPixel( aRect
);
505 aRect
.SetPos( aRect
.TopLeft() - aDiff
);
506 Point aPos
= m_aResizer
.GetTrackPosPixel( aRect
);
512 /*************************************************************************
513 |* SvResizeWindow::MouseButtonUp()
516 *************************************************************************/
517 void SvResizeWindow::MouseButtonUp( const MouseEvent
& rEvt
)
519 if( m_aResizer
.GetGrab() != -1 )
521 Rectangle
aRect( m_aResizer
.GetTrackRectPixel( rEvt
.GetPosPixel() ) );
522 Point aDiff
= GetPosPixel();
523 aRect
.SetPos( aRect
.TopLeft() + aDiff
);
524 // aRect -= GetAllBorderPixel();
525 m_aResizer
.ValidateRect( aRect
);
527 m_pWrapper
->QueryObjAreaPixel( aRect
);
530 if( m_aResizer
.SelectRelease( this, rEvt
.GetPosPixel(), aOutRect
) )
533 SetPointer( m_aOldPointer
);
534 m_pWrapper
->RequestObjAreaPixel( aRect
);
539 /*************************************************************************
540 |* SvResizeWindow::KeyEvent()
543 *************************************************************************/
544 void SvResizeWindow::KeyInput( const KeyEvent
& rEvt
)
546 if( rEvt
.GetKeyCode().GetCode() == KEY_ESCAPE
)
548 m_aResizer
.Release( this );
549 m_pWrapper
->InplaceDeactivate();
553 /*************************************************************************
554 |* SvResizeWindow::Resize()
557 *************************************************************************/
558 void SvResizeWindow::Resize()
560 m_aResizer
.InvalidateBorder( this ); // alten Bereich
561 m_aResizer
.SetOuterRectPixel( Rectangle( Point(), GetOutputSizePixel() ) );
562 m_aResizer
.InvalidateBorder( this ); // neuen Bereich
565 /*************************************************************************
566 |* SvResizeWindow::Paint()
569 *************************************************************************/
570 void SvResizeWindow::Paint( const Rectangle
& /*rRect*/ )
572 m_aResizer
.Draw( this );
575 long SvResizeWindow::PreNotify( NotifyEvent
& rEvt
)
577 if ( rEvt
.GetType() == EVENT_GETFOCUS
&& !m_bActive
)
579 m_bActive
= sal_True
;
580 m_pWrapper
->Activated();
583 return Window::PreNotify(rEvt
);
586 long SvResizeWindow::Notify( NotifyEvent
& rEvt
)
588 if ( rEvt
.GetType() == EVENT_LOSEFOCUS
&& m_bActive
)
590 sal_Bool bHasFocus
= HasChildPathFocus(sal_True
);
593 m_bActive
= sal_False
;
594 m_pWrapper
->Deactivated();
598 return Window::Notify(rEvt
);
601 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */