summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/kselection.h
blob: 7e69d9bdf0573f0b22f7f9351c6358ffebe36af6 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/***************************************************************************
                          kbuffersection.h  -  description
                             -------------------
    begin                : 22.06.2003
    copyright            : (C) 2003 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.       *
 *                                                                         *
 ***************************************************************************/


#ifndef KHE_KSELECTION_H
#define KHE_KSELECTION_H

#include "ksection.h"

namespace KHE
{

/** This class describes a selected section of the buffer.
  * As it is used as selection controlled by
  * mouse and keyboard commands it offers two ways to set its range:
  * - by giving the startposition (of the cursor) of an interactive selection
  *   and the subsequent end positions (until selection is finished)
  * - direct setting (as provided by KSection)
  *
  * the interactive selection takes care that
  *
  *@author Friedrich W. H.  Kossebau
  */
class TDESelection : public KSection
{
  public:
    /** creates a selection with a given start.
      * @param Index index in front of which the selection begins
      */
    TDESelection( int Index );
    /** creates an invalid selection */
    TDESelection();
    ~TDESelection();

  public:
    TDESelection &operator=( const TDESelection &S );
    TDESelection &operator=( const KSection &S );

  public: // modification access
    /** starts the selection. 
      * For this the anchor, start and end are set to the given index,
      * so the initial selection is empty.
      * @param Index index in front of which the selection begins
      */
    void setStart( int Index );
    /** sets the end of the current selection 
      * If the end is before the start the selection will reach from the given index 
      * @param Index index in front of which the selection ends
      */
    void setEnd( int Index );
    /** sets the selection to be invalid
      */
    void cancel();
    /** sets the anchor to the start 
      * If the selection has not started the behaviour is undefined.
      */
    void setForward();
    /** sets the anchor to the end
      * If the selection has not started the behaviour is undefined.
      */
    void setBackward();
    /** swaps anchor from start to end or vice versa 
      * If the selection has not started the behaviour is undefined.
      */
    void reverse();

  public: // value access
    /** 
      * @return anchor value
      */
    int anchor() const;

  public: // logic access
    /** 
      * @return @c true if the anchor has been set, otherwise @c false.
      */
    bool started() const;
    /** 
      * @return @c true if the anchor has been set and the selection is empty, otherwise @c false.
      */
    bool justStarted() const;
    /** 
      * @return @c true if the anchor is at the begin of the selection 
      */
    bool isForward() const;

  protected:
    /** cursor index where the selection starts */
    int Anchor;
};


inline TDESelection::TDESelection() : Anchor( -1 ) {}
inline TDESelection::TDESelection( int Index ) : Anchor( Index )  {}
inline TDESelection::~TDESelection() {}

inline TDESelection &TDESelection::operator=( const TDESelection &S )
{ 
  KSection::operator=(S); 
  Anchor = S.Anchor; 
  return *this; 
}

inline TDESelection &TDESelection::operator=( const KSection &S )
{ 
  KSection::operator=(S); 
  Anchor = start(); 
  return *this; 
}


inline void TDESelection::setStart( int Index )
{
  Anchor = Index;
  unset();
}


inline void TDESelection::setEnd( int Index )
{
  // nothing selected?
  if( Index == Anchor )
    unset();
  // selecting forwards?
  else if( Index > Anchor )
  {
    KSection::setStart( Anchor );
    KSection::setEnd( Index-1 );
  }
  // selecting backwards
  else
  {
    KSection::setStart( Index );
    KSection::setEnd( Anchor-1 );
  }
}

inline void TDESelection::reverse() 
{
   Anchor = isForward() ? end()+1 : start();
}

inline void TDESelection::setForward() 
{
   Anchor = start();
}

inline void TDESelection::setBackward() 
{
   Anchor = end()+1;
}

inline int TDESelection::anchor() const { return Anchor; }

inline void TDESelection::cancel() { Anchor = -1; unset(); }

inline bool TDESelection::started() const { return Anchor != -1; }

inline bool TDESelection::justStarted() const { return Anchor != -1 && start() == -1; }

inline bool TDESelection::isForward() const { return Anchor == start(); }

}

#endif