LCOV - code coverage report
Current view: top level - vcl/source/window - seleng.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 40 198 20.2 %
Date: 2014-11-03 Functions: 10 19 52.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10