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