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