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