]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
952aa0a2a286a80b43a86af891cecf2d263b79f8
[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 "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 #include "frontends/Application.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 extern bool quitting;
49
50 class InsetName {
51 public:
52         InsetName(std::string const & n, InsetCode c) : name(n), code(c) {}
53         std::string name;
54         InsetCode code;
55 };
56
57
58 typedef std::map<std::string, InsetCode> TranslatorMap;
59
60
61 static TranslatorMap const build_translator()
62 {
63         InsetName const insetnames[] = {
64                 InsetName("toc", TOC_CODE),
65                 InsetName("quote", QUOTE_CODE),
66                 InsetName("ref", REF_CODE),
67                 InsetName("href", HYPERLINK_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                 InsetName("collapsable", COLLAPSABLE_CODE),
108                 InsetName("newpage", NEWPAGE_CODE),
109         };
110
111         std::size_t const insetnames_size =
112                 sizeof(insetnames) / sizeof(insetnames[0]);
113
114         std::map<std::string, InsetCode> data;
115         for (std::size_t i = 0; i != insetnames_size; ++i) {
116                 InsetName const & var = insetnames[i];
117                 data[var.name] = var.code;
118         }
119
120         return data;
121 }
122
123
124 docstring Inset::name() const
125 {
126         return from_ascii("unknown");
127 }
128
129
130 Dimension const Inset::dimension(BufferView const & bv) const
131 {
132         return bv.coordCache().getInsets().dim(this);
133 }
134
135
136 InsetCode insetCode(std::string const & name)
137 {
138         static TranslatorMap const translator = build_translator();
139
140         TranslatorMap::const_iterator it = translator.find(name);
141         return it == translator.end() ? NO_CODE : it->second;
142 }
143
144
145 std::string insetName(InsetCode c) 
146 {
147         static TranslatorMap const translator = build_translator();
148
149         TranslatorMap::const_iterator it =  translator.begin();
150         TranslatorMap::const_iterator end = translator.end();
151         for (; it != end; ++it) {
152                 if (it->second == c)
153                         return it->first;
154         }
155         return std::string();
156 }
157
158
159 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
160 {
161         cur.updateFlags(Update::Force | Update::FitCursor);
162         cur.dispatched();
163         doDispatch(cur, cmd);
164 }
165
166
167 void Inset::doDispatch(Cursor & cur, FuncRequest &)
168 {
169         cur.noUpdate();
170         cur.undispatched();
171 }
172
173
174 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
175         FuncStatus & flag) const
176 {
177         // LFUN_INSET_APPLY is sent from the dialogs when the data should
178         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
179         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
180         // not belong to us, i. e. the dialog was open, and the user moved
181         // the cursor in our inset) in LyXFunc::getStatus().
182         // Dialogs::checkStatus() ensures that the dialog is deactivated if
183         // LFUN_INSET_APPLY is disabled.
184
185         switch (cmd.action) {
186         case LFUN_INSET_MODIFY:
187                 // Allow modification of our data.
188                 // This needs to be handled in the doDispatch method of our
189                 // instantiatable children.
190                 flag.enabled(true);
191                 return true;
192
193         case LFUN_INSET_INSERT:
194                 // Don't allow insertion of new insets.
195                 // Every inset that wants to allow new insets from open
196                 // dialogs needs to override this.
197                 flag.enabled(false);
198                 return true;
199
200         default:
201                 return false;
202         }
203 }
204
205
206 void Inset::edit(Cursor &, bool)
207 {
208         LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": edit left/right");
209 }
210
211
212 Inset * Inset::editXY(Cursor &, int x, int y)
213 {
214         LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": x=" << x << " y= " << y);
215         return this;
216 }
217
218
219 Inset::idx_type Inset::index(row_type row, col_type col) const
220 {
221         if (row != 0)
222                 lyxerr << BOOST_CURRENT_FUNCTION
223                        << ": illegal row: " << row << std::endl;
224         if (col != 0)
225                 lyxerr << BOOST_CURRENT_FUNCTION
226                        << ": illegal col: " << col << std::endl;
227         return 0;
228 }
229
230
231 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
232 {
233         return from <= idx && idx <= to;
234 }
235
236
237 bool Inset::idxUpDown(Cursor &, bool) const
238 {
239         return false;
240 }
241
242
243 int Inset::docbook(Buffer const &,
244         odocstream &, OutputParams const &) const
245 {
246         return 0;
247 }
248
249
250 bool Inset::directWrite() const
251 {
252         return false;
253 }
254
255
256 Inset::EDITABLE Inset::editable() const
257 {
258         return NOT_EDITABLE;
259 }
260
261
262 bool Inset::autoDelete() const
263 {
264         return false;
265 }
266
267
268 docstring const Inset::editMessage() const
269 {
270         return _("Opened inset");
271 }
272
273
274 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
275                 bool, int & x, int & y) const
276 {
277         lyxerr << "Inset::cursorPos called directly" << std::endl;
278         x = 100;
279         y = 100;
280 }
281
282
283 void Inset::metricsMarkers(Dimension & dim, int framesize) const
284 {
285         dim.wid += 2 * framesize;
286         dim.des += framesize;
287 }
288
289
290 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
291 {
292         dim.wid += 2 * framesize;
293         dim.asc += framesize;
294         dim.des += framesize;
295 }
296
297
298 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
299 {
300         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
301                 Color_mathframe : Color_mathcorners;
302
303         Dimension const dim = dimension(*pi.base.bv);
304
305         int const t = x + dim.width() - 1;
306         int const d = y + dim.descent();
307         pi.pain.line(x, d - 3, x, d, pen_color);
308         pi.pain.line(t, d - 3, t, d, pen_color);
309         pi.pain.line(x, d, x + 3, d, pen_color);
310         pi.pain.line(t - 3, d, t, d, pen_color);
311         setPosCache(pi, x, y);
312 }
313
314
315 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
316 {
317         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
318                 Color_mathframe : Color_mathcorners;
319
320         drawMarkers(pi, x, y);
321         Dimension const dim = dimension(*pi.base.bv);
322         int const t = x + dim.width() - 1;
323         int const a = y - dim.ascent();
324         pi.pain.line(x, a + 3, x, a, pen_color);
325         pi.pain.line(t, a + 3, t, a, pen_color);
326         pi.pain.line(x, a, x + 3, a, pen_color);
327         pi.pain.line(t - 3, a, t, a, pen_color);
328         setPosCache(pi, x, y);
329 }
330
331
332 bool Inset::editing(BufferView * bv) const
333 {
334         return bv->cursor().isInside(this);
335 }
336
337
338 int Inset::xo(BufferView const & bv) const
339 {
340         return bv.coordCache().getInsets().x(this);
341 }
342
343
344 int Inset::yo(BufferView const & bv) const
345 {
346         return bv.coordCache().getInsets().y(this);
347 }
348
349
350 bool Inset::covers(BufferView const & bv, int x, int y) const
351 {
352         return bv.coordCache().getInsets().covers(this, x, y);
353 }
354
355
356 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
357 {
358         return bp.getTextClass().insetlayout(name());  
359 }
360
361
362 void Inset::dump() const
363 {
364         Buffer buf("foo", 1);
365         write(buf, lyxerr);
366 }
367
368
369 ColorCode Inset::backgroundColor() const
370 {
371         return Color_background;
372 }
373
374
375 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
376 {
377         //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
378         pi.base.bv->coordCache().insets().add(this, x, y);
379 }
380
381
382 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
383 {
384         mi.base.bv->coordCache().insets().add(this, dim);
385 }
386
387
388 Buffer const * Inset::updateFrontend() const
389 {
390         if (quitting)
391                 return 0;
392         return theApp()->updateInset(this);
393 }
394
395 } // namespace lyx