]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
Change inset label from ": filename" to "Program Listing: filename" for listings...
[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/lassert.h"
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
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("accent", ACCENT_CODE),
73                 InsetName("math", MATH_CODE),
74                 InsetName("index", INDEX_CODE),
75                 InsetName("nomenclature", NOMENCL_CODE),
76                 InsetName("include", INCLUDE_CODE),
77                 InsetName("graphics", GRAPHICS_CODE),
78                 InsetName("bibitem", BIBITEM_CODE),
79                 InsetName("bibtex", BIBTEX_CODE),
80                 InsetName("text", TEXT_CODE),
81                 InsetName("ert", ERT_CODE),
82                 InsetName("foot", FOOT_CODE),
83                 InsetName("margin", MARGIN_CODE),
84                 InsetName("float", FLOAT_CODE),
85                 InsetName("wrap", WRAP_CODE),
86                 InsetName("specialchar", SPECIALCHAR_CODE),
87                 InsetName("tabular", TABULAR_CODE),
88                 InsetName("external", EXTERNAL_CODE),
89                 InsetName("caption", CAPTION_CODE),
90                 InsetName("mathmacro", MATHMACRO_CODE),
91                 InsetName("citation", CITE_CODE),
92                 InsetName("floatlist", FLOAT_LIST_CODE),
93                 InsetName("index_print", INDEX_PRINT_CODE),
94                 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
95                 InsetName("optarg", OPTARG_CODE),
96                 InsetName("environment", ENVIRONMENT_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 docstring Inset::name() const
152 {
153         return from_ascii("unknown");
154 }
155
156
157 void Inset::initView()
158 {
159         if (isLabeled())
160                 lyx::updateLabels(buffer());
161 }
162
163
164 docstring Inset::toolTip(BufferView const &, int, int) const
165 {
166         return docstring();
167 }
168
169
170 docstring Inset::contextMenu(BufferView const &, int, int) const
171 {
172         return docstring();
173 }
174
175
176 Dimension const Inset::dimension(BufferView const & bv) const
177 {
178         return bv.coordCache().getInsets().dim(this);
179 }
180
181
182 InsetCode insetCode(string const & name)
183 {
184         static TranslatorMap const translator = build_translator();
185
186         TranslatorMap::const_iterator it = translator.find(name);
187         return it == translator.end() ? NO_CODE : it->second;
188 }
189
190
191 string insetName(InsetCode c) 
192 {
193         static TranslatorMap const translator = build_translator();
194
195         TranslatorMap::const_iterator it =  translator.begin();
196         TranslatorMap::const_iterator end = translator.end();
197         for (; it != end; ++it) {
198                 if (it->second == c)
199                         return it->first;
200         }
201         return string();
202 }
203
204
205 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
206 {
207         cur.updateFlags(Update::Force | Update::FitCursor);
208         cur.dispatched();
209         doDispatch(cur, cmd);
210 }
211
212
213 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
214 {
215         switch (cmd.action) {
216         case LFUN_INSET_TOGGLE:
217                 edit(cur, true);
218                 cur.dispatched();
219                 break;
220         default:
221                 cur.noUpdate();
222                 cur.undispatched();
223                 break;
224         }
225 }
226
227
228 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
229         FuncStatus & flag) const
230 {
231         // LFUN_INSET_APPLY is sent from the dialogs when the data should
232         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
233         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
234         // not belong to us, i. e. the dialog was open, and the user moved
235         // the cursor in our inset) in LyXFunc::getStatus().
236         // Dialogs::checkStatus() ensures that the dialog is deactivated if
237         // LFUN_INSET_APPLY is disabled.
238
239         switch (cmd.action) {
240         case LFUN_INSET_MODIFY:
241                 // Allow modification of our data.
242                 // This needs to be handled in the doDispatch method of our
243                 // instantiatable children.
244                 flag.setEnabled(true);
245                 return true;
246
247         case LFUN_INSET_INSERT:
248                 // Don't allow insertion of new insets.
249                 // Every inset that wants to allow new insets from open
250                 // dialogs needs to override this.
251                 flag.setEnabled(false);
252                 return true;
253
254         case LFUN_INSET_TOGGLE:
255                 // remove this if we dissociate toggle from edit.
256                 flag.setEnabled(editable() == IS_EDITABLE);
257                 return true;
258
259         default:
260                 break;
261         }
262         return false;
263 }
264
265
266 void Inset::edit(Cursor &, bool, EntryDirection)
267 {
268         LYXERR(Debug::INSETS, "edit left/right");
269 }
270
271
272 Inset * Inset::editXY(Cursor &, int x, int y)
273 {
274         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
275         return this;
276 }
277
278
279 Inset::idx_type Inset::index(row_type row, col_type col) const
280 {
281         if (row != 0)
282                 LYXERR0("illegal row: " << row);
283         if (col != 0)
284                 LYXERR0("illegal col: " << col);
285         return 0;
286 }
287
288
289 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
290 {
291         return from <= idx && idx <= to;
292 }
293
294
295 bool Inset::idxUpDown(Cursor &, bool) const
296 {
297         return false;
298 }
299
300
301 int Inset::docbook(odocstream &, OutputParams const &) const
302 {
303         return 0;
304 }
305
306
307 bool Inset::directWrite() const
308 {
309         return false;
310 }
311
312
313 Inset::EDITABLE Inset::editable() const
314 {
315         return NOT_EDITABLE;
316 }
317
318
319 bool Inset::autoDelete() const
320 {
321         return false;
322 }
323
324
325 docstring Inset::editMessage() const
326 {
327         return _("Opened inset");
328 }
329
330
331 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
332                 bool, int & x, int & y) const
333 {
334         LYXERR0("Inset::cursorPos called directly");
335         x = 100;
336         y = 100;
337 }
338
339
340 void Inset::metricsMarkers(Dimension & dim, int framesize) const
341 {
342         dim.wid += 2 * framesize;
343         dim.des += framesize;
344 }
345
346
347 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
348 {
349         dim.wid += 2 * framesize;
350         dim.asc += framesize;
351         dim.des += framesize;
352 }
353
354
355 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
356 {
357         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
358                 Color_mathframe : Color_mathcorners;
359
360         Dimension const dim = dimension(*pi.base.bv);
361
362         int const t = x + dim.width() - 1;
363         int const d = y + dim.descent();
364         pi.pain.line(x, d - 3, x, d, pen_color);
365         pi.pain.line(t, d - 3, t, d, pen_color);
366         pi.pain.line(x, d, x + 3, d, pen_color);
367         pi.pain.line(t - 3, d, t, d, pen_color);
368         setPosCache(pi, x, y);
369 }
370
371
372 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
373 {
374         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
375                 Color_mathframe : Color_mathcorners;
376
377         drawMarkers(pi, x, y);
378         Dimension const dim = dimension(*pi.base.bv);
379         int const t = x + dim.width() - 1;
380         int const a = y - dim.ascent();
381         pi.pain.line(x, a + 3, x, a, pen_color);
382         pi.pain.line(t, a + 3, t, a, pen_color);
383         pi.pain.line(x, a, x + 3, a, pen_color);
384         pi.pain.line(t - 3, a, t, a, pen_color);
385         setPosCache(pi, x, y);
386 }
387
388
389 bool Inset::editing(BufferView const * bv) const
390 {
391         return bv->cursor().isInside(this);
392 }
393
394
395 int Inset::xo(BufferView const & bv) const
396 {
397         return bv.coordCache().getInsets().x(this);
398 }
399
400
401 int Inset::yo(BufferView const & bv) const
402 {
403         return bv.coordCache().getInsets().y(this);
404 }
405
406
407 bool Inset::covers(BufferView const & bv, int x, int y) const
408 {
409         return bv.coordCache().getInsets().covers(this, x, y);
410 }
411
412
413 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
414 {
415         return bp.documentClass().insetLayout(name());  
416 }
417
418
419 void Inset::dump() const
420 {
421         write(lyxerr);
422 }
423
424
425 ColorCode Inset::backgroundColor() const
426 {
427         return Color_background;
428 }
429
430
431 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
432 {
433         //LYXERR("Inset: set position cache to " << x << " " << y);
434         pi.base.bv->coordCache().insets().add(this, x, y);
435 }
436
437
438 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
439 {
440         mi.base.bv->coordCache().insets().add(this, dim);
441 }
442
443
444 Buffer const * Inset::updateFrontend() const
445 {
446         return theApp() ? theApp()->updateInset(this) : 0;
447 }
448
449
450 docstring Inset::completionPrefix(Cursor const &) const 
451 {
452         return docstring();
453 }
454
455 } // namespace lyx