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