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