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