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