]> git.lyx.org Git - features.git/blob - src/cursor.C
more global cursor work
[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         os << "\n";
45         for (size_t i = 0, n = cursor.data_.size(); i != n; ++i)
46                 os << "   " << cursor.data_[i] << "\n";
47         return os;
48 }
49
50
51 DispatchResult LCursor::dispatch(FuncRequest const & cmd)
52 {
53         for (int i = data_.size() - 1; i >= 0; --i) {
54                 CursorItem const & citem = data_[i];
55                 lyxerr << "trying to dispatch to inset" << citem.inset_ << endl;
56                 DispatchResult res = citem.inset_->dispatch(cmd);
57                 if (handleResult(res))
58                         return DispatchResult(true, true);
59         }
60         lyxerr << "trying to dispatch to main text " << bv_->text << endl;
61         DispatchResult res = bv_->text->dispatch(cmd);
62         lyxerr << "   result: " << res.val() << endl;
63         return res;
64 }
65
66
67 bool LCursor::handleResult(DispatchResult const & res)
68 {
69         lyxerr << "LCursor::handleResult: " << res.val() << endl;
70         switch (res.val()) {
71                 case FINISHED:
72                         ///pop();
73                         return true;
74
75                 case FINISHED_RIGHT: {
76                         ///pop();
77                         //InsetText * inset = static_cast<InsetText *>(innerInset());
78                         //if (inset)
79                         //      inset->moveRightIntern(bv_, false, false);
80                         //else
81                         //      bv_->text->cursorRight(bv_);
82                         innerText()->cursorRight(bv_);
83                         return true;
84                 }
85
86                 case FINISHED_UP: {
87                         ///pop();
88                         //InsetText * inset = static_cast<InsetText *>(inset());
89                         //if (inset)
90                         //      result = inset->moveUp(bv);
91                         return true;
92                 }
93
94                 case FINISHED_DOWN: {
95                         ///pop();
96                         //InsetText * inset = static_cast<InsetText *>(inset());
97                         //if (inset)
98                         //      result = inset->moveDown(bv);
99                         return true;
100                 }
101
102                 default:
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         lyxerr << "LCursor::push()  inset: " << inset << " text: " << text
118                 << endl;
119         data_.push_back(CursorItem(inset, text));
120 }
121
122
123 void LCursor::pop()
124 {
125         lyxerr << "LCursor::pop() " << endl;
126         //BOOST_ASSERT(!data_.empty());
127         if (data_.empty())
128                 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
129         else
130                 data_.pop_back();
131 }
132
133
134 InsetOld * LCursor::innerInset() const
135 {
136         return data_.empty() ? 0 : data_.back().inset_;
137 }
138
139
140 LyXText * LCursor::innerText() const
141 {
142         return data_.empty() ? bv_->text : data_.back().text_;
143 }