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