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