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