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