]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
static_cast-based key/mouse-state. Kill insetKeyPress.
[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 #include "debug.h"
19 #include "BufferView.h"
20 #include "support/lstrings.h"
21 #include "frontends/Painter.h"
22 #include "frontends/mouse_state.h" 
23 #include "commandtags.h"
24 #include "support/lstrings.h"
25 #include "gettext.h"
26 #include "lyxfont.h"
27 #include "lyxcursor.h"
28 #include "lyxtext.h"
29
30 using std::endl;
31
32 // Insets default methods
33
34 // Initialization of the counter for the inset id's,
35 unsigned int Inset::inset_id = 0;
36
37 Inset::Inset()
38         : top_x(0), topx_set(false), top_baseline(0), scx(0),
39           id_(inset_id++), owner_(0), par_owner_(0),
40           background_color_(LColor::inherit)
41 {}
42
43
44 Inset::Inset(Inset const & in, bool same_id)
45         : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
46           name_(in.name_), background_color_(in.background_color_)
47 {
48         if (same_id)
49                 id_ = in.id();
50         else
51                 id_ = inset_id++;
52 }
53
54
55 bool Inset::deletable() const
56 {
57         return true;
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::validate(LaTeXFeatures &) const
74 {}
75
76
77 bool Inset::autoDelete() const
78 {
79         return false;
80 }
81
82
83 void Inset::edit(BufferView *, int, int, mouse_button::state)
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::insetButtonPress(BufferView *, int x, int y, mouse_button::state button)
177 {
178         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
179                        << ", y=" << y << ", button=" << button << endl;
180 }
181
182
183 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, mouse_button::state button)
184 {
185         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
186                        << ", y=" << y << ", button=" << button << endl;
187         return false;
188 }
189
190
191 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, mouse_button::state state)
192 {
193         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
194                        << ", y=" << y << ", state=" << state << endl;
195 }
196
197
198 void UpdatableInset::insetUnlock(BufferView *)
199 {
200         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
201 }
202
203
204 // An updatable inset is highly editable by definition
205 Inset::EDITABLE UpdatableInset::editable() const
206 {
207         return HIGHLY_EDITABLE;
208 }
209
210
211 void UpdatableInset::toggleInsetCursor(BufferView *)
212 {}
213
214
215 void UpdatableInset::showInsetCursor(BufferView *, bool)
216 {}
217
218
219 void UpdatableInset::hideInsetCursor(BufferView *)
220 {}
221
222
223 void UpdatableInset::fitInsetCursor(BufferView *) const
224 {}
225
226
227 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
228 {}
229
230
231 void UpdatableInset::edit(BufferView *, bool)
232 {}
233
234
235 void UpdatableInset::draw(BufferView *, LyXFont const &,
236                           int /* baseline */, float & x,
237                           bool/*cleared*/) const
238 {
239         x += float(scx);
240         // ATTENTION: don't do the following here!!!
241         //    top_x = int(x);
242         //    top_baseline = baseline;
243 }
244
245
246 void UpdatableInset::scroll(BufferView * bv, float s) const
247 {
248         LyXFont font;
249
250         if (!s) {
251                 scx = 0;
252                 return;
253         }
254
255         int const workW = bv->workWidth();
256         int const tmp_top_x = top_x - scx;
257
258         if (tmp_top_x > 0 &&
259             (tmp_top_x + width(bv, font)) < workW)
260                 return;
261         if (s > 0 && top_x > 0)
262                 return;
263
264         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
265         //int const save_scx = scx;
266
267         scx = int(s * workW / 2);
268         // if (!display())
269         // scx += 20;
270
271         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
272                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
273         }
274
275         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
276 }
277
278 void UpdatableInset::scroll(BufferView * bv, int offset) const
279 {
280         if (offset > 0) {
281                 if (!scx && top_x >= 20)
282                         return;
283                 if ((top_x + offset) > 20)
284                         scx = 0;
285                 // scx += offset - (top_x - scx + offset - 20);
286                 else
287                         scx += offset;
288         } else {
289                 LyXFont const font;
290                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
291                         return;
292                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
293                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
294                 } else {
295                         scx += offset;
296                 }
297         }
298 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
299 }
300
301
302 ///  An updatable inset could handle lyx editing commands
303 UpdatableInset::RESULT
304 UpdatableInset::localDispatch(BufferView * bv,
305                               kb_action action, string const & arg)
306 {
307         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
308                 if (arg.find('.') != arg.npos) {
309                         float const xx = static_cast<float>(strToDbl(arg));
310                         scroll(bv, xx);
311                 } else {
312                         int const xx = strToInt(arg);
313                         scroll(bv, xx);
314                 }
315                 bv->updateInset(this, false);
316
317                 return DISPATCHED;
318         }
319         return UNDISPATCHED;
320 }
321
322
323 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
324 {
325         int w;
326         if (owner()) {
327                 w = static_cast<UpdatableInset*>
328                         (owner())->getMaxWidth(bv, this);
329         } else {
330                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
331         }
332         if (w < 0) {
333                 return -1;
334         }
335         // check for margins left/right and extra right margin "const 5"
336         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
337                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
338         if (topx_set && owner()) {
339                 w -= top_x;
340                 w += owner()->x();
341         } else if (owner()) {
342                 // this is needed as otherwise we might have a too large inset if
343                 // its top_x afterwards changes to LeftMargin so we try to put at
344                 // least the default margin as top_x
345                 w -= 20;
346         }
347         if (w < 10) {
348                 w = 10;
349         }
350         return w;
351 }
352
353
354 LyXCursor const & Inset::cursor(BufferView * bv) const
355 {
356         if (owner())
357                 return owner()->getLyXText(bv, false)->cursor;
358         return bv->text->cursor;
359 }
360
361
362 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
363                                             float & value) const
364 {
365         // we have to unlock ourself in this function by default!
366         bv->unlockInset(const_cast<UpdatableInset *>(this));
367         value = 0;
368         return string();
369 }
370
371
372 bool UpdatableInset::searchForward(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 }
379
380
381 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
382                                     bool, bool)
383 {
384         // we have to unlock ourself in this function by default!
385         bv->unlockInset(const_cast<UpdatableInset *>(this));
386         return false;
387 }