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