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