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