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