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