]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
Fixed small drawing bug in InsetText.
[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
157 // some stuff for inset locking
158
159 UpdatableInset::UpdatableInset()
160         : Inset(), cursor_visible_(false), block_drawing_(false)
161 {}
162
163
164 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
165         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
166 {}
167
168
169 void UpdatableInset::insetButtonPress(BufferView *, int x, int y, int button)
170 {
171         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
172                        << ", y=" << y << ", button=" << button << endl;
173 }
174
175
176 bool UpdatableInset::insetButtonRelease(BufferView *, int x, int y, int button)
177 {
178         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
179                        << ", y=" << y << ", button=" << button << endl;
180         return false;
181 }
182
183
184 void UpdatableInset::insetKeyPress(XKeyEvent *)
185 {
186         lyxerr[Debug::INFO] << "Inset Keypress" << endl;
187 }
188
189
190 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, int state)
191 {
192         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
193                        << ", y=" << y << ", state=" << state << endl;
194 }
195
196
197 void UpdatableInset::insetUnlock(BufferView *)
198 {
199         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
200 }
201
202
203 // An updatable inset is highly editable by definition
204 Inset::EDITABLE UpdatableInset::editable() const
205 {
206         return HIGHLY_EDITABLE;
207 }
208
209
210 void UpdatableInset::toggleInsetCursor(BufferView *)
211 {}
212
213
214 void UpdatableInset::showInsetCursor(BufferView *, bool)
215 {}
216
217
218 void UpdatableInset::hideInsetCursor(BufferView *)
219 {}
220
221
222 void UpdatableInset::fitInsetCursor(BufferView *) const
223 {}
224
225
226 void UpdatableInset::edit(BufferView *, int, int, unsigned int)
227 {}
228
229
230 void UpdatableInset::edit(BufferView *, bool)
231 {}
232
233
234 void UpdatableInset::draw(BufferView *, LyXFont const &,
235                           int /* baseline */, float & x,
236                           bool/*cleared*/) const
237 {
238         x += float(scx);
239         // ATTENTION: don't do the following here!!!
240         //    top_x = int(x);
241         //    top_baseline = baseline;
242 }
243
244
245 void UpdatableInset::scroll(BufferView * bv, float s) const
246 {
247         LyXFont font;
248         
249         if (!s) {
250                 scx = 0;
251                 return;
252         }
253
254         int const workW = bv->workWidth();
255         int const tmp_top_x = top_x - scx;
256         
257         if (tmp_top_x > 0 && 
258             (tmp_top_x + width(bv, font)) < workW)
259                 return;
260         if (s > 0 && top_x > 0)
261                 return;
262
263         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
264         //int const save_scx = scx;
265     
266         scx = int(s * workW / 2);
267         // if (!display())
268         // scx += 20;
269
270         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
271                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
272         }
273
274         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
275 }
276
277 void UpdatableInset::scroll(BufferView * bv, int offset) const
278 {
279         if (offset > 0) {
280                 if (!scx && top_x >= 20)
281                         return;
282                 if ((top_x + offset) > 20)
283                         scx = 0;
284                 // scx += offset - (top_x - scx + offset - 20);
285                 else
286                         scx += offset;
287         } else {
288                 LyXFont const font;
289                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
290                         return;
291                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
292                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20; 
293                 } else {
294                         scx += offset;
295                 }
296         }
297 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
298 }
299
300
301 ///  An updatable inset could handle lyx editing commands
302 UpdatableInset::RESULT
303 UpdatableInset::localDispatch(BufferView * bv, 
304                               kb_action action, string const & arg) 
305 {
306         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
307                 if (arg.find('.') != arg.npos) {
308                         float const xx = static_cast<float>(strToDbl(arg));
309                         scroll(bv, xx);
310                 } else {
311                         int const xx = strToInt(arg);
312                         scroll(bv, xx);
313                 }
314                 bv->updateInset(this, false);
315                 
316                 return DISPATCHED;
317         }
318         return UNDISPATCHED; 
319 }
320
321
322 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
323 {
324         int w;
325         if (owner()){
326                 w = static_cast<UpdatableInset*>
327                         (owner())->getMaxWidth(bv, this);
328         } else {
329                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
330         }
331         if (w < 0) {
332                 return -1;
333         }
334         // check for margins left/right and extra right margin "const 5"
335         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
336                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
337         if (topx_set && owner()) {
338                 w -= top_x;
339                 w += owner()->x();
340         }
341 #if 0 // already handled above now!!!
342         else if (!owner()) {
343                 // give some left margin this should be made better!
344                 // Idea: LyXText::giveLeftMargin(Inset * inset) will search the
345                 //       inset in the text and return the LeftMargin of that row!
346                 lyxerr << "w -= 20\n";
347                 w -= 20;
348         }
349 #endif
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 string const UpdatableInset::selectNextWordToSpellcheck(BufferView *bv,
366                                             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 string();
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 }