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