LCOV - code coverage report
Current view: top level - vcl/source/window - seleng.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 39 193 20.2 %
Date: 2014-04-11 Functions: 8 16 50.0 %
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       24706 : FunctionSet::~FunctionSet()
      25             : {
      26       24706 : }
      27             : 
      28          45 : inline bool SelectionEngine::ShouldDeselect( bool bModifierKey1 ) const
      29             : {
      30          45 :     return eSelMode != MULTIPLE_SELECTION || !bModifierKey1;
      31             : }
      32             : 
      33             : // TODO: throw out FunctionSet::SelectAtPoint
      34             : 
      35       26438 : SelectionEngine::SelectionEngine( Window* pWindow, FunctionSet* pFuncSet,
      36             :                                   sal_uLong nAutoRepeatInterval ) :
      37             :     pWin( pWindow ),
      38       26438 :     nUpdateInterval( nAutoRepeatInterval )
      39             : {
      40       26438 :     eSelMode = SINGLE_SELECTION;
      41       26438 :     pFunctionSet = pFuncSet;
      42       26438 :     nFlags = SELENG_EXPANDONMOVE;
      43       26438 :     nLockedMods = 0;
      44             : 
      45       26438 :     aWTimer.SetTimeoutHdl( LINK( this, SelectionEngine, ImpWatchDog ) );
      46       26438 :     aWTimer.SetTimeout( nUpdateInterval );
      47       26438 : }
      48             : 
      49       49412 : SelectionEngine::~SelectionEngine()
      50             : {
      51       24706 :     aWTimer.Stop();
      52       24706 : }
      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       26505 : void SelectionEngine::SetSelectionMode( SelectionMode eMode )
      62             : {
      63       26505 :     eSelMode = eMode;
      64       26505 : }
      65             : 
      66          45 : void SelectionEngine::CursorPosChanging( bool bShift, bool bMod1 )
      67             : {
      68          45 :     if ( !pFunctionSet )
      69          45 :         return;
      70             : 
      71          45 :     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          45 :         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          45 :             if( ShouldDeselect( bMod1 ) )
     106          45 :                 pFunctionSet->DeselectAll();
     107             :             else
     108           0 :                 pFunctionSet->DestroyAnchor();
     109          45 :             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 :        pWin->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 : bool SelectionEngine::SelMouseMove( const MouseEvent& rMEvt )
     280             : {
     281             : 
     282           0 :     if ( !pFunctionSet || !(nFlags & SELENG_IN_SEL) ||
     283           0 :          (nFlags & (SELENG_CMDEVT | SELENG_WAIT_UPEVT)) )
     284           0 :         return false;
     285             : 
     286           0 :     if( !(nFlags & SELENG_EXPANDONMOVE) )
     287           0 :         return false; // wait for DragEvent!
     288             : 
     289           0 :     aLastMove = rMEvt;
     290             :     // if the mouse is outside the area, the frequency of
     291             :     // SetCursorAtPoint() is only set by the Timer
     292           0 :     if( aWTimer.IsActive() && !aArea.IsInside( rMEvt.GetPosPixel() ))
     293           0 :         return true;
     294             : 
     295           0 :     aWTimer.SetTimeout( nUpdateInterval );
     296           0 :     aWTimer.Start();
     297           0 :     if ( eSelMode != SINGLE_SELECTION )
     298             :     {
     299           0 :         if ( !(nFlags & SELENG_HAS_ANCH) )
     300             :         {
     301           0 :             pFunctionSet->CreateAnchor();
     302           0 :             nFlags |= SELENG_HAS_ANCH;
     303             :         }
     304             :     }
     305             : 
     306           0 :     pFunctionSet->SetCursorAtPoint( rMEvt.GetPosPixel() );
     307             : 
     308           0 :     return true;
     309             : }
     310             : 
     311          51 : void SelectionEngine::SetWindow( Window* pNewWin )
     312             : {
     313          51 :     if( pNewWin != pWin )
     314             :     {
     315          10 :         if ( pWin && (nFlags & SELENG_IN_SEL) )
     316           0 :             pWin->ReleaseMouse();
     317          10 :         pWin = pNewWin;
     318          10 :         if ( pWin && ( nFlags & SELENG_IN_SEL ) )
     319           0 :             pWin->CaptureMouse();
     320             :     }
     321          51 : }
     322             : 
     323         424 : void SelectionEngine::Reset()
     324             : {
     325         424 :     aWTimer.Stop();
     326         424 :     if ( nFlags & SELENG_IN_SEL )
     327           0 :         pWin->ReleaseMouse();
     328         424 :     nFlags &= ~(SELENG_HAS_ANCH | SELENG_IN_SEL);
     329         424 :     nLockedMods = 0;
     330         424 : }
     331             : 
     332           0 : void SelectionEngine::Command( const CommandEvent& rCEvt )
     333             : {
     334             :     // Timer aWTimer is active during enlarging a selection
     335           0 :     if ( !pFunctionSet || !pWin || aWTimer.IsActive() )
     336           0 :         return;
     337           0 :     aWTimer.Stop();
     338           0 :     nFlags |= SELENG_CMDEVT;
     339           0 :     if ( rCEvt.GetCommand() == COMMAND_STARTDRAG )
     340             :     {
     341           0 :         if ( nFlags & SELENG_DRG_ENAB )
     342             :         {
     343             :             DBG_ASSERT( rCEvt.IsMouseEvent(), "STARTDRAG: Not a MouseEvent" );
     344           0 :             if ( pFunctionSet->IsSelectionAtPoint( rCEvt.GetMousePosPixel() ) )
     345             :             {
     346           0 :                 aLastMove = MouseEvent( rCEvt.GetMousePosPixel(),
     347           0 :                                aLastMove.GetClicks(), aLastMove.GetMode(),
     348           0 :                                aLastMove.GetButtons(), aLastMove.GetModifier() );
     349           0 :                 pFunctionSet->BeginDrag();
     350           0 :                 nFlags &= ~(SELENG_CMDEVT|SELENG_WAIT_UPEVT|SELENG_IN_SEL);
     351             :             }
     352             :             else
     353           0 :                 nFlags &= ~SELENG_CMDEVT;
     354             :         }
     355             :         else
     356           0 :             nFlags &= ~SELENG_CMDEVT;
     357             :     }
     358             : }
     359             : 
     360           0 : void SelectionEngine::SetUpdateInterval( sal_uLong nInterval )
     361             : {
     362           0 :     if (nInterval < SELENG_AUTOREPEAT_INTERVAL_MIN)
     363             :         // Set a lower threshold.  On Windows, setting this value too low
     364             :         // would cause selection to get updated indefinitely.
     365           0 :         nInterval = SELENG_AUTOREPEAT_INTERVAL_MIN;
     366             : 
     367           0 :     if (nUpdateInterval == nInterval)
     368             :         // no update needed.
     369           0 :         return;
     370             : 
     371           0 :     if (aWTimer.IsActive())
     372             :     {
     373             :         // reset the timer right away on interval change.
     374           0 :         aWTimer.Stop();
     375           0 :         aWTimer.SetTimeout(nInterval);
     376           0 :         aWTimer.Start();
     377             :     }
     378             :     else
     379           0 :         aWTimer.SetTimeout(nInterval);
     380             : 
     381           0 :     nUpdateInterval = nInterval;
     382             : }
     383             : 
     384             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10