]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.C
Asger's commentary, const-correct and bug-squashing little beauty.
[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 "gettext.h"
24 #include "lyxtext.h"
25 #include "metricsinfo.h"
26
27 #include "frontends/Painter.h"
28
29 #include <map>
30
31
32 namespace {
33
34 class InsetName {
35 public:
36         InsetName(std::string const & n, InsetBase::Code c)
37                 : name(n), code(c) {}
38         std::string name;
39         InsetBase::Code code;
40 };
41
42
43 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
44
45
46 TranslatorMap const build_translator()
47 {
48         InsetName const insetnames[] = {
49                 InsetName("toc", InsetBase::TOC_CODE),
50                 InsetName("quote", InsetBase::QUOTE_CODE),
51                 InsetName("ref", InsetBase::REF_CODE),
52                 InsetName("url", InsetBase::URL_CODE),
53                 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
54                 InsetName("separator", InsetBase::SEPARATOR_CODE),
55                 InsetName("ending", InsetBase::ENDING_CODE),
56                 InsetName("label", InsetBase::LABEL_CODE),
57                 InsetName("note", InsetBase::NOTE_CODE),
58                 InsetName("accent", InsetBase::ACCENT_CODE),
59                 InsetName("math", InsetBase::MATH_CODE),
60                 InsetName("index", InsetBase::INDEX_CODE),
61                 InsetName("include", InsetBase::INCLUDE_CODE),
62                 InsetName("graphics", InsetBase::GRAPHICS_CODE),
63                 InsetName("bibitem", InsetBase::BIBITEM_CODE),
64                 InsetName("bibtex", InsetBase::BIBTEX_CODE),
65                 InsetName("text", InsetBase::TEXT_CODE),
66                 InsetName("ert", InsetBase::ERT_CODE),
67                 InsetName("foot", InsetBase::FOOT_CODE),
68                 InsetName("margin", InsetBase::MARGIN_CODE),
69                 InsetName("float", InsetBase::FLOAT_CODE),
70                 InsetName("wrap", InsetBase::WRAP_CODE),
71                 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
72                 InsetName("tabular", InsetBase::TABULAR_CODE),
73                 InsetName("external", InsetBase::EXTERNAL_CODE),
74                 InsetName("caption", InsetBase::CAPTION_CODE),
75                 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
76                 InsetName("error", InsetBase::ERROR_CODE),
77                 InsetName("cite", InsetBase::CITE_CODE),
78                 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
79                 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
80                 InsetName("optarg", InsetBase::OPTARG_CODE),
81                 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
82                 InsetName("hfill", InsetBase::HFILL_CODE),
83                 InsetName("newline", InsetBase::NEWLINE_CODE),
84                 InsetName("line", InsetBase::LINE_CODE),
85                 InsetName("branch", InsetBase::BRANCH_CODE),
86                 InsetName("box", InsetBase::BOX_CODE),
87                 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
88                 InsetName("vspace", InsetBase::VSPACE_CODE),
89                 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
90         };
91
92         std::size_t const insetnames_size =
93                 sizeof(insetnames) / sizeof(insetnames[0]);
94
95         std::map<std::string, InsetBase::Code> data;
96         for (std::size_t i = 0; i != insetnames_size; ++i) {
97                 InsetName const & var = insetnames[i];
98                 data[var.name] = var.code;
99         }
100
101         return data;
102 }
103
104 } // namespace anon
105
106
107 InsetBase::InsetBase()
108 {}
109
110
111 InsetBase::InsetBase(InsetBase const &)
112 {}
113
114
115 InsetBase::Code InsetBase::translate(std::string const & name)
116 {
117         static TranslatorMap const translator = build_translator();
118
119         TranslatorMap::const_iterator it = translator.find(name);
120         return it == translator.end() ? NO_CODE : it->second;
121 }
122
123
124 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
125 {
126         cur.needsUpdate();
127         cur.dispatched();
128         doDispatch(cur, cmd);
129 }
130
131
132 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
133 {
134         cur.noUpdate();
135         cur.undispatched();
136 }
137
138
139 bool InsetBase::getStatus(LCursor &, FuncRequest const &, FuncStatus &) const
140 {
141         return false;
142 }
143
144
145 void InsetBase::edit(LCursor &, bool)
146 {
147         lyxerr << "InsetBase: edit left/right" << std::endl;
148 }
149
150
151 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
152 {
153         lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
154         return const_cast<InsetBase*>(this);
155 }
156
157
158 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
159 {
160         if (row != 0)
161                 lyxerr << "illegal row: " << row << std::endl;
162         if (col != 0)
163                 lyxerr << "illegal col: " << col << std::endl;
164         return 0;
165 }
166
167
168 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
169 {
170         return from <= idx && idx <= to;
171 }
172
173
174 bool InsetBase::idxUpDown(LCursor &, bool) const
175 {
176         return false;
177 }
178
179
180 int InsetBase::plaintext(Buffer const &,
181         std::ostream &, OutputParams const &) const
182 {
183         return 0;
184 }
185
186
187 int InsetBase::linuxdoc(Buffer const &,
188         std::ostream &, OutputParams const &) const
189 {
190         return 0;
191 }
192
193
194 int InsetBase::docbook(Buffer const &,
195         std::ostream &, OutputParams const &) const
196 {
197         return 0;
198 }
199
200
201 bool InsetBase::directWrite() const
202 {
203         return false;
204 }
205
206
207 InsetBase::EDITABLE InsetBase::editable() const
208 {
209         return NOT_EDITABLE;
210 }
211
212
213 bool InsetBase::autoDelete() const
214 {
215         return false;
216 }
217
218
219 std::string const InsetBase::editMessage() const
220 {
221         return _("Opened inset");
222 }
223
224
225 std::string const & InsetBase::getInsetName() const
226 {
227         static std::string const name = "unknown";
228         return name;
229 }
230
231
232 void InsetBase::markErased()
233 {}
234
235
236 void InsetBase::getCursorPos(CursorSlice const &, int & x, int & y) const
237 {
238         lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
239         x = 100;
240         y = 100;
241 }
242
243
244 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
245 {
246         dim.wid += 2 * framesize;
247         dim.asc += framesize;
248 }
249
250
251 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
252 {
253         dim.wid += 2 * framesize;
254         dim.asc += framesize;
255         dim.des += framesize;
256 }
257
258
259 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
260 {
261         if (!editing(pi.base.bv))
262                 return;
263         int const t = x + width() - 1;
264         int const d = y + descent();
265         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
266         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
267         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
268         pi.pain.line(t - 3, d, t, d, LColor::mathframe);
269         setPosCache(pi, x, y);
270 }
271
272
273 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
274 {
275         if (!editing(pi.base.bv))
276                 return;
277         drawMarkers(pi, x, y);
278         int const t = x + width() - 1;
279         int const a = y - ascent();
280         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
281         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
282         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
283         pi.pain.line(t - 3, a, t, a, LColor::mathframe);
284         setPosCache(pi, x, y);
285 }
286
287
288 bool InsetBase::editing(BufferView * bv) const
289 {
290         return bv->cursor().isInside(this);
291 }
292
293
294 int InsetBase::xo() const
295 {
296         return theCoords.getInsets().x(this);
297 }
298
299
300 int InsetBase::yo() const
301 {
302         return theCoords.getInsets().y(this);
303 }
304
305
306 bool InsetBase::covers(int x, int y) const
307 {
308         //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
309         //      << " xo: " << xo() << " yo: " << yo()
310         //      << " x1: " << xo() << " x2: " << xo() + width()
311         //      << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
312         //      << std::endl;
313         return theCoords.getInsets().has(this)
314                         && x >= xo()
315                         && x <= xo() + width()
316                         && y >= yo() - ascent()
317                         && y <= yo() + descent();
318 }
319
320
321 void InsetBase::dump() const
322 {
323         Buffer buf("foo", 1);
324         write(buf, lyxerr);
325 }
326
327
328 /////////////////////////////////////////
329
330 bool isEditableInset(InsetBase const * inset)
331 {
332         return inset && inset->editable();
333 }
334
335
336 bool isHighlyEditableInset(InsetBase const * inset)
337 {
338         return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;
339 }