]> git.lyx.org Git - lyx.git/blob - src/insets/updatableinset.C
Rename LatexRunParams::fragile as moving_arg.
[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, bool same_id)
34         : Inset(in, same_id), 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(BufferView *, LyXFont const &,
56                           int /* baseline */, float & x) const
57 {
58         x += float(scx);
59         // ATTENTION: don't do the following here!!!
60         //    top_x = int(x);
61         //    top_baseline = baseline;
62 }
63
64
65 void UpdatableInset::scroll(BufferView * bv, float s) const
66 {
67         LyXFont font;
68
69         if (!s) {
70                 scx = 0;
71                 return;
72         }
73
74         int const workW = bv->workWidth();
75         int const tmp_top_x = top_x - scx;
76
77         if (tmp_top_x > 0 &&
78             (tmp_top_x + width(bv, font)) < workW)
79                 return;
80         if (s > 0 && top_x > 0)
81                 return;
82
83         // int mx_scx=abs((width(bv,font) - bv->workWidth())/2);
84         //int const save_scx = scx;
85
86         scx = int(s * workW / 2);
87         // if (!display())
88         // scx += 20;
89
90         if ((tmp_top_x + scx + width(bv, font)) < (workW / 2)) {
91                 scx += (workW / 2) - (tmp_top_x + scx + width(bv, font));
92         }
93 }
94
95 void UpdatableInset::scroll(BufferView * bv, int offset) const
96 {
97         if (offset > 0) {
98                 if (!scx && top_x >= 20)
99                         return;
100                 if ((top_x + offset) > 20)
101                         scx = 0;
102                 // scx += offset - (top_x - scx + offset - 20);
103                 else
104                         scx += offset;
105         } else {
106                 LyXFont const font;
107                 if (!scx && (top_x + width(bv, font)) < (bv->workWidth() - 20))
108                         return;
109                 if ((top_x - scx + offset + width(bv, font)) < (bv->workWidth() - 20)) {
110                         scx = bv->workWidth() - width(bv, font) - top_x + scx - 20;
111                 } else {
112                         scx += offset;
113                 }
114         }
115 }
116
117
118 ///  An updatable inset could handle lyx editing commands
119 Inset::RESULT UpdatableInset::localDispatch(FuncRequest const & ev)
120 {
121         if (ev.action == LFUN_MOUSE_RELEASE)
122                 return (editable() == IS_EDITABLE) ? DISPATCHED : UNDISPATCHED;
123
124         if (!ev.argument.empty() && ev.action == LFUN_SCROLL_INSET) {
125                 if (ev.argument.find('.') != ev.argument.npos) {
126                         float const xx = static_cast<float>(strToDbl(ev.argument));
127                         scroll(ev.view(), xx);
128                 } else {
129                         int const xx = strToInt(ev.argument);
130                         scroll(ev.view(), xx);
131                 }
132                 ev.view()->updateInset(this);
133
134                 return DISPATCHED;
135         }
136         return UNDISPATCHED;
137 }
138
139
140 int UpdatableInset::getMaxWidth(BufferView * bv, UpdatableInset const *) const
141 {
142         int w;
143
144         if (owner()) {
145                 w = static_cast<UpdatableInset*> (owner())->getMaxWidth(bv, this);
146         } else {
147                 w = bv->text->workWidth(const_cast<UpdatableInset *>(this));
148         }
149         if (w < 0) {
150                 return -1;
151         }
152         // check for margins left/right and extra right margin "const 5"
153         if ((w - ((2 * TEXT_TO_INSET_OFFSET) + 5)) >= 0)
154                 w -= (2 * TEXT_TO_INSET_OFFSET) + 5;
155
156         // Deep magic. I don't understand this either.
157         if (owner() && owner()->owner()) {
158                 // add the right paper margin
159                 w -= 20;
160         }
161
162         // FIXME: why ?
163         if (w < 10) {
164                 w = 10;
165         }
166         return w;
167 }
168
169
170 LyXCursor const & Inset::cursor(BufferView * bv) const
171 {
172         if (owner())
173                 return owner()->getLyXText(bv, false)->cursor;
174         return bv->text->cursor;
175 }
176
177
178 WordLangTuple const
179 UpdatableInset::selectNextWordToSpellcheck(BufferView *bv, float & value) const
180 {
181         // we have to unlock ourself in this function by default!
182         bv->unlockInset(const_cast<UpdatableInset *>(this));
183         value = 0;
184         return WordLangTuple();
185 }
186
187
188 bool UpdatableInset::nextChange(BufferView * bv, lyx::pos_type &)
189 {
190         // we have to unlock ourself in this function by default!
191         bv->unlockInset(const_cast<UpdatableInset *>(this));
192         return false;
193 }
194
195
196 bool UpdatableInset::searchForward(BufferView * bv, string const &,
197                                    bool, bool)
198 {
199         // we have to unlock ourself in this function by default!
200         bv->unlockInset(const_cast<UpdatableInset *>(this));
201         return false;
202 }
203
204
205 bool UpdatableInset::searchBackward(BufferView * bv, string const &,
206                                     bool, bool)
207 {
208         // we have to unlock ourself in this function by default!
209         bv->unlockInset(const_cast<UpdatableInset *>(this));
210         return false;
211 }