]> git.lyx.org Git - lyx.git/blob - src/insets/inset.C
5d748761078886fe02b1f0a8c673f657005fece5
[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-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation "lyxinset.h"
15 #endif
16
17 #include "lyxinset.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
26 using std::endl;
27
28 // Insets default methods
29
30 bool Inset::Deletable() const
31 {
32         return true;
33 }
34
35
36 bool Inset::DirectWrite() const
37 {
38         return false;
39 }
40
41
42 Inset::EDITABLE Inset::Editable() const
43 {
44         return NOT_EDITABLE;
45 }
46
47
48 void Inset::Validate(LaTeXFeatures &) const
49 {}
50
51
52 bool Inset::AutoDelete() const
53 {
54         return false;
55 }
56
57
58 void Inset::Edit(BufferView *, int, int, unsigned int)
59 {}
60
61
62 LyXFont const Inset::ConvertFont(LyXFont const & font) const
63 {
64         return LyXFont(font);
65 }
66
67
68 string const Inset::EditMessage() const 
69 {
70         return _("Opened inset");
71 }
72
73
74 LyXText * Inset::getLyXText(BufferView const * bv, bool const) const
75 {
76         if (owner())
77                 return owner()->getLyXText(bv, false);
78         else
79                 return bv->text;
80 }
81
82
83 // some stuff for inset locking
84
85 void UpdatableInset::InsetButtonPress(BufferView *, int x, int y, int button)
86 {
87         lyxerr.debug() << "Inset Button Press x=" << x
88                        << ", y=" << y << ", button=" << button << endl;
89 }
90
91
92 void UpdatableInset::InsetButtonRelease(BufferView *, int x, int y, int button)
93 {
94         lyxerr.debug() << "Inset Button Release x=" << x
95                        << ", y=" << y << ", button=" << button << endl;
96 }
97
98
99 void UpdatableInset::InsetKeyPress(XKeyEvent *)
100 {
101         lyxerr.debug() << "Inset Keypress" << endl;
102 }
103
104
105 void UpdatableInset::InsetMotionNotify(BufferView *, int x, int y, int state)
106 {
107         lyxerr.debug() << "Inset Motion Notify x=" << x
108                        << ", y=" << y << ", state=" << state << endl;
109 }
110
111
112 void UpdatableInset::InsetUnlock(BufferView *)
113 {
114         lyxerr.debug() << "Inset Unlock" << endl;
115 }
116
117
118 // An updatable inset is highly editable by definition
119 Inset::EDITABLE UpdatableInset::Editable() const
120 {
121         return HIGHLY_EDITABLE;
122 }
123
124
125 void UpdatableInset::ToggleInsetCursor(BufferView *)
126 {}
127
128
129 void UpdatableInset::ShowInsetCursor(BufferView *, bool)
130 {}
131
132
133 void UpdatableInset::HideInsetCursor(BufferView *)
134 {}
135
136
137 void UpdatableInset::Edit(BufferView *, int, int, unsigned int)
138 {}
139
140
141 void UpdatableInset::draw(BufferView *, LyXFont const &,
142                           int /* baseline */, float & x,
143                           bool/*cleared*/) const
144 {
145         x += float(scx);
146         // ATTENTION: don't do the following here!!!
147         //    top_x = int(x);
148         //    top_baseline = baseline;
149 }
150
151
152 void UpdatableInset::SetFont(BufferView *, LyXFont const &, bool )
153 {}
154
155 void UpdatableInset::scroll(BufferView * bv, float s) const
156 {
157         LyXFont font;
158         
159         if (!s) {
160                 scx = 0;
161                 return;
162         }
163
164         int const workW = bv->workWidth();
165         int const tmp_top_x = top_x - scx;
166         
167         if (tmp_top_x > 0 && 
168             (tmp_top_x + width(bv, font)) < workW)
169                 return;
170         if (s > 0 && top_x > 0)
171                 return;
172
173         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
174         //int const save_scx = scx;
175     
176         scx = int(s * workW / 2);
177         // if (!display())
178         // scx += 20;
179
180         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
181                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
182         }
183
184         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
185 }
186
187 void UpdatableInset::scroll(BufferView * bv, int offset) const
188 {
189         if (offset > 0) {
190                 if (!scx && top_x >= 20)
191                         return;
192                 if ((top_x + offset) > 20)
193                         scx = 0;
194                 // scx += offset - (top_x - scx + offset - 20);
195                 else
196                         scx += offset;
197         } else {
198                 LyXFont const font;
199                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
200                         return;
201                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
202                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20; 
203                 } else {
204                         scx += offset;
205                 }
206         }
207 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
208 }
209
210
211 ///  An updatable inset could handle lyx editing commands
212 UpdatableInset::RESULT
213 UpdatableInset::LocalDispatch(BufferView * bv, 
214                               kb_action action, string const & arg) 
215 {
216         if (!arg.empty() && (action==LFUN_SCROLL_INSET)) {
217                 if (arg.find('.') != arg.npos) {
218                         float const xx = static_cast<float>(strToDbl(arg));
219                         scroll(bv, xx);
220                 } else {
221                         int const xx = strToInt(arg);
222                         scroll(bv, xx);
223                 }
224                 bv->updateInset(this, false);
225                 
226                 return DISPATCHED;
227         }
228         return UNDISPATCHED; 
229 }
230
231
232 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
233 {
234         if (owner())
235                 return static_cast<UpdatableInset*>
236                         (owner())->getMaxWidth(bv, this);
237         return bv->workWidth();
238 }