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 // << " par: " << item.par_
41 // << " pos: " << item.pos_
42 << " x: " << item.inset_->x()
43 << " y: " << item.inset_->y()
49 LyXText * CursorItem::text() const
51 return inset_->getText(0);
55 std::ostream & operator<<(std::ostream & os, LCursor const & cursor)
58 for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
59 os << " " << cursor.data_[i] << "\n";
64 LCursor::LCursor(BufferView * bv)
69 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
71 lyxerr << "\nLCursor::dispatch: " << *this << endl;
72 FuncRequest cmd = cmd0;
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);
82 // remove one level of cursor
86 cmd = FuncRequest(bv_, LFUN_FINISHED_LEFT);
90 cmd = FuncRequest(bv_, LFUN_FINISHED_RIGHT);
94 cmd = FuncRequest(bv_, LFUN_FINISHED_UP);
98 cmd = FuncRequest(bv_, LFUN_FINISHED_DOWN);
101 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
105 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
106 DispatchResult res = bv_->text()->dispatch(cmd);
107 lyxerr << " result: " << res.val() << endl;
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);
120 void LCursor::push(UpdatableInset * inset)
122 lyxerr << "LCursor::push() inset: " << inset << endl;
123 data_.push_back(CursorItem(inset));
128 void LCursor::pop(int depth)
130 lyxerr << "LCursor::pop() to " << depth << endl;
131 while (depth < data_.size()) {
132 lyxerr << "LCursor::pop a level " << endl;
140 lyxerr << "LCursor::pop() " << endl;
141 //BOOST_ASSERT(!data_.empty());
143 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
149 UpdatableInset * LCursor::innerInset() const
151 return data_.empty() ? 0 : data_.back().inset_;
155 LyXText * LCursor::innerText() const
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)
162 return data_[i].text();
168 void LCursor::updatePos()
171 cached_y_ = bv_->top_y() + innerInset()->y();
175 void LCursor::getDim(int & asc, int & desc) const
177 LyXText * txt = innerText();
180 Row const & row = *txt->cursorRow();
181 asc = row.baseline();
182 desc = row.height() - asc;
184 innerInset()->getCursorDim(asc, desc);
188 void LCursor::getPos(int & x, int & y) const
191 x = bv_->text()->cursor.x();
192 y = bv_->text()->cursor.y();
193 // y -= bv_->top_y();
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.
204 //inset->getCursorPos(bv_, x, y);
205 inset->getCursorPos(x, y);
212 UpdatableInset * LCursor::innerInsetOfType(int code) const
214 for (int i = data_.size() - 1; i >= 0; --i)
215 if (data_[i].inset_->lyxCode() == code)
216 return data_[i].inset_;
221 InsetTabular * LCursor::innerInsetTabular() const
223 return static_cast<InsetTabular *>
224 (innerInsetOfType(InsetOld::TABULAR_CODE));