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