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