]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
39e9a31685589d6d0950f72b174323c5437f5493
[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 "Painter.h"
22 #include "commandtags.h"
23 #include "support/lstrings.h"
24 #include "gettext.h"
25 #include "lyxfont.h"
26 #include "lyxcursor.h"
27 #include "lyxtext.h"
28
29 using std::endl;
30
31 // Insets default methods
32
33 // Initialization of the counter for the inset id's,
34 unsigned int Inset::inset_id = 0;
35
36 Inset::Inset()
37         : top_x(0), topx_set(false), top_baseline(0), scx(0),
38           id_(inset_id++), owner_(0), par_owner_(0),
39           background_color_(LColor::inherit)
40 {}
41
42
43 Inset::Inset(Inset const & in, bool same_id)
44         : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
45           name_(in.name_), background_color_(in.background_color_)
46 {
47         if (same_id)
48                 id_ = in.id();
49         else
50                 id_ = inset_id++;
51 }
52
53
54 bool Inset::deletable() const
55 {
56         return true;
57 }
58
59
60 bool Inset::directWrite() const
61 {
62         return false;
63 }
64
65
66 Inset::EDITABLE Inset::editable() const
67 {
68         return NOT_EDITABLE;
69 }
70
71
72 void Inset::validate(LaTeXFeatures &) const
73 {}
74
75
76 bool Inset::autoDelete() const
77 {
78         return false;
79 }
80
81
82 void Inset::edit(BufferView *, int, int, unsigned int)
83 {}
84
85
86 void Inset::edit(BufferView *, bool)
87 {}
88
89
90 #if 0
91 LyXFont const Inset::convertFont(LyXFont const & font) const
92 {
93 #if 1
94         return font;
95 #else
96         return LyXFont(font);
97 #endif
98 }
99 #endif
100
101
102 string const Inset::editMessage() const 
103 {
104         return _("Opened inset");
105 }
106
107
108 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
109 {
110         if (owner())
111                 return owner()->getLyXText(bv, false);
112         else
113                 return bv->text;
114 }
115
116
117 void Inset::setBackgroundColor(LColor::color color)
118 {
119         background_color_ = color;
120 }
121
122
123 LColor::color Inset::backgroundColor() const
124 {
125         if (background_color_ == LColor::inherit) {
126                 if (owner())
127                         return owner()->backgroundColor();
128                 else
129                         return LColor::background;
130         } else
131                 return background_color_;
132 }
133
134
135 int Inset::id() const
136 {
137         return id_;
138 }
139
140 void Inset::id(int id_arg)
141 {
142         id_ = id_arg;
143 }
144
145 void Inset::setFont(BufferView *, LyXFont const &, bool, bool )
146 {}
147
148 // some stuff for inset locking
149
150 UpdatableInset::UpdatableInset()
151         : Inset(), cursor_visible_(false), block_drawing_(false)
152 {}
153
154
155 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
156         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
157 {}
158
159
160 void UpdatableInset::insetButtonPress(BufferView *, int x, int y, int button)
161 {
162         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
163                        << ", y=" << y << ", button=" << button << endl;
164 }
165
166
167 void UpdatableInset::insetButtonRelease(BufferView *, int x, int y, int button)
168 {
169         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
170                        << ", y=" << y << ", button=" << button << endl;
171 }
172
173
174 void UpdatableInset::insetKeyPress(XKeyEvent *)
175 {
176         lyxerr[Debug::INFO] << "Inset Keypress" << endl;
177 }
178
179
180 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, int state)
181 {
182         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
183                        << ", y=" << y << ", state=" << state << endl;
184 }
185
186
187 void UpdatableInset::insetUnlock(BufferView *)
188 {
189         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
190 }
191
192
193 // An updatable inset is highly editable by definition
194 Inset::EDITABLE UpdatableInset::editable() const
195 {
196         return HIGHLY_EDITABLE;
197 }
198
199
200 void UpdatableInset::toggleInsetCursor(BufferView *)
201 {}
202
203
204 void UpdatableInset::showInsetCursor(BufferView *, bool)
205 {}
206
207
208 void UpdatableInset::hideInsetCursor(BufferView *)
209 {}
210
211
212 void UpdatableInset::fitInsetCursor(BufferView *) const
213 {}
214
215
216 void UpdatableInset::edit(BufferView *, int, int, unsigned int)
217 {}
218
219
220 void UpdatableInset::edit(BufferView *, bool)
221 {}
222
223
224 void UpdatableInset::draw(BufferView *, LyXFont const &,
225                           int /* baseline */, float & x,
226                           bool/*cleared*/) const
227 {
228         x += float(scx);
229         // ATTENTION: don't do the following here!!!
230         //    top_x = int(x);
231         //    top_baseline = baseline;
232 }
233
234
235 void UpdatableInset::scroll(BufferView * bv, float s) const
236 {
237         LyXFont font;
238         
239         if (!s) {
240                 scx = 0;
241                 return;
242         }
243
244         int const workW = bv->workWidth();
245         int const tmp_top_x = top_x - scx;
246         
247         if (tmp_top_x > 0 && 
248             (tmp_top_x + width(bv, font)) < workW)
249                 return;
250         if (s > 0 && top_x > 0)
251                 return;
252
253         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
254         //int const save_scx = scx;
255     
256         scx = int(s * workW / 2);
257         // if (!display())
258         // scx += 20;
259
260         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
261                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
262         }
263
264         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
265 }
266
267 void UpdatableInset::scroll(BufferView * bv, int offset) const
268 {
269         if (offset > 0) {
270                 if (!scx && top_x >= 20)
271                         return;
272                 if ((top_x + offset) > 20)
273                         scx = 0;
274                 // scx += offset - (top_x - scx + offset - 20);
275                 else
276                         scx += offset;
277         } else {
278                 LyXFont const font;
279                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
280                         return;
281                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
282                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20; 
283                 } else {
284                         scx += offset;
285                 }
286         }
287 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
288 }
289
290
291 ///  An updatable inset could handle lyx editing commands
292 UpdatableInset::RESULT
293 UpdatableInset::localDispatch(BufferView * bv, 
294                               kb_action action, string const & arg) 
295 {
296         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
297                 if (arg.find('.') != arg.npos) {
298                         float const xx = static_cast<float>(strToDbl(arg));
299                         scroll(bv, xx);
300                 } else {
301                         int const xx = strToInt(arg);
302                         scroll(bv, xx);
303                 }
304                 bv->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         }
331 #if 0 // already handled above now!!!
332         else if (!owner()) {
333                 // give some left margin this should be made better!
334                 // Idea: LyXText::giveLeftMargin(Inset * inset) will search the
335                 //       inset in the text and return the LeftMargin of that row!
336                 lyxerr << "w -= 20\n";
337                 w -= 20;
338         }
339 #endif
340         if (w < 10) {
341                 w = 10;
342         }
343         return w;
344 }
345
346
347 LyXCursor const & Inset::cursor(BufferView * bv) const
348 {
349         return bv->text->cursor;
350 }
351
352
353 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
354                                             float & value) const
355 {
356         // we have to unlock ourself in this function by default!
357         bv->unlockInset(const_cast<UpdatableInset *>(this));
358         value = 0;
359         return string();
360 }
361
362
363 bool UpdatableInset::searchForward(BufferView * bv, string const &,
364                                    bool const &, bool const &)
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 const &, bool const &)
374 {
375         // we have to unlock ourself in this function by default!
376         bv->unlockInset(const_cast<UpdatableInset *>(this));
377         return false;
378 }