]> git.lyx.org Git - lyx.git/blob - src/cursor.C
some renaming + safety stuff
[lyx.git] / src / cursor.C
1 /**
2  * \file cursor.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "buffer.h"
14 #include "BufferView.h"
15 #include "cursor.h"
16 #include "debug.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "iterators.h"
20 #include "lfuns.h"
21 #include "lyxtext.h"
22 #include "paragraph.h"
23 #include "lyxrow.h"
24
25 #include "insets/updatableinset.h"
26 #include "insets/insettabular.h"
27 #include "insets/insettext.h"
28
29 #include <boost/assert.hpp>
30
31 using std::vector;
32 using std::endl;
33
34
35 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
36 {
37         os << "\n";
38         for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
39                 os << "   (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
40         return os;
41 }
42
43
44 LCursor::LCursor(BufferView * bv)
45         : cursor_(1), anchor_(1), bv_(bv)
46 {}
47
48
49 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
50 {
51         lyxerr << "\nLCursor::dispatch: " << *this << endl;
52         FuncRequest cmd = cmd0;
53
54         for (int i = cursor_.size() - 1; i >= 1; --i) {
55                 CursorSlice const & citem = cursor_[i];
56                 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
57                 DispatchResult res = citem.inset_->dispatch(cmd);
58                 if (res.dispatched()) {
59                         lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
60                         return DispatchResult(true, true);
61                 }
62                 // remove one level of cursor
63                 switch (res.val()) {
64                         case FINISHED:
65                                 pop(i);
66                                 cmd = FuncRequest(bv_, LFUN_FINISHED_LEFT);
67                                 break;
68                         case FINISHED_RIGHT:
69                                 pop(i);
70                                 cmd = FuncRequest(bv_, LFUN_FINISHED_RIGHT);
71                                 break;
72                         case FINISHED_UP:
73                                 pop(i);
74                                 cmd = FuncRequest(bv_, LFUN_FINISHED_UP);
75                                 break;
76                         case FINISHED_DOWN:
77                                 pop(i);
78                                 cmd = FuncRequest(bv_, LFUN_FINISHED_DOWN);
79                                 break;
80                         default:
81                                 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
82                                 break;
83                 }
84         }
85         lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
86         DispatchResult res = bv_->text()->dispatch(cmd);
87         lyxerr << "   result: " << res.val() << endl;
88
89         if (!res.dispatched()) {
90                 lyxerr << "trying to dispatch to bv " << bv_ << endl;
91                 bool sucess = bv_->dispatch(cmd);
92                 lyxerr << "   result: " << sucess << endl;
93                 res.dispatched(sucess);
94         }
95
96         return res;
97 }
98
99
100 void LCursor::push(UpdatableInset * inset)
101 {
102         lyxerr << "LCursor::push()  inset: " << inset << endl;
103         cursor_.push_back(CursorSlice(inset));
104         anchor_.push_back(CursorSlice(inset));
105         updatePos();
106 }
107
108
109
110 void LCursor::pop(int depth)
111 {
112         lyxerr << "LCursor::pop() to " << depth << endl;
113         while (cursor_.size() > 1 && depth < cursor_.size()) {
114                 lyxerr <<   "LCursor::pop a level " << endl;
115                 cursor_.pop_back();
116                 anchor_.pop_back();
117         }
118 }
119
120
121 void LCursor::pop()
122 {
123         lyxerr << "LCursor::pop() " << endl;
124         //BOOST_ASSERT(!cursor_.empty());
125         if (cursor_.size() <= 1)
126                 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
127         else {
128                 cursor_.pop_back();
129                 anchor_.pop_back();
130         }
131 }
132
133
134 UpdatableInset * LCursor::innerInset() const
135 {
136         return cursor_.size() <= 1 ? 0 : cursor_.back().asUpdatableInset();
137 }
138
139
140 LyXText * LCursor::innerText() const
141 {
142         if (cursor_.size() > 1) {
143                 // go up until first non-0 text is hit
144                 // (innermost text is 0 e.g. for mathed and the outer tabular level)
145                 for (int i = cursor_.size() - 1; i >= 1; --i)
146                         if (cursor_[i].text())
147                                 return cursor_[i].text();
148         }
149         return bv_->text();
150 }
151
152
153 void LCursor::updatePos()
154 {
155         if (cursor_.size() > 1)
156                 cached_y_ = bv_->top_y() + innerInset()->y();
157 }
158
159
160 void LCursor::getDim(int & asc, int & desc) const
161 {
162         LyXText * txt = innerText();
163
164         if (txt) {
165                 Row const & row = *txt->cursorRow();
166                 asc = row.baseline();
167                 desc = row.height() - asc;
168         } else
169                 innerInset()->getCursorDim(asc, desc);
170 }
171
172
173 void LCursor::getPos(int & x, int & y) const
174 {
175         if (cursor_.size() <= 1) {
176                 x = bv_->text()->cursorX();
177                 y = bv_->text()->cursorY();
178 //              y -= bv_->top_y();
179         } else {
180                 // Would be nice to clean this up to make some understandable sense...
181                 UpdatableInset * inset = innerInset();
182                 // Non-obvious. The reason we have to have these
183                 // extra checks is that the ->getCursor() calls rely
184                 // on the inset's own knowledge of its screen position.
185                 // If we scroll up or down in a big enough increment, the
186                 // inset->draw() is not called: this doesn't update
187                 // inset.top_baseline, so getCursor() returns an old value.
188                 // Ugly as you like.
189                 inset->getCursorPos(cursor_.back().idx_, x, y);
190                 x += inset->x();
191                 y += cached_y_;
192         }
193 }
194
195
196 UpdatableInset * LCursor::innerInsetOfType(int code) const
197 {
198         for (int i = cursor_.size() - 1; i >= 1; --i)
199                 if (cursor_[i].asUpdatableInset()->lyxCode() == code)
200                         return cursor_[i].asUpdatableInset();
201         return 0;
202 }
203
204
205 InsetTabular * LCursor::innerInsetTabular() const
206 {
207         return static_cast<InsetTabular *>
208                 (innerInsetOfType(InsetOld::TABULAR_CODE));
209 }
210
211
212 void LCursor::cell(int idx)
213 {
214         BOOST_ASSERT(!cursor_.empty());
215         cursor_.back().idx_ = idx;
216 }
217
218
219 int LCursor::cell() const
220 {
221         BOOST_ASSERT(!cursor_.empty());
222         return cursor_.back().idx_;
223 }
224
225
226 void LCursor::resetAnchor()
227 {
228         anchor_ = cursor_;
229 }