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