]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.C
remove per-inset position cahce, use new external map instead.
[lyx.git] / src / insets / insetbase.C
1 /**
2  * \file insetbase.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetbase.h"
14
15 #include "buffer.h"
16 #include "coordcache.h"
17 #include "BufferView.h"
18 #include "LColor.h"
19 #include "cursor.h"
20 #include "debug.h"
21 #include "dimension.h"
22 #include "dispatchresult.h"
23 #include "gettext.h"
24 #include "lyxtext.h"
25 #include "metricsinfo.h"
26
27 #include "frontends/Painter.h"
28
29 #include <map>
30
31
32 namespace {
33
34 struct InsetName {
35         InsetName(std::string const & n, InsetBase::Code c)
36                 : name(n), code(c) {}
37         std::string name;
38         InsetBase::Code code;
39 };
40
41
42 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
43
44
45 TranslatorMap const build_translator()
46 {
47         InsetName const insetnames[] = {
48                 InsetName("toc", InsetBase::TOC_CODE),
49                 InsetName("quote", InsetBase::QUOTE_CODE),
50                 InsetName("ref", InsetBase::REF_CODE),
51                 InsetName("url", InsetBase::URL_CODE),
52                 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
53                 InsetName("separator", InsetBase::SEPARATOR_CODE),
54                 InsetName("ending", InsetBase::ENDING_CODE),
55                 InsetName("label", InsetBase::LABEL_CODE),
56                 InsetName("note", InsetBase::NOTE_CODE),
57                 InsetName("accent", InsetBase::ACCENT_CODE),
58                 InsetName("math", InsetBase::MATH_CODE),
59                 InsetName("index", InsetBase::INDEX_CODE),
60                 InsetName("include", InsetBase::INCLUDE_CODE),
61                 InsetName("graphics", InsetBase::GRAPHICS_CODE),
62                 InsetName("bibitem", InsetBase::BIBITEM_CODE),
63                 InsetName("bibtex", InsetBase::BIBTEX_CODE),
64                 InsetName("text", InsetBase::TEXT_CODE),
65                 InsetName("ert", InsetBase::ERT_CODE),
66                 InsetName("foot", InsetBase::FOOT_CODE),
67                 InsetName("margin", InsetBase::MARGIN_CODE),
68                 InsetName("float", InsetBase::FLOAT_CODE),
69                 InsetName("wrap", InsetBase::WRAP_CODE),
70                 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
71                 InsetName("tabular", InsetBase::TABULAR_CODE),
72                 InsetName("external", InsetBase::EXTERNAL_CODE),
73                 InsetName("caption", InsetBase::CAPTION_CODE),
74                 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
75                 InsetName("error", InsetBase::ERROR_CODE),
76                 InsetName("cite", InsetBase::CITE_CODE),
77                 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
78                 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
79                 InsetName("optarg", InsetBase::OPTARG_CODE),
80                 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
81                 InsetName("hfill", InsetBase::HFILL_CODE),
82                 InsetName("newline", InsetBase::NEWLINE_CODE),
83                 InsetName("line", InsetBase::LINE_CODE),
84                 InsetName("branch", InsetBase::BRANCH_CODE),
85                 InsetName("box", InsetBase::BOX_CODE),
86                 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
87                 InsetName("vspace", InsetBase::VSPACE_CODE),
88                 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
89         };
90
91         std::size_t const insetnames_size =
92                 sizeof(insetnames) / sizeof(insetnames[0]);
93
94         std::map<std::string, InsetBase::Code> data;
95         for (std::size_t i = 0; i != insetnames_size; ++i) {
96                 InsetName const & var = insetnames[i];
97                 data[var.name] = var.code;
98         }
99
100         return data;
101 }
102
103 } // namespace anon
104
105
106 InsetBase::Code InsetBase::translate(std::string const & name)
107 {
108         static TranslatorMap const translator = build_translator();
109
110         TranslatorMap::const_iterator it = translator.find(name);
111         return it == translator.end() ? NO_CODE : it->second;
112 }
113
114
115 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
116 {
117         cur.needsUpdate();
118         cur.dispatched();
119         priv_dispatch(cur, cmd);
120 }
121
122
123 void InsetBase::priv_dispatch(LCursor & cur, FuncRequest &)
124 {
125         cur.noUpdate();
126         cur.undispatched();
127 }
128
129
130 bool InsetBase::getStatus(LCursor &, FuncRequest const &, FuncStatus &) const
131 {
132         return false;
133 }
134
135
136 void InsetBase::edit(LCursor &, bool)
137 {
138         lyxerr << "InsetBase: edit left/right" << std::endl;
139 }
140
141
142 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
143 {
144         lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
145         return const_cast<InsetBase*>(this);
146 }
147
148
149 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
150 {
151         if (row != 0)
152                 lyxerr << "illegal row: " << row << std::endl;
153         if (col != 0)
154                 lyxerr << "illegal col: " << col << std::endl;
155         return 0;
156 }
157
158
159 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
160 {
161         return from <= idx && idx <= to;
162 }
163
164
165 bool InsetBase::idxUpDown(LCursor &, bool) const
166 {
167         return false;
168 }
169
170
171 bool InsetBase::idxUpDown2(LCursor &, bool) const
172 {
173         return false;
174 }
175
176
177 int InsetBase::plaintext(Buffer const &,
178         std::ostream &, OutputParams const &) const
179 {
180         return 0;
181 }
182
183
184 int InsetBase::linuxdoc(Buffer const &,
185         std::ostream &, OutputParams const &) const
186 {
187         return 0;
188 }
189
190
191 int InsetBase::docbook(Buffer const &,
192         std::ostream &, OutputParams const &) const
193 {
194         return 0;
195 }
196
197
198 bool InsetBase::directWrite() const
199 {
200         return false;
201 }
202
203
204 InsetBase::EDITABLE InsetBase::editable() const
205 {
206         return NOT_EDITABLE;
207 }
208
209
210 bool InsetBase::autoDelete() const
211 {
212         return false;
213 }
214
215
216 std::string const InsetBase::editMessage() const
217 {
218         return _("Opened inset");
219 }
220
221
222 std::string const & InsetBase::getInsetName() const
223 {
224         static std::string const name = "unknown";
225         return name;
226 }
227
228
229 void InsetBase::markErased()
230 {}
231
232
233 void InsetBase::getCursorPos(LCursor const &, int & x, int & y) const
234 {
235         lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
236         x = 100;
237         y = 100;
238 }
239
240
241 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
242 {
243         dim.wid += 2 * framesize;
244         dim.asc += framesize;
245 }
246
247
248 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
249 {
250         dim.wid += 2 * framesize;
251         dim.asc += framesize;
252         dim.des += framesize;
253 }
254
255
256 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
257 {
258         if (!editing(pi.base.bv))
259                 return;
260         int const t = x + width() - 1;
261         int const d = y + descent();
262         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
263         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
264         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
265         pi.pain.line(t - 3, d, t, d, LColor::mathframe);
266         setPosCache(pi, x, y);
267 }
268
269
270 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
271 {
272         if (!editing(pi.base.bv))
273                 return;
274         drawMarkers(pi, x, y);
275         int const t = x + width() - 1;
276         int const a = y - ascent();
277         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
278         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
279         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
280         pi.pain.line(t - 3, a, t, a, LColor::mathframe);
281         setPosCache(pi, x, y);
282 }
283
284
285 bool InsetBase::editing(BufferView * bv) const
286 {
287         return bv->cursor().isInside(this);
288 }
289
290
291 int InsetBase::xo() const
292 {
293         return theCoords.insets_.x(this);
294 }
295
296
297 int InsetBase::yo() const
298 {
299         return theCoords.insets_.y(this);
300 }
301
302
303 bool InsetBase::covers(int x, int y) const
304 {
305         //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
306         //      << " xo: " << xo() << " yo: " << yo()
307         //      << " x1: " << xo() << " x2: " << xo() + width()
308         //      << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
309         //      << std::endl;
310         return theCoords.insets_.has(this)
311                         && x >= xo()
312                         && x <= xo() + width()
313                         && y >= yo() - ascent()
314                         && y <= yo() + descent();
315 }
316
317
318 void InsetBase::dump() const
319 {
320         Buffer buf("foo", 1);
321         write(buf, lyxerr);
322 }
323
324
325 /////////////////////////////////////////
326
327 bool isEditableInset(InsetBase const * inset)
328 {
329         return inset && inset->editable();
330 }
331
332
333 bool isHighlyEditableInset(InsetBase const * inset)
334 {
335         return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;
336 }