]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
The markDirty() and fitCursor() changes
[lyx.git] / src / insets / updatableinset.C
1 /**
2  * \file updatableinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Matthias Ettrich
10  *
11  * Full author contact details are available in file CREDITS
12  */
13
14 #include <config.h>
15
16 #include "updatableinset.h"
17 #include "BufferView.h"
18 #include "funcrequest.h"
19 #include "debug.h"
20 #include "lyxfont.h"
21 #include "WordLangTuple.h"
22 #include "lyxtext.h"
23 #include "support/lstrings.h"
24
25
26 // some stuff for inset locking
27
28 UpdatableInset::UpdatableInset()
29         : Inset(), cursor_visible_(false), block_drawing_(false)
30 {}
31
32
33 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
34         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
35 {}
36
37
38 void UpdatableInset::insetUnlock(BufferView *)
39 {
40         lyxerr[Debug::INFO] << "Inset Unlock" << std::endl;
41 }
42
43
44 // An updatable inset is highly editable by definition
45 Inset::EDITABLE UpdatableInset::editable() const
46 {
47         return HIGHLY_EDITABLE;
48 }
49
50
51 void UpdatableInset::toggleInsetCursor(BufferView *)
52 {}
53
54
55 void UpdatableInset::showInsetCursor(BufferView *, bool)
56 {}
57
58
59 void UpdatableInset::hideInsetCursor(BufferView *)
60 {}
61
62
63 void UpdatableInset::fitInsetCursor(BufferView *) const
64 {}
65
66
67 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
68 {}
69
70
71 void UpdatableInset::edit(BufferView *, bool)
72 {}
73
74
75 void UpdatableInset::draw(BufferView *, LyXFont const &,
76                           int /* baseline */, float & x) const
77 {
78         x += float(scx);
79         // ATTENTION: don't do the following here!!!
80         //    top_x = int(x);
81         //    top_baseline = baseline;
82 }
83
84
85 void UpdatableInset::scroll(BufferView * bv, float s) const
86 {
87         LyXFont font;
88
89         if (!s) {
90                 scx = 0;
91                 return;
92         }
93
94         int const workW = bv->workWidth();
95         int const tmp_top_x = top_x - scx;
96
97         if (tmp_top_x > 0 &&
98             (tmp_top_x + width(bv, font)) < workW)
99                 return;
100         if (s > 0 && top_x > 0)
101                 return;
102
103         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
104         //int const save_scx = scx;
105
106         scx = int(s * workW / 2);
107         // if (!display())
108         // scx += 20;
109
110         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
111                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
112         }
113 }
114
115 void UpdatableInset::scroll(BufferView * bv, int offset) const
116 {
117         if (offset > 0) {
118                 if (!scx && top_x >= 20)
119                         return;
120                 if ((top_x + offset) > 20)
121                         scx = 0;
122                 // scx += offset - (top_x - scx + offset - 20);
123                 else
124                         scx += offset;
125         } else {
126                 LyXFont const font;
127                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
128                         return;
129                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
130                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
131                 } else {
132                         scx += offset;
133                 }
134         }
135 }
136
137
138 ///  An updatable inset could handle lyx editing commands
139 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
140 {
141         if (ev.action == LFUN_MOUSE_RELEASE)
142                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
143
144         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
145                 if (ev.argument.find('.') != ev.argument.npos) {
146                         float const xx = static_cast<float>(strToDbl(ev.argument));
147                         scroll(ev.view(), xx);
148                 } else {
149                         int const xx = strToInt(ev.argument);
150                         scroll(ev.view(), xx);
151                 }
152                 ev.view()->updateInset(this);
153
154                 return DISPATCHED;
155         }
156         return UNDISPATCHED;
157 }
158
159
160 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
161 {
162         int w;
163         if (owner()) {
164                 w = static_cast<UpdatableInset*>
165                         (owner())->getMaxWidth(bv, this);
166         } else {
167                 w = bv->text->workWidth(const_cast<UpdatableInset *>(this));
168         }
169         if (w < 0) {
170                 return -1;
171         }
172         // check for margins left/right and extra right margin "const 5"
173         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
174                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
175         if (topx_set && owner()) {
176                 w -= top_x;
177                 w += owner()->x();
178         } else if (owner()) {
179                 // this is needed as otherwise we might have a too large inset if
180                 // its top_x afterwards changes to LeftMargin so we try to put at
181                 // least the default margin as top_x
182                 w -= 20;
183         }
184         if (w < 10) {
185                 w = 10;
186         }
187         return w;
188 }
189
190
191 LyXCursor const & Inset::cursor(BufferView * bv) const
192 {
193         if (owner())
194                 return owner()->getLyXText(bv, false)->cursor;
195         return bv->text->cursor;
196 }
197
198
199 WordLangTuple const
200 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
201 {
202         // we have to unlock ourself in this function by default!
203         bv->unlockInset(const_cast<UpdatableInset *>(this));
204         value = 0;
205         return WordLangTuple();
206 }
207
208
209 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
210 {
211         // we have to unlock ourself in this function by default!
212         bv->unlockInset(const_cast<UpdatableInset *>(this));
213         return false;
214 }
215
216
217 bool UpdatableInset::searchForward(BufferView * bv, string const &,
218                                    bool, bool)
219 {
220         // we have to unlock ourself in this function by default!
221         bv->unlockInset(const_cast<UpdatableInset *>(this));
222         return false;
223 }
224
225
226 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
227                                     bool, bool)
228 {
229         // we have to unlock ourself in this function by default!
230         bv->unlockInset(const_cast<UpdatableInset *>(this));
231         return false;
232 }