]> git.lyx.org Git - lyx.git/blob - src/factory.C
make sure inserting a tabular invokes a dialog
[lyx.git] / src / factory.C
1 /**
2  * \file factory.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 "factory.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "bufferparams.h"
18 #include "debug.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "paragraph.h"
24
25 #include "insets/insetbibitem.h"
26 #include "insets/insetbibtex.h"
27 #include "insets/insetcaption.h"
28 #include "insets/insetcite.h"
29 #include "insets/insetcharstyle.h"
30 #include "insets/insetenv.h"
31 #include "insets/insetert.h"
32 #include "insets/insetexternal.h"
33 #include "insets/insetfloat.h"
34 #include "insets/insetfloatlist.h"
35 #include "insets/insetfoot.h"
36 #include "insets/insetgraphics.h"
37 #include "insets/insethfill.h"
38 #include "insets/insetinclude.h"
39 #include "insets/insetindex.h"
40 #include "insets/insetlabel.h"
41 #include "insets/insetline.h"
42 #include "insets/insetmarginal.h"
43 #include "insets/insetnote.h"
44 #include "insets/insetbox.h"
45 #include "insets/insetbranch.h"
46 #include "insets/insetoptarg.h"
47 #include "insets/insetpagebreak.h"
48 #include "insets/insetref.h"
49 #include "insets/insetspace.h"
50 #include "insets/insettabular.h"
51 #include "insets/insettoc.h"
52 #include "insets/inseturl.h"
53 #include "insets/insetvspace.h"
54 #include "insets/insetwrap.h"
55
56 #include "mathed/math_macrotemplate.h"
57 #include "mathed/math_hullinset.h"
58
59 #include "support/lstrings.h"
60
61 #include <boost/assert.hpp>
62
63 #include <sstream>
64
65 using lyx::support::compare_ascii_no_case;
66
67 using std::auto_ptr;
68 using std::endl;
69 using std::string;
70
71
72 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
73 {
74         BufferParams const & params = bv->buffer()->params();
75
76         switch (cmd.action) {
77         case LFUN_HFILL:
78                 return new InsetHFill;
79
80         case LFUN_INSERT_LINE:
81                 return new InsetLine;
82
83         case LFUN_INSERT_PAGEBREAK:
84                 return new InsetPagebreak;
85
86         case LFUN_INSERT_CHARSTYLE: {
87                 string s = cmd.getArg(0);
88                 LyXTextClass tclass = params.getLyXTextClass();
89                 CharStyles::iterator found_cs = tclass.charstyle(s);
90                 if (found_cs != tclass.charstyles().end())
91                         return new InsetCharStyle(params, found_cs);
92                 else
93                         return new InsetCharStyle(params, s);
94         }
95
96         case LFUN_INSERT_NOTE: {
97                 string arg = cmd.getArg(0);
98                 if (arg.empty())
99                         arg = "Note";
100                 return new InsetNote(params, arg);
101         }
102
103         case LFUN_INSERT_BOX: {
104                 string arg = cmd.getArg(0);
105                 if (arg.empty())
106                         arg = "Boxed";
107                 return new InsetBox(params, arg);
108         }
109
110         case LFUN_INSERT_BRANCH: {
111                 string arg = cmd.getArg(0);
112                 if (arg.empty())
113                         arg = "none";
114                 return new InsetBranch(params, InsetBranchParams(arg));
115         }
116
117         case LFUN_INSET_ERT:
118                 return new InsetERT(params);
119
120         case LFUN_INSET_FOOTNOTE:
121                 return new InsetFoot(params);
122
123         case LFUN_INSET_MARGINAL:
124                 return new InsetMarginal(params);
125
126         case LFUN_INSET_OPTARG:
127                 return new InsetOptArg(params);
128
129         case LFUN_INSERT_BIBITEM:
130                 return new InsetBibitem(InsetCommandParams("bibitem"));
131
132         case LFUN_INSET_FLOAT:
133                 // check if the float type exists
134                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
135                         return new InsetFloat(params, cmd.argument);
136                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
137                 return 0;
138
139         case LFUN_INSET_WIDE_FLOAT:
140                 // check if the float type exists
141                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
142                         auto_ptr<InsetFloat> p(new InsetFloat(params, cmd.argument));
143                         p->wide(true, params);
144                         return p.release();
145                 }
146                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
147                 return 0;
148
149         case LFUN_INSET_WRAP:
150                 if (cmd.argument == "figure")
151                         return new InsetWrap(params, cmd.argument);
152                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
153                 return 0;
154
155         case LFUN_INDEX_INSERT: {
156                 // Try and generate a valid index entry.
157                 InsetCommandParams icp("index");
158                 string const contents = cmd.argument.empty() ?
159                         bv->getLyXText()->getStringToIndex(bv->cursor()) :
160                         cmd.argument;
161                 icp.setContents(contents);
162                 return new InsetIndex(icp);
163         }
164
165         case LFUN_TABULAR_INSERT: {
166                 if (cmd.argument.empty()) 
167                         return 0;
168                 std::istringstream ss(cmd.argument);
169                 int r = 0, c = 0;
170                 ss >> r >> c;
171                 if (r <= 0)
172                         r = 2;
173                 if (c <= 0)
174                         c = 2;
175                 return new InsetTabular(*bv->buffer(), r, c);
176         }
177
178         case LFUN_INSET_CAPTION: {
179                 auto_ptr<InsetCaption> inset(new InsetCaption(params));
180                 inset->setAutoBreakRows(true);
181                 inset->setDrawFrame(true);
182                 inset->setFrameColor(LColor::captionframe);
183                 return inset.release();
184         }
185
186         case LFUN_INDEX_PRINT:
187                 return new InsetPrintIndex(InsetCommandParams("printindex"));
188
189         case LFUN_TOC_INSERT:
190                 return new InsetTOC(InsetCommandParams("tableofcontents"));
191
192         case LFUN_ENVIRONMENT_INSERT:
193                 return new InsetEnvironment(params, cmd.argument);
194
195 #if 0
196         case LFUN_INSET_LIST:
197                 return new InsetList;
198
199         case LFUN_INSET_THEOREM:
200                 return new InsetTheorem;
201 #endif
202
203         case LFUN_INSET_INSERT: {
204                 string const name = cmd.getArg(0);
205
206                 if (name == "bibitem") {
207                         InsetCommandParams icp;
208                         InsetCommandMailer::string2params(name, cmd.argument,
209                                                           icp);
210                         return new InsetBibitem(icp);
211
212                 } else if (name == "bibtex") {
213                         InsetCommandParams icp;
214                         InsetCommandMailer::string2params(name, cmd.argument,
215                                                           icp);
216                         return new InsetBibtex(icp);
217
218                 } else if (name == "citation") {
219                         InsetCommandParams icp;
220                         InsetCommandMailer::string2params(name, cmd.argument,
221                                                           icp);
222                         return new InsetCitation(icp);
223
224                 } else if (name == "ert") {
225                         InsetCollapsable::CollapseStatus st;
226                         InsetERTMailer::string2params(cmd.argument, st);
227                         return new InsetERT(params, st);
228
229                 } else if (name == "external") {
230                         Buffer const & buffer = *bv->buffer();
231                         InsetExternalParams iep;
232                         InsetExternalMailer::string2params(cmd.argument,
233                                                            buffer, iep);
234                         auto_ptr<InsetExternal> inset(new InsetExternal);
235                         inset->setParams(iep, buffer);
236                         return inset.release();
237
238                 } else if (name == "graphics") {
239                         Buffer const & buffer = *bv->buffer();
240                         InsetGraphicsParams igp;
241                         InsetGraphicsMailer::string2params(cmd.argument,
242                                                            buffer, igp);
243                         auto_ptr<InsetGraphics> inset(new InsetGraphics);
244                         inset->setParams(igp);
245                         return inset.release();
246
247                 } else if (name == "include") {
248                         InsetCommandParams iip;
249                         InsetIncludeMailer::string2params(cmd.argument, iip);
250                         return new InsetInclude(iip);
251
252                 } else if (name == "index") {
253                         InsetCommandParams icp;
254                         InsetCommandMailer::string2params(name, cmd.argument,
255                                                           icp);
256                         return new InsetIndex(icp);
257
258                 } else if (name == "label") {
259                         InsetCommandParams icp;
260                         InsetCommandMailer::string2params(name, cmd.argument,
261                                                           icp);
262                         return new InsetLabel(icp);
263
264                 } else if (name == "ref") {
265                         InsetCommandParams icp;
266                         InsetCommandMailer::string2params(name, cmd.argument,
267                                                           icp);
268                         return new InsetRef(icp, *bv->buffer());
269
270                 } else if (name == "toc") {
271                         InsetCommandParams icp;
272                         InsetCommandMailer::string2params(name, cmd.argument,
273                                                           icp);
274                         return new InsetTOC(icp);
275
276                 } else if (name == "url") {
277                         InsetCommandParams icp;
278                         InsetCommandMailer::string2params(name, cmd.argument,
279                                                           icp);
280                         return new InsetUrl(icp);
281
282                 } else if (name == "vspace") {
283                         VSpace vspace;
284                         InsetVSpaceMailer::string2params(cmd.argument, vspace);
285                         return new InsetVSpace(vspace);
286                 }
287         }
288
289         case LFUN_SPACE_INSERT: {
290                 string const name = cmd.argument;
291                 if (name == "normal")
292                         return new InsetSpace(InsetSpace::NORMAL);
293                 else if (name == "protected")
294                         return new InsetSpace(InsetSpace::PROTECTED);
295                 else if (name == "thin")
296                         return new InsetSpace(InsetSpace::THIN);
297                 else if (name == "quad")
298                         return new InsetSpace(InsetSpace::QUAD);
299                 else if (name == "qquad")
300                         return new InsetSpace(InsetSpace::QQUAD);
301                 else if (name == "enspace")
302                         return new InsetSpace(InsetSpace::ENSPACE);
303                 else if (name == "enskip")
304                         return new InsetSpace(InsetSpace::ENSKIP);
305                 else if (name == "negthinspace")
306                         return new InsetSpace(InsetSpace::NEGTHIN);
307                 else if (name.empty())
308                         lyxerr << "LyX function 'space' needs an argument." << endl;
309                 else
310                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
311         }
312
313         break;
314
315         default:
316                 break;
317         }
318
319         return 0;
320 }
321
322
323 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
324 {
325         // consistency check
326         if (lex.getString() != "\\begin_inset") {
327                 lyxerr << "Buffer::readInset: Consistency check failed."
328                        << endl;
329         }
330
331         auto_ptr<InsetBase> inset;
332
333         LyXTextClass tclass = buf.params().getLyXTextClass();
334
335         lex.next();
336         string tmptok = lex.getString();
337
338         // test the different insets
339         if (tmptok == "LatexCommand") {
340                 InsetCommandParams inscmd;
341                 inscmd.read(lex);
342
343                 string const cmdName = inscmd.getCmdName();
344
345                 // This strange command allows LyX to recognize "natbib" style
346                 // citations: citet, citep, Citet etc.
347                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
348                         inset.reset(new InsetCitation(inscmd));
349                 } else if (cmdName == "bibitem") {
350                         lex.printError("Wrong place for bibitem");
351                         inset.reset(new InsetBibitem(inscmd));
352                 } else if (cmdName == "bibtex") {
353                         inset.reset(new InsetBibtex(inscmd));
354                 } else if (cmdName == "index") {
355                         inset.reset(new InsetIndex(inscmd));
356                 } else if (cmdName == "include") {
357                         inset.reset(new InsetInclude(inscmd));
358                 } else if (cmdName == "label") {
359                         inset.reset(new InsetLabel(inscmd));
360                 } else if (cmdName == "url"
361                            || cmdName == "htmlurl") {
362                         inset.reset(new InsetUrl(inscmd));
363                 } else if (cmdName == "ref"
364                            || cmdName == "eqref"
365                            || cmdName == "pageref"
366                            || cmdName == "vref"
367                            || cmdName == "vpageref"
368                            || cmdName == "prettyref") {
369                         if (!inscmd.getOptions().empty()
370                             || !inscmd.getContents().empty()) {
371                                 inset.reset(new InsetRef(inscmd, buf));
372                         }
373                 } else if (cmdName == "tableofcontents") {
374                         inset.reset(new InsetTOC(inscmd));
375                 } else if (cmdName == "listofalgorithms") {
376                         inset.reset(new InsetFloatList("algorithm"));
377                 } else if (cmdName == "listoffigures") {
378                         inset.reset(new InsetFloatList("figure"));
379                 } else if (cmdName == "listoftables") {
380                         inset.reset(new InsetFloatList("table"));
381                 } else if (cmdName == "printindex") {
382                         inset.reset(new InsetPrintIndex(inscmd));
383                 } else {
384                         lyxerr << "unknown CommandInset '" << cmdName
385                                << "'" << std::endl;
386                         while (lex.isOK() && lex.getString() != "\\end_inset")
387                                 lex.next();
388                         return 0;
389                 }
390         } else {
391                 if (tmptok == "Quotes") {
392                         inset.reset(new InsetQuotes);
393                 } else if (tmptok == "External") {
394                         inset.reset(new InsetExternal);
395                 } else if (tmptok == "FormulaMacro") {
396                         inset.reset(new MathMacroTemplate);
397                 } else if (tmptok == "Formula") {
398                         inset.reset(new MathHullInset);
399                 } else if (tmptok == "Graphics") {
400                         inset.reset(new InsetGraphics);
401                 } else if (tmptok == "Note") {
402                         inset.reset(new InsetNote(buf.params(), tmptok));
403                 } else if (tmptok == "Box") {
404                         inset.reset(new InsetBox(buf.params(), tmptok));
405                 } else if (tmptok == "CharStyle") {
406                         lex.next();
407                         string s = lex.getString();
408                         CharStyles::iterator found_cs = tclass.charstyle(s);
409                         if (found_cs != tclass.charstyles().end())
410                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
411                         else {
412                                 // "Undefined" inset
413                                 inset.reset(new InsetCharStyle(buf.params(), s));
414                         }
415                 } else if (tmptok == "Branch") {
416                         inset.reset(new InsetBranch(buf.params(),
417                                                     InsetBranchParams()));
418                 } else if (tmptok == "Include") {
419                         InsetCommandParams p("Include");
420                         inset.reset(new InsetInclude(p));
421                 } else if (tmptok == "Environment") {
422                         lex.next();
423                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
424                 } else if (tmptok == "ERT") {
425                         inset.reset(new InsetERT(buf.params()));
426                 } else if (tmptok == "InsetSpace") {
427                         inset.reset(new InsetSpace);
428                 } else if (tmptok == "Tabular") {
429                         inset.reset(new InsetTabular(buf));
430                 } else if (tmptok == "Text") {
431                         inset.reset(new InsetText(buf.params()));
432                 } else if (tmptok == "VSpace") {
433                         inset.reset(new InsetVSpace);
434                 } else if (tmptok == "Foot") {
435                         inset.reset(new InsetFoot(buf.params()));
436                 } else if (tmptok == "Marginal") {
437                         inset.reset(new InsetMarginal(buf.params()));
438                 } else if (tmptok == "OptArg") {
439                         inset.reset(new InsetOptArg(buf.params()));
440                 } else if (tmptok == "Float") {
441                         lex.next();
442                         string tmptok = lex.getString();
443                         inset.reset(new InsetFloat(buf.params(), tmptok));
444                 } else if (tmptok == "Wrap") {
445                         lex.next();
446                         string tmptok = lex.getString();
447                         inset.reset(new InsetWrap(buf.params(), tmptok));
448 #if 0
449                 } else if (tmptok == "List") {
450                         inset.reset(new InsetList);
451                 } else if (tmptok == "Theorem") {
452                         inset.reset(new InsetList);
453 #endif
454                 } else if (tmptok == "Caption") {
455                         inset.reset(new InsetCaption(buf.params()));
456                 } else if (tmptok == "FloatList") {
457                         inset.reset(new InsetFloatList);
458                 } else {
459                         lyxerr << "unknown Inset type '" << tmptok
460                                << "'" << std::endl;
461                         while (lex.isOK() && lex.getString() != "\\end_inset")
462                                 lex.next();
463                         return 0;
464                 }
465
466                 inset->read(buf, lex);
467
468 #ifdef WITH_WARNINGS
469 #warning hack..
470 #endif
471                 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
472                         MathMacroTemplate const * tmpl =
473                                 static_cast<MathMacroTemplate*>(inset.get());
474                         MacroTable::globalMacros().insert
475                                 (tmpl->name(), tmpl->asMacroData());
476                         lyxerr[Debug::DEBUG]
477                                 << BOOST_CURRENT_FUNCTION
478                                 << ": creating local macro " << tmpl->name()
479                                 << endl;
480                 }
481         }
482
483         return inset.release();
484 }