]> git.lyx.org Git - lyx.git/blob - src/lyxcursor.h
Alfredo's second patch
[lyx.git] / src / lyxcursor.h
1 // -*- C++ -*-
2 /**
3  * \file lyxcursor.h
4  * Copyright 1995-2001 the LyX Team
5  * Read the file COPYING
6  *
7  * \author Matthias Ettrich
8  */
9
10 #ifndef LYXCURSOR_H
11 #define LYXCURSOR_H
12
13 #include "RowList.h"
14 #include "ParagraphList.h"
15 #include "support/types.h"
16
17 /**
18  * The cursor class describes the position of a cursor within a document.
19  * Several cursors exist within LyX; for example, when locking an inset,
20  * the position of the cursor in the containing inset is stored.
21  *
22  * FIXME: true ?
23  */
24 class LyXCursor {
25 public:
26         LyXCursor();
27         /// set the paragraph that contains this cursor
28         void par(ParagraphList::iterator pit);
29         /// return the paragraph this cursor is in
30         ParagraphList::iterator par() const;
31         /// set the position within the paragraph
32         void pos(lyx::pos_type p);
33         /// return the position within the paragraph
34         lyx::pos_type pos() const;
35         /// FIXME
36         void boundary(bool b);
37         /// FIXME
38         bool boundary() const;
39         /// set the x position in pixels
40         void x(int i);
41         /// return the x position in pixels
42         int x() const;
43         /// set the stored next-line position when at the end of a row
44         void ix(int i);
45         /**
46          * Return the x position of the start of the next row, when this
47          * cursor is at the end of the previous row, for insets that take
48          * a full row.
49          *
50          * FIXME: explain why we need this ?
51          */
52         int ix() const;
53         /// set the cached x position
54         void x_fix(int i);
55         /**
56          * Return the cached x position of the cursor. This is used for when
57          * we have text like :
58          *
59          * blah blah blah blah| blah blah blah
60          * blah blah blah
61          * blah blah blah blah blah blah
62          *
63          * When we move onto row 3, we would like to be vertically aligned
64          * with where we were in row 1, despite the fact that row 2 is
65          * shorter than x()
66          */
67         int x_fix() const;
68         /// set the y position in pixels
69         void y(int i);
70         /// return the y position in pixels
71         int y() const;
72         /// set the stored next-line y position when at the end of a row
73         void iy(int i);
74         /**
75          * Return the y position of the start of the next row, when this
76          * cursor is at the end of the previous row, for insets that take
77          * a full row.
78          *
79          * FIXME: explain why we need this ? especially for y...
80          */
81         int iy() const;
82         /// set the row of the paragraph the cursor is in
83         void row(RowList::iterator r);
84         /// return the row of the paragraph this cursor is in
85         RowList::iterator row() const;
86         /// set the stored next row
87         void irow(RowList::iterator r);
88         /**
89          * Return the next row, when this
90          * cursor is at the end of the previous row, for insets that take
91          * a full row.
92          *
93          * FIXME: explain why we need this ? especially for y...
94          */
95         RowList::iterator irow() const;
96 private:
97         /// The paragraph the cursor is in.
98         ParagraphList::iterator par_;
99         /// The position inside the paragraph
100         lyx::pos_type pos_;
101         /**
102          * When the cursor position is i, is the cursor is after the i-th char
103          * or before the i+1-th char ? Normally, these two interpretations are
104          * equivalent, except when the fonts of the i-th and i+1-th char
105          * differ.
106          * We use boundary_ to distinguish between the two options:
107          * If boundary_=true, then the cursor is after the i-th char
108          * and if boundary_=false, then the cursor is before the i+1-th char.
109          *
110          * We currently use the boundary only when the language direction of
111          * the i-th char is different than the one of the i+1-th char.
112          * In this case it is important to distinguish between the two
113          * cursor interpretations, in order to give a reasonable behavior to
114          * the user.
115          */
116         bool boundary_;
117         /// the pixel x position
118         int x_;
119         /// the stored next-row x position
120         int ix_;
121         /// the cached x position
122         int x_fix_;
123         /// the pixel y position
124         int y_;
125         /// the stored next-row y position
126         int iy_;
127         /// the containing row
128         RowList::iterator row_;
129         /// the containing row for the next line
130         RowList::iterator irow_;
131 };
132
133 /// these three dictate the others
134 inline
135 bool operator==(LyXCursor const & a, LyXCursor const & b)
136 {
137         return (a.par() == b.par())
138                 && (a.pos() == b.pos())
139                 && a.boundary() == b.boundary();
140 }
141
142 inline
143 bool operator!=(LyXCursor const & a, LyXCursor const & b)
144 {
145         return !(a == b);
146 }
147
148 /// only compares y() and pos(). Can this be done in another way?
149 inline
150 bool operator<(LyXCursor const & a, LyXCursor const & b)
151 {
152         return (a.y() < b.y() && a.pos() < b.pos());
153 }
154
155 inline
156 bool operator>(LyXCursor const & a, LyXCursor const & b)
157 {
158         return b < a;
159 }
160
161 inline
162 bool operator>=(LyXCursor const & a, LyXCursor const & b)
163 {
164         return !(a < b);
165 }
166
167
168 inline
169 bool operator<=(LyXCursor const & a, LyXCursor const & b)
170 {
171         return !(a > b);
172 }
173
174 #endif // LYXCURSOR_H