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