]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.C
the Paragraph::inInset() changes
[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("mathgrid", InsetBase::MATHGRID_CODE),
88                 InsetName("mathhull", InsetBase::MATHHULL_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::Code InsetBase::translate(std::string const & name)
107 {
108         static TranslatorMap const translator = build_translator();
109
110         TranslatorMap::const_iterator it = translator.find(name);
111         return it == translator.end() ? NO_CODE : it->second;
112 }
113
114
115 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
116 {
117         priv_dispatch(cur, cmd);
118 }
119
120
121 void InsetBase::priv_dispatch(LCursor & cur, FuncRequest &)
122 {
123         cur.noUpdate();
124         cur.undispatched();
125 }
126
127
128 bool InsetBase::getStatus(LCursor &, FuncRequest const &, FuncStatus &) const
129 {
130         return false;
131 }
132
133
134 void InsetBase::edit(LCursor &, bool)
135 {
136         lyxerr << "InsetBase: edit left/right" << std::endl;
137 }
138
139
140 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
141 {
142         lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
143         return this;
144 }
145
146
147 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
148 {
149         if (row != 0)
150                 lyxerr << "illegal row: " << row << std::endl;
151         if (col != 0)
152                 lyxerr << "illegal col: " << col << std::endl;
153         return 0;
154 }
155
156
157 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
158 {
159         return from <= idx && idx <= to;
160 }
161
162
163 bool InsetBase::idxUpDown(LCursor &, bool) const
164 {
165         return false;
166 }
167
168
169 bool InsetBase::idxUpDown2(LCursor &, bool) const
170 {
171         return false;
172 }
173
174
175 int InsetBase::plaintext(Buffer const &,
176         std::ostream &, OutputParams const &) const
177 {
178         return 0;
179 }
180
181
182 int InsetBase::linuxdoc(Buffer const &,
183         std::ostream &, OutputParams const &) const
184 {
185         return 0;
186 }
187
188
189 int InsetBase::docbook(Buffer const &,
190         std::ostream &, OutputParams const &) const
191 {
192         return 0;
193 }
194
195
196 bool InsetBase::directWrite() const
197 {
198         return false;
199 }
200
201
202 InsetBase::EDITABLE InsetBase::editable() const
203 {
204         return NOT_EDITABLE;
205 }
206
207
208 bool InsetBase::autoDelete() const
209 {
210         return false;
211 }
212
213
214 std::string const InsetBase::editMessage() const
215 {
216         return _("Opened inset");
217 }
218
219
220 std::string const & InsetBase::getInsetName() const
221 {
222         static std::string const name = "unknown";
223         return name;
224 }
225
226
227 void InsetBase::markErased()
228 {}
229
230
231 void InsetBase::getCursorPos(CursorSlice const &, int & x, int & y) const
232 {
233         lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
234         x = 100;
235         y = 100;
236 }
237
238
239 void InsetBase::metricsMarkers(Dimension & dim, int) const
240 {
241         dim.wid += 2;
242         dim.asc += 1;
243 }
244
245
246 void InsetBase::metricsMarkers2(Dimension & dim, int) const
247 {
248         dim.wid += 2;
249         dim.asc += 1;
250         dim.des += 1;
251 }
252
253
254 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
255 {
256         if (!editing(pi.base.bv))
257                 return;
258         int const t = x + width() - 1;
259         int const d = y + descent();
260         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
261         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
262         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
263         pi.pain.line(t - 3, d, t, d, LColor::mathframe);
264         setPosCache(pi, x, y);
265 }
266
267
268 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
269 {
270         if (!editing(pi.base.bv))
271                 return;
272         drawMarkers(pi, x, y);
273         int const t = x + width() - 1;
274         int const a = y - ascent();
275         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
276         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
277         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
278         pi.pain.line(t - 3, a, t, a, LColor::mathframe);
279         setPosCache(pi, x, y);
280 }
281
282
283 bool InsetBase::editing(BufferView * bv) const
284 {
285         return bv->cursor().isInside(this);
286 }
287
288
289 bool InsetBase::covers(int x, int y) const
290 {
291         //lyxerr << "InsetBase::covers, x: " << x << " y: " << y 
292         //      << " xo: " << xo() << " yo: " << yo()
293         //      << " x1: " << xo() << " x2: " << xo() + width()
294         //      << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
295         //      << std::endl;
296         return x >= xo()
297                         && x <= xo() + width()
298                         && y >= yo() - ascent()
299                         && y <= yo() + descent();
300 }
301
302
303 void InsetBase::dump() const
304 {
305         Buffer buf("foo", 1);
306         write(buf, lyxerr);
307 }
308
309
310 /////////////////////////////////////////
311
312 bool isEditableInset(InsetBase const * inset)
313 {
314         return inset && inset->editable();
315 }
316
317
318 bool isHighlyEditableInset(InsetBase const * inset)
319 {
320         return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;
321 }