]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
7fe812bda8aa60aacd0d80207562f9398af1f4a6
[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::validate(LaTeXFeatures &) const
78 {}
79
80
81 bool Inset::autoDelete() const
82 {
83         return false;
84 }
85
86
87 void Inset::edit(BufferView *, int, int, mouse_button::state)
88 {}
89
90
91 void Inset::edit(BufferView *, bool)
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::insetButtonPress(BufferView *, int x, int y, mouse_button::state button)
181 {
182         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
183                        << ", y=" << y << ", button=" << button << endl;
184 }
185
186
187 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, mouse_button::state button)
188 {
189         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
190                        << ", y=" << y << ", button=" << button << endl;
191         return false;
192 }
193
194
195 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, mouse_button::state state)
196 {
197         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
198                        << ", y=" << y << ", state=" << state << endl;
199 }
200
201
202 void UpdatableInset::insetUnlock(BufferView *)
203 {
204         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
205 }
206
207
208 // An updatable inset is highly editable by definition
209 Inset::EDITABLE UpdatableInset::editable() const
210 {
211         return HIGHLY_EDITABLE;
212 }
213
214
215 void UpdatableInset::toggleInsetCursor(BufferView *)
216 {}
217
218
219 void UpdatableInset::showInsetCursor(BufferView *, bool)
220 {}
221
222
223 void UpdatableInset::hideInsetCursor(BufferView *)
224 {}
225
226
227 void UpdatableInset::fitInsetCursor(BufferView *) const
228 {}
229
230
231 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
232 {}
233
234
235 void UpdatableInset::edit(BufferView *, bool)
236 {}
237
238
239 void UpdatableInset::draw(BufferView *, LyXFont const &,
240                           int /* baseline */, float & x,
241                           bool/*cleared*/) const
242 {
243         x += float(scx);
244         // ATTENTION: don't do the following here!!!
245         //    top_x = int(x);
246         //    top_baseline = baseline;
247 }
248
249
250 void UpdatableInset::scroll(BufferView * bv, float s) const
251 {
252         LyXFont font;
253
254         if (!s) {
255                 scx = 0;
256                 return;
257         }
258
259         int const workW = bv->workWidth();
260         int const tmp_top_x = top_x - scx;
261
262         if (tmp_top_x > 0 &&
263             (tmp_top_x + width(bv, font)) < workW)
264                 return;
265         if (s > 0 && top_x > 0)
266                 return;
267
268         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
269         //int const save_scx = scx;
270
271         scx = int(s * workW / 2);
272         // if (!display())
273         // scx += 20;
274
275         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
276                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
277         }
278
279         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
280 }
281
282 void UpdatableInset::scroll(BufferView * bv, int offset) const
283 {
284         if (offset > 0) {
285                 if (!scx && top_x >= 20)
286                         return;
287                 if ((top_x + offset) > 20)
288                         scx = 0;
289                 // scx += offset - (top_x - scx + offset - 20);
290                 else
291                         scx += offset;
292         } else {
293                 LyXFont const font;
294                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
295                         return;
296                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
297                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
298                 } else {
299                         scx += offset;
300                 }
301         }
302 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
303 }
304
305
306 ///  An updatable inset could handle lyx editing commands
307 UpdatableInset::RESULT
308 UpdatableInset::localDispatch(FuncRequest const & ev)
309 {
310         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
311                 if (ev.argument.find('.') != ev.argument.npos) {
312                         float const xx = static_cast<float>(strToDbl(ev.argument));
313                         scroll(ev.view(), xx);
314                 } else {
315                         int const xx = strToInt(ev.argument);
316                         scroll(ev.view(), xx);
317                 }
318                 ev.view()->updateInset(this, false);
319
320                 return DISPATCHED;
321         }
322         return UNDISPATCHED;
323 }
324
325
326 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
327 {
328         int w;
329         if (owner()) {
330                 w = static_cast<UpdatableInset*>
331                         (owner())->getMaxWidth(bv, this);
332         } else {
333                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
334         }
335         if (w < 0) {
336                 return -1;
337         }
338         // check for margins left/right and extra right margin "const 5"
339         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
340                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
341         if (topx_set && owner()) {
342                 w -= top_x;
343                 w += owner()->x();
344         } else if (owner()) {
345                 // this is needed as otherwise we might have a too large inset if
346                 // its top_x afterwards changes to LeftMargin so we try to put at
347                 // least the default margin as top_x
348                 w -= 20;
349         }
350         if (w < 10) {
351                 w = 10;
352         }
353         return w;
354 }
355
356
357 LyXCursor const & Inset::cursor(BufferView * bv) const
358 {
359         if (owner())
360                 return owner()->getLyXText(bv, false)->cursor;
361         return bv->text->cursor;
362 }
363
364
365 WordLangTuple const
366 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
367 {
368         // we have to unlock ourself in this function by default!
369         bv->unlockInset(const_cast<UpdatableInset *>(this));
370         value = 0;
371         return WordLangTuple();
372 }
373
374
375 bool UpdatableInset::searchForward(BufferView * bv, string const &,
376                                    bool, bool)
377 {
378         // we have to unlock ourself in this function by default!
379         bv->unlockInset(const_cast<UpdatableInset *>(this));
380         return false;
381 }
382
383
384 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
385                                     bool, bool)
386 {
387         // we have to unlock ourself in this function by default!
388         bv->unlockInset(const_cast<UpdatableInset *>(this));
389         return false;
390 }