]> git.lyx.org Git - lyx.git/blob - src/factory.C
288aa0e15043192bde889bc9e3142ab6ab6855e3
[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
21 #include "insets/insetbibitem.h"
22 #include "insets/insetbibtex.h"
23 #include "insets/insetcaption.h"
24 #include "insets/insetcite.h"
25 #include "insets/insetert.h"
26 #include "insets/insetexternal.h"
27 #include "insets/insetfloat.h"
28 #include "insets/insetfoot.h"
29 #include "insets/insetindex.h"
30 #include "insets/insetmarginal.h"
31 #include "insets/insetminipage.h"
32 #include "insets/insetnote.h"
33 #include "insets/insetoptarg.h"
34 #include "insets/insetparent.h"
35 #include "insets/insetref.h"
36 #include "insets/insettabular.h"
37 #include "insets/insettext.h"
38 #include "insets/insettoc.h"
39 #include "insets/inseturl.h"
40 #include "insets/insetwrap.h"
41
42 #include "frontends/Dialogs.h"
43 #include "frontends/LyXView.h"
44
45 #include <cstdio>
46
47 using std::endl;
48
49 Inset * createInset(FuncRequest const & cmd)
50 {
51         BufferView * bv = cmd.view();
52         BufferParams const & params = bv->buffer()->params;
53
54         switch (cmd.action) {
55         case LFUN_INSET_MINIPAGE:
56                 return new InsetMinipage(params);
57
58         case LFUN_INSERT_NOTE:
59                 return new InsetNote(params);
60
61         case LFUN_INSET_ERT:
62                 return new InsetERT(params);
63
64         case LFUN_INSET_EXTERNAL:
65                 return new InsetExternal;
66
67         case LFUN_INSET_FOOTNOTE:
68                 return new InsetFoot(params);
69
70         case LFUN_INSET_MARGINAL:
71                 return new InsetMarginal(params);
72
73         case LFUN_INSET_OPTARG:
74                 return new InsetOptArg(params);
75
76         case LFUN_INSERT_BIBITEM:
77                 return new InsetBibitem(InsetCommandParams("bibitem"));
78
79         case LFUN_INSET_FLOAT:
80                 // check if the float type exists
81                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
82                         return new InsetFloat(params, cmd.argument);
83                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
84                 return 0;
85
86         case LFUN_INSET_WIDE_FLOAT:
87                 // check if the float type exists
88                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
89                         InsetFloat * p = new InsetFloat(params, cmd.argument);
90                         p->wide(true, params);
91                 }
92                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
93                 return 0;
94
95         case LFUN_INSET_WRAP:
96                 if (cmd.argument == "figure")
97                         return new InsetWrap(params, cmd.argument);
98                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
99                 return 0;
100
101         case LFUN_INDEX_INSERT: {
102                 // Try and generate a valid index entry.
103                 InsetCommandParams icp("index");
104                 string const contents = cmd.argument.empty() ?
105                         bv->getLyXText()->getStringToIndex(bv) :
106                         cmd.argument;
107                 icp.setContents(contents);
108
109                 string data = InsetCommandMailer::params2string("index", icp);
110                 LyXView * lv = bv->owner();
111
112                 if (icp.getContents().empty()) {
113                         lv->getDialogs().show("index", data, 0);
114                 } else {
115                         FuncRequest fr(bv, LFUN_INSET_APPLY, data);
116                         lv->dispatch(fr);
117                 }
118                 return 0;
119         }
120
121         case LFUN_TABULAR_INSERT:
122                 if (!cmd.argument.empty()) {
123                         int r = 2;
124                         int c = 2;
125                         ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
126                         return new InsetTabular(*bv->buffer(), r, c);
127                 }
128                 bv->owner()->getDialogs().showTabularCreate();
129                 return 0;
130
131         case LFUN_INSET_CAPTION:
132                 if (bv->theLockingInset()) {
133                         lyxerr << "Locking inset code: "
134                                << static_cast<int>(bv->theLockingInset()->lyxCode());
135                         InsetCaption * inset = new InsetCaption(params);
136                         inset->setOwner(bv->theLockingInset());
137                         inset->setAutoBreakRows(true);
138                         inset->setDrawFrame(0, InsetText::LOCKED);
139                         inset->setFrameColor(0, LColor::captionframe);
140                         return inset;
141                 }
142                 return 0;
143
144         case LFUN_INDEX_PRINT:
145                 return new InsetPrintIndex(InsetCommandParams("printindex"));
146
147         case LFUN_TOC_INSERT:
148                 return new InsetTOC(InsetCommandParams("tableofcontents"));
149
150         case LFUN_PARENTINSERT:
151                 return new InsetParent(
152                         InsetCommandParams("lyxparent", cmd.argument), *bv->buffer());
153
154         case LFUN_INSERT_URL:
155         {
156                 InsetCommandParams p;
157                 p.setFromString(cmd.argument);
158                 return new InsetUrl(p);
159         }
160
161 #if 0
162         case LFUN_INSET_LIST:
163                 return new InsetList;
164
165         case LFUN_INSET_THEOREM:
166                 return new InsetTheorem;
167 #endif
168
169         case LFUN_INSET_APPLY: {
170                 string const name = cmd.getArg(0);
171
172                 if (name == "bibitem") {
173                         InsetCommandParams icp;
174                         InsetCommandMailer::string2params(cmd.argument, icp);
175                         return new InsetBibitem(icp);
176
177                 } else if (name == "bibtex") {
178                         InsetCommandParams icp;
179                         InsetCommandMailer::string2params(cmd.argument, icp);
180                         return new InsetBibtex(icp);
181
182                 } else if (name == "citation") {
183                         InsetCommandParams icp;
184                         InsetCommandMailer::string2params(cmd.argument, icp);
185                         InsetCitation * inset = new InsetCitation(icp);
186                         inset->setLoadingBuffer(bv->buffer(), false);
187                         return inset;
188
189                 } else if (name == "ert") {
190                         InsetERT * inset = new InsetERT(params);
191                         InsetERT::ERTStatus s;
192                         InsetERTMailer::string2params(cmd.argument, s);
193                         inset->status(bv, s);
194                         return inset;
195
196                 } else if (name == "index") {
197                         InsetCommandParams icp;
198                         InsetCommandMailer::string2params(cmd.argument, icp);
199                         return new InsetIndex(icp);
200
201                 } else if (name == "ref") {
202                         InsetCommandParams icp;
203                         InsetCommandMailer::string2params(cmd.argument, icp);
204                         return new InsetRef(icp, *bv->buffer());
205
206                 } else if (name == "toc") {
207                         InsetCommandParams icp;
208                         InsetCommandMailer::string2params(cmd.argument, icp);
209                         return new InsetTOC(icp);
210
211                 } else if (name == "url") {
212                         InsetCommandParams icp;
213                         InsetCommandMailer::string2params(cmd.argument, icp);
214                         return new InsetUrl(icp);
215                 }
216         }
217         break;
218
219         default:
220                 break;
221         }
222
223         return 0;
224 }