]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
c12fbdc4698d72420f90a29d94a4601bc1b1755d
[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("newline", NEWLINE_CODE),
97                 InsetName("line", LINE_CODE),
98                 InsetName("branch", BRANCH_CODE),
99                 InsetName("box", BOX_CODE),
100                 InsetName("flex", FLEX_CODE),
101                 InsetName("space", SPACE_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                 InsetName("tablecell", CELL_CODE)
109         };
110
111         size_t const insetnames_size =
112                 sizeof(insetnames) / sizeof(insetnames[0]);
113
114         map<string, InsetCode> data;
115         for (size_t i = 0; i != insetnames_size; ++i) {
116                 InsetName const & var = insetnames[i];
117                 data[var.name] = var.code;
118         }
119
120         return data;
121 }
122
123
124 void Inset::setBuffer(Buffer & buffer)
125 {
126         buffer_ = &buffer;
127 }
128
129
130 Buffer & Inset::buffer()
131 {
132         if (!buffer_) {
133                 odocstringstream s;
134                 lyxerr << "LyX Code: " << lyxCode() << " name: " << name() << std::endl;
135                 s << "LyX Code: " << lyxCode() << " name: " << name();
136                 BOOST_ASSERT(false);
137                 throw ExceptionMessage(BufferException, 
138                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
139         }
140         return *buffer_;
141 }
142
143
144 Buffer const & Inset::buffer() const
145 {
146         return const_cast<Inset *>(this)->buffer();
147 }
148
149
150 docstring Inset::name() const
151 {
152         return from_ascii("unknown");
153 }
154
155
156 void Inset::initView()
157 {
158         if (isLabeled())
159                 lyx::updateLabels(buffer());
160 }
161
162
163 docstring Inset::toolTip(BufferView const &, int, int) const
164 {
165         return docstring();
166 }
167
168
169 docstring Inset::contextMenu(BufferView const &, int, int) const
170 {
171         return docstring();
172 }
173
174
175 Dimension const Inset::dimension(BufferView const & bv) const
176 {
177         return bv.coordCache().getInsets().dim(this);
178 }
179
180
181 InsetCode insetCode(string const & name)
182 {
183         static TranslatorMap const translator = build_translator();
184
185         TranslatorMap::const_iterator it = translator.find(name);
186         return it == translator.end() ? NO_CODE : it->second;
187 }
188
189
190 string insetName(InsetCode c) 
191 {
192         static TranslatorMap const translator = build_translator();
193
194         TranslatorMap::const_iterator it =  translator.begin();
195         TranslatorMap::const_iterator end = translator.end();
196         for (; it != end; ++it) {
197                 if (it->second == c)
198                         return it->first;
199         }
200         return string();
201 }
202
203
204 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
205 {
206         cur.updateFlags(Update::Force | Update::FitCursor);
207         cur.dispatched();
208         doDispatch(cur, cmd);
209 }
210
211
212 void Inset::doDispatch(Cursor & cur, FuncRequest &)
213 {
214         cur.noUpdate();
215         cur.undispatched();
216 }
217
218
219 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
220         FuncStatus & flag) const
221 {
222         // LFUN_INSET_APPLY is sent from the dialogs when the data should
223         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
224         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
225         // not belong to us, i. e. the dialog was open, and the user moved
226         // the cursor in our inset) in LyXFunc::getStatus().
227         // Dialogs::checkStatus() ensures that the dialog is deactivated if
228         // LFUN_INSET_APPLY is disabled.
229
230         switch (cmd.action) {
231         case LFUN_INSET_MODIFY:
232                 // Allow modification of our data.
233                 // This needs to be handled in the doDispatch method of our
234                 // instantiatable children.
235                 flag.enabled(true);
236                 return true;
237
238         case LFUN_INSET_INSERT:
239                 // Don't allow insertion of new insets.
240                 // Every inset that wants to allow new insets from open
241                 // dialogs needs to override this.
242                 flag.enabled(false);
243                 return true;
244
245         default:
246                 return false;
247         }
248 }
249
250
251 void Inset::edit(Cursor &, bool, EntryDirection)
252 {
253         LYXERR(Debug::INSETS, "edit left/right");
254 }
255
256
257 Inset * Inset::editXY(Cursor &, int x, int y)
258 {
259         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
260         return this;
261 }
262
263
264 Inset::idx_type Inset::index(row_type row, col_type col) const
265 {
266         if (row != 0)
267                 LYXERR0("illegal row: " << row);
268         if (col != 0)
269                 LYXERR0("illegal col: " << col);
270         return 0;
271 }
272
273
274 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
275 {
276         return from <= idx && idx <= to;
277 }
278
279
280 bool Inset::idxUpDown(Cursor &, bool) const
281 {
282         return false;
283 }
284
285
286 int Inset::docbook(odocstream &, OutputParams const &) const
287 {
288         return 0;
289 }
290
291
292 bool Inset::directWrite() const
293 {
294         return false;
295 }
296
297
298 Inset::EDITABLE Inset::editable() const
299 {
300         return NOT_EDITABLE;
301 }
302
303
304 bool Inset::autoDelete() const
305 {
306         return false;
307 }
308
309
310 docstring Inset::editMessage() const
311 {
312         return _("Opened inset");
313 }
314
315
316 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
317                 bool, int & x, int & y) const
318 {
319         LYXERR0("Inset::cursorPos called directly");
320         x = 100;
321         y = 100;
322 }
323
324
325 void Inset::metricsMarkers(Dimension & dim, int framesize) const
326 {
327         dim.wid += 2 * framesize;
328         dim.des += framesize;
329 }
330
331
332 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
333 {
334         dim.wid += 2 * framesize;
335         dim.asc += framesize;
336         dim.des += framesize;
337 }
338
339
340 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
341 {
342         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
343                 Color_mathframe : Color_mathcorners;
344
345         Dimension const dim = dimension(*pi.base.bv);
346
347         int const t = x + dim.width() - 1;
348         int const d = y + dim.descent();
349         pi.pain.line(x, d - 3, x, d, pen_color);
350         pi.pain.line(t, d - 3, t, d, pen_color);
351         pi.pain.line(x, d, x + 3, d, pen_color);
352         pi.pain.line(t - 3, d, t, d, pen_color);
353         setPosCache(pi, x, y);
354 }
355
356
357 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
358 {
359         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
360                 Color_mathframe : Color_mathcorners;
361
362         drawMarkers(pi, x, y);
363         Dimension const dim = dimension(*pi.base.bv);
364         int const t = x + dim.width() - 1;
365         int const a = y - dim.ascent();
366         pi.pain.line(x, a + 3, x, a, pen_color);
367         pi.pain.line(t, a + 3, t, a, pen_color);
368         pi.pain.line(x, a, x + 3, a, pen_color);
369         pi.pain.line(t - 3, a, t, a, pen_color);
370         setPosCache(pi, x, y);
371 }
372
373
374 bool Inset::editing(BufferView const * bv) const
375 {
376         return bv->cursor().isInside(this);
377 }
378
379
380 int Inset::xo(BufferView const & bv) const
381 {
382         return bv.coordCache().getInsets().x(this);
383 }
384
385
386 int Inset::yo(BufferView const & bv) const
387 {
388         return bv.coordCache().getInsets().y(this);
389 }
390
391
392 bool Inset::covers(BufferView const & bv, int x, int y) const
393 {
394         return bv.coordCache().getInsets().covers(this, x, y);
395 }
396
397
398 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
399 {
400         return bp.documentClass().insetLayout(name());  
401 }
402
403
404 void Inset::dump() const
405 {
406         write(lyxerr);
407 }
408
409
410 ColorCode Inset::backgroundColor() const
411 {
412         return Color_background;
413 }
414
415
416 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
417 {
418         //LYXERR("Inset: set position cache to " << x << " " << y);
419         pi.base.bv->coordCache().insets().add(this, x, y);
420 }
421
422
423 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
424 {
425         mi.base.bv->coordCache().insets().add(this, dim);
426 }
427
428
429 Buffer const * Inset::updateFrontend() const
430 {
431         return theApp() ? theApp()->updateInset(this) : 0;
432 }
433
434
435 docstring Inset::completionPrefix(Cursor const &) const 
436 {
437         return docstring();
438 }
439
440 } // namespace lyx