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