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