]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
e1a2e1a867a9381d76104816f79ab436822621e4
[lyx.git] / src / insets / inset.C
1 /**
2  * \file inset.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 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "inset.h"
21
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "lyxcursor.h"
27 #include "lyxfont.h"
28 #include "lyxtext.h"
29 #include "WordLangTuple.h"
30
31 #include "frontends/Painter.h"
32 #include "frontends/mouse_state.h"
33
34 #include "support/lstrings.h"
35 #include "support/lstrings.h"
36
37 using std::endl;
38
39 // Insets default methods
40
41 // Initialization of the counter for the inset id's,
42 unsigned int Inset::inset_id = 0;
43
44 Inset::Inset()
45         : top_x(0), topx_set(false), top_baseline(0), scx(0),
46           id_(inset_id++), owner_(0), par_owner_(0),
47           background_color_(LColor::inherit)
48 {}
49
50
51 Inset::Inset(Inset const & in, bool same_id)
52         : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
53           name_(in.name_), background_color_(in.background_color_)
54 {
55         if (same_id)
56                 id_ = in.id();
57         else
58                 id_ = inset_id++;
59 }
60
61
62 bool Inset::deletable() const
63 {
64         return true;
65 }
66
67
68 bool Inset::directWrite() const
69 {
70         return false;
71 }
72
73
74 Inset::EDITABLE Inset::editable() const
75 {
76         return NOT_EDITABLE;
77 }
78
79
80 void Inset::edit(BufferView *, int, int, mouse_button::state)
81 {}
82
83
84 void Inset::validate(LaTeXFeatures &) const
85 {}
86
87
88 bool Inset::autoDelete() const
89 {
90         return false;
91 }
92
93
94 void Inset::edit(BufferView *, bool)
95 {}
96
97
98 Inset::RESULT Inset::localDispatch(FuncRequest const &)
99 {
100         return UNDISPATCHED;
101 }
102
103
104 #if 0
105 LyXFont const Inset::convertFont(LyXFont const & font) const
106 {
107 #if 1
108         return font;
109 #else
110         return LyXFont(font);
111 #endif
112 }
113 #endif
114
115
116 string const Inset::editMessage() const
117 {
118         return _("Opened inset");
119 }
120
121
122 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
123 {
124         if (owner())
125                 return owner()->getLyXText(bv, false);
126         else
127                 return bv->text;
128 }
129
130
131 void Inset::setBackgroundColor(LColor::color color)
132 {
133         background_color_ = color;
134 }
135
136
137 LColor::color Inset::backgroundColor() const
138 {
139         if (background_color_ == LColor::inherit) {
140                 if (owner())
141                         return owner()->backgroundColor();
142                 else
143                         return LColor::background;
144         } else
145                 return background_color_;
146 }
147
148
149 int Inset::id() const
150 {
151         return id_;
152 }
153
154 void Inset::id(int id_arg)
155 {
156         id_ = id_arg;
157 }
158
159 void Inset::setFont(BufferView *, LyXFont const &, bool, bool)
160 {}
161
162
163 bool Inset::forceDefaultParagraphs(Inset const * in) const
164 {
165         if (owner())
166                 return owner()->forceDefaultParagraphs(in);
167         return false;
168 }
169
170 int Inset::latexTextWidth(BufferView * bv) const
171 {
172         if (owner())
173                 return (owner()->latexTextWidth(bv));
174         return bv->workWidth();
175 }
176
177 // some stuff for inset locking
178
179 UpdatableInset::UpdatableInset()
180         : Inset(), cursor_visible_(false), block_drawing_(false)
181 {}
182
183
184 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
185         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
186 {}
187
188
189 void UpdatableInset::insetUnlock(BufferView *)
190 {
191         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
192 }
193
194
195 // An updatable inset is highly editable by definition
196 Inset::EDITABLE UpdatableInset::editable() const
197 {
198         return HIGHLY_EDITABLE;
199 }
200
201
202 void UpdatableInset::toggleInsetCursor(BufferView *)
203 {}
204
205
206 void UpdatableInset::showInsetCursor(BufferView *, bool)
207 {}
208
209
210 void UpdatableInset::hideInsetCursor(BufferView *)
211 {}
212
213
214 void UpdatableInset::fitInsetCursor(BufferView *) const
215 {}
216
217
218 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
219 {}
220
221
222 void UpdatableInset::edit(BufferView *, bool)
223 {}
224
225
226 void UpdatableInset::draw(BufferView *, LyXFont const &,
227                           int /* baseline */, float & x,
228                           bool/*cleared*/) const
229 {
230         x += float(scx);
231         // ATTENTION: don't do the following here!!!
232         //    top_x = int(x);
233         //    top_baseline = baseline;
234 }
235
236
237 void UpdatableInset::scroll(BufferView * bv, float s) const
238 {
239         LyXFont font;
240
241         if (!s) {
242                 scx = 0;
243                 return;
244         }
245
246         int const workW = bv->workWidth();
247         int const tmp_top_x = top_x - scx;
248
249         if (tmp_top_x > 0 &&
250             (tmp_top_x + width(bv, font)) < workW)
251                 return;
252         if (s > 0 && top_x > 0)
253                 return;
254
255         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
256         //int const save_scx = scx;
257
258         scx = int(s * workW / 2);
259         // if (!display())
260         // scx += 20;
261
262         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
263                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
264         }
265
266         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
267 }
268
269 void UpdatableInset::scroll(BufferView * bv, int offset) const
270 {
271         if (offset > 0) {
272                 if (!scx && top_x >= 20)
273                         return;
274                 if ((top_x + offset) > 20)
275                         scx = 0;
276                 // scx += offset - (top_x - scx + offset - 20);
277                 else
278                         scx += offset;
279         } else {
280                 LyXFont const font;
281                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
282                         return;
283                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
284                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
285                 } else {
286                         scx += offset;
287                 }
288         }
289 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
290 }
291
292
293 ///  An updatable inset could handle lyx editing commands
294 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
295 {
296         if (ev.action == LFUN_MOUSE_RELEASE)
297                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
298                 
299         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
300                 if (ev.argument.find('.') != ev.argument.npos) {
301                         float const xx = static_cast<float>(strToDbl(ev.argument));
302                         scroll(ev.view(), xx);
303                 } else {
304                         int const xx = strToInt(ev.argument);
305                         scroll(ev.view(), xx);
306                 }
307                 ev.view()->updateInset(this, false);
308
309                 return DISPATCHED;
310         }
311         return UNDISPATCHED;
312 }
313
314
315 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
316 {
317         int w;
318         if (owner()) {
319                 w = static_cast<UpdatableInset*>
320                         (owner())->getMaxWidth(bv, this);
321         } else {
322                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
323         }
324         if (w < 0) {
325                 return -1;
326         }
327         // check for margins left/right and extra right margin "const 5"
328         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
329                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
330         if (topx_set && owner()) {
331                 w -= top_x;
332                 w += owner()->x();
333         } else if (owner()) {
334                 // this is needed as otherwise we might have a too large inset if
335                 // its top_x afterwards changes to LeftMargin so we try to put at
336                 // least the default margin as top_x
337                 w -= 20;
338         }
339         if (w < 10) {
340                 w = 10;
341         }
342         return w;
343 }
344
345
346 LyXCursor const & Inset::cursor(BufferView * bv) const
347 {
348         if (owner())
349                 return owner()->getLyXText(bv, false)->cursor;
350         return bv->text->cursor;
351 }
352
353
354 WordLangTuple const
355 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
356 {
357         // we have to unlock ourself in this function by default!
358         bv->unlockInset(const_cast<UpdatableInset *>(this));
359         value = 0;
360         return WordLangTuple();
361 }
362
363
364 bool UpdatableInset::searchForward(BufferView * bv, string const &,
365                                    bool, bool)
366 {
367         // we have to unlock ourself in this function by default!
368         bv->unlockInset(const_cast<UpdatableInset *>(this));
369         return false;
370 }
371
372
373 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
374                                     bool, bool)
375 {
376         // we have to unlock ourself in this function by default!
377         bv->unlockInset(const_cast<UpdatableInset *>(this));
378         return false;
379 }