]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
Small fixes to various InsetText problems.
[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), background_color_(LColor::inherit)
39 {}
40
41
42 Inset::Inset(Inset const & in, bool same_id)
43         : top_x(0), topx_set(false), top_baseline(0), scx(0), owner_(0),
44           name_(in.name_), background_color_(in.background_color_)
45 {
46         if (same_id)
47                 id_ = in.id();
48         else
49                 id_ = inset_id++;
50 }
51
52
53 bool Inset::deletable() const
54 {
55         return true;
56 }
57
58
59 bool Inset::directWrite() const
60 {
61         return false;
62 }
63
64
65 Inset::EDITABLE Inset::editable() const
66 {
67         return NOT_EDITABLE;
68 }
69
70
71 void Inset::validate(LaTeXFeatures &) const
72 {}
73
74
75 bool Inset::autoDelete() const
76 {
77         return false;
78 }
79
80
81 void Inset::edit(BufferView *, int, int, unsigned int)
82 {}
83
84
85 void Inset::edit(BufferView *, bool)
86 {}
87
88
89 #if 0
90 LyXFont const Inset::convertFont(LyXFont const & font) const
91 {
92 #if 1
93         return font;
94 #else
95         return LyXFont(font);
96 #endif
97 }
98 #endif
99
100
101 string const Inset::editMessage() const 
102 {
103         return _("Opened inset");
104 }
105
106
107 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
108 {
109         if (owner())
110                 return owner()->getLyXText(bv, false);
111         else
112                 return bv->text;
113 }
114
115
116 void Inset::setBackgroundColor(LColor::color color)
117 {
118         background_color_ = color;
119 }
120
121
122 LColor::color Inset::backgroundColor() const
123 {
124         if (background_color_ == LColor::inherit) {
125                 if (owner())
126                         return owner()->backgroundColor();
127                 else
128                         return LColor::background;
129         } else
130                 return background_color_;
131 }
132
133
134 int Inset::id() const
135 {
136         return id_;
137 }
138
139 void Inset::id(int id_arg)
140 {
141         id_ = id_arg;
142 }
143
144 void Inset::setFont(BufferView *, LyXFont const &, bool, bool )
145 {}
146
147 // some stuff for inset locking
148
149 UpdatableInset::UpdatableInset()
150         : Inset(), cursor_visible_(false), block_drawing_(false)
151 {}
152
153
154 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
155         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
156 {}
157
158
159 void UpdatableInset::insetButtonPress(BufferView *, int x, int y, int button)
160 {
161         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
162                        << ", y=" << y << ", button=" << button << endl;
163 }
164
165
166 void UpdatableInset::insetButtonRelease(BufferView *, int x, int y, int button)
167 {
168         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
169                        << ", y=" << y << ", button=" << button << endl;
170 }
171
172
173 void UpdatableInset::insetKeyPress(XKeyEvent *)
174 {
175         lyxerr[Debug::INFO] << "Inset Keypress" << endl;
176 }
177
178
179 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, int state)
180 {
181         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
182                        << ", y=" << y << ", state=" << state << endl;
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, unsigned int)
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 UpdatableInset::RESULT
292 UpdatableInset::localDispatch(BufferView * bv, 
293                               kb_action action, string const & arg) 
294 {
295         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
296                 if (arg.find('.') != arg.npos) {
297                         float const xx = static_cast<float>(strToDbl(arg));
298                         scroll(bv, xx);
299                 } else {
300                         int const xx = strToInt(arg);
301                         scroll(bv, xx);
302                 }
303                 bv->updateInset(this, false);
304                 
305                 return DISPATCHED;
306         }
307         return UNDISPATCHED; 
308 }
309
310
311 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
312 {
313         int w;
314         if (owner()){
315                 w = static_cast<UpdatableInset*>
316                         (owner())->getMaxWidth(bv, this);
317         } else {
318                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
319         }
320         if (w < 0) {
321                 return -1;
322         }
323         // check for margins left/right and extra right margin "const 5"
324         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
325                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
326         if (topx_set && owner()) {
327                 w -= top_x;
328                 w += owner()->x();
329         }
330 #if 0 // already handled above now!!!
331         else if (!owner()) {
332                 // give some left margin this should be made better!
333                 // Idea: LyXText::giveLeftMargin(Inset * inset) will search the
334                 //       inset in the text and return the LeftMargin of that row!
335                 lyxerr << "w -= 20\n";
336                 w -= 20;
337         }
338 #endif
339         if (w < 10) {
340                 w = 10;
341         }
342         return w;
343 }
344
345
346 LyXCursor const & Inset::cursor(BufferView * bv) const
347 {
348         return bv->text->cursor;
349 }
350
351
352 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
353                                             float & value) const
354 {
355         // we have to unlock ourself in this function by default!
356         bv->unlockInset(const_cast<UpdatableInset *>(this));
357         value = 0;
358         return string();
359 }
360
361
362 bool UpdatableInset::searchForward(BufferView * bv, string const &,
363                                    bool const &, bool const &)
364 {
365         // we have to unlock ourself in this function by default!
366         bv->unlockInset(const_cast<UpdatableInset *>(this));
367         return false;
368 }
369
370
371 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
372                                     bool const &, bool const &)
373 {
374         // we have to unlock ourself in this function by default!
375         bv->unlockInset(const_cast<UpdatableInset *>(this));
376         return false;
377 }