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