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