Update ooo320-m1
[ooovba.git] / vcl / source / window / cursor.cxx
blob30cf7541da89b4644774bb0569ab647820a5d28f
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: cursor.cxx,v $
10 * $Revision: 1.13 $
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_vcl.hxx"
33 #include <vcl/svapp.hxx>
34 #include <vcl/timer.hxx>
35 #include <vcl/settings.hxx>
36 #include <vcl/window.hxx>
37 #include <vcl/window.h>
38 #include <tools/poly.hxx>
39 #include <vcl/cursor.hxx>
42 // =======================================================================
44 struct ImplCursorData
46 AutoTimer maTimer; // Timer
47 Point maPixPos; // Pixel-Position
48 Point maPixRotOff; // Pixel-Offset-Position
49 Size maPixSize; // Pixel-Size
50 long mnPixSlant; // Pixel-Slant
51 short mnOrientation; // Pixel-Orientation
52 unsigned char mnDirection; // indicates writing direction
53 USHORT mnStyle; // Cursor-Style
54 BOOL mbCurVisible; // Ist Cursor aktuell sichtbar
55 Window* mpWindow; // Zugeordnetes Windows
58 // =======================================================================
60 static void ImplCursorInvert( ImplCursorData* pData )
62 Window* pWindow = pData->mpWindow;
63 BOOL bMapMode = pWindow->IsMapModeEnabled();
64 pWindow->EnableMapMode( FALSE );
65 USHORT nInvertStyle;
66 if ( pData->mnStyle & CURSOR_SHADOW )
67 nInvertStyle = INVERT_50;
68 else
69 nInvertStyle = 0;
71 Rectangle aRect( pData->maPixPos, pData->maPixSize );
72 if ( pData->mnDirection || pData->mnOrientation || pData->mnPixSlant )
74 Polygon aPoly( aRect );
75 if( aPoly.GetSize() == 5 )
77 aPoly[1].X() += 1; // include the right border
78 aPoly[2].X() += 1;
79 if ( pData->mnPixSlant )
81 Point aPoint = aPoly.GetPoint( 0 );
82 aPoint.X() += pData->mnPixSlant;
83 aPoly.SetPoint( aPoint, 0 );
84 aPoly.SetPoint( aPoint, 4 );
85 aPoint = aPoly.GetPoint( 1 );
86 aPoint.X() += pData->mnPixSlant;
87 aPoly.SetPoint( aPoint, 1 );
90 // apply direction flag after slant to use the correct shape
91 if ( pData->mnDirection )
93 Point pAry[7];
94 int delta = 3*aRect.getWidth()+1;
95 if( pData->mnDirection == CURSOR_DIRECTION_LTR )
97 // left-to-right
98 pAry[0] = aPoly.GetPoint( 0 );
99 pAry[1] = aPoly.GetPoint( 1 );
100 pAry[2] = pAry[1];
101 pAry[2].X() += delta;
102 pAry[3] = pAry[1];
103 pAry[3].Y() += delta;
104 pAry[4] = aPoly.GetPoint( 2 );
105 pAry[5] = aPoly.GetPoint( 3 );
106 pAry[6] = aPoly.GetPoint( 4 );
108 else if( pData->mnDirection == CURSOR_DIRECTION_RTL )
110 // right-to-left
111 pAry[0] = aPoly.GetPoint( 0 );
112 pAry[1] = aPoly.GetPoint( 1 );
113 pAry[2] = aPoly.GetPoint( 2 );
114 pAry[3] = aPoly.GetPoint( 3 );
115 pAry[4] = pAry[0];
116 pAry[4].Y() += delta;
117 pAry[5] = pAry[0];
118 pAry[5].X() -= delta;
119 pAry[6] = aPoly.GetPoint( 4 );
121 aPoly = Polygon( 7, pAry);
124 if ( pData->mnOrientation )
125 aPoly.Rotate( pData->maPixRotOff, pData->mnOrientation );
126 pWindow->Invert( aPoly, nInvertStyle );
129 else
130 pWindow->Invert( aRect, nInvertStyle );
131 pWindow->EnableMapMode( bMapMode );
134 // -----------------------------------------------------------------------
136 void Cursor::ImplDraw()
138 if ( mpData && mpData->mpWindow && !mpData->mbCurVisible )
140 Window* pWindow = mpData->mpWindow;
141 mpData->maPixPos = pWindow->LogicToPixel( maPos );
142 mpData->maPixSize = pWindow->LogicToPixel( maSize );
143 mpData->mnPixSlant = pWindow->LogicToPixel( Size( mnSlant, 0 ) ).Width();
144 mpData->mnOrientation = mnOrientation;
145 mpData->mnDirection = mnDirection;
146 long nOffsetY = pWindow->LogicToPixel( Size( 0, mnOffsetY ) ).Height();
148 // Position um den Offset korrigieren
149 mpData->maPixPos.Y() -= nOffsetY;
150 mpData->maPixRotOff = mpData->maPixPos;
151 mpData->maPixRotOff.Y() += nOffsetY;
153 // Wenn groesse 0 ist, nehmen wir die breite, die in den
154 // Settings eingestellt ist
155 if ( !mpData->maPixSize.Width() )
156 mpData->maPixSize.Width() = pWindow->GetSettings().GetStyleSettings().GetCursorSize();
158 // Ausgabeflaeche berechnen und ausgeben
159 ImplCursorInvert( mpData );
160 mpData->mbCurVisible = TRUE;
164 // -----------------------------------------------------------------------
166 void Cursor::ImplRestore()
168 if ( mpData && mpData->mbCurVisible )
170 ImplCursorInvert( mpData );
171 mpData->mbCurVisible = FALSE;
175 // -----------------------------------------------------------------------
177 void Cursor::ImplShow( BOOL bDrawDirect )
179 if ( mbVisible )
181 Window* pWindow;
182 if ( mpWindow )
183 pWindow = mpWindow;
184 else
186 // Gibt es ein aktives Fenster und ist der Cursor in dieses Fenster
187 // selektiert, dann zeige den Cursor an
188 pWindow = Application::GetFocusWindow();
189 if ( !pWindow || (pWindow->mpWindowImpl->mpCursor != this) || pWindow->mpWindowImpl->mbInPaint
190 || !pWindow->mpWindowImpl->mpFrameData->mbHasFocus )
191 pWindow = NULL;
194 if ( pWindow )
196 if ( !mpData )
198 mpData = new ImplCursorData;
199 mpData->mbCurVisible = FALSE;
200 mpData->maTimer.SetTimeoutHdl( LINK( this, Cursor, ImplTimerHdl ) );
203 mpData->mpWindow = pWindow;
204 mpData->mnStyle = mnStyle;
205 if ( bDrawDirect )
206 ImplDraw();
208 if ( !mpWindow )
210 mpData->maTimer.SetTimeout( pWindow->GetSettings().GetStyleSettings().GetCursorBlinkTime() );
211 if ( mpData->maTimer.GetTimeout() != STYLE_CURSOR_NOBLINKTIME )
212 mpData->maTimer.Start();
213 else if ( !mpData->mbCurVisible )
214 ImplDraw();
220 // -----------------------------------------------------------------------
222 void Cursor::ImplHide()
224 if ( mpData && mpData->mpWindow )
226 if ( mpData->mbCurVisible )
227 ImplRestore();
229 mpData->maTimer.Stop();
230 mpData->mpWindow = NULL;
234 // -----------------------------------------------------------------------
236 void Cursor::ImplNew()
238 if ( mbVisible && mpData && mpData->mpWindow )
240 if ( mpData->mbCurVisible )
241 ImplRestore();
243 ImplDraw();
244 if ( !mpWindow )
246 if ( mpData->maTimer.GetTimeout() != STYLE_CURSOR_NOBLINKTIME )
247 mpData->maTimer.Start();
252 // -----------------------------------------------------------------------
254 IMPL_LINK( Cursor, ImplTimerHdl, AutoTimer*, EMPTYARG )
256 if ( mpData->mbCurVisible )
257 ImplRestore();
258 else
259 ImplDraw();
260 return 0;
263 // =======================================================================
265 Cursor::Cursor()
267 mpData = NULL;
268 mpWindow = NULL;
269 mnSlant = 0;
270 mnOffsetY = 0;
271 mnOrientation = 0;
272 mnDirection = 0;
273 mnStyle = 0;
274 mbVisible = FALSE;
277 // -----------------------------------------------------------------------
279 Cursor::Cursor( const Cursor& rCursor ) :
280 maSize( rCursor.maSize ),
281 maPos( rCursor.maPos )
283 mpData = NULL;
284 mpWindow = NULL;
285 mnSlant = rCursor.mnSlant;
286 mnOrientation = rCursor.mnOrientation;
287 mnDirection = rCursor.mnDirection;
288 mnStyle = 0;
289 mbVisible = rCursor.mbVisible;
292 // -----------------------------------------------------------------------
294 Cursor::~Cursor()
296 if ( mpData )
298 if ( mpData->mbCurVisible )
299 ImplRestore();
301 delete mpData;
305 // -----------------------------------------------------------------------
307 void Cursor::SetStyle( USHORT nStyle )
309 if ( mnStyle != nStyle )
311 mnStyle = nStyle;
312 ImplNew();
316 // -----------------------------------------------------------------------
318 void Cursor::Show()
320 if ( !mbVisible )
322 mbVisible = TRUE;
323 ImplShow();
327 // -----------------------------------------------------------------------
329 void Cursor::Hide()
331 if ( mbVisible )
333 mbVisible = FALSE;
334 ImplHide();
338 // -----------------------------------------------------------------------
340 void Cursor::SetWindow( Window* pWindow )
342 if ( mpWindow != pWindow )
344 mpWindow = pWindow;
345 ImplNew();
349 // -----------------------------------------------------------------------
351 void Cursor::SetPos( const Point& rPoint )
353 if ( maPos != rPoint )
355 maPos = rPoint;
356 ImplNew();
360 // -----------------------------------------------------------------------
362 void Cursor::SetOffsetY( long nNewOffsetY )
364 if ( mnOffsetY != nNewOffsetY )
366 mnOffsetY = nNewOffsetY;
367 ImplNew();
371 // -----------------------------------------------------------------------
373 void Cursor::SetSize( const Size& rSize )
375 if ( maSize != rSize )
377 maSize = rSize;
378 ImplNew();
382 // -----------------------------------------------------------------------
384 void Cursor::SetWidth( long nNewWidth )
386 if ( maSize.Width() != nNewWidth )
388 maSize.Width() = nNewWidth;
389 ImplNew();
393 // -----------------------------------------------------------------------
395 void Cursor::SetHeight( long nNewHeight )
397 if ( maSize.Height() != nNewHeight )
399 maSize.Height() = nNewHeight;
400 ImplNew();
404 // -----------------------------------------------------------------------
406 void Cursor::SetSlant( long nNewSlant )
408 if ( mnSlant != nNewSlant )
410 mnSlant = nNewSlant;
411 ImplNew();
415 // -----------------------------------------------------------------------
417 void Cursor::SetOrientation( short nNewOrientation )
419 if ( mnOrientation != nNewOrientation )
421 mnOrientation = nNewOrientation;
422 ImplNew();
426 // -----------------------------------------------------------------------
428 void Cursor::SetDirection( unsigned char nNewDirection )
430 if ( mnDirection != nNewDirection )
432 mnDirection = nNewDirection;
433 ImplNew();
437 // -----------------------------------------------------------------------
439 Cursor& Cursor::operator=( const Cursor& rCursor )
441 maPos = rCursor.maPos;
442 maSize = rCursor.maSize;
443 mnSlant = rCursor.mnSlant;
444 mnOrientation = rCursor.mnOrientation;
445 mnDirection = rCursor.mnDirection;
446 mbVisible = rCursor.mbVisible;
447 ImplNew();
449 return *this;
452 // -----------------------------------------------------------------------
454 BOOL Cursor::operator==( const Cursor& rCursor ) const
456 if ( (maPos == rCursor.maPos) &&
457 (maSize == rCursor.maSize) &&
458 (mnSlant == rCursor.mnSlant) &&
459 (mnOrientation == rCursor.mnOrientation) &&
460 (mnDirection == rCursor.mnDirection) &&
461 (mbVisible == rCursor.mbVisible) )
462 return TRUE;
463 else
464 return FALSE;