]> git.lyx.org Git - lyx.git/blob - src/factory.C
5fad59694e98880c556d52ecedcbc90515855e3b
[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 #include "funcrequest.h"
15 #include "buffer.h"
16 #include "FloatList.h"
17 #include "debug.h"
18 #include "BufferView.h"
19 #include "lyxlex.h"
20
21 #include "insets/insetbibitem.h"
22 #include "insets/insetbibtex.h"
23 #include "insets/insetcaption.h"
24 #include "insets/insetcite.h"
25 #include "insets/insetenv.h"
26 #include "insets/insetert.h"
27 #include "insets/insetexternal.h"
28 #include "insets/insetfloat.h"
29 #include "insets/insetfloatlist.h"
30 #include "insets/insetfoot.h"
31 #include "insets/insetgraphics.h"
32 #include "insets/insethfill.h"
33 #include "insets/insetinclude.h"
34 #include "insets/insetindex.h"
35 #include "insets/insetlabel.h"
36 #include "insets/insetmarginal.h"
37 #include "insets/insetminipage.h"
38 #include "insets/insetnote.h"
39 #include "insets/insetbranch.h"
40 #include "insets/insetoptarg.h"
41 #include "insets/insetref.h"
42 #include "insets/insetspace.h"
43 #include "insets/insettabular.h"
44 #include "insets/insettoc.h"
45 #include "insets/inseturl.h"
46 #include "insets/insetwrap.h"
47 #include "mathed/formulamacro.h"
48 #include "mathed/formula.h"
49
50 #include "frontends/Dialogs.h"
51 #include "frontends/LyXView.h"
52 #include "support/lstrings.h"
53 #include "support/std_sstream.h"
54
55 using namespace lyx::support;
56
57 using std::endl;
58
59
60 InsetOld * createInset(FuncRequest const & cmd)
61 {
62         BufferView * bv = cmd.view();
63         BufferParams const & params = bv->buffer()->params;
64
65         switch (cmd.action) {
66         case LFUN_HFILL:
67                 return new InsetHFill();
68
69         case LFUN_INSET_MINIPAGE:
70                 return new InsetMinipage(params);
71
72         case LFUN_INSERT_NOTE:
73                 {
74                         string arg = cmd.getArg(0);
75                         if (arg.empty())
76                                 arg = "Note";
77                         return new InsetNote(params, arg);
78                 }
79         case LFUN_INSERT_BRANCH:
80                 {
81                         string arg = cmd.getArg(0);
82                         if (arg.empty())
83                                 arg = "none";
84                         return new InsetBranch(params, arg);
85                 }
86
87         case LFUN_INSET_ERT:
88                 return new InsetERT(params);
89
90         case LFUN_INSET_FOOTNOTE:
91                 return new InsetFoot(params);
92
93         case LFUN_INSET_MARGINAL:
94                 return new InsetMarginal(params);
95
96         case LFUN_INSET_OPTARG:
97                 return new InsetOptArg(params);
98
99         case LFUN_INSERT_BIBITEM:
100                 return new InsetBibitem(InsetCommandParams("bibitem"));
101
102         case LFUN_INSET_FLOAT:
103                 // check if the float type exists
104                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
105                         return new InsetFloat(params, cmd.argument);
106                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
107                 return 0;
108
109         case LFUN_INSET_WIDE_FLOAT:
110                 // check if the float type exists
111                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
112                         InsetFloat * p = new InsetFloat(params, cmd.argument);
113                         p->wide(true, params);
114                 }
115                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
116                 return 0;
117
118         case LFUN_INSET_WRAP:
119                 if (cmd.argument == "figure")
120                         return new InsetWrap(params, cmd.argument);
121                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
122                 return 0;
123
124         case LFUN_INDEX_INSERT: {
125                 // Try and generate a valid index entry.
126                 InsetCommandParams icp("index");
127                 string const contents = cmd.argument.empty() ?
128                         bv->getLyXText()->getStringToIndex() :
129                         cmd.argument;
130                 icp.setContents(contents);
131
132                 string data = InsetCommandMailer::params2string("index", icp);
133                 LyXView * lv = bv->owner();
134
135                 if (icp.getContents().empty()) {
136                         lv->getDialogs().show("index", data, 0);
137                 } else {
138                         lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
139                 }
140                 return 0;
141         }
142
143         case LFUN_TABULAR_INSERT:
144                 if (!cmd.argument.empty()) {
145                         std::istringstream ss(cmd.argument);
146                         int r, c;
147                         ss >> r >> c;
148                         if (r <= 0) r = 2;
149                         if (c <= 0) c = 2;
150                         return new InsetTabular(*bv->buffer(), r, c);
151                 }
152                 bv->owner()->getDialogs().show("tabularcreate");
153                 return 0;
154
155         case LFUN_INSET_CAPTION:
156                 if (bv->theLockingInset()) {
157                         lyxerr << "Locking inset code: "
158                                << static_cast<int>(bv->theLockingInset()->lyxCode());
159                         InsetCaption * inset = new InsetCaption(params);
160                         inset->setOwner(bv->theLockingInset());
161                         inset->setAutoBreakRows(true);
162                         inset->setDrawFrame(InsetText::LOCKED);
163                         inset->setFrameColor(LColor::captionframe);
164                         return inset;
165                 }
166                 return 0;
167
168         case LFUN_INDEX_PRINT:
169                 return new InsetPrintIndex(InsetCommandParams("printindex"));
170
171         case LFUN_TOC_INSERT:
172                 return new InsetTOC(InsetCommandParams("tableofcontents"));
173
174         case LFUN_ENVIRONMENT_INSERT:
175                 return new InsetEnvironment(params, cmd.argument);
176
177 #if 0
178         case LFUN_INSET_LIST:
179                 return new InsetList;
180
181         case LFUN_INSET_THEOREM:
182                 return new InsetTheorem;
183 #endif
184
185         case LFUN_INSET_INSERT: {
186                 string const name = cmd.getArg(0);
187
188                 if (name == "bibitem") {
189                         InsetCommandParams icp;
190                         InsetCommandMailer::string2params(cmd.argument, icp);
191                         return new InsetBibitem(icp);
192
193                 } else if (name == "bibtex") {
194                         InsetCommandParams icp;
195                         InsetCommandMailer::string2params(cmd.argument, icp);
196                         return new InsetBibtex(icp);
197
198                 } else if (name == "citation") {
199                         InsetCommandParams icp;
200                         InsetCommandMailer::string2params(cmd.argument, icp);
201                         InsetCitation * inset = new InsetCitation(icp);
202                         inset->setLoadingBuffer(*bv->buffer(), false);
203                         return inset;
204
205                 } else if (name == "ert") {
206                         InsetERT * inset = new InsetERT(params);
207                         InsetERT::ERTStatus s;
208                         InsetERTMailer::string2params(cmd.argument, s);
209                         inset->status(bv, s);
210                         return inset;
211
212                 } else if (name == "external") {
213                         Buffer const & buffer = *cmd.view()->buffer();
214                         InsetExternal::Params iep;
215                         InsetExternalMailer::string2params(cmd.argument,
216                                                            buffer, iep);
217                         InsetExternal * inset = new InsetExternal;
218                         inset->setParams(iep, buffer);
219                         return inset;
220
221                 } else if (name == "graphics") {
222                         Buffer const & buffer = *cmd.view()->buffer();
223                         InsetGraphicsParams igp;
224                         InsetGraphicsMailer::string2params(cmd.argument,
225                                                            buffer, igp);
226                         InsetGraphics * inset = new InsetGraphics;
227                         inset->setParams(igp);
228                         return inset;
229
230                 } else if (name == "include") {
231                         InsetInclude::Params iip;
232                         InsetIncludeMailer::string2params(cmd.argument, iip);
233                         return new InsetInclude(iip);
234
235                 } else if (name == "index") {
236                         InsetCommandParams icp;
237                         InsetCommandMailer::string2params(cmd.argument, icp);
238                         return new InsetIndex(icp);
239
240                 } else if (name == "label") {
241                         InsetCommandParams icp;
242                         InsetCommandMailer::string2params(cmd.argument, icp);
243                         return new InsetLabel(icp);
244
245                 } else if (name == "ref") {
246                         InsetCommandParams icp;
247                         InsetCommandMailer::string2params(cmd.argument, icp);
248                         return new InsetRef(icp, *bv->buffer());
249
250                 } else if (name == "toc") {
251                         InsetCommandParams icp;
252                         InsetCommandMailer::string2params(cmd.argument, icp);
253                         return new InsetTOC(icp);
254
255                 } else if (name == "url") {
256                         InsetCommandParams icp;
257                         InsetCommandMailer::string2params(cmd.argument, icp);
258                         return new InsetUrl(icp);
259                 }
260         }
261
262         case LFUN_SPACE_INSERT: {
263                 string const name = cmd.argument;
264                 if (name == "normal")
265                         return new InsetSpace(InsetSpace::NORMAL);
266                 else if (name == "protected")
267                         return new InsetSpace(InsetSpace::PROTECTED);
268                 else if (name == "thin")
269                         return new InsetSpace(InsetSpace::THIN);
270                 else if (name == "quad")
271                         return new InsetSpace(InsetSpace::QUAD);
272                 else if (name == "qquad")
273                         return new InsetSpace(InsetSpace::QQUAD);
274                 else if (name == "enspace")
275                         return new InsetSpace(InsetSpace::ENSPACE);
276                 else if (name == "enskip")
277                         return new InsetSpace(InsetSpace::ENSKIP);
278                 else if (name == "negthinspace")
279                         return new InsetSpace(InsetSpace::NEGTHIN);
280                 else if (name.empty())
281                         lyxerr << "LyX function 'space' needs an argument." << endl;
282                 else
283                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
284         }
285
286         break;
287
288         default:
289                 break;
290         }
291
292         return 0;
293 }
294
295
296 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
297 {
298         // consistency check
299         if (lex.getString() != "\\begin_inset") {
300                 lyxerr << "Buffer::readInset: Consistency check failed."
301                        << endl;
302         }
303
304         InsetOld * inset = 0;
305
306         lex.next();
307         string const tmptok = lex.getString();
308
309         // test the different insets
310         if (tmptok == "LatexCommand") {
311                 InsetCommandParams inscmd;
312                 inscmd.read(lex);
313
314                 string const cmdName = inscmd.getCmdName();
315
316                 // This strange command allows LyX to recognize "natbib" style
317                 // citations: citet, citep, Citet etc.
318                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
319                         inset = new InsetCitation(inscmd);
320                 } else if (cmdName == "bibitem") {
321                         lex.printError("Wrong place for bibitem");
322                         inset = new InsetBibitem(inscmd);
323                 } else if (cmdName == "bibtex") {
324                         inset = new InsetBibtex(inscmd);
325                 } else if (cmdName == "index") {
326                         inset = new InsetIndex(inscmd);
327                 } else if (cmdName == "include") {
328                         inset = new InsetInclude(inscmd, buf);
329                 } else if (cmdName == "label") {
330                         inset = new InsetLabel(inscmd);
331                 } else if (cmdName == "url"
332                            || cmdName == "htmlurl") {
333                         inset = new InsetUrl(inscmd);
334                 } else if (cmdName == "ref"
335                            || cmdName == "eqref"
336                            || cmdName == "pageref"
337                            || cmdName == "vref"
338                            || cmdName == "vpageref"
339                            || cmdName == "prettyref") {
340                         if (!inscmd.getOptions().empty()
341                             || !inscmd.getContents().empty()) {
342                                 inset = new InsetRef(inscmd, buf);
343                         }
344                 } else if (cmdName == "tableofcontents") {
345                         inset = new InsetTOC(inscmd);
346                 } else if (cmdName == "listofalgorithms") {
347                         inset = new InsetFloatList("algorithm");
348                 } else if (cmdName == "listoffigures") {
349                         inset = new InsetFloatList("figure");
350                 } else if (cmdName == "listoftables") {
351                         inset = new InsetFloatList("table");
352                 } else if (cmdName == "printindex") {
353                         inset = new InsetPrintIndex(inscmd);
354                 }
355         } else {
356                 if (tmptok == "Quotes") {
357                         inset = new InsetQuotes;
358                 } else if (tmptok == "External") {
359                         inset = new InsetExternal;
360                 } else if (tmptok == "FormulaMacro") {
361                         inset = new InsetFormulaMacro;
362                 } else if (tmptok == "Formula") {
363                         inset = new InsetFormula;
364                 } else if (tmptok == "Graphics") {
365                         inset = new InsetGraphics;
366                 } else if (tmptok == "Note"     || tmptok == "Comment"
367                                 || tmptok == "Greyedout") {
368                         inset = new InsetNote(buf.params, tmptok);
369                 } else if (tmptok == "Branch") {
370                         inset = new InsetBranch(buf.params, tmptok);
371                 } else if (tmptok == "Include") {
372                         InsetCommandParams p("Include");
373                         inset = new InsetInclude(p, buf);
374                 } else if (tmptok == "Environment") {
375                         lex.next();
376                         inset = new InsetEnvironment(buf.params, lex.getString());
377                 } else if (tmptok == "ERT") {
378                         inset = new InsetERT(buf.params);
379                 } else if (tmptok == "InsetSpace") {
380                         inset = new InsetSpace;
381                 } else if (tmptok == "Tabular") {
382                         inset = new InsetTabular(buf);
383                 } else if (tmptok == "Text") {
384                         inset = new InsetText(buf.params);
385                 } else if (tmptok == "Foot") {
386                         inset = new InsetFoot(buf.params);
387                 } else if (tmptok == "Marginal") {
388                         inset = new InsetMarginal(buf.params);
389                 } else if (tmptok == "OptArg") {
390                         inset = new InsetOptArg(buf.params);
391                 } else if (tmptok == "Minipage") {
392                         inset = new InsetMinipage(buf.params);
393                 } else if (tmptok == "Float") {
394                         lex.next();
395                         string tmptok = lex.getString();
396                         inset = new InsetFloat(buf.params, tmptok);
397                 } else if (tmptok == "Wrap") {
398                         lex.next();
399                         string tmptok = lex.getString();
400                         inset = new InsetWrap(buf.params, tmptok);
401 #if 0
402                 } else if (tmptok == "List") {
403                         inset = new InsetList;
404                 } else if (tmptok == "Theorem") {
405                         inset = new InsetList;
406 #endif
407                 } else if (tmptok == "Caption") {
408                         inset = new InsetCaption(buf.params);
409                 } else if (tmptok == "FloatList") {
410                         inset = new InsetFloatList;
411                 }
412
413                 if (inset)
414                         inset->read(buf, lex);
415         }
416
417         return inset;
418 }