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