]> git.lyx.org Git - features.git/blob - src/cursor.C
fix a few 'OUT OF SYNC'
[features.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 "iterators.h"
19 #include "lyxtext.h"
20 #include "paragraph.h"
21
22 #include "insets/updatableinset.h"
23 #include "insets/insettext.h"
24
25 #include <boost/assert.hpp>
26
27 using std::vector;
28 using std::endl;
29
30
31 std::ostream & operator<<(std::ostream & os, CursorItem const & item)
32 {
33         os << " inset: " << item.inset_
34            << " idx: " << item.idx_
35            << " text: " << item.text_
36            << " par: " << item.par_
37            << " pos: " << item.pos_;
38         return os;
39 }
40
41
42 std::ostream & operator<<(std::ostream & os, LCursor const & cursor)
43 {
44         for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
45                 os << "   " << cursor.data_[i];
46         return os;
47 }
48
49
50 DispatchResult LCursor::dispatch(FuncRequest const & cmd)
51 {
52         for (int i = data_.size() - 1; i >= 0; --i) {
53                 CursorItem const & citem = data_[i];
54                 lyxerr << "trying to dispatch to inset" << citem.inset_ << endl;
55                 DispatchResult res = citem.inset_->dispatch(cmd);
56                 if (handleResult(res))
57                         return DispatchResult(true, true);
58         }
59         lyxerr << "trying to dispatch to main text " << bv_->text << endl;
60         DispatchResult res = bv_->text->dispatch(cmd);
61         lyxerr << "   result: " << res.val() << endl;
62         return res;
63 }
64
65
66 bool LCursor::handleResult(DispatchResult const & res)
67 {
68         lyxerr << "LCursor::handleResult: " << res.val() << endl;
69         switch (res.val()) {
70                 case FINISHED:
71                         pop();
72                         return true;
73
74                 case FINISHED_RIGHT: {
75                         pop();
76                         //InsetText * inset = static_cast<InsetText *>(innerInset());
77                         //if (inset)
78                         //      inset->moveRightIntern(bv_, false, false);
79                         //else
80                         //      bv_->text->cursorRight(bv_);
81                         innerText()->cursorRight(bv_);
82                         return true;
83                 }
84
85                 case FINISHED_UP: {
86                         pop();
87                         //InsetText * inset = static_cast<InsetText *>(inset());
88                         //if (inset)
89                         //      result = inset->moveUp(bv);
90                         return true;
91                 }
92
93                 case FINISHED_DOWN: {
94                         pop();
95                         //InsetText * inset = static_cast<InsetText *>(inset());
96                         //if (inset)
97                         //      result = inset->moveDown(bv);
98                         return true;
99                 }
100
101                 default:
102                         break;
103                         lyxerr << "# unhandled result: " << res.val() << endl;
104                         return false;
105         }
106 }
107
108
109
110 LCursor::LCursor(BufferView * bv)
111         : bv_(bv)
112 {}
113
114
115 void LCursor::push(InsetOld * inset, LyXText * text)
116 {
117         data_.push_back(CursorItem(inset, text));
118 }
119
120
121 void LCursor::pop()
122 {
123         //BOOST_ASSERT(!data_.empty());
124         if (data_.empty())
125                 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
126         else
127                 data_.pop_back();
128 }
129
130
131 InsetOld * LCursor::innerInset() const
132 {
133         return data_.empty() ? 0 : data_.back().inset_;
134 }
135
136
137 LyXText * LCursor::innerText() const
138 {
139         return data_.empty() ? bv_->text : data_.back().text_;
140 }