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, LCursor const & cur)
38 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
39 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
45 : cursor_(1), anchor_(1), bv_(0)
49 LCursor::LCursor(BufferView & bv)
50 : cursor_(1), anchor_(1), bv_(&bv)
54 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
56 lyxerr << "\nLCursor::dispatch: " << *this << endl;
57 FuncRequest cmd = cmd0;
59 for (int i = cursor_.size() - 1; i >= 1; --i) {
60 CursorSlice const & citem = cursor_[i];
61 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
62 DispatchResult res = citem.inset_->dispatch(*bv_, cmd);
63 if (res.dispatched()) {
64 lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
65 return DispatchResult(true, true);
67 // remove one level of cursor
71 cmd = FuncRequest(LFUN_FINISHED_LEFT);
75 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
79 cmd = FuncRequest(LFUN_FINISHED_UP);
83 cmd = FuncRequest(LFUN_FINISHED_DOWN);
86 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
90 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
91 DispatchResult res = bv_->text()->dispatch(*bv_, cmd);
92 lyxerr << " result: " << res.val() << endl;
94 if (!res.dispatched()) {
95 lyxerr << "trying to dispatch to bv " << bv_ << endl;
96 bool sucess = bv_->dispatch(cmd);
97 lyxerr << " result: " << sucess << endl;
98 res.dispatched(sucess);
105 void LCursor::push(InsetBase * inset)
107 lyxerr << "LCursor::push() inset: " << inset << endl;
108 cursor_.push_back(CursorSlice(inset));
109 anchor_.push_back(CursorSlice(inset));
115 void LCursor::pop(int depth)
117 lyxerr << "LCursor::pop() to " << depth << endl;
118 while (cursor_.size() > 1 && depth < cursor_.size()) {
119 lyxerr << "LCursor::pop a level " << endl;
128 lyxerr << "LCursor::pop() " << endl;
129 //BOOST_ASSERT(!cursor_.empty());
130 if (cursor_.size() <= 1)
131 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
139 UpdatableInset * LCursor::innerInset() const
141 return cursor_.size() <= 1 ? 0 : cursor_.back().asUpdatableInset();
145 LyXText * LCursor::innerText() const
147 if (cursor_.size() > 1) {
148 // go up until first non-0 text is hit
149 // (innermost text is 0 e.g. for mathed and the outer tabular level)
150 for (int i = cursor_.size() - 1; i >= 1; --i)
151 if (cursor_[i].text())
152 return cursor_[i].text();
158 void LCursor::updatePos()
160 if (cursor_.size() > 1)
161 cached_y_ = bv_->top_y() + cursor_.back().inset()->y();
165 void LCursor::getDim(int & asc, int & desc) const
167 LyXText * txt = innerText();
170 Row const & row = *txt->cursorRow();
171 asc = row.baseline();
172 desc = row.height() - asc;
174 innerInset()->getCursorDim(asc, desc);
179 void LCursor::getPos(int & x, int & y) const
181 if (cursor_.size() <= 1) {
182 x = bv_->text()->cursorX();
183 y = bv_->text()->cursorY();
184 // y -= bv_->top_y();
186 // Would be nice to clean this up to make some understandable sense...
187 UpdatableInset * inset = innerInset();
188 // Non-obvious. The reason we have to have these
189 // extra checks is that the ->getCursor() calls rely
190 // on the inset's own knowledge of its screen position.
191 // If we scroll up or down in a big enough increment, the
192 // inset->draw() is not called: this doesn't update
193 // inset.top_baseline, so getCursor() returns an old value.
196 inset->getCursorPos(cursor_.back().idx_, x, y);
204 UpdatableInset * LCursor::innerInsetOfType(int code) const
206 for (int i = cursor_.size() - 1; i >= 1; --i)
207 if (cursor_[i].asUpdatableInset()->lyxCode() == code)
208 return cursor_[i].asUpdatableInset();
213 InsetTabular * LCursor::innerInsetTabular() const
215 return static_cast<InsetTabular *>
216 (innerInsetOfType(InsetOld::TABULAR_CODE));
220 void LCursor::cell(int idx)
222 BOOST_ASSERT(!cursor_.empty());
223 cursor_.back().idx_ = idx;
227 int LCursor::cell() const
229 BOOST_ASSERT(!cursor_.empty());
230 return cursor_.back().idx_;
234 void LCursor::resetAnchor()