]> git.lyx.org Git - lyx.git/blob - src/cursor.C
Overhaul the branches code.
[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, CursorItem const & item)
36 {
37         os << " inset: " << item.inset_
38            << " code: " << item.inset_->lyxCode()
39            << " text: " << item.text()
40            << " idx: " << item.idx_
41 //         << " par: " << item.par_
42 //         << " pos: " << item.pos_
43            << " x: " << item.inset_->x()
44            << " y: " << item.inset_->y()
45 ;
46         return os;
47 }
48
49
50 LyXText * CursorItem::text() const
51 {
52         return inset_->getText(0);
53 }
54
55
56 std::ostream & operator<<(std::ostream & os, LCursor const & cursor)
57 {
58         os << "\n";
59         for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
60                 os << "   " << cursor.data_[i] << "\n";
61         return os;
62 }
63
64
65 LCursor::LCursor(BufferView * bv)
66         : bv_(bv)
67 {}
68
69
70 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
71 {
72         lyxerr << "\nLCursor::dispatch: " << *this << endl;
73         FuncRequest cmd = cmd0;
74
75         for (int i = data_.size() - 1; i >= 0; --i) {
76                 CursorItem const & citem = data_[i];
77                 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
78                 DispatchResult res = citem.inset_->dispatch(cmd);
79                 if (res.dispatched()) {
80                         lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
81                         return DispatchResult(true, true);
82                 }
83                 // remove one level of cursor
84                 switch (res.val()) {
85                         case FINISHED:
86                                 pop(i);
87                                 cmd = FuncRequest(bv_, LFUN_FINISHED_LEFT);
88                                 break;
89                         case FINISHED_RIGHT:
90                                 pop(i);
91                                 cmd = FuncRequest(bv_, LFUN_FINISHED_RIGHT);
92                                 break;
93                         case FINISHED_UP:
94                                 pop(i);
95                                 cmd = FuncRequest(bv_, LFUN_FINISHED_UP);
96                                 break;
97                         case FINISHED_DOWN:
98                                 pop(i);
99                                 cmd = FuncRequest(bv_, LFUN_FINISHED_DOWN);
100                                 break;
101                         default:
102                                 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
103                                 break;
104                 }
105         }
106         lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
107         DispatchResult res = bv_->text()->dispatch(cmd);
108         lyxerr << "   result: " << res.val() << endl;
109
110         if (!res.dispatched()) {
111                 lyxerr << "trying to dispatch to bv " << bv_ << endl;
112                 bool sucess = bv_->dispatch(cmd);
113                 lyxerr << "   result: " << sucess << endl;
114                 res.dispatched(sucess);
115         }
116
117         return res;
118 }
119
120
121 void LCursor::push(UpdatableInset * inset)
122 {
123         lyxerr << "LCursor::push()  inset: " << inset << endl;
124         data_.push_back(CursorItem(inset));
125         updatePos();
126 }
127
128
129 void LCursor::pop(int depth)
130 {
131         lyxerr << "LCursor::pop() to " << depth << endl;
132         while (depth < data_.size()) {
133                 lyxerr <<   "LCursor::pop a level " << endl;
134                 data_.pop_back();
135         }
136 }
137
138
139 void LCursor::pop()
140 {
141         lyxerr << "LCursor::pop() " << endl;
142         //BOOST_ASSERT(!data_.empty());
143         if (data_.empty())
144                 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
145         else
146                 data_.pop_back();
147 }
148
149
150 UpdatableInset * LCursor::innerInset() const
151 {
152         return data_.empty() ? 0 : data_.back().inset_;
153 }
154
155
156 LyXText * LCursor::innerText() const
157 {
158         if (!data_.empty()) {
159                 // go up until first non-0 text is hit
160                 // (innermost text is 0 e.g. for mathed and the outer tabular level)
161                 for (int i = data_.size() - 1; i >= 0; --i)
162                         if (data_[i].text())
163                                 return data_[i].text();
164         }
165         return bv_->text();
166 }
167
168
169 void LCursor::updatePos()
170 {
171         if (!data_.empty())
172                 cached_y_ = bv_->top_y() + innerInset()->y();
173 }
174
175
176 void LCursor::getDim(int & asc, int & desc) const
177 {
178         LyXText * txt = innerText();
179
180         if (txt) {
181                 Row const & row = *txt->cursorRow();
182                 asc = row.baseline();
183                 desc = row.height() - asc;
184         } else
185                 innerInset()->getCursorDim(asc, desc);
186 }
187
188
189 void LCursor::getPos(int & x, int & y) const
190 {
191         if (data_.empty()) {
192                 x = bv_->text()->cursor.x();
193                 y = bv_->text()->cursor.y();
194 //              y -= bv_->top_y();
195         } else {
196                 // Would be nice to clean this up to make some understandable sense...
197                 UpdatableInset * inset = innerInset();
198                 // Non-obvious. The reason we have to have these
199                 // extra checks is that the ->getCursor() calls rely
200                 // on the inset's own knowledge of its screen position.
201                 // If we scroll up or down in a big enough increment, the
202                 // inset->draw() is not called: this doesn't update
203                 // inset.top_baseline, so getCursor() returns an old value.
204                 // Ugly as you like.
205                 //inset->getCursorPos(bv_, x, y);
206                 inset->getCursorPos(x, y);
207                 x += inset->x();
208                 y += cached_y_;
209         }
210 }
211
212
213 UpdatableInset * LCursor::innerInsetOfType(int code) const
214 {
215         for (int i = data_.size() - 1; i >= 0; --i)
216                 if (data_[i].inset_->lyxCode() == code)
217                         return data_[i].inset_;
218         return 0;
219 }
220
221
222 InsetTabular * LCursor::innerInsetTabular() const
223 {
224         return static_cast<InsetTabular *>
225                 (innerInsetOfType(InsetOld::TABULAR_CODE));
226 }