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