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