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