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