]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
Show corners of mathed with mouse hover, details see bug 3825
[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 std::auto_ptr<Inset> Inset::clone() const
125 {
126         std::auto_ptr<Inset> b = doClone();
127         BOOST_ASSERT(typeid(*b) == typeid(*this));
128         return b;
129 }
130
131
132 Inset::Code Inset::translate(std::string const & name)
133 {
134         static TranslatorMap const translator = build_translator();
135
136         TranslatorMap::const_iterator it = translator.find(name);
137         return it == translator.end() ? NO_CODE : it->second;
138 }
139
140
141 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
142 {
143         cur.updateFlags(Update::Force | Update::FitCursor);
144         cur.dispatched();
145         doDispatch(cur, cmd);
146 }
147
148
149 void Inset::doDispatch(Cursor & cur, FuncRequest &)
150 {
151         cur.noUpdate();
152         cur.undispatched();
153 }
154
155
156 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
157         FuncStatus & flag) const
158 {
159         // LFUN_INSET_APPLY is sent from the dialogs when the data should
160         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
161         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
162         // not belong to us, i. e. the dialog was open, and the user moved
163         // the cursor in our inset) in LyXFunc::getStatus().
164         // Dialogs::checkStatus() ensures that the dialog is deactivated if
165         // LFUN_INSET_APPLY is disabled.
166
167         switch (cmd.action) {
168         case LFUN_INSET_MODIFY:
169                 // Allow modification of our data.
170                 // This needs to be handled in the doDispatch method of our
171                 // instantiatable children.
172                 flag.enabled(true);
173                 return true;
174
175         case LFUN_INSET_INSERT:
176                 // Don't allow insertion of new insets.
177                 // Every inset that wants to allow new insets from open
178                 // dialogs needs to override this.
179                 flag.enabled(false);
180                 return true;
181
182         default:
183                 return false;
184         }
185 }
186
187
188 void Inset::edit(Cursor &, bool)
189 {
190         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
191                               << ": edit left/right" << std::endl;
192 }
193
194
195 Inset * Inset::editXY(Cursor &, int x, int y)
196 {
197         LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
198                               << ": x=" << x << " y= " << y
199                               << std::endl;
200         return this;
201 }
202
203
204 Inset::idx_type Inset::index(row_type row, col_type col) const
205 {
206         if (row != 0)
207                 lyxerr << BOOST_CURRENT_FUNCTION
208                        << ": illegal row: " << row << std::endl;
209         if (col != 0)
210                 lyxerr << BOOST_CURRENT_FUNCTION
211                        << ": illegal col: " << col << std::endl;
212         return 0;
213 }
214
215
216 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
217 {
218         return from <= idx && idx <= to;
219 }
220
221
222 bool Inset::idxUpDown(Cursor &, bool) const
223 {
224         return false;
225 }
226
227
228 int Inset::docbook(Buffer const &,
229         odocstream &, OutputParams const &) const
230 {
231         return 0;
232 }
233
234
235 bool Inset::directWrite() const
236 {
237         return false;
238 }
239
240
241 Inset::EDITABLE Inset::editable() const
242 {
243         return NOT_EDITABLE;
244 }
245
246
247 bool Inset::autoDelete() const
248 {
249         return false;
250 }
251
252
253 docstring const Inset::editMessage() const
254 {
255         return _("Opened inset");
256 }
257
258
259 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
260                 bool, int & x, int & y) const
261 {
262         lyxerr << "Inset::cursorPos called directly" << std::endl;
263         x = 100;
264         y = 100;
265 }
266
267
268 void Inset::metricsMarkers(Dimension & dim, int framesize) const
269 {
270         dim.wid += 2 * framesize;
271         dim.des += framesize;
272 }
273
274
275 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
276 {
277         dim.wid += 2 * framesize;
278         dim.asc += framesize;
279         dim.des += framesize;
280 }
281
282
283 bool Inset::editing(BufferView * bv) const
284 {
285         return bv->cursor().isInside(this);
286 }
287
288
289 int Inset::xo(BufferView const & bv) const
290 {
291         return bv.coordCache().getInsets().x(this);
292 }
293
294
295 int Inset::yo(BufferView const & bv) const
296 {
297         return bv.coordCache().getInsets().y(this);
298 }
299
300
301 bool Inset::covers(BufferView const & bv, int x, int y) const
302 {
303         //lyxerr << "Inset::covers, x: " << x << " y: " << y
304         //      << " xo: " << xo(bv) << " yo: " << yo()
305         //      << " x1: " << xo(bv) << " x2: " << xo() + width()
306         //      << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
307         //      << std::endl;
308         return bv.coordCache().getInsets().has(this)
309                         && x >= xo(bv)
310                         && x <= xo(bv) + width()
311                         && y >= yo(bv) - ascent()
312                         && y <= yo(bv) + descent();
313 }
314
315
316 void Inset::dump() const
317 {
318         Buffer buf("foo", 1);
319         write(buf, lyxerr);
320 }
321
322
323 void Inset::setBackgroundColor(Color_color color)
324 {
325         background_color_ = color;
326 }
327
328
329 Color_color Inset::backgroundColor() const
330 {
331         return Color::color(background_color_);
332 }
333
334
335 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
336 {
337         //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
338         pi.base.bv->coordCache().insets().add(this, x, y);
339 }
340
341
342 /////////////////////////////////////////
343
344 bool isEditableInset(Inset const * inset)
345 {
346         return inset && inset->editable();
347 }
348
349
350 bool isHighlyEditableInset(Inset const * inset)
351 {
352         return inset && inset->editable() == Inset::HIGHLY_EDITABLE;
353 }
354
355
356 } // namespace lyx