]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
Update/cursor and drawing fixes.
[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), top_baseline(0), scx(0), id_(inset_id++), owner_(0),
38           background_color_(LColor::inherit)
39 {}
40
41
42 Inset::Inset(Inset const & in, bool same_id)
43         : top_x(0), top_baseline(0), scx(0), owner_(0), name_(in.name_),
44           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 LyXFont const Inset::convertFont(LyXFont const & font) const
90 {
91         return LyXFont(font);
92 }
93
94
95 string const Inset::editMessage() const 
96 {
97         return _("Opened inset");
98 }
99
100
101 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
102 {
103         if (owner())
104                 return owner()->getLyXText(bv, false);
105         else
106                 return bv->text;
107 }
108
109
110 void Inset::setBackgroundColor(LColor::color color)
111 {
112         background_color_ = color;
113 }
114
115
116 LColor::color Inset::backgroundColor() const
117 {
118         if (background_color_ == LColor::inherit) {
119                 if (owner())
120                         return owner()->backgroundColor();
121                 else
122                         return LColor::background;
123         } else
124                 return background_color_;
125 }
126
127
128 int Inset::id() const
129 {
130         return id_;
131 }
132
133 void Inset::id(int id_arg)
134 {
135         id_ = id_arg;
136 }
137
138 void Inset::setFont(BufferView *, LyXFont const &, bool, bool )
139 {}
140
141 // some stuff for inset locking
142
143 UpdatableInset::UpdatableInset()
144         : Inset(), cursor_visible_(false), block_drawing_(false)
145 {}
146
147
148 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
149         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
150 {}
151
152
153 void UpdatableInset::insetButtonPress(BufferView *, int x, int y, int button)
154 {
155         lyxerr[Debug::INFO] << "Inset Button Press x=" << x
156                        << ", y=" << y << ", button=" << button << endl;
157 }
158
159
160 void UpdatableInset::insetButtonRelease(BufferView *, int x, int y, int button)
161 {
162         lyxerr[Debug::INFO] << "Inset Button Release x=" << x
163                        << ", y=" << y << ", button=" << button << endl;
164 }
165
166
167 void UpdatableInset::insetKeyPress(XKeyEvent *)
168 {
169         lyxerr[Debug::INFO] << "Inset Keypress" << endl;
170 }
171
172
173 void UpdatableInset::insetMotionNotify(BufferView *, int x, int y, int state)
174 {
175         lyxerr[Debug::INFO] << "Inset Motion Notify x=" << x
176                        << ", y=" << y << ", state=" << state << endl;
177 }
178
179
180 void UpdatableInset::insetUnlock(BufferView *)
181 {
182         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
183 }
184
185
186 // An updatable inset is highly editable by definition
187 Inset::EDITABLE UpdatableInset::editable() const
188 {
189         return HIGHLY_EDITABLE;
190 }
191
192
193 void UpdatableInset::toggleInsetCursor(BufferView *)
194 {}
195
196
197 void UpdatableInset::showInsetCursor(BufferView *, bool)
198 {}
199
200
201 void UpdatableInset::hideInsetCursor(BufferView *)
202 {}
203
204
205 void UpdatableInset::fitInsetCursor(BufferView *) const
206 {}
207
208
209 void UpdatableInset::edit(BufferView *, int, int, unsigned int)
210 {}
211
212
213 void UpdatableInset::edit(BufferView *, bool)
214 {}
215
216
217 void UpdatableInset::draw(BufferView *, LyXFont const &,
218                           int /* baseline */, float & x,
219                           bool/*cleared*/) const
220 {
221         x += float(scx);
222         // ATTENTION: don't do the following here!!!
223         //    top_x = int(x);
224         //    top_baseline = baseline;
225 }
226
227
228 void UpdatableInset::scroll(BufferView * bv, float s) const
229 {
230         LyXFont font;
231         
232         if (!s) {
233                 scx = 0;
234                 return;
235         }
236
237         int const workW = bv->workWidth();
238         int const tmp_top_x = top_x - scx;
239         
240         if (tmp_top_x > 0 && 
241             (tmp_top_x + width(bv, font)) < workW)
242                 return;
243         if (s > 0 && top_x > 0)
244                 return;
245
246         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
247         //int const save_scx = scx;
248     
249         scx = int(s * workW / 2);
250         // if (!display())
251         // scx += 20;
252
253         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
254                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
255         }
256
257         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
258 }
259
260 void UpdatableInset::scroll(BufferView * bv, int offset) const
261 {
262         if (offset > 0) {
263                 if (!scx && top_x >= 20)
264                         return;
265                 if ((top_x + offset) > 20)
266                         scx = 0;
267                 // scx += offset - (top_x - scx + offset - 20);
268                 else
269                         scx += offset;
270         } else {
271                 LyXFont const font;
272                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
273                         return;
274                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
275                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20; 
276                 } else {
277                         scx += offset;
278                 }
279         }
280 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
281 }
282
283
284 ///  An updatable inset could handle lyx editing commands
285 UpdatableInset::RESULT
286 UpdatableInset::localDispatch(BufferView * bv, 
287                               kb_action action, string const & arg) 
288 {
289         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
290                 if (arg.find('.') != arg.npos) {
291                         float const xx = static_cast<float>(strToDbl(arg));
292                         scroll(bv, xx);
293                 } else {
294                         int const xx = strToInt(arg);
295                         scroll(bv, xx);
296                 }
297                 bv->updateInset(this, false);
298                 
299                 return DISPATCHED;
300         }
301         return UNDISPATCHED; 
302 }
303
304
305 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
306 {
307         if (owner())
308                 return static_cast<UpdatableInset*>
309                         (owner())->getMaxWidth(bv, this);
310         return bv->workWidth();
311 }
312
313
314 LyXCursor const & Inset::cursor(BufferView * bv) const
315 {
316         return bv->text->cursor;
317 }
318
319
320 string const UpdatableInset::selectNextWord(BufferView *bv,
321                                             float & value) const
322 {
323         // we have to unlock ourself in this function by default!
324         bv->unlockInset(const_cast<UpdatableInset *>(this));
325         value = 0;
326         return string();
327 }
328
329
330 bool UpdatableInset::searchForward(BufferView * bv, string const &,
331                                    bool const &, bool const &)
332 {
333         // we have to unlock ourself in this function by default!
334         bv->unlockInset(const_cast<UpdatableInset *>(this));
335         return false;
336 }
337
338
339 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
340                                     bool const &, bool const &)
341 {
342         // we have to unlock ourself in this function by default!
343         bv->unlockInset(const_cast<UpdatableInset *>(this));
344         return false;
345 }