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