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 << " 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()
50 LyXText * CursorItem::text() const
52 return inset_->getText(0);
56 std::ostream & operator<<(std::ostream & os, LCursor const & cursor)
59 for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
60 os << " " << cursor.data_[i] << "\n";
65 LCursor::LCursor(BufferView * bv)
70 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
72 lyxerr << "\nLCursor::dispatch: " << *this << endl;
73 FuncRequest cmd = cmd0;
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);
83 // remove one level of cursor
87 cmd = FuncRequest(bv_, LFUN_FINISHED_LEFT);
91 cmd = FuncRequest(bv_, LFUN_FINISHED_RIGHT);
95 cmd = FuncRequest(bv_, LFUN_FINISHED_UP);
99 cmd = FuncRequest(bv_, LFUN_FINISHED_DOWN);
102 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
106 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
107 DispatchResult res = bv_->text()->dispatch(cmd);
108 lyxerr << " result: " << res.val() << endl;
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);
121 void LCursor::push(UpdatableInset * inset)
123 lyxerr << "LCursor::push() inset: " << inset << endl;
124 data_.push_back(CursorItem(inset));
129 void LCursor::pop(int depth)
131 lyxerr << "LCursor::pop() to " << depth << endl;
132 while (depth < data_.size()) {
133 lyxerr << "LCursor::pop a level " << endl;
141 lyxerr << "LCursor::pop() " << endl;
142 //BOOST_ASSERT(!data_.empty());
144 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
150 UpdatableInset * LCursor::innerInset() const
152 return data_.empty() ? 0 : data_.back().inset_;
156 LyXText * LCursor::innerText() const
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)
163 return data_[i].text();
169 void LCursor::updatePos()
172 cached_y_ = bv_->top_y() + innerInset()->y();
176 void LCursor::getDim(int & asc, int & desc) const
178 LyXText * txt = innerText();
181 Row const & row = *txt->cursorRow();
182 asc = row.baseline();
183 desc = row.height() - asc;
185 innerInset()->getCursorDim(asc, desc);
189 void LCursor::getPos(int & x, int & y) const
192 x = bv_->text()->cursor.x();
193 y = bv_->text()->cursor.y();
194 // y -= bv_->top_y();
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.
205 //inset->getCursorPos(bv_, x, y);
206 inset->getCursorPos(x, y);
213 UpdatableInset * LCursor::innerInsetOfType(int code) const
215 for (int i = data_.size() - 1; i >= 0; --i)
216 if (data_[i].inset_->lyxCode() == code)
217 return data_[i].inset_;
222 InsetTabular * LCursor::innerInsetTabular() const
224 return static_cast<InsetTabular *>
225 (innerInsetOfType(InsetOld::TABULAR_CODE));