]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
7df76d7676d7354abdeff0a34f00a903676ff9f1
[lyx.git] / src / insets / updatableinset.C
1
2 #include "updatableinset.h"
3 #include "BufferView.h"
4 #include "funcrequest.h"
5 #include "debug.h"
6 #include "lyxfont.h"
7 #include "WordLangTuple.h"
8 #include "lyxtext.h"
9 #include "support/lstrings.h"
10
11
12 // some stuff for inset locking
13
14 UpdatableInset::UpdatableInset()
15         : Inset(), cursor_visible_(false), block_drawing_(false)
16 {}
17
18
19 UpdatableInset::UpdatableInset(UpdatableInset const & in, bool same_id)
20         : Inset(in, same_id), cursor_visible_(false), block_drawing_(false)
21 {}
22
23
24 void UpdatableInset::insetUnlock(BufferView *)
25 {
26         lyxerr[Debug::INFO] << "Inset Unlock" << endl;
27 }
28
29
30 // An updatable inset is highly editable by definition
31 Inset::EDITABLE UpdatableInset::editable() const
32 {
33         return HIGHLY_EDITABLE;
34 }
35
36
37 void UpdatableInset::toggleInsetCursor(BufferView *)
38 {}
39
40
41 void UpdatableInset::showInsetCursor(BufferView *, bool)
42 {}
43
44
45 void UpdatableInset::hideInsetCursor(BufferView *)
46 {}
47
48
49 void UpdatableInset::fitInsetCursor(BufferView *) const
50 {}
51
52
53 void UpdatableInset::edit(BufferView *, int, int, mouse_button::state)
54 {}
55
56
57 void UpdatableInset::edit(BufferView *, bool)
58 {}
59
60
61 void UpdatableInset::draw(BufferView *, LyXFont const &,
62                           int /* baseline */, float & x,
63                           bool/*cleared*/) const
64 {
65         x += float(scx);
66         // ATTENTION: don't do the following here!!!
67         //    top_x = int(x);
68         //    top_baseline = baseline;
69 }
70
71
72 void UpdatableInset::scroll(BufferView * bv, float s) const
73 {
74         LyXFont font;
75
76         if (!s) {
77                 scx = 0;
78                 return;
79         }
80
81         int const workW = bv->workWidth();
82         int const tmp_top_x = top_x - scx;
83
84         if (tmp_top_x > 0 &&
85             (tmp_top_x + width(bv, font)) < workW)
86                 return;
87         if (s > 0 && top_x > 0)
88                 return;
89
90         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
91         //int const save_scx = scx;
92
93         scx = int(s * workW / 2);
94         // if (!display())
95         // scx += 20;
96
97         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
98                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
99         }
100
101         // bv->updateInset(const_cast<UpdatableInset *>(this), false);
102 }
103
104 void UpdatableInset::scroll(BufferView * bv, int offset) const
105 {
106         if (offset > 0) {
107                 if (!scx && top_x >= 20)
108                         return;
109                 if ((top_x + offset) > 20)
110                         scx = 0;
111                 // scx += offset - (top_x - scx + offset - 20);
112                 else
113                         scx += offset;
114         } else {
115                 LyXFont const font;
116                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
117                         return;
118                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
119                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
120                 } else {
121                         scx += offset;
122                 }
123         }
124 //      bv->updateInset(const_cast<UpdatableInset *>(this), false);
125 }
126
127
128 ///  An updatable inset could handle lyx editing commands
129 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
130 {
131         if (ev.action == LFUN_MOUSE_RELEASE)
132                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
133
134         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
135                 if (ev.argument.find('.') != ev.argument.npos) {
136                         float const xx = static_cast<float>(strToDbl(ev.argument));
137                         scroll(ev.view(), xx);
138                 } else {
139                         int const xx = strToInt(ev.argument);
140                         scroll(ev.view(), xx);
141                 }
142                 ev.view()->updateInset(this, false);
143
144                 return DISPATCHED;
145         }
146         return UNDISPATCHED;
147 }
148
149
150 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
151 {
152         int w;
153         if (owner()) {
154                 w = static_cast<UpdatableInset*>
155                         (owner())->getMaxWidth(bv, this);
156         } else {
157                 w = bv->text->workWidth(bv, const_cast<UpdatableInset *>(this));
158         }
159         if (w < 0) {
160                 return -1;
161         }
162         // check for margins left/right and extra right margin "const 5"
163         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
164                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
165         if (topx_set && owner()) {
166                 w -= top_x;
167                 w += owner()->x();
168         } else if (owner()) {
169                 // this is needed as otherwise we might have a too large inset if
170                 // its top_x afterwards changes to LeftMargin so we try to put at
171                 // least the default margin as top_x
172                 w -= 20;
173         }
174         if (w < 10) {
175                 w = 10;
176         }
177         return w;
178 }
179
180
181 LyXCursor const & Inset::cursor(BufferView * bv) const
182 {
183         if (owner())
184                 return owner()->getLyXText(bv, false)->cursor;
185         return bv->text->cursor;
186 }
187
188
189 WordLangTuple const
190 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
191 {
192         // we have to unlock ourself in this function by default!
193         bv->unlockInset(const_cast<UpdatableInset *>(this));
194         value = 0;
195         return WordLangTuple();
196 }
197
198
199 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
200 {
201         // we have to unlock ourself in this function by default!
202         bv->unlockInset(const_cast<UpdatableInset *>(this));
203         return false;
204 }
205
206  
207 bool UpdatableInset::searchForward(BufferView * bv, string const &,
208                                    bool, bool)
209 {
210         // we have to unlock ourself in this function by default!
211         bv->unlockInset(const_cast<UpdatableInset *>(this));
212         return false;
213 }
214
215
216 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
217                                     bool, bool)
218 {
219         // we have to unlock ourself in this function by default!
220         bv->unlockInset(const_cast<UpdatableInset *>(this));
221         return false;
222 }