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