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