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