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