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