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