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