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