]> git.lyx.org Git - lyx.git/blob - src/factory.C
bit of undo
[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/insetref.h"
42 #include "insets/insetspace.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_ENVIRONMENT_INSERT:
161                 return new InsetEnvironment(params, cmd.argument);
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_INSERT: {
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                         inset->setParams(igp);
210                         return inset;
211
212                 } else if (name == "include") {
213                         InsetInclude::Params iip;
214                         InsetIncludeMailer::string2params(cmd.argument, iip);
215                         return new InsetInclude(iip);
216
217                 } else if (name == "index") {
218                         InsetCommandParams icp;
219                         InsetCommandMailer::string2params(cmd.argument, icp);
220                         return new InsetIndex(icp);
221
222                 } else if (name == "label") {
223                         InsetCommandParams icp;
224                         InsetCommandMailer::string2params(cmd.argument, icp);
225                         return new InsetLabel(icp);
226
227                 } else if (name == "ref") {
228                         InsetCommandParams icp;
229                         InsetCommandMailer::string2params(cmd.argument, icp);
230                         return new InsetRef(icp, *bv->buffer());
231
232                 } else if (name == "toc") {
233                         InsetCommandParams icp;
234                         InsetCommandMailer::string2params(cmd.argument, icp);
235                         return new InsetTOC(icp);
236
237                 } else if (name == "url") {
238                         InsetCommandParams icp;
239                         InsetCommandMailer::string2params(cmd.argument, icp);
240                         return new InsetUrl(icp);
241                 }
242         }
243
244         case LFUN_SPACE_INSERT: {
245                 string const name = cmd.argument;
246                 if (name == "normal")
247                         return new InsetSpace(InsetSpace::NORMAL);
248                 else if (name == "protected")
249                         return new InsetSpace(InsetSpace::PROTECTED);
250                 else if (name == "thin")
251                         return new InsetSpace(InsetSpace::THIN);
252                 else if (name == "quad")
253                         return new InsetSpace(InsetSpace::QUAD);
254                 else if (name == "qquad")
255                         return new InsetSpace(InsetSpace::QQUAD);
256                 else if (name == "enspace")
257                         return new InsetSpace(InsetSpace::ENSPACE);
258                 else if (name == "enskip")
259                         return new InsetSpace(InsetSpace::ENSKIP);
260                 else if (name == "negthinspace")
261                         return new InsetSpace(InsetSpace::NEGTHIN);
262                 else if (name.empty())
263                         lyxerr << "LyX function 'space' needs an argument." << endl;
264                 else
265                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
266         }
267
268         break;
269
270         default:
271                 break;
272         }
273
274         return 0;
275 }
276
277
278 Inset * readInset(LyXLex & lex, Buffer const & buf)
279 {
280         // consistency check
281         if (lex.getString() != "\\begin_inset") {
282                 lyxerr << "Buffer::readInset: Consistency check failed."
283                        << endl;
284         }
285
286         Inset * inset = 0;
287
288         lex.next();
289         string const tmptok = lex.getString();
290
291         // test the different insets
292         if (tmptok == "LatexCommand") {
293                 InsetCommandParams inscmd;
294                 inscmd.read(lex);
295
296                 string const cmdName = inscmd.getCmdName();
297
298                 // This strange command allows LyX to recognize "natbib" style
299                 // citations: citet, citep, Citet etc.
300                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
301                         inset = new InsetCitation(inscmd);
302                 } else if (cmdName == "bibitem") {
303                         lex.printError("Wrong place for bibitem");
304                         inset = new InsetBibitem(inscmd);
305                 } else if (cmdName == "bibtex") {
306                         inset = new InsetBibtex(inscmd);
307                 } else if (cmdName == "index") {
308                         inset = new InsetIndex(inscmd);
309                 } else if (cmdName == "include") {
310                         inset = new InsetInclude(inscmd, buf);
311                 } else if (cmdName == "label") {
312                         inset = new InsetLabel(inscmd);
313                 } else if (cmdName == "url"
314                            || cmdName == "htmlurl") {
315                         inset = new InsetUrl(inscmd);
316                 } else if (cmdName == "ref"
317                            || cmdName == "eqref"
318                            || cmdName == "pageref"
319                            || cmdName == "vref"
320                            || cmdName == "vpageref"
321                            || cmdName == "prettyref") {
322                         if (!inscmd.getOptions().empty()
323                             || !inscmd.getContents().empty()) {
324                                 inset = new InsetRef(inscmd, buf);
325                         }
326                 } else if (cmdName == "tableofcontents") {
327                         inset = new InsetTOC(inscmd);
328                 } else if (cmdName == "listofalgorithms") {
329                         inset = new InsetFloatList("algorithm");
330                 } else if (cmdName == "listoffigures") {
331                         inset = new InsetFloatList("figure");
332                 } else if (cmdName == "listoftables") {
333                         inset = new InsetFloatList("table");
334                 } else if (cmdName == "printindex") {
335                         inset = new InsetPrintIndex(inscmd);
336                 }
337         } else {
338                 if (tmptok == "Quotes") {
339                         inset = new InsetQuotes;
340                 } else if (tmptok == "External") {
341                         inset = new InsetExternal;
342                 } else if (tmptok == "FormulaMacro") {
343                         inset = new InsetFormulaMacro;
344                 } else if (tmptok == "Formula") {
345                         inset = new InsetFormula;
346                 } else if (tmptok == "Graphics") {
347                         inset = new InsetGraphics;
348                 } else if (tmptok == "Note") {
349                         inset = new InsetNote(buf.params);
350                 } else if (tmptok == "Include") {
351                         InsetCommandParams p("Include");
352                         inset = new InsetInclude(p, buf);
353                 } else if (tmptok == "Environment") {
354                         lex.next();
355                         inset = new InsetEnvironment(buf.params, lex.getString());
356                 } else if (tmptok == "ERT") {
357                         inset = new InsetERT(buf.params);
358                 } else if (tmptok == "InsetSpace") {
359                         inset = new InsetSpace;
360                 } else if (tmptok == "Tabular") {
361                         inset = new InsetTabular(buf);
362                 } else if (tmptok == "Text") {
363                         inset = new InsetText(buf.params);
364                 } else if (tmptok == "Foot") {
365                         inset = new InsetFoot(buf.params);
366                 } else if (tmptok == "Marginal") {
367                         inset = new InsetMarginal(buf.params);
368                 } else if (tmptok == "OptArg") {
369                         inset = new InsetOptArg(buf.params);
370                 } else if (tmptok == "Minipage") {
371                         inset = new InsetMinipage(buf.params);
372                 } else if (tmptok == "Float") {
373                         lex.next();
374                         string tmptok = lex.getString();
375                         inset = new InsetFloat(buf.params, tmptok);
376                 } else if (tmptok == "Wrap") {
377                         lex.next();
378                         string tmptok = lex.getString();
379                         inset = new InsetWrap(buf.params, tmptok);
380 #if 0
381                 } else if (tmptok == "List") {
382                         inset = new InsetList;
383                 } else if (tmptok == "Theorem") {
384                         inset = new InsetList;
385 #endif
386                 } else if (tmptok == "Caption") {
387                         inset = new InsetCaption(buf.params);
388                 } else if (tmptok == "FloatList") {
389                         inset = new InsetFloatList;
390                 }
391
392                 if (inset)
393                         inset->read(&buf, lex);
394         }
395
396         return inset;
397 }