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