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