3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "iterators.h"
22 #include "paragraph.h"
25 #include "insets/updatableinset.h"
26 #include "insets/insettabular.h"
27 #include "insets/insettext.h"
29 #include <boost/assert.hpp>
35 std::ostream & operator<<(std::ostream & os, CursorItem const & item)
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()
48 LyXText * CursorItem::text() const
50 return inset_->getText(0);
54 std::ostream & operator<<(std::ostream & os, LCursor const & cursor)
57 for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
58 os << " " << cursor.data_[i] << "\n";
63 LCursor::LCursor(BufferView * bv)
68 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
70 lyxerr << "\nLCursor::dispatch: " << *this << endl;
71 FuncRequest cmd = cmd0;
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);
81 // remove one level of cursor
85 cmd = FuncRequest(bv_, LFUN_FINISHED_LEFT);
89 cmd = FuncRequest(bv_, LFUN_FINISHED_RIGHT);
93 cmd = FuncRequest(bv_, LFUN_FINISHED_UP);
97 cmd = FuncRequest(bv_, LFUN_FINISHED_DOWN);
100 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
104 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
105 DispatchResult res = bv_->text()->dispatch(cmd);
106 lyxerr << " result: " << res.val() << endl;
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);
119 void LCursor::push(UpdatableInset * inset)
121 lyxerr << "LCursor::push() inset: " << inset << endl;
122 data_.push_back(CursorItem(inset));
127 void LCursor::pop(int depth)
129 lyxerr << "LCursor::pop() to " << depth << endl;
130 while (depth < data_.size()) {
131 lyxerr << "LCursor::pop a level " << endl;
139 lyxerr << "LCursor::pop() " << endl;
140 //BOOST_ASSERT(!data_.empty());
142 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
148 UpdatableInset * LCursor::innerInset() const
150 return data_.empty() ? 0 : data_.back().inset_;
154 LyXText * LCursor::innerText() const
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)
161 return data_[i].text();
167 void LCursor::updatePos()
170 cached_y_ = bv_->top_y() + innerInset()->y();
174 void LCursor::getDim(int & asc, int & desc) const
176 LyXText * txt = innerText();
179 Row const & row = *txt->cursorRow();
180 asc = row.baseline();
181 desc = row.height() - asc;
183 innerInset()->getCursorDim(asc, desc);
187 void LCursor::getPos(int & x, int & y) const
190 x = bv_->text()->cursor.x();
191 y = bv_->text()->cursor.y();
192 // y -= bv_->top_y();
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.
203 //inset->getCursorPos(bv_, x, y);
204 inset->getCursorPos(x, y);
211 UpdatableInset * LCursor::innerInsetOfType(int code) const
213 for (int i = data_.size() - 1; i >= 0; --i)
214 if (data_[i].inset_->lyxCode() == code)
215 return data_[i].inset_;
220 InsetTabular * LCursor::innerInsetTabular() const
222 return static_cast<InsetTabular *>
223 (innerInsetOfType(InsetOld::TABULAR_CODE));