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