]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
adjust
[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 "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "gettext.h"
31 #include "Text.h"
32 #include "TextClass.h"
33 #include "MetricsInfo.h"
34 #include "MetricsInfo.h"
35
36 #include "frontends/Painter.h"
37
38 #include "support/convert.h"
39
40 #include <boost/current_function.hpp>
41
42 #include <map>
43 #include <typeinfo>
44
45
46 namespace lyx {
47
48 class InsetName {
49 public:
50         InsetName(std::string const & n, InsetCode c)
51                 : name(n), code(c) {}
52         std::string name;
53         InsetCode code;
54 };
55
56
57 typedef std::map<std::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("hfill", HFILL_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("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("pagebreak", PAGEBREAK_CODE),
108         };
109
110         std::size_t const insetnames_size =
111                 sizeof(insetnames) / sizeof(insetnames[0]);
112
113         std::map<std::string, InsetCode> data;
114         for (std::size_t i = 0; i != insetnames_size; ++i) {
115                 InsetName const & var = insetnames[i];
116                 data[var.name] = var.code;
117         }
118
119         return data;
120 }
121
122
123 Inset::Inset()
124 {}
125
126
127 Dimension const Inset::dimension(BufferView const & bv) const
128 {
129         return bv.coordCache().getInsets().dim(this);
130 }
131
132
133 InsetCode insetCode(std::string const & name)
134 {
135         static TranslatorMap const translator = build_translator();
136
137         TranslatorMap::const_iterator it = translator.find(name);
138         return it == translator.end() ? NO_CODE : it->second;
139 }
140
141
142 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
143 {
144         cur.updateFlags(Update::Force | Update::FitCursor);
145         cur.dispatched();
146         doDispatch(cur, cmd);
147 }
148
149
150 void Inset::doDispatch(Cursor & cur, FuncRequest &)
151 {
152         cur.noUpdate();
153         cur.undispatched();
154 }
155
156
157 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
158         FuncStatus & flag) const
159 {
160         // LFUN_INSET_APPLY is sent from the dialogs when the data should
161         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
162         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
163         // not belong to us, i. e. the dialog was open, and the user moved
164         // the cursor in our inset) in LyXFunc::getStatus().
165         // Dialogs::checkStatus() ensures that the dialog is deactivated if
166         // LFUN_INSET_APPLY is disabled.
167
168         switch (cmd.action) {
169         case LFUN_INSET_MODIFY:
170                 // Allow modification of our data.
171                 // This needs to be handled in the doDispatch method of our
172                 // instantiatable children.
173                 flag.enabled(true);
174                 return true;
175
176         case LFUN_INSET_INSERT:
177                 // Don't allow insertion of new insets.
178                 // Every inset that wants to allow new insets from open
179                 // dialogs needs to override this.
180                 flag.enabled(false);
181                 return true;
182
183         default:
184                 return false;
185         }
186 }
187
188
189 void Inset::edit(Cursor &, bool)
190 {
191         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
192                               << ": edit left/right" << std::endl;
193 }
194
195
196 Inset * Inset::editXY(Cursor &, int x, int y)
197 {
198         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
199                               << ": x=" << x << " y= " << y
200                               << std::endl;
201         return this;
202 }
203
204
205 Inset::idx_type Inset::index(row_type row, col_type col) const
206 {
207         if (row != 0)
208                 lyxerr << BOOST_CURRENT_FUNCTION
209                        << ": illegal row: " << row << std::endl;
210         if (col != 0)
211                 lyxerr << BOOST_CURRENT_FUNCTION
212                        << ": illegal col: " << col << std::endl;
213         return 0;
214 }
215
216
217 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
218 {
219         return from <= idx && idx <= to;
220 }
221
222
223 bool Inset::idxUpDown(Cursor &, bool) const
224 {
225         return false;
226 }
227
228
229 int Inset::docbook(Buffer const &,
230         odocstream &, OutputParams const &) const
231 {
232         return 0;
233 }
234
235
236 bool Inset::directWrite() const
237 {
238         return false;
239 }
240
241
242 Inset::EDITABLE Inset::editable() const
243 {
244         return NOT_EDITABLE;
245 }
246
247
248 bool Inset::autoDelete() const
249 {
250         return false;
251 }
252
253
254 docstring const Inset::editMessage() const
255 {
256         return _("Opened inset");
257 }
258
259
260 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
261                 bool, int & x, int & y) const
262 {
263         lyxerr << "Inset::cursorPos called directly" << std::endl;
264         x = 100;
265         y = 100;
266 }
267
268
269 void Inset::metricsMarkers(Dimension & dim, int framesize) const
270 {
271         dim.wid += 2 * framesize;
272         dim.des += framesize;
273 }
274
275
276 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
277 {
278         dim.wid += 2 * framesize;
279         dim.asc += framesize;
280         dim.des += framesize;
281 }
282
283
284 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
285 {
286         Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
287                 Color::mathframe : Color::mathcorners;
288
289         Dimension const dim = dimension(*pi.base.bv);
290
291         int const t = x + dim.width() - 1;
292         int const d = y + dim.descent();
293         pi.pain.line(x, d - 3, x, d, pen_color);
294         pi.pain.line(t, d - 3, t, d, pen_color);
295         pi.pain.line(x, d, x + 3, d, pen_color);
296         pi.pain.line(t - 3, d, t, d, pen_color);
297         setPosCache(pi, x, y);
298 }
299
300
301 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
302 {
303         Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
304                 Color::mathframe : Color::mathcorners;
305
306         drawMarkers(pi, x, y);
307         Dimension const dim = dimension(*pi.base.bv);
308         int const t = x + dim.width() - 1;
309         int const a = y - dim.ascent();
310         pi.pain.line(x, a + 3, x, a, pen_color);
311         pi.pain.line(t, a + 3, t, a, pen_color);
312         pi.pain.line(x, a, x + 3, a, pen_color);
313         pi.pain.line(t - 3, a, t, a, pen_color);
314         setPosCache(pi, x, y);
315 }
316
317
318 bool Inset::editing(BufferView * bv) const
319 {
320         return bv->cursor().isInside(this);
321 }
322
323
324 int Inset::xo(BufferView const & bv) const
325 {
326         return bv.coordCache().getInsets().x(this);
327 }
328
329
330 int Inset::yo(BufferView const & bv) const
331 {
332         return bv.coordCache().getInsets().y(this);
333 }
334
335
336 bool Inset::covers(BufferView const & bv, int x, int y) const
337 {
338         return bv.coordCache().getInsets().covers(this, x, y);
339 }
340
341
342 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
343 {
344         return bp.getTextClass().insetlayout(name());  
345 }
346
347
348 void Inset::dump() const
349 {
350         Buffer buf("foo", 1);
351         write(buf, lyxerr);
352 }
353
354
355 Color_color Inset::backgroundColor() const
356 {
357         return Color::background;
358 }
359
360
361 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
362 {
363         //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
364         pi.base.bv->coordCache().insets().add(this, x, y);
365 }
366
367
368 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
369 {
370         mi.base.bv->coordCache().insets().add(this, dim);
371 }
372
373 } // namespace lyx