]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
Enable the external inset to handle unknown templates gracefully.
[lyx.git] / src / insets / updatableinset.C
1 /**
2  * \file updatableinset.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Matthias Ettrich
10  *
11  * Full author contact details are available in file CREDITS
12  */
13
14 #include <config.h>
15
16 #include "updatableinset.h"
17 #include "BufferView.h"
18 #include "funcrequest.h"
19 #include "debug.h"
20 #include "lyxfont.h"
21 #include "WordLangTuple.h"
22 #include "lyxtext.h"
23 #include "support/lstrings.h"
24
25
26 // some stuff for inset locking
27
28 UpdatableInset::UpdatableInset()
29         : Inset(), block_drawing_(false)
30 {}
31
32
33 UpdatableInset::UpdatableInset(UpdatableInset const & in)
34         : Inset(in), block_drawing_(false)
35 {}
36
37
38 void UpdatableInset::insetUnlock(BufferView *)
39 {
40         lyxerr[Debug::INFO] << "Inset Unlock" << std::endl;
41 }
42
43
44 // An updatable inset is highly editable by definition
45 Inset::EDITABLE UpdatableInset::editable() const
46 {
47         return HIGHLY_EDITABLE;
48 }
49
50
51 void UpdatableInset::fitInsetCursor(BufferView *) const
52 {}
53
54
55 void UpdatableInset::draw(PainterInfo &, int, int) const
56 {
57         // ATTENTION: don't do the following here!!!
58         //    top_x = x;
59         //    top_baseline = y;
60 }
61
62
63 void UpdatableInset::scroll(BufferView * bv, float s) const
64 {
65         LyXFont font;
66
67         if (!s) {
68                 scx = 0;
69                 return;
70         }
71
72         int const workW = bv->workWidth();
73         int const tmp_top_x = top_x - scx;
74
75         if (tmp_top_x > 0 &&
76             (tmp_top_x + width(bv, font)) < workW)
77                 return;
78         if (s > 0 && top_x > 0)
79                 return;
80
81         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
82         //int const save_scx = scx;
83
84         scx = int(s * workW / 2);
85         // if (!display())
86         // scx += 20;
87
88         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
89                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
90         }
91 }
92
93 void UpdatableInset::scroll(BufferView * bv, int offset) const
94 {
95         if (offset > 0) {
96                 if (!scx && top_x >= 20)
97                         return;
98                 if ((top_x + offset) > 20)
99                         scx = 0;
100                 // scx += offset - (top_x - scx + offset - 20);
101                 else
102                         scx += offset;
103         } else {
104                 LyXFont const font;
105                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
106                         return;
107                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
108                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
109                 } else {
110                         scx += offset;
111                 }
112         }
113 }
114
115
116 ///  An updatable inset could handle lyx editing commands
117 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
118 {
119         if (ev.action == LFUN_MOUSE_RELEASE)
120                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
121
122         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
123                 if (ev.argument.find('.') != ev.argument.npos) {
124                         float const xx = static_cast<float>(strToDbl(ev.argument));
125                         scroll(ev.view(), xx);
126                 } else {
127                         int const xx = strToInt(ev.argument);
128                         scroll(ev.view(), xx);
129                 }
130                 ev.view()->updateInset(this);
131
132                 return DISPATCHED;
133         }
134         return UNDISPATCHED;
135 }
136
137
138 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
139 {
140         int w;
141
142         if (owner()) {
143                 w = static_cast<UpdatableInset *>(owner())->getMaxWidth(bv, this);
144         } else {
145                 w = bv->text->workWidth(this);
146         }
147         if (w < 0) {
148                 return -1;
149         }
150         // check for margins left/right and extra right margin "const 5"
151         if ((w - (2 * TEXT_TO_INSET_OFFSET + 5)) >= 0)
152                 w -= 2 * TEXT_TO_INSET_OFFSET + 5;
153
154         // Deep magic. I don't understand this either.
155         if (owner() && owner()->owner()) {
156                 // add the right paper margin
157                 w -= 20;
158         }
159
160         // FIXME: why ?
161         if (w < 10) {
162                 w = 10;
163         }
164         return w;
165 }
166
167
168 LyXCursor const & Inset::cursor(BufferView * bv) const
169 {
170         if (owner())
171                 return owner()->getLyXText(bv, false)->cursor;
172         return bv->text->cursor;
173 }
174
175
176 WordLangTuple const
177 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
178 {
179         // we have to unlock ourself in this function by default!
180         bv->unlockInset(const_cast<UpdatableInset *>(this));
181         value = 0;
182         return WordLangTuple();
183 }
184
185
186 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
187 {
188         // we have to unlock ourself in this function by default!
189         bv->unlockInset(const_cast<UpdatableInset *>(this));
190         return false;
191 }
192
193
194 bool UpdatableInset::searchForward(BufferView * bv, string const &,
195                                    bool, bool)
196 {
197         // we have to unlock ourself in this function by default!
198         bv->unlockInset(const_cast<UpdatableInset *>(this));
199         return false;
200 }
201
202
203 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
204                                     bool, bool)
205 {
206         // we have to unlock ourself in this function by default!
207         bv->unlockInset(const_cast<UpdatableInset *>(this));
208         return false;
209 }