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