]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
Inset::translate() -> lyx::insetCode()
[lyx.git] / src / insets / Inset.cpp
1 /**
2  * \file Inset.cpp
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  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Inset.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Color.h"
23 #include "CoordCache.h"
24 #include "Cursor.h"
25 #include "debug.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "gettext.h"
31 #include "Text.h"
32 #include "TextClass.h"
33 #include "MetricsInfo.h"
34 #include "MetricsInfo.h"
35
36 #include "frontends/Painter.h"
37
38 #include "support/convert.h"
39
40 #include <boost/current_function.hpp>
41
42 #include <map>
43 #include <typeinfo>
44
45
46 namespace lyx {
47
48 class InsetName {
49 public:
50         InsetName(std::string const & n, InsetCode c)
51                 : name(n), code(c) {}
52         std::string name;
53         InsetCode code;
54 };
55
56
57 typedef std::map<std::string, InsetCode> TranslatorMap;
58
59
60 static TranslatorMap const build_translator()
61 {
62         InsetName const insetnames[] = {
63                 InsetName("toc", TOC_CODE),
64                 InsetName("quote", QUOTE_CODE),
65                 InsetName("ref", REF_CODE),
66                 InsetName("url", URL_CODE),
67                 InsetName("htmlurl", HTMLURL_CODE),
68                 InsetName("separator", SEPARATOR_CODE),
69                 InsetName("ending", ENDING_CODE),
70                 InsetName("label", LABEL_CODE),
71                 InsetName("note", NOTE_CODE),
72                 InsetName("accent", ACCENT_CODE),
73                 InsetName("math", MATH_CODE),
74                 InsetName("index", INDEX_CODE),
75                 InsetName("nomenclature", NOMENCL_CODE),
76                 InsetName("include", INCLUDE_CODE),
77                 InsetName("graphics", GRAPHICS_CODE),
78                 InsetName("bibitem", BIBITEM_CODE),
79                 InsetName("bibtex", BIBTEX_CODE),
80                 InsetName("text", TEXT_CODE),
81                 InsetName("ert", ERT_CODE),
82                 InsetName("foot", FOOT_CODE),
83                 InsetName("margin", MARGIN_CODE),
84                 InsetName("float", FLOAT_CODE),
85                 InsetName("wrap", WRAP_CODE),
86                 InsetName("specialchar", SPECIALCHAR_CODE),
87                 InsetName("tabular", TABULAR_CODE),
88                 InsetName("external", EXTERNAL_CODE),
89                 InsetName("caption", CAPTION_CODE),
90                 InsetName("mathmacro", MATHMACRO_CODE),
91                 InsetName("citation", CITE_CODE),
92                 InsetName("floatlist", FLOAT_LIST_CODE),
93                 InsetName("index_print", INDEX_PRINT_CODE),
94                 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
95                 InsetName("optarg", OPTARG_CODE),
96                 InsetName("environment", ENVIRONMENT_CODE),
97                 InsetName("hfill", HFILL_CODE),
98                 InsetName("newline", NEWLINE_CODE),
99                 InsetName("line", LINE_CODE),
100                 InsetName("branch", BRANCH_CODE),
101                 InsetName("box", BOX_CODE),
102                 InsetName("flex", FLEX_CODE),
103                 InsetName("vspace", VSPACE_CODE),
104                 InsetName("mathmacroarg", MATHMACROARG_CODE),
105                 InsetName("listings", LISTINGS_CODE),
106                 InsetName("info", INFO_CODE),
107         };
108
109         std::size_t const insetnames_size =
110                 sizeof(insetnames) / sizeof(insetnames[0]);
111
112         std::map<std::string, InsetCode> data;
113         for (std::size_t i = 0; i != insetnames_size; ++i) {
114                 InsetName const & var = insetnames[i];
115                 data[var.name] = var.code;
116         }
117
118         return data;
119 }
120
121
122 Inset::Inset()
123 {}
124
125
126 Dimension const Inset::dimension(BufferView const & bv) const
127 {
128         return bv.coordCache().getInsets().dim(this);
129 }
130
131
132 InsetCode insetCode(std::string const & name)
133 {
134         static TranslatorMap const translator = build_translator();
135
136         TranslatorMap::const_iterator it = translator.find(name);
137         return it == translator.end() ? NO_CODE : it->second;
138 }
139
140
141 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
142 {
143         cur.updateFlags(Update::Force | Update::FitCursor);
144         cur.dispatched();
145         doDispatch(cur, cmd);
146 }
147
148
149 void Inset::doDispatch(Cursor & cur, FuncRequest &)
150 {
151         cur.noUpdate();
152         cur.undispatched();
153 }
154
155
156 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
157         FuncStatus & flag) const
158 {
159         // LFUN_INSET_APPLY is sent from the dialogs when the data should
160         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
161         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
162         // not belong to us, i. e. the dialog was open, and the user moved
163         // the cursor in our inset) in LyXFunc::getStatus().
164         // Dialogs::checkStatus() ensures that the dialog is deactivated if
165         // LFUN_INSET_APPLY is disabled.
166
167         switch (cmd.action) {
168         case LFUN_INSET_MODIFY:
169                 // Allow modification of our data.
170                 // This needs to be handled in the doDispatch method of our
171                 // instantiatable children.
172                 flag.enabled(true);
173                 return true;
174
175         case LFUN_INSET_INSERT:
176                 // Don't allow insertion of new insets.
177                 // Every inset that wants to allow new insets from open
178                 // dialogs needs to override this.
179                 flag.enabled(false);
180                 return true;
181
182         default:
183                 return false;
184         }
185 }
186
187
188 void Inset::edit(Cursor &, bool)
189 {
190         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
191                               << ": edit left/right" << std::endl;
192 }
193
194
195 Inset * Inset::editXY(Cursor &, int x, int y)
196 {
197         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
198                               << ": x=" << x << " y= " << y
199                               << std::endl;
200         return this;
201 }
202
203
204 Inset::idx_type Inset::index(row_type row, col_type col) const
205 {
206         if (row != 0)
207                 lyxerr << BOOST_CURRENT_FUNCTION
208                        << ": illegal row: " << row << std::endl;
209         if (col != 0)
210                 lyxerr << BOOST_CURRENT_FUNCTION
211                        << ": illegal col: " << col << std::endl;
212         return 0;
213 }
214
215
216 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
217 {
218         return from <= idx && idx <= to;
219 }
220
221
222 bool Inset::idxUpDown(Cursor &, bool) const
223 {
224         return false;
225 }
226
227
228 int Inset::docbook(Buffer const &,
229         odocstream &, OutputParams const &) const
230 {
231         return 0;
232 }
233
234
235 bool Inset::directWrite() const
236 {
237         return false;
238 }
239
240
241 Inset::EDITABLE Inset::editable() const
242 {
243         return NOT_EDITABLE;
244 }
245
246
247 bool Inset::autoDelete() const
248 {
249         return false;
250 }
251
252
253 docstring const Inset::editMessage() const
254 {
255         return _("Opened inset");
256 }
257
258
259 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
260                 bool, int & x, int & y) const
261 {
262         lyxerr << "Inset::cursorPos called directly" << std::endl;
263         x = 100;
264         y = 100;
265 }
266
267
268 void Inset::metricsMarkers(Dimension & dim, int framesize) const
269 {
270         dim.wid += 2 * framesize;
271         dim.des += framesize;
272 }
273
274
275 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
276 {
277         dim.wid += 2 * framesize;
278         dim.asc += framesize;
279         dim.des += framesize;
280 }
281
282
283 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
284 {
285         Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
286                 Color::mathframe : Color::mathcorners;
287
288         Dimension const dim = dimension(*pi.base.bv);
289
290         int const t = x + dim.width() - 1;
291         int const d = y + dim.descent();
292         pi.pain.line(x, d - 3, x, d, pen_color);
293         pi.pain.line(t, d - 3, t, d, pen_color);
294         pi.pain.line(x, d, x + 3, d, pen_color);
295         pi.pain.line(t - 3, d, t, d, pen_color);
296         setPosCache(pi, x, y);
297 }
298
299
300 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
301 {
302         Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
303                 Color::mathframe : Color::mathcorners;
304
305         drawMarkers(pi, x, y);
306         Dimension const dim = dimension(*pi.base.bv);
307         int const t = x + dim.width() - 1;
308         int const a = y - dim.ascent();
309         pi.pain.line(x, a + 3, x, a, pen_color);
310         pi.pain.line(t, a + 3, t, a, pen_color);
311         pi.pain.line(x, a, x + 3, a, pen_color);
312         pi.pain.line(t - 3, a, t, a, pen_color);
313         setPosCache(pi, x, y);
314 }
315
316
317 bool Inset::editing(BufferView * bv) const
318 {
319         return bv->cursor().isInside(this);
320 }
321
322
323 int Inset::xo(BufferView const & bv) const
324 {
325         return bv.coordCache().getInsets().x(this);
326 }
327
328
329 int Inset::yo(BufferView const & bv) const
330 {
331         return bv.coordCache().getInsets().y(this);
332 }
333
334
335 bool Inset::covers(BufferView const & bv, int x, int y) const
336 {
337         return bv.coordCache().getInsets().covers(this, x, y);
338 }
339
340
341 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
342 {
343         return bp.getTextClass().insetlayout(name());  
344 }
345
346
347 void Inset::dump() const
348 {
349         Buffer buf("foo", 1);
350         write(buf, lyxerr);
351 }
352
353
354 Color_color Inset::backgroundColor() const
355 {
356         return Color::background;
357 }
358
359
360 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
361 {
362         //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
363         pi.base.bv->coordCache().insets().add(this, x, y);
364 }
365
366
367 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
368 {
369         mi.base.bv->coordCache().insets().add(this, dim);
370 }
371
372
373
374 /////////////////////////////////////////
375
376 bool isEditableInset(Inset const * inset)
377 {
378         return inset && inset->editable();
379 }
380
381
382 bool isHighlyEditableInset(Inset const * inset)
383 {
384         return inset && inset->editable() == Inset::HIGHLY_EDITABLE;
385 }
386
387
388 } // namespace lyx