]> git.lyx.org Git - features.git/blob - src/factory.cpp
'using namespace std' instead of 'using std::xxx'
[features.git] / src / factory.cpp
1 /**
2  * \file factory.cpp
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 "BufferParams.h"
17 #include "FloatList.h"
18 #include "FuncRequest.h"
19 #include "Lexer.h"
20 #include "LyX.h"
21 #include "TextClass.h"
22
23 #include "insets/InsetBibitem.h"
24 #include "insets/InsetBibtex.h"
25 #include "insets/InsetCaption.h"
26 #include "insets/InsetCitation.h"
27 #include "insets/InsetFlex.h"
28 #include "insets/InsetEnvironment.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetListings.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetFloatList.h"
34 #include "insets/InsetFoot.h"
35 #include "insets/InsetGraphics.h"
36 #include "insets/InsetHFill.h"
37 #include "insets/InsetInclude.h"
38 #include "insets/InsetIndex.h"
39 #include "insets/InsetInfo.h"
40 #include "insets/InsetNomencl.h"
41 #include "insets/InsetLabel.h"
42 #include "insets/InsetLine.h"
43 #include "insets/InsetMarginal.h"
44 #include "insets/InsetNote.h"
45 #include "insets/InsetBox.h"
46 #include "insets/InsetBranch.h"
47 #include "insets/InsetOptArg.h"
48 #include "insets/InsetNewpage.h"
49 #include "insets/InsetRef.h"
50 #include "insets/InsetSpace.h"
51 #include "insets/InsetTabular.h"
52 #include "insets/InsetTOC.h"
53 #include "insets/InsetHyperlink.h"
54 #include "insets/InsetVSpace.h"
55 #include "insets/InsetWrap.h"
56
57 #include "mathed/MathMacroTemplate.h"
58 #include "mathed/InsetMathHull.h"
59
60 #include "frontends/alert.h"
61
62 #include "support/debug.h"
63 #include "support/lstrings.h"
64 #include "support/ExceptionMessage.h"
65
66 #include <boost/assert.hpp>
67
68 #include <sstream>
69
70 using namespace std;
71
72 namespace lyx {
73
74 namespace Alert = frontend::Alert;
75
76
77 Inset * createInset(Buffer & buf, FuncRequest const & cmd)
78 {
79         BufferParams const & params = buf.params();
80
81         try {
82
83                 switch (cmd.action) {
84                 case LFUN_HFILL_INSERT:
85                         return new InsetHFill;
86
87                 case LFUN_LINE_INSERT:
88                         return new InsetLine;
89
90                 case LFUN_NEWPAGE_INSERT:
91                         return new InsetNewpage;
92
93                 case LFUN_PAGEBREAK_INSERT:
94                         return new InsetPagebreak;
95
96                 case LFUN_CLEARPAGE_INSERT:
97                         return new InsetClearPage;
98
99                 case LFUN_CLEARDOUBLEPAGE_INSERT:
100                         return new InsetClearDoublePage;
101
102                 case LFUN_FLEX_INSERT: {
103                         string s = cmd.getArg(0);
104                         TextClass const & tclass = params.getTextClass();
105                         InsetLayout const & il = tclass.insetlayout(from_utf8(s));
106                         return new InsetFlex(params, il);
107                 }
108
109                 case LFUN_NOTE_INSERT: {
110                         string arg = cmd.getArg(0);
111                         if (arg.empty())
112                                 arg = "Note";
113                         return new InsetNote(params, arg);
114                 }
115
116                 case LFUN_BOX_INSERT: {
117                         string arg = cmd.getArg(0);
118                         if (arg.empty())
119                                 arg = "Boxed";
120                         return new InsetBox(params, arg);
121                 }
122
123                 case LFUN_BRANCH_INSERT: {
124                         docstring arg = cmd.argument();
125                         if (arg.empty())
126                                 arg = from_ascii("none");
127                         return new InsetBranch(params, InsetBranchParams(arg));
128                 }
129
130                 case LFUN_ERT_INSERT:
131                         return new InsetERT(params);
132
133                 case LFUN_LISTING_INSERT:
134                         return new InsetListings(params);
135
136                 case LFUN_FOOTNOTE_INSERT:
137                         return new InsetFoot(params);
138
139                 case LFUN_MARGINALNOTE_INSERT:
140                         return new InsetMarginal(params);
141
142                 case LFUN_OPTIONAL_INSERT:
143                         return new InsetOptArg(params);
144
145                 case LFUN_BIBITEM_INSERT:
146                         return new InsetBibitem(InsetCommandParams(BIBITEM_CODE));
147
148                 case LFUN_FLOAT_INSERT: {
149                         // check if the float type exists
150                         string const argument = to_utf8(cmd.argument());
151                         if (params.getTextClass().floats().typeExist(argument))
152                                 return new InsetFloat(params, argument);
153                         lyxerr << "Non-existent float type: " << argument << endl;
154                 }
155
156                 case LFUN_FLOAT_WIDE_INSERT: {
157                         // check if the float type exists
158                         string const argument = to_utf8(cmd.argument());
159                         if (params.getTextClass().floats().typeExist(argument)) {
160                                 auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
161                                 p->wide(true, params);
162                                 return p.release();
163                         }
164                         lyxerr << "Non-existent float type: " << argument << endl;
165                         return 0;
166                 }
167
168                 case LFUN_WRAP_INSERT: {
169                         string const argument = to_utf8(cmd.argument());
170                         if (argument == "figure" || argument == "table")
171                                 return new InsetWrap(params, argument);
172                         lyxerr << "Non-existent wrapfig type: " << argument << endl;
173                         return 0;
174                 }
175
176                 case LFUN_INDEX_INSERT:
177                         return new InsetIndex(params);
178
179                 case LFUN_NOMENCL_INSERT: {
180                         InsetCommandParams icp(NOMENCL_CODE);
181                         icp["symbol"] = cmd.argument();
182                         return new InsetNomencl(icp);
183                 }
184
185                 case LFUN_TABULAR_INSERT: {
186                         if (cmd.argument().empty())
187                                 return 0;
188                         std::istringstream ss(to_utf8(cmd.argument()));
189                         int r = 0, c = 0;
190                         ss >> r >> c;
191                         if (r <= 0)
192                                 r = 2;
193                         if (c <= 0)
194                                 c = 2;
195                         return new InsetTabular(buf, r, c);
196                 }
197
198                 case LFUN_CAPTION_INSERT: {
199                         auto_ptr<InsetCaption> inset(new InsetCaption(params));
200                         inset->setAutoBreakRows(true);
201                         inset->setDrawFrame(true);
202                         inset->setFrameColor(Color_captionframe);
203                         return inset.release();
204                 }
205
206                 case LFUN_INDEX_PRINT:
207                         return new InsetPrintIndex(InsetCommandParams(INDEX_PRINT_CODE));
208
209                 case LFUN_NOMENCL_PRINT:
210                         return new InsetPrintNomencl(InsetCommandParams(NOMENCL_PRINT_CODE));
211
212                 case LFUN_TOC_INSERT:
213                         return new InsetTOC(InsetCommandParams(TOC_CODE));
214
215                 case LFUN_ENVIRONMENT_INSERT:
216                         return new InsetEnvironment(params, cmd.argument());
217
218                 case LFUN_INFO_INSERT:
219                         return new InsetInfo(params, to_utf8(cmd.argument()));
220 #if 0
221                 case LFUN_THEOREM_INSERT:
222                         return new InsetTheorem;
223 #endif
224
225                 case LFUN_INSET_INSERT: {
226                         string const name = cmd.getArg(0);
227                         InsetCode code = insetCode(name);
228                         switch (code) {
229                         case NO_CODE:
230                                 lyxerr << "No such inset '" << name << "'.";
231                                 return 0;
232                         
233                         case BIBITEM_CODE: {
234                                 InsetCommandParams icp(code);
235                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
236                                 return new InsetBibitem(icp);
237                         }
238                         
239                         case BIBTEX_CODE: {
240                                 InsetCommandParams icp(code);
241                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
242                                 return new InsetBibtex(icp);
243                         }
244                         
245                         case CITE_CODE: {
246                                 InsetCommandParams icp(code);
247                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
248                                 return new InsetCitation(icp);
249                         }
250                         
251                         case ERT_CODE: {
252                                 InsetCollapsable::CollapseStatus st;
253                                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
254                                 return new InsetERT(params, st);
255                         }
256                                 
257                         case LISTINGS_CODE: {
258                                 InsetListingsParams par;
259                                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), par);
260                                 return new InsetListings(params, par);
261                         }
262                         
263                         case EXTERNAL_CODE: {
264                                 InsetExternalParams iep;
265                                 InsetExternalMailer::string2params(to_utf8(cmd.argument()), buf, iep);
266                                 auto_ptr<InsetExternal> inset(new InsetExternal);
267                                 inset->setParams(iep, buf);
268                                 return inset.release();
269                         }
270                         
271                         case GRAPHICS_CODE: {
272                                 InsetGraphicsParams igp;
273                                 InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buf, igp);
274                                 auto_ptr<InsetGraphics> inset(new InsetGraphics);
275                                 inset->setParams(igp);
276                                 return inset.release();
277                         }
278                         
279                         case HYPERLINK_CODE: {
280                                 InsetCommandParams icp(code);
281                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
282                                 return new InsetHyperlink(icp);
283                         }
284                         
285                         case INCLUDE_CODE: {
286                                 InsetCommandParams icp(code);
287                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
288                                 return new InsetInclude(icp);
289                         }
290                         
291                         case INDEX_CODE:
292                                 return new InsetIndex(params);
293                         
294                         case NOMENCL_CODE: {
295                                 InsetCommandParams icp(code);
296                                 InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), icp);
297                                 return new InsetNomencl(icp);
298                         }
299                         
300                         case LABEL_CODE: {
301                                 InsetCommandParams icp(code);
302                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
303                                 return new InsetLabel(icp);
304                         }
305                         
306                         case REF_CODE: {
307                                 InsetCommandParams icp(code);
308                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
309                                 return new InsetRef(icp, buf);
310                         }
311                         
312                         case TOC_CODE: {
313                                 InsetCommandParams icp(code);
314                                 InsetCommandMailer::string2params(name, to_utf8(cmd.argument()), icp);
315                                 return new InsetTOC(icp);
316                         }
317                         
318                         case VSPACE_CODE: {
319                                 VSpace vspace;
320                                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
321                                 return new InsetVSpace(vspace);
322                         }
323                         
324                         default:
325                                 lyxerr << "Inset '" << name << "' not permitted with LFUN_INSET_INSERT."
326                                                 << std::endl;
327                                 return 0;
328                         
329                         }
330                         } //end LFUN_INSET_INSERT
331
332                 case LFUN_SPACE_INSERT: {
333                         string const name = to_utf8(cmd.argument());
334                         if (name == "normal")
335                                 return new InsetSpace(InsetSpace::NORMAL);
336                         if (name == "protected")
337                                 return new InsetSpace(InsetSpace::PROTECTED);
338                         if (name == "thin")
339                                 return new InsetSpace(InsetSpace::THIN);
340                         if (name == "quad")
341                                 return new InsetSpace(InsetSpace::QUAD);
342                         if (name == "qquad")
343                                 return new InsetSpace(InsetSpace::QQUAD);
344                         if (name == "enspace")
345                                 return new InsetSpace(InsetSpace::ENSPACE);
346                         if (name == "enskip")
347                                 return new InsetSpace(InsetSpace::ENSKIP);
348                         if (name == "negthinspace")
349                                 return new InsetSpace(InsetSpace::NEGTHIN);
350                         if (name.empty())
351                                 lyxerr << "LyX function 'space' needs an argument." << endl;
352                         else
353                                 lyxerr << "Wrong argument for LyX function 'space'." << endl;
354                 }
355                 break;
356
357                 default:
358                         break;
359                 }
360
361         } catch (support::ExceptionMessage const & message) {
362                 if (message.type_ == support::ErrorException) {
363                         Alert::error(message.title_, message.details_);
364                         LyX::cref().emergencyCleanup();
365                         abort();
366                 } else if (message.type_ == support::WarningException) {
367                         Alert::warning(message.title_, message.details_);
368                         return 0;
369                 }
370         }
371
372
373         return 0;
374 }
375
376
377 Inset * readInset(Lexer & lex, Buffer const & buf)
378 {
379         // consistency check
380         if (lex.getString() != "\\begin_inset") {
381                 lyxerr << "Buffer::readInset: Consistency check failed."
382                        << endl;
383         }
384
385         auto_ptr<Inset> inset;
386
387         TextClass const & tclass = buf.params().getTextClass();
388
389         lex.next();
390         string tmptok = lex.getString();
391
392         // test the different insets
393         
394         //FIXME It would be better if we did not have this branch and could
395         //just do one massive switch for all insets. But at present, it's easier 
396         //to do it this way, and we can't do the massive switch until the conversion 
397         //mentioned below. 
398         //Note that if we do want to do a single switch, we need to remove
399         //this "CommandInset" line---or replace it with a single "InsetType" line
400         //that would be used in all insets.
401         if (tmptok == "CommandInset") {
402                 lex.next();
403                 string const insetType = lex.getString();
404                 lex.pushToken(insetType);
405                 
406                 InsetCode const code = insetCode(insetType);
407                 
408                 //FIXME If we do the one massive switch, we cannot do this here, since
409                 //we do not know in advance that we're dealing with a command inset.
410                 //Worst case, we could put it in each case below. Better, we could
411                 //pass the lexer to the constructor and let the params be built there.
412                 InsetCommandParams inscmd(code);
413                 inscmd.read(lex);
414
415                 switch (code) {
416                         case BIBITEM_CODE:
417                                 inset.reset(new InsetBibitem(inscmd));
418                                 break;
419                         case BIBTEX_CODE:
420                                 inset.reset(new InsetBibtex(inscmd));
421                                 break;
422                         case CITE_CODE: 
423                                 inset.reset(new InsetCitation(inscmd));
424                                 break;
425                         case HYPERLINK_CODE:
426                                 inset.reset(new InsetHyperlink(inscmd));
427                                 break;
428                         case INCLUDE_CODE:
429                                 inset.reset(new InsetInclude(inscmd));
430                                 break;
431                         case INDEX_PRINT_CODE:
432                                 inset.reset(new InsetPrintIndex(inscmd));
433                                 break;
434                         case LABEL_CODE:
435                                 inset.reset(new InsetLabel(inscmd));
436                                 break;
437                         case NOMENCL_CODE:
438                                 inset.reset(new InsetNomencl(inscmd));
439                                 break;
440                         case NOMENCL_PRINT_CODE:
441                                 inset.reset(new InsetPrintNomencl(inscmd));
442                                 break;
443                         case REF_CODE:
444                                 if (!inscmd["name"].empty() || !inscmd["reference"].empty())
445                                         inset.reset(new InsetRef(inscmd, buf));
446                                 break;
447                         case TOC_CODE:
448                                 inset.reset(new InsetTOC(inscmd));
449                                 break;
450                         case NO_CODE:
451                         default:
452                                 lyxerr << "unknown CommandInset '" << insetType
453                                                         << "'" << std::endl;
454                                 while (lex.isOK() && lex.getString() != "\\end_inset")
455                                         lex.next();
456                                 return 0;
457                 }
458         } else { 
459                 // FIXME This branch should be made to use inset codes as the preceding 
460                 // branch does. Unfortunately, that will take some doing. It requires
461                 // converting the representation of the insets in LyX files so that they
462                 // use the inset names listed in Inset.cpp. Then, as above, the inset names
463                 // can be translated to inset codes using insetCode(). And the insets'
464                 // write() routines should use insetName() rather than hardcoding it.
465                 if (tmptok == "Quotes") {
466                         inset.reset(new InsetQuotes);
467                 } else if (tmptok == "External") {
468                         inset.reset(new InsetExternal);
469                 } else if (tmptok == "FormulaMacro") {
470                         inset.reset(new MathMacroTemplate);
471                 } else if (tmptok == "Formula") {
472                         inset.reset(new InsetMathHull);
473                 } else if (tmptok == "Graphics") {
474                         inset.reset(new InsetGraphics);
475                 } else if (tmptok == "Note") {
476                         inset.reset(new InsetNote(buf.params(), tmptok));
477                 } else if (tmptok == "Box") {
478                         inset.reset(new InsetBox(buf.params(), tmptok));
479                 } else if (tmptok == "Flex") {
480                         lex.next();
481                         string s = lex.getString();
482                         InsetLayout const & il = tclass.insetlayout(from_utf8(s));
483                         inset.reset(new InsetFlex(buf.params(), il));
484                 } else if (tmptok == "Branch") {
485                         inset.reset(new InsetBranch(buf.params(),
486                                                     InsetBranchParams()));
487                 } else if (tmptok == "Environment") {
488                         lex.next();
489                         inset.reset(new InsetEnvironment(buf.params(), lex.getDocString()));
490                 } else if (tmptok == "ERT") {
491                         inset.reset(new InsetERT(buf.params()));
492                 } else if (tmptok == "listings") {
493                         inset.reset(new InsetListings(buf.params()));
494                 } else if (tmptok == "InsetSpace") {
495                         inset.reset(new InsetSpace);
496                 } else if (tmptok == "Tabular") {
497                         inset.reset(new InsetTabular(buf));
498                 } else if (tmptok == "Text") {
499                         inset.reset(new InsetText(buf.params()));
500                 } else if (tmptok == "VSpace") {
501                         inset.reset(new InsetVSpace);
502                 } else if (tmptok == "Foot") {
503                         inset.reset(new InsetFoot(buf.params()));
504                 } else if (tmptok == "Marginal") {
505                         inset.reset(new InsetMarginal(buf.params()));
506                 } else if (tmptok == "OptArg") {
507                         inset.reset(new InsetOptArg(buf.params()));
508                 } else if (tmptok == "Float") {
509                         lex.next();
510                         string tmptok = lex.getString();
511                         inset.reset(new InsetFloat(buf.params(), tmptok));
512                 } else if (tmptok == "Wrap") {
513                         lex.next();
514                         string tmptok = lex.getString();
515                         inset.reset(new InsetWrap(buf.params(), tmptok));
516 #if 0
517                 } else if (tmptok == "Theorem") {
518                         inset.reset(new InsetList);
519 #endif
520                 } else if (tmptok == "Caption") {
521                         inset.reset(new InsetCaption(buf.params()));
522                 } else if (tmptok == "Index") {
523                         inset.reset(new InsetIndex(buf.params()));
524                 } else if (tmptok == "FloatList") {
525                         inset.reset(new InsetFloatList);
526                 } else if (tmptok == "Info") {
527                         inset.reset(new InsetInfo(buf.params()));
528                 } else {
529                         lyxerr << "unknown Inset type '" << tmptok
530                                << "'" << std::endl;
531                         while (lex.isOK() && lex.getString() != "\\end_inset")
532                                 lex.next();
533                         return 0;
534                 }
535
536                 inset->read(buf, lex);
537         }
538
539         return inset.release();
540 }
541
542
543 } // namespace lyx