merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / hatchwindow / ipwin.cxx
blobd98ddd54ea9dcbfb40e3229991c486814b20e958
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: ipwin.cxx,v $
10 * $Revision: 1.9 $
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_svtools.hxx"
33 #include <com/sun/star/accessibility/AccessibleRole.hpp>
35 #include <vcl/svapp.hxx>
37 #include <ipwin.hxx>
38 #include <hatchwindow.hxx>
40 /************************************************************************/
41 /*************************************************************************
42 |* SvResizeHelper::SvResizeHelper()
44 |* Beschreibung
45 *************************************************************************/
46 SvResizeHelper::SvResizeHelper()
47 : aBorder( 5, 5 )
48 , nGrab( -1 )
49 , bResizeable( TRUE )
53 /*************************************************************************
54 |* SvResizeHelper::FillHandleRects()
56 |* Beschreibung: Die acht Handles zum vergroessern
57 *************************************************************************/
58 void SvResizeHelper::FillHandleRectsPixel( Rectangle aRects[ 8 ] ) const
60 // nur wegen EMPTY_RECT
61 Point aBottomRight = aOuter.BottomRight();
63 // Links Oben
64 aRects[ 0 ] = Rectangle( aOuter.TopLeft(), aBorder );
65 // Oben Mitte
66 aRects[ 1 ] = Rectangle( Point( aOuter.Center().X() - aBorder.Width() / 2,
67 aOuter.Top() ),
68 aBorder );
69 // Oben Rechts
70 aRects[ 2 ] = Rectangle( Point( aBottomRight.X() - aBorder.Width() +1,
71 aOuter.Top() ),
72 aBorder );
73 // Mitte Rechts
74 aRects[ 3 ] = Rectangle( Point( aBottomRight.X() - aBorder.Width() +1,
75 aOuter.Center().Y() - aBorder.Height() / 2 ),
76 aBorder );
77 // Unten Rechts
78 aRects[ 4 ] = Rectangle( Point( aBottomRight.X() - aBorder.Width() +1,
79 aBottomRight.Y() - aBorder.Height() +1 ),
80 aBorder );
81 // Mitte Unten
82 aRects[ 5 ] = Rectangle( Point( aOuter.Center().X() - aBorder.Width() / 2,
83 aBottomRight.Y() - aBorder.Height() +1),
84 aBorder );
85 // Links Unten
86 aRects[ 6 ] = Rectangle( Point( aOuter.Left(),
87 aBottomRight.Y() - aBorder.Height() +1),
88 aBorder );
89 // Mitte Links
90 aRects[ 7 ] = Rectangle( Point( aOuter.Left(),
91 aOuter.Center().Y() - aBorder.Height() / 2 ),
92 aBorder );
95 /*************************************************************************
96 |* SvResizeHelper::FillMoveRectsPixel()
98 |* Beschreibung: Die vier Kanten werden berechnet
99 *************************************************************************/
100 void SvResizeHelper::FillMoveRectsPixel( Rectangle aRects[ 4 ] ) const
102 // Oben
103 aRects[ 0 ] = aOuter;
104 aRects[ 0 ].Bottom() = aRects[ 0 ].Top() + aBorder.Height() -1;
105 // Rechts
106 aRects[ 1 ] = aOuter;
107 aRects[ 1 ].Left() = aRects[ 1 ].Right() - aBorder.Width() -1;
108 //Unten
109 aRects[ 2 ] = aOuter;
110 aRects[ 2 ].Top() = aRects[ 2 ].Bottom() - aBorder.Height() -1;
111 //Links
112 aRects[ 3 ] = aOuter;
113 aRects[ 3 ].Right() = aRects[ 3 ].Left() + aBorder.Width() -1;
116 /*************************************************************************
117 |* SvResizeHelper::Draw()
119 |* Beschreibung
120 *************************************************************************/
121 void SvResizeHelper::Draw( OutputDevice * pDev )
123 pDev->Push();
124 pDev->SetMapMode( MapMode() );
125 Color aColBlack;
126 Color aFillColor( COL_LIGHTGRAY );
128 pDev->SetFillColor( aFillColor );
129 pDev->SetLineColor();
131 Rectangle aMoveRects[ 4 ];
132 FillMoveRectsPixel( aMoveRects );
133 USHORT i;
134 for( i = 0; i < 4; i++ )
135 pDev->DrawRect( aMoveRects[ i ] );
136 if( bResizeable )
138 // Handles malen
139 pDev->SetFillColor( aColBlack );
140 Rectangle aRects[ 8 ];
141 FillHandleRectsPixel( aRects );
142 for( i = 0; i < 8; i++ )
143 pDev->DrawRect( aRects[ i ] );
145 pDev->Pop();
148 /*************************************************************************
149 |* SvResizeHelper::InvalidateBorder()
151 |* Beschreibung
152 *************************************************************************/
153 void SvResizeHelper::InvalidateBorder( Window * pWin )
155 Rectangle aMoveRects[ 4 ];
156 FillMoveRectsPixel( aMoveRects );
157 for( USHORT i = 0; i < 4; i++ )
158 pWin->Invalidate( aMoveRects[ i ] );
161 /*************************************************************************
162 |* SvResizeHelper::SelectBegin()
164 |* Beschreibung
165 *************************************************************************/
166 BOOL SvResizeHelper::SelectBegin( Window * pWin, const Point & rPos )
168 if( -1 == nGrab )
170 nGrab = SelectMove( pWin, rPos );
171 if( -1 != nGrab )
173 aSelPos = rPos; // Start-Position merken
174 pWin->CaptureMouse();
175 return TRUE;
178 return FALSE;
181 /*************************************************************************
182 |* SvResizeHelper::SelectMove()
184 |* Beschreibung
185 *************************************************************************/
186 short SvResizeHelper::SelectMove( Window * pWin, const Point & rPos )
188 if( -1 == nGrab )
190 if( bResizeable )
192 Rectangle aRects[ 8 ];
193 FillHandleRectsPixel( aRects );
194 for( USHORT i = 0; i < 8; i++ )
195 if( aRects[ i ].IsInside( rPos ) )
196 return i;
198 // Move-Rect ueberlappen Handles
199 Rectangle aMoveRects[ 4 ];
200 FillMoveRectsPixel( aMoveRects );
201 for( USHORT i = 0; i < 4; i++ )
202 if( aMoveRects[ i ].IsInside( rPos ) )
203 return 8;
205 else
207 Rectangle aRect( GetTrackRectPixel( rPos ) );
208 aRect.SetSize( pWin->PixelToLogic( aRect.GetSize() ) );
209 aRect.SetPos( pWin->PixelToLogic( aRect.TopLeft() ) );
210 pWin->ShowTracking( aRect );
212 return nGrab;
215 Point SvResizeHelper::GetTrackPosPixel( const Rectangle & rRect ) const
217 // wie das Rechteck zurueckkommt ist egal, es zaehlt welches Handle
218 // initial angefasst wurde
219 Point aPos;
220 Rectangle aRect( rRect );
221 aRect.Justify();
222 // nur wegen EMPTY_RECT
223 Point aBR = aOuter.BottomRight();
224 Point aTR = aOuter.TopRight();
225 Point aBL = aOuter.BottomLeft();
226 switch( nGrab )
228 case 0:
229 aPos = aRect.TopLeft() - aOuter.TopLeft();
230 break;
231 case 1:
232 aPos.Y() = aRect.Top() - aOuter.Top();
233 break;
234 case 2:
235 aPos = aRect.TopRight() - aTR;
236 break;
237 case 3:
238 aPos.X() = aRect.Right() - aTR.X();
239 break;
240 case 4:
241 aPos = aRect.BottomRight() - aBR;
242 break;
243 case 5:
244 aPos.Y() = aRect.Bottom() - aBR.Y();
245 break;
246 case 6:
247 aPos = aRect.BottomLeft() - aBL;
248 break;
249 case 7:
250 aPos.X() = aRect.Left() - aOuter.Left();
251 break;
252 case 8:
253 aPos = aRect.TopLeft() - aOuter.TopLeft();
254 break;
256 return aPos += aSelPos;
259 /*************************************************************************
260 |* SvResizeHelper::GetTrackRectPixel()
262 |* Beschreibung
263 *************************************************************************/
264 Rectangle SvResizeHelper::GetTrackRectPixel( const Point & rTrackPos ) const
266 Rectangle aTrackRect;
267 if( -1 != nGrab )
269 Point aDiff = rTrackPos - aSelPos;
270 aTrackRect = aOuter;
271 Point aBR = aOuter.BottomRight();
272 switch( nGrab )
274 case 0:
275 aTrackRect.Top() += aDiff.Y();
276 aTrackRect.Left() += aDiff.X();
277 break;
278 case 1:
279 aTrackRect.Top() += aDiff.Y();
280 break;
281 case 2:
282 aTrackRect.Top() += aDiff.Y();
283 aTrackRect.Right() = aBR.X() + aDiff.X();
284 break;
285 case 3:
286 aTrackRect.Right() = aBR.X() + aDiff.X();
287 break;
288 case 4:
289 aTrackRect.Bottom() = aBR.Y() + aDiff.Y();
290 aTrackRect.Right() = aBR.X() + aDiff.X();
291 break;
292 case 5:
293 aTrackRect.Bottom() = aBR.Y() + aDiff.Y();
294 break;
295 case 6:
296 aTrackRect.Bottom() = aBR.Y() + aDiff.Y();
297 aTrackRect.Left() += aDiff.X();
298 break;
299 case 7:
300 aTrackRect.Left() += aDiff.X();
301 break;
302 case 8:
303 if( Application::GetSettings().GetLayoutRTL() )
304 aDiff.X() = -aDiff.X(); // workaround for move in RTL mode
305 aTrackRect.SetPos( aTrackRect.TopLeft() + aDiff );
306 break;
308 case 0:
309 aTrackRect = Rectangle( rTrackPos, aOuter.BottomRight() );
310 break;
311 case 1:
312 aTrackRect = Rectangle( Point( aOuter.Left(), rTrackPos.Y() ),
313 aOuter.BottomRight() );
314 break;
315 case 2:
316 aTrackRect = Rectangle( rTrackPos, aOuter.BottomLeft() );
317 break;
318 case 3:
319 aTrackRect = Rectangle( Point( rTrackPos.X(), aOuter.Top() ),
320 aOuter.BottomLeft() );
321 break;
322 case 4:
323 aTrackRect = Rectangle( rTrackPos, aOuter.TopLeft() );
324 break;
325 case 5:
326 aTrackRect = Rectangle( aOuter.TopLeft(),
327 Point( aOuter.Right(), rTrackPos.Y() ) );
328 break;
329 case 6:
330 aTrackRect = Rectangle( aOuter.TopRight(), rTrackPos );
331 break;
332 case 7:
333 aTrackRect = Rectangle( Point( rTrackPos.X(), aOuter.Top() ),
334 aOuter.BottomRight() );
335 break;
336 case 8:
337 aTrackRect = Rectangle( aOuter.TopLeft() + rTrackPos - aSelPos,
338 aOuter.GetSize() );
339 break;
343 return aTrackRect;
346 void SvResizeHelper::ValidateRect( Rectangle & rValidate ) const
348 switch( nGrab )
350 case 0:
351 if( rValidate.Top() > rValidate.Bottom() )
353 rValidate.Top() = rValidate.Bottom();
354 rValidate.Bottom() = RECT_EMPTY;
356 if( rValidate.Left() > rValidate.Right() )
358 rValidate.Left() = rValidate.Right();
359 rValidate.Right() = RECT_EMPTY;
361 break;
362 case 1:
363 if( rValidate.Top() > rValidate.Bottom() )
365 rValidate.Top() = rValidate.Bottom();
366 rValidate.Bottom() = RECT_EMPTY;
368 break;
369 case 2:
370 if( rValidate.Top() > rValidate.Bottom() )
372 rValidate.Top() = rValidate.Bottom();
373 rValidate.Bottom() = RECT_EMPTY;
375 if( rValidate.Left() > rValidate.Right() )
376 rValidate.Right() = RECT_EMPTY;
377 break;
378 case 3:
379 if( rValidate.Left() > rValidate.Right() )
380 rValidate.Right() = RECT_EMPTY;
381 break;
382 case 4:
383 if( rValidate.Top() > rValidate.Bottom() )
384 rValidate.Bottom() = RECT_EMPTY;
385 if( rValidate.Left() > rValidate.Right() )
386 rValidate.Right() = RECT_EMPTY;
387 break;
388 case 5:
389 if( rValidate.Top() > rValidate.Bottom() )
390 rValidate.Bottom() = RECT_EMPTY;
391 break;
392 case 6:
393 if( rValidate.Top() > rValidate.Bottom() )
394 rValidate.Bottom() = RECT_EMPTY;
395 if( rValidate.Left() > rValidate.Right() )
397 rValidate.Left() = rValidate.Right();
398 rValidate.Right() = RECT_EMPTY;
400 break;
401 case 7:
402 if( rValidate.Left() > rValidate.Right() )
404 rValidate.Left() = rValidate.Right();
405 rValidate.Right() = RECT_EMPTY;
407 break;
409 if( rValidate.Right() == RECT_EMPTY )
410 rValidate.Right() = rValidate.Left();
411 if( rValidate.Bottom() == RECT_EMPTY )
412 rValidate.Bottom() = rValidate.Top();
414 // Mindestgr"osse 5 x 5
415 if( rValidate.Left() + 5 > rValidate.Right() )
416 rValidate.Right() = rValidate.Left() +5;
417 if( rValidate.Top() + 5 > rValidate.Bottom() )
418 rValidate.Bottom() = rValidate.Top() +5;
421 /*************************************************************************
422 |* SvResizeHelper::SelectRelease()
424 |* Beschreibung
425 *************************************************************************/
426 BOOL SvResizeHelper::SelectRelease( Window * pWin, const Point & rPos,
427 Rectangle & rOutPosSize )
429 if( -1 != nGrab )
431 rOutPosSize = GetTrackRectPixel( rPos );
432 rOutPosSize.Justify();
433 nGrab = -1;
434 pWin->ReleaseMouse();
435 pWin->HideTracking();
436 return TRUE;
438 return FALSE;
441 /*************************************************************************
442 |* SvResizeHelper::Release()
444 |* Beschreibung
445 *************************************************************************/
446 void SvResizeHelper::Release( Window * pWin )
448 if( nGrab != -1 )
450 pWin->ReleaseMouse();
451 pWin->HideTracking();
452 nGrab = -1;
456 /*************************************************************************
457 |* SvResizeWindow::SvResizeWindow()
459 |* Beschreibung
460 *************************************************************************/
461 SvResizeWindow::SvResizeWindow
463 Window * pParent,
464 VCLXHatchWindow* pWrapper
466 : Window( pParent, WB_CLIPCHILDREN )
467 , m_nMoveGrab( -1 )
468 , m_bActive( sal_False )
469 , m_pWrapper( pWrapper )
471 OSL_ENSURE( pParent != NULL && pWrapper != NULL, "Wrong initialization of hatch window!\n" );
472 SetBackground();
473 SetAccessibleRole( ::com::sun::star::accessibility::AccessibleRole::EMBEDDED_OBJECT );
474 m_aResizer.SetOuterRectPixel( Rectangle( Point(), GetOutputSizePixel() ) );
477 /*************************************************************************
478 |* SvResizeWindow::SetHatchBorderPixel()
480 |* Beschreibung
481 *************************************************************************/
482 void SvResizeWindow::SetHatchBorderPixel( const Size & rSize )
484 m_aResizer.SetBorderPixel( rSize );
487 /*************************************************************************
488 |* SvResizeWindow::SelectMouse()
490 |* Beschreibung
491 *************************************************************************/
492 void SvResizeWindow::SelectMouse( const Point & rPos )
494 short nGrab = m_aResizer.SelectMove( this, rPos );
495 if( nGrab >= 4 )
496 nGrab -= 4;
497 if( m_nMoveGrab != nGrab )
498 { // Pointer hat sich geaendert
499 if( -1 == nGrab )
500 SetPointer( m_aOldPointer );
501 else
503 PointerStyle aStyle = POINTER_MOVE;
504 if( nGrab == 3 )
505 aStyle = POINTER_ESIZE;
506 else if( nGrab == 2 )
507 aStyle = POINTER_NESIZE;
508 else if( nGrab == 1 )
509 aStyle = POINTER_SSIZE;
510 else if( nGrab == 0 )
511 aStyle = POINTER_SESIZE;
512 if( m_nMoveGrab == -1 ) // das erste mal
514 m_aOldPointer = GetPointer();
515 SetPointer( Pointer( aStyle ) );
517 else
518 SetPointer( Pointer( aStyle ) );
520 m_nMoveGrab = nGrab;
524 /*************************************************************************
525 |* SvResizeWindow::MouseButtonDown()
527 |* Beschreibung
528 *************************************************************************/
529 void SvResizeWindow::MouseButtonDown( const MouseEvent & rEvt )
531 if( m_aResizer.SelectBegin( this, rEvt.GetPosPixel() ) )
532 SelectMouse( rEvt.GetPosPixel() );
535 /*************************************************************************
536 |* SvResizeWindow::MouseMove()
538 |* Beschreibung
539 *************************************************************************/
540 void SvResizeWindow::MouseMove( const MouseEvent & rEvt )
542 if( m_aResizer.GetGrab() == -1 )
543 SelectMouse( rEvt.GetPosPixel() );
544 else
546 Rectangle aRect( m_aResizer.GetTrackRectPixel( rEvt.GetPosPixel() ) );
547 Point aDiff = GetPosPixel();
548 aRect.SetPos( aRect.TopLeft() + aDiff );
549 m_aResizer.ValidateRect( aRect );
551 m_pWrapper->QueryObjAreaPixel( aRect );
552 aRect.SetPos( aRect.TopLeft() - aDiff );
553 Point aPos = m_aResizer.GetTrackPosPixel( aRect );
555 SelectMouse( aPos );
559 /*************************************************************************
560 |* SvResizeWindow::MouseButtonUp()
562 |* Beschreibung
563 *************************************************************************/
564 void SvResizeWindow::MouseButtonUp( const MouseEvent & rEvt )
566 if( m_aResizer.GetGrab() != -1 )
568 Rectangle aRect( m_aResizer.GetTrackRectPixel( rEvt.GetPosPixel() ) );
569 Point aDiff = GetPosPixel();
570 aRect.SetPos( aRect.TopLeft() + aDiff );
571 // aRect -= GetAllBorderPixel();
572 m_aResizer.ValidateRect( aRect );
574 m_pWrapper->QueryObjAreaPixel( aRect );
576 Rectangle aOutRect;
577 if( m_aResizer.SelectRelease( this, rEvt.GetPosPixel(), aOutRect ) )
579 m_nMoveGrab = -1;
580 SetPointer( m_aOldPointer );
581 m_pWrapper->RequestObjAreaPixel( aRect );
586 /*************************************************************************
587 |* SvResizeWindow::KeyEvent()
589 |* Beschreibung
590 *************************************************************************/
591 void SvResizeWindow::KeyInput( const KeyEvent & rEvt )
593 if( rEvt.GetKeyCode().GetCode() == KEY_ESCAPE )
595 m_aResizer.Release( this );
596 m_pWrapper->InplaceDeactivate();
600 /*************************************************************************
601 |* SvResizeWindow::Resize()
603 |* Beschreibung
604 *************************************************************************/
605 void SvResizeWindow::Resize()
607 m_aResizer.InvalidateBorder( this ); // alten Bereich
608 m_aResizer.SetOuterRectPixel( Rectangle( Point(), GetOutputSizePixel() ) );
609 m_aResizer.InvalidateBorder( this ); // neuen Bereich
612 /*************************************************************************
613 |* SvResizeWindow::Paint()
615 |* Beschreibung
616 *************************************************************************/
617 void SvResizeWindow::Paint( const Rectangle & /*rRect*/ )
619 m_aResizer.Draw( this );
622 long SvResizeWindow::PreNotify( NotifyEvent& rEvt )
624 if ( rEvt.GetType() == EVENT_GETFOCUS && !m_bActive )
626 m_bActive = sal_True;
627 m_pWrapper->Activated();
630 return Window::PreNotify(rEvt);
633 long SvResizeWindow::Notify( NotifyEvent& rEvt )
635 if ( rEvt.GetType() == EVENT_LOSEFOCUS && m_bActive )
637 BOOL bHasFocus = HasChildPathFocus(TRUE);
638 if ( !bHasFocus )
640 m_bActive = sal_False;
641 m_pWrapper->Deactivated();
645 return Window::Notify(rEvt);