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