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