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