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