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