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