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