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