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