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