summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/controller/knavigator.cpp
blob: 3258ce942fd847c37cdc7c7d5e5ecc3de4d10c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************************************************************
                          knavigator.cpp  -  description
                             -------------------
    begin                : Sa Dez 4 2004
    copyright            : (C) 2004 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/



// qt specific
#include <tqevent.h>
// lib specific
#include "kdatabuffer.h"
#include "kbufferranges.h"
#include "kbuffercursor.h"
#include "kwordbufferservice.h"
#include "khexedit.h"
#include "knavigator.h"


using namespace KHE;

KNavigator::KNavigator( KHexEdit* HE, KController *P )
  : KController( HE, P )
{
}

bool KNavigator::handleKeyPress( TQKeyEvent *KeyEvent )
{
  bool KeyUsed = true;

  //bool clearUndoRedoInfo = true;
  bool ShiftPressed =  KeyEvent->state() & TQt::ShiftButton;
  bool ControlPressed = KeyEvent->state() & TQt::ControlButton;
  //bool AltPressed = KeyEvent->state() & AltButton;

  // we only care for cursor keys and the like, won't hardcode any other keys
  // we also don't check whether the commands are allowed
  // as the commands are also available as API so the check has to be done
  // in each command anyway
  switch( KeyEvent->key() )
  {
    case TQt::Key_Left:
      moveCursor( ControlPressed ? MoveWordBackward : MoveBackward, ShiftPressed );
      break;
    case TQt::Key_Right:
      moveCursor( ControlPressed ? MoveWordForward : MoveForward, ShiftPressed );
      break;
    case TQt::Key_Up:
      moveCursor( ControlPressed ? MovePgUp : MoveUp, ShiftPressed );
      break;
    case TQt::Key_Down:
      moveCursor( ControlPressed ? MovePgDown : MoveDown, ShiftPressed );
      break;
    case TQt::Key_Home:
      moveCursor( ControlPressed ? MoveHome : MoveLineStart, ShiftPressed );
      break;
    case TQt::Key_End:
      moveCursor( ControlPressed ? MoveEnd : MoveLineEnd, ShiftPressed );
      break;
    case TQt::Key_Prior:
      moveCursor( MovePgUp, ShiftPressed );
      break;
    case TQt::Key_Next:
      moveCursor( MovePgDown, ShiftPressed );
      break;

    default:
      KeyUsed = false;
  }

  return KeyUsed ? true : KController::handleKeyPress(KeyEvent);
}


void KNavigator::moveCursor( KMoveAction Action, bool Select )
{
  HexEdit->pauseCursor( true );

  KBufferCursor *BufferCursor = HexEdit->BufferCursor;
  KBufferRanges *BufferRanges = HexEdit->BufferRanges;

  if( Select )
  {
    if( !BufferRanges->selectionStarted() )
      BufferRanges->setSelectionStart( BufferCursor->realIndex() );
  }
  else
    BufferRanges->removeSelection();

  HexEdit->resetInputContext();
  switch( Action )
  {
    case MoveBackward:     BufferCursor->gotoPreviousByte(); break;
    case MoveWordBackward: {
      KWordBufferService WBS( HexEdit->DataBuffer, HexEdit->Codec );
      int NewIndex = WBS.indexOfPreviousWordStart( BufferCursor->realIndex() );
      BufferCursor->gotoIndex( NewIndex );
    }
    break;
    case MoveForward:      BufferCursor->gotoNextByte();     break;
    case MoveWordForward:  {
      KWordBufferService WBS( HexEdit->DataBuffer, HexEdit->Codec );
      int NewIndex = WBS.indexOfNextWordStart( BufferCursor->realIndex() );
      BufferCursor->gotoCIndex( NewIndex );
    }
    break;
    case MoveUp:           BufferCursor->gotoUp();             break;
    case MovePgUp:         BufferCursor->gotoPageUp();         break;
    case MoveDown:         BufferCursor->gotoDown();           break;
    case MovePgDown:       BufferCursor->gotoPageDown();       break;
    case MoveLineStart:    BufferCursor->gotoLineStart();      break;
    case MoveHome:         BufferCursor->gotoStart();          break;
    case MoveLineEnd:      BufferCursor->gotoLineEnd();        break;
    case MoveEnd:          BufferCursor->gotoEnd();            break;
  }

  if( Select )
    BufferRanges->setSelectionEnd( BufferCursor->realIndex() );

  HexEdit->tqrepaintChanged();
  HexEdit->ensureCursorVisible();

  HexEdit->unpauseCursor();

  if( BufferRanges->isModified() )
  {
    if( !HexEdit->isOverwriteMode() ) emit HexEdit->cutAvailable( BufferRanges->hasSelection() );
    emit HexEdit->copyAvailable( BufferRanges->hasSelection() );
    KSection Selection = BufferRanges->selection();
    emit HexEdit->selectionChanged( Selection.start(), Selection.end() );
  }
}