]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
don't forget <config.h>...
[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,
77                           bool/*cleared*/) const
78 {
79         x += float(scx);
80         // ATTENTION: don't do the following here!!!
81         //    top_x = int(x);
82         //    top_baseline = baseline;
83 }
84
85
86 void UpdatableInset::scroll(BufferView * bv, float s) const
87 {
88         LyXFont font;
89
90         if (!s) {
91                 scx = 0;
92                 return;
93         }
94
95         int const workW = bv->workWidth();
96         int const tmp_top_x = top_x - scx;
97
98         if (tmp_top_x > 0 &&
99             (tmp_top_x + width(bv, font)) < workW)
100                 return;
101         if (s > 0 && top_x > 0)
102                 return;
103
104         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
105         //int const save_scx = scx;
106
107         scx = int(s * workW / 2);
108         // if (!display())
109         // scx += 20;
110
111         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
112                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
113         }
114
115         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
116 }
117
118 void UpdatableInset::scroll(BufferView * bv, int offset) const
119 {
120         if (offset > 0) {
121                 if (!scx && top_x >= 20)
122                         return;
123                 if ((top_x + offset) > 20)
124                         scx = 0;
125                 // scx += offset - (top_x - scx + offset - 20);
126                 else
127                         scx += offset;
128         } else {
129                 LyXFont const font;
130                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
131                         return;
132                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
133                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
134                 } else {
135                         scx += offset;
136                 }
137         }
138 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
139 }
140
141
142 ///  An updatable inset could handle lyx editing commands
143 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
144 {
145         if (ev.action == LFUN_MOUSE_RELEASE)
146                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
147
148         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
149                 if (ev.argument.find('.') != ev.argument.npos) {
150                         float const xx = static_cast<float>(strToDbl(ev.argument));
151                         scroll(ev.view(), xx);
152                 } else {
153                         int const xx = strToInt(ev.argument);
154                         scroll(ev.view(), xx);
155                 }
156                 ev.view()->updateInset(this, false);
157
158                 return DISPATCHED;
159         }
160         return UNDISPATCHED;
161 }
162
163
164 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
165 {
166         int w;
167         if (owner()) {
168                 w = static_cast<UpdatableInset*>
169                         (owner())->getMaxWidth(bv, this);
170         } else {
171                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
172         }
173         if (w < 0) {
174                 return -1;
175         }
176         // check for margins left/right and extra right margin "const 5"
177         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
178                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
179         if (topx_set && owner()) {
180                 w -= top_x;
181                 w += owner()->x();
182         } else if (owner()) {
183                 // this is needed as otherwise we might have a too large inset if
184                 // its top_x afterwards changes to LeftMargin so we try to put at
185                 // least the default margin as top_x
186                 w -= 20;
187         }
188         if (w < 10) {
189                 w = 10;
190         }
191         return w;
192 }
193
194
195 LyXCursor const & Inset::cursor(BufferView * bv) const
196 {
197         if (owner())
198                 return owner()->getLyXText(bv, false)->cursor;
199         return bv->text->cursor;
200 }
201
202
203 WordLangTuple const
204 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
205 {
206         // we have to unlock ourself in this function by default!
207         bv->unlockInset(const_cast<UpdatableInset *>(this));
208         value = 0;
209         return WordLangTuple();
210 }
211
212
213 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
214 {
215         // we have to unlock ourself in this function by default!
216         bv->unlockInset(const_cast<UpdatableInset *>(this));
217         return false;
218 }
219
220  
221 bool UpdatableInset::searchForward(BufferView * bv, string const &,
222                                    bool, bool)
223 {
224         // we have to unlock ourself in this function by default!
225         bv->unlockInset(const_cast<UpdatableInset *>(this));
226         return false;
227 }
228
229
230 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
231                                     bool, bool)
232 {
233         // we have to unlock ourself in this function by default!
234         bv->unlockInset(const_cast<UpdatableInset *>(this));
235         return false;
236 }