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/window.hxx>
21 : #include <vcl/seleng.hxx>
22 : #include <tools/debug.hxx>
23 :
24 40538 : FunctionSet::~FunctionSet()
25 : {
26 40538 : }
27 :
28 190 : inline bool SelectionEngine::ShouldDeselect( bool bModifierKey1 ) const
29 : {
30 190 : return eSelMode != MULTIPLE_SELECTION || !bModifierKey1;
31 : }
32 :
33 : // TODO: throw out FunctionSet::SelectAtPoint
34 :
35 42879 : SelectionEngine::SelectionEngine( vcl::Window* pWindow, FunctionSet* pFuncSet,
36 : sal_uLong nAutoRepeatInterval ) :
37 : pWin( pWindow ),
38 42879 : nUpdateInterval( nAutoRepeatInterval )
39 : {
40 42879 : eSelMode = SINGLE_SELECTION;
41 42879 : pFunctionSet = pFuncSet;
42 42879 : nFlags = SelectionEngineFlags::EXPANDONMOVE;
43 42879 : nLockedMods = 0;
44 :
45 42879 : aWTimer.SetTimeoutHdl( LINK( this, SelectionEngine, ImpWatchDog ) );
46 42879 : aWTimer.SetTimeout( nUpdateInterval );
47 42879 : }
48 :
49 81076 : SelectionEngine::~SelectionEngine()
50 : {
51 40538 : aWTimer.Stop();
52 40538 : }
53 :
54 0 : IMPL_LINK_NOARG_TYPED(SelectionEngine, ImpWatchDog, Timer *, void)
55 : {
56 0 : if ( !aArea.IsInside( aLastMove.GetPosPixel() ) )
57 0 : SelMouseMove( aLastMove );
58 0 : }
59 :
60 42906 : void SelectionEngine::SetSelectionMode( SelectionMode eMode )
61 : {
62 42906 : eSelMode = eMode;
63 42906 : }
64 :
65 190 : void SelectionEngine::CursorPosChanging( bool bShift, bool bMod1 )
66 : {
67 190 : if ( !pFunctionSet )
68 190 : return;
69 :
70 190 : if ( bShift && eSelMode != SINGLE_SELECTION )
71 : {
72 0 : if ( IsAddMode() )
73 : {
74 0 : if ( !(nFlags & SelectionEngineFlags::HAS_ANCH) )
75 : {
76 0 : pFunctionSet->CreateAnchor();
77 0 : nFlags |= SelectionEngineFlags::HAS_ANCH;
78 : }
79 : }
80 : else
81 : {
82 0 : if ( !(nFlags & SelectionEngineFlags::HAS_ANCH) )
83 : {
84 0 : if( ShouldDeselect( bMod1 ) )
85 0 : pFunctionSet->DeselectAll();
86 0 : pFunctionSet->CreateAnchor();
87 0 : nFlags |= SelectionEngineFlags::HAS_ANCH;
88 : }
89 : }
90 : }
91 : else
92 : {
93 190 : if ( IsAddMode() )
94 : {
95 0 : if ( nFlags & SelectionEngineFlags::HAS_ANCH )
96 : {
97 : // pFunctionSet->CreateCursor();
98 0 : pFunctionSet->DestroyAnchor();
99 0 : nFlags &= (~SelectionEngineFlags::HAS_ANCH);
100 : }
101 : }
102 : else
103 : {
104 190 : if( ShouldDeselect( bMod1 ) )
105 190 : pFunctionSet->DeselectAll();
106 : else
107 0 : pFunctionSet->DestroyAnchor();
108 190 : nFlags &= (~SelectionEngineFlags::HAS_ANCH);
109 : }
110 : }
111 : }
112 :
113 1 : bool SelectionEngine::SelMouseButtonDown( const MouseEvent& rMEvt )
114 : {
115 1 : nFlags &= (~SelectionEngineFlags::CMDEVT);
116 1 : if ( !pFunctionSet || !pWin || rMEvt.GetClicks() > 1 || rMEvt.IsRight() )
117 0 : return false;
118 :
119 1 : sal_uInt16 nModifier = rMEvt.GetModifier() | nLockedMods;
120 1 : if ( nModifier & KEY_MOD2 )
121 0 : return false;
122 : // in SingleSelection: filter Control-Key,
123 : // so that a D&D can be also started with a Ctrl-Click
124 1 : if ( nModifier == KEY_MOD1 && eSelMode == SINGLE_SELECTION )
125 0 : nModifier = 0;
126 :
127 1 : Point aPos = rMEvt.GetPosPixel();
128 1 : aLastMove = rMEvt;
129 :
130 1 : if( !rMEvt.IsRight() )
131 : {
132 1 : pWin->CaptureMouse();
133 1 : nFlags |= SelectionEngineFlags::IN_SEL;
134 : }
135 : else
136 : {
137 0 : nModifier = 0;
138 : }
139 :
140 1 : switch ( nModifier )
141 : {
142 : case 0: // KEY_NO_KEY
143 : {
144 1 : bool bSelAtPoint = pFunctionSet->IsSelectionAtPoint( aPos );
145 1 : nFlags &= (~SelectionEngineFlags::IN_ADD);
146 1 : if ( (nFlags & SelectionEngineFlags::DRG_ENAB) && bSelAtPoint )
147 : {
148 0 : nFlags |= SelectionEngineFlags::WAIT_UPEVT;
149 0 : nFlags &= ~(SelectionEngineFlags::IN_SEL);
150 0 : pWin->ReleaseMouse();
151 0 : return true; // wait for STARTDRAG-Command-Event
152 : }
153 1 : if ( eSelMode != SINGLE_SELECTION )
154 : {
155 1 : if( !IsAddMode() )
156 1 : pFunctionSet->DeselectAll();
157 : else
158 0 : pFunctionSet->DestroyAnchor();
159 1 : nFlags &= (~SelectionEngineFlags::HAS_ANCH); // bHasAnchor = false;
160 : }
161 1 : pFunctionSet->SetCursorAtPoint( aPos );
162 : // special case Single-Selection, to enable simple Select+Drag
163 1 : if (eSelMode == SINGLE_SELECTION && (nFlags & SelectionEngineFlags::DRG_ENAB))
164 0 : nFlags |= SelectionEngineFlags::WAIT_UPEVT;
165 1 : return true;
166 : }
167 :
168 : case KEY_SHIFT:
169 0 : if ( eSelMode == SINGLE_SELECTION )
170 : {
171 0 : pWin->ReleaseMouse();
172 0 : nFlags &= (~SelectionEngineFlags::IN_SEL);
173 0 : return false;
174 : }
175 0 : if ( nFlags & SelectionEngineFlags::ADD_ALW )
176 0 : nFlags |= SelectionEngineFlags::IN_ADD;
177 : else
178 0 : nFlags &= (~SelectionEngineFlags::IN_ADD);
179 :
180 0 : if( !(nFlags & SelectionEngineFlags::HAS_ANCH) )
181 : {
182 0 : if ( !(nFlags & SelectionEngineFlags::IN_ADD) )
183 0 : pFunctionSet->DeselectAll();
184 0 : pFunctionSet->CreateAnchor();
185 0 : nFlags |= SelectionEngineFlags::HAS_ANCH;
186 : }
187 0 : pFunctionSet->SetCursorAtPoint( aPos );
188 0 : return true;
189 :
190 : case KEY_MOD1:
191 : // allow Control only for Multi-Select
192 0 : if ( eSelMode != MULTIPLE_SELECTION )
193 : {
194 0 : nFlags &= (~SelectionEngineFlags::IN_SEL);
195 0 : pWin->ReleaseMouse();
196 0 : return true; // skip Mouse-Click
197 : }
198 0 : if ( nFlags & SelectionEngineFlags::HAS_ANCH )
199 : {
200 : // pFunctionSet->CreateCursor();
201 0 : pFunctionSet->DestroyAnchor();
202 0 : nFlags &= (~SelectionEngineFlags::HAS_ANCH);
203 : }
204 0 : if ( pFunctionSet->IsSelectionAtPoint( aPos ) )
205 : {
206 0 : pFunctionSet->DeselectAtPoint( aPos );
207 0 : pFunctionSet->SetCursorAtPoint( aPos, true );
208 : }
209 : else
210 : {
211 0 : pFunctionSet->SetCursorAtPoint( aPos );
212 : }
213 0 : return true;
214 :
215 : case KEY_SHIFT + KEY_MOD1:
216 0 : if ( eSelMode != MULTIPLE_SELECTION )
217 : {
218 0 : pWin->ReleaseMouse();
219 0 : nFlags &= (~SelectionEngineFlags::IN_SEL);
220 0 : return false;
221 : }
222 0 : nFlags |= SelectionEngineFlags::IN_ADD; //bIsInAddMode = true;
223 0 : if ( !(nFlags & SelectionEngineFlags::HAS_ANCH) )
224 : {
225 0 : pFunctionSet->CreateAnchor();
226 0 : nFlags |= SelectionEngineFlags::HAS_ANCH;
227 : }
228 0 : pFunctionSet->SetCursorAtPoint( aPos );
229 0 : return true;
230 : }
231 :
232 0 : return false;
233 : }
234 :
235 1 : bool SelectionEngine::SelMouseButtonUp( const MouseEvent& rMEvt )
236 : {
237 1 : aWTimer.Stop();
238 1 : if( !pFunctionSet || !pWin )
239 : {
240 0 : const SelectionEngineFlags nMask = (SelectionEngineFlags::CMDEVT | SelectionEngineFlags::WAIT_UPEVT | SelectionEngineFlags::IN_SEL);
241 0 : nFlags &= ~nMask;
242 0 : return false;
243 : }
244 :
245 1 : if( !rMEvt.IsRight() )
246 : {
247 1 : ReleaseMouse();
248 : }
249 :
250 1 : if( (nFlags & SelectionEngineFlags::WAIT_UPEVT) && !(nFlags & SelectionEngineFlags::CMDEVT) &&
251 0 : eSelMode != SINGLE_SELECTION)
252 : {
253 : // MouseButtonDown in Sel but no CommandEvent yet
254 : // ==> deselektieren
255 0 : sal_uInt16 nModifier = aLastMove.GetModifier() | nLockedMods;
256 0 : if( nModifier == KEY_MOD1 || IsAlwaysAdding() )
257 : {
258 0 : if( !(nModifier & KEY_SHIFT) )
259 : {
260 0 : pFunctionSet->DestroyAnchor();
261 0 : nFlags &= (~SelectionEngineFlags::HAS_ANCH); // uncheck anchor
262 : }
263 0 : pFunctionSet->DeselectAtPoint( aLastMove.GetPosPixel() );
264 0 : nFlags &= (~SelectionEngineFlags::HAS_ANCH); // uncheck anchor
265 0 : pFunctionSet->SetCursorAtPoint( aLastMove.GetPosPixel(), true );
266 : }
267 : else
268 : {
269 0 : pFunctionSet->DeselectAll();
270 0 : nFlags &= (~SelectionEngineFlags::HAS_ANCH); // uncheck anchor
271 0 : pFunctionSet->SetCursorAtPoint( aLastMove.GetPosPixel() );
272 : }
273 : }
274 :
275 1 : const SelectionEngineFlags nMask = (SelectionEngineFlags::CMDEVT | SelectionEngineFlags::WAIT_UPEVT | SelectionEngineFlags::IN_SEL);
276 1 : nFlags &= ~nMask;
277 1 : return true;
278 : }
279 :
280 1 : void SelectionEngine::ReleaseMouse()
281 : {
282 1 : if (!pWin)
283 1 : return;
284 1 : pWin->ReleaseMouse();
285 : }
286 :
287 0 : bool SelectionEngine::SelMouseMove( const MouseEvent& rMEvt )
288 : {
289 :
290 0 : if ( !pFunctionSet || !(nFlags & SelectionEngineFlags::IN_SEL) ||
291 0 : (nFlags & (SelectionEngineFlags::CMDEVT | SelectionEngineFlags::WAIT_UPEVT)) )
292 0 : return false;
293 :
294 0 : if( !(nFlags & SelectionEngineFlags::EXPANDONMOVE) )
295 0 : return false; // wait for DragEvent!
296 :
297 0 : aLastMove = rMEvt;
298 : // if the mouse is outside the area, the frequency of
299 : // SetCursorAtPoint() is only set by the Timer
300 0 : if( aWTimer.IsActive() && !aArea.IsInside( rMEvt.GetPosPixel() ))
301 0 : return true;
302 :
303 0 : aWTimer.SetTimeout( nUpdateInterval );
304 0 : aWTimer.Start();
305 0 : if ( eSelMode != SINGLE_SELECTION )
306 : {
307 0 : if ( !(nFlags & SelectionEngineFlags::HAS_ANCH) )
308 : {
309 0 : pFunctionSet->CreateAnchor();
310 0 : nFlags |= SelectionEngineFlags::HAS_ANCH;
311 : }
312 : }
313 :
314 0 : pFunctionSet->SetCursorAtPoint( rMEvt.GetPosPixel() );
315 :
316 0 : return true;
317 : }
318 :
319 62 : void SelectionEngine::SetWindow( vcl::Window* pNewWin )
320 : {
321 62 : if( pNewWin != pWin )
322 : {
323 10 : if ( pWin && (nFlags & SelectionEngineFlags::IN_SEL) )
324 0 : pWin->ReleaseMouse();
325 10 : pWin = pNewWin;
326 10 : if ( pWin && ( nFlags & SelectionEngineFlags::IN_SEL ) )
327 0 : pWin->CaptureMouse();
328 : }
329 62 : }
330 :
331 712 : void SelectionEngine::Reset()
332 : {
333 712 : aWTimer.Stop();
334 712 : if ( nFlags & SelectionEngineFlags::IN_SEL )
335 0 : pWin->ReleaseMouse();
336 712 : SelectionEngineFlags nMask = (SelectionEngineFlags::HAS_ANCH | SelectionEngineFlags::IN_SEL);
337 712 : nFlags &= ~nMask;
338 712 : nLockedMods = 0;
339 712 : }
340 :
341 0 : void SelectionEngine::Command( const CommandEvent& rCEvt )
342 : {
343 : // Timer aWTimer is active during enlarging a selection
344 0 : if ( !pFunctionSet || !pWin || aWTimer.IsActive() )
345 0 : return;
346 0 : aWTimer.Stop();
347 0 : nFlags |= SelectionEngineFlags::CMDEVT;
348 0 : if ( rCEvt.GetCommand() == CommandEventId::StartDrag )
349 : {
350 0 : if ( nFlags & SelectionEngineFlags::DRG_ENAB )
351 : {
352 : DBG_ASSERT( rCEvt.IsMouseEvent(), "STARTDRAG: Not a MouseEvent" );
353 0 : if ( pFunctionSet->IsSelectionAtPoint( rCEvt.GetMousePosPixel() ) )
354 : {
355 0 : aLastMove = MouseEvent( rCEvt.GetMousePosPixel(),
356 0 : aLastMove.GetClicks(), aLastMove.GetMode(),
357 0 : aLastMove.GetButtons(), aLastMove.GetModifier() );
358 0 : pFunctionSet->BeginDrag();
359 0 : const SelectionEngineFlags nMask = (SelectionEngineFlags::CMDEVT|SelectionEngineFlags::WAIT_UPEVT|SelectionEngineFlags::IN_SEL);
360 0 : nFlags &= ~nMask;
361 : }
362 : else
363 0 : nFlags &= ~SelectionEngineFlags::CMDEVT;
364 : }
365 : else
366 0 : nFlags &= ~SelectionEngineFlags::CMDEVT;
367 : }
368 : }
369 :
370 0 : void SelectionEngine::SetUpdateInterval( sal_uLong nInterval )
371 : {
372 0 : if (nInterval < SELENG_AUTOREPEAT_INTERVAL_MIN)
373 : // Set a lower threshold. On Windows, setting this value too low
374 : // would cause selection to get updated indefinitely.
375 0 : nInterval = SELENG_AUTOREPEAT_INTERVAL_MIN;
376 :
377 0 : if (nUpdateInterval == nInterval)
378 : // no update needed.
379 0 : return;
380 :
381 0 : if (aWTimer.IsActive())
382 : {
383 : // reset the timer right away on interval change.
384 0 : aWTimer.Stop();
385 0 : aWTimer.SetTimeout(nInterval);
386 0 : aWTimer.Start();
387 : }
388 : else
389 0 : aWTimer.SetTimeout(nInterval);
390 :
391 0 : nUpdateInterval = nInterval;
392 : }
393 :
394 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|