]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.C
disable open dialogs if applying them is not allowed
[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                 // Only allow modification of our own data.
155                 // This needs to be handled in the doDispatch method of our
156                 // instantiatable children.
157                 if (lyxCode() == translate(cmd.getArg(0))) {
158                         flag.enabled(true);
159                         return true;
160                 }
161                 return false;
162
163         case LFUN_INSET_INSERT:
164                 // Don't allow insertion of new insets.
165                 // Every inset that wants to allow new insets from open
166                 // dialogs needs to override this.
167                 flag.enabled(false);
168                 return true;
169
170         default:
171                 return false;
172         }
173 }
174
175
176 void InsetBase::edit(LCursor &, bool)
177 {
178         lyxerr << "InsetBase: edit left/right" << std::endl;
179 }
180
181
182 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
183 {
184         lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
185         return const_cast<InsetBase*>(this);
186 }
187
188
189 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
190 {
191         if (row != 0)
192                 lyxerr << "illegal row: " << row << std::endl;
193         if (col != 0)
194                 lyxerr << "illegal col: " << col << std::endl;
195         return 0;
196 }
197
198
199 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
200 {
201         return from <= idx && idx <= to;
202 }
203
204
205 bool InsetBase::idxUpDown(LCursor &, bool) const
206 {
207         return false;
208 }
209
210
211 int InsetBase::plaintext(Buffer const &,
212         std::ostream &, OutputParams const &) const
213 {
214         return 0;
215 }
216
217
218 int InsetBase::linuxdoc(Buffer const &,
219         std::ostream &, OutputParams const &) const
220 {
221         return 0;
222 }
223
224
225 int InsetBase::docbook(Buffer const &,
226         std::ostream &, OutputParams const &) const
227 {
228         return 0;
229 }
230
231
232 bool InsetBase::directWrite() const
233 {
234         return false;
235 }
236
237
238 InsetBase::EDITABLE InsetBase::editable() const
239 {
240         return NOT_EDITABLE;
241 }
242
243
244 bool InsetBase::autoDelete() const
245 {
246         return false;
247 }
248
249
250 std::string const InsetBase::editMessage() const
251 {
252         return _("Opened inset");
253 }
254
255
256 std::string const & InsetBase::getInsetName() const
257 {
258         static std::string const name = "unknown";
259         return name;
260 }
261
262
263 void InsetBase::markErased()
264 {}
265
266
267 void InsetBase::getCursorPos(CursorSlice const &, int & x, int & y) const
268 {
269         lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
270         x = 100;
271         y = 100;
272 }
273
274
275 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
276 {
277         dim.wid += 2 * framesize;
278         dim.asc += framesize;
279 }
280
281
282 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
283 {
284         dim.wid += 2 * framesize;
285         dim.asc += framesize;
286         dim.des += framesize;
287 }
288
289
290 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
291 {
292         if (!editing(pi.base.bv))
293                 return;
294         int const t = x + width() - 1;
295         int const d = y + descent();
296         pi.pain.line(x, d - 3, x, d, LColor::mathframe);
297         pi.pain.line(t, d - 3, t, d, LColor::mathframe);
298         pi.pain.line(x, d, x + 3, d, LColor::mathframe);
299         pi.pain.line(t - 3, d, t, d, LColor::mathframe);
300         setPosCache(pi, x, y);
301 }
302
303
304 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
305 {
306         if (!editing(pi.base.bv))
307                 return;
308         drawMarkers(pi, x, y);
309         int const t = x + width() - 1;
310         int const a = y - ascent();
311         pi.pain.line(x, a + 3, x, a, LColor::mathframe);
312         pi.pain.line(t, a + 3, t, a, LColor::mathframe);
313         pi.pain.line(x, a, x + 3, a, LColor::mathframe);
314         pi.pain.line(t - 3, a, t, a, LColor::mathframe);
315         setPosCache(pi, x, y);
316 }
317
318
319 bool InsetBase::editing(BufferView * bv) const
320 {
321         return bv->cursor().isInside(this);
322 }
323
324
325 int InsetBase::xo() const
326 {
327         return theCoords.getInsets().x(this);
328 }
329
330
331 int InsetBase::yo() const
332 {
333         return theCoords.getInsets().y(this);
334 }
335
336
337 bool InsetBase::covers(int x, int y) const
338 {
339         //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
340         //      << " xo: " << xo() << " yo: " << yo()
341         //      << " x1: " << xo() << " x2: " << xo() + width()
342         //      << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
343         //      << std::endl;
344         return theCoords.getInsets().has(this)
345                         && x >= xo()
346                         && x <= xo() + width()
347                         && y >= yo() - ascent()
348                         && y <= yo() + descent();
349 }
350
351
352 void InsetBase::dump() const
353 {
354         Buffer buf("foo", 1);
355         write(buf, lyxerr);
356 }
357
358
359 /////////////////////////////////////////
360
361 bool isEditableInset(InsetBase const * inset)
362 {
363         return inset && inset->editable();
364 }
365
366
367 bool isHighlyEditableInset(InsetBase const * inset)
368 {
369         return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;
370 }