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