]> git.lyx.org Git - lyx.git/blob - src/factory.C
port the graphics dialog to the new scheme and get rid of the ControlInset
[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/insetinclude.h"
31 #include "insets/insetindex.h"
32 #include "insets/insetlabel.h"
33 #include "insets/insetmarginal.h"
34 #include "insets/insetminipage.h"
35 #include "insets/insetnote.h"
36 #include "insets/insetoptarg.h"
37 #include "insets/insetparent.h"
38 #include "insets/insetref.h"
39 #include "insets/insettabular.h"
40 #include "insets/insettext.h"
41 #include "insets/insettoc.h"
42 #include "insets/inseturl.h"
43 #include "insets/insetwrap.h"
44
45 #include "frontends/Dialogs.h"
46 #include "frontends/LyXView.h"
47
48 #include <cstdio>
49
50 using std::endl;
51
52 Inset * createInset(FuncRequest const & cmd)
53 {
54         BufferView * bv = cmd.view();
55         BufferParams const & params = bv->buffer()->params;
56
57         switch (cmd.action) {
58         case LFUN_INSET_MINIPAGE:
59                 return new InsetMinipage(params);
60
61         case LFUN_INSERT_NOTE:
62                 return new InsetNote(params);
63
64         case LFUN_INSET_ERT:
65                 return new InsetERT(params);
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 #if 0
155         case LFUN_INSET_LIST:
156                 return new InsetList;
157
158         case LFUN_INSET_THEOREM:
159                 return new InsetTheorem;
160 #endif
161
162         case LFUN_INSET_APPLY: {
163                 string const name = cmd.getArg(0);
164
165                 if (name == "bibitem") {
166                         InsetCommandParams icp;
167                         InsetCommandMailer::string2params(cmd.argument, icp);
168                         return new InsetBibitem(icp);
169
170                 } else if (name == "bibtex") {
171                         InsetCommandParams icp;
172                         InsetCommandMailer::string2params(cmd.argument, icp);
173                         return new InsetBibtex(icp);
174
175                 } else if (name == "citation") {
176                         InsetCommandParams icp;
177                         InsetCommandMailer::string2params(cmd.argument, icp);
178                         InsetCitation * inset = new InsetCitation(icp);
179                         inset->setLoadingBuffer(bv->buffer(), false);
180                         return inset;
181
182                 } else if (name == "ert") {
183                         InsetERT * inset = new InsetERT(params);
184                         InsetERT::ERTStatus s;
185                         InsetERTMailer::string2params(cmd.argument, s);
186                         inset->status(bv, s);
187                         return inset;
188
189                 } else if (name == "external") {
190                         InsetExternal::Params iep;                      
191                         InsetExternalMailer::string2params(cmd.argument, iep);
192                         InsetExternal * inset = new InsetExternal;
193                         inset->setFromParams(iep);
194                         return inset;
195
196                 } else if (name == "graphics") {
197                         InsetGraphicsParams igp;                        
198                         InsetGraphicsMailer::string2params(cmd.argument, igp);
199                         InsetGraphics * inset = new InsetGraphics;
200                         string const fpath = cmd.view()->buffer()->filePath();
201                         inset->setParams(igp, fpath);
202                         return inset;
203
204                 } else if (name == "include") {
205                         InsetInclude::Params iip;
206                         InsetIncludeMailer::string2params(cmd.argument, iip);
207                         return new InsetInclude(iip);
208
209                 } else if (name == "index") {
210                         InsetCommandParams icp;
211                         InsetCommandMailer::string2params(cmd.argument, icp);
212                         return new InsetIndex(icp);
213
214                 } else if (name == "label") {
215                         InsetCommandParams icp;
216                         InsetCommandMailer::string2params(cmd.argument, icp);
217                         return new InsetLabel(icp);
218
219                 } else if (name == "ref") {
220                         InsetCommandParams icp;
221                         InsetCommandMailer::string2params(cmd.argument, icp);
222                         return new InsetRef(icp, *bv->buffer());
223
224                 } else if (name == "toc") {
225                         InsetCommandParams icp;
226                         InsetCommandMailer::string2params(cmd.argument, icp);
227                         return new InsetTOC(icp);
228
229                 } else if (name == "url") {
230                         InsetCommandParams icp;
231                         InsetCommandMailer::string2params(cmd.argument, icp);
232                         return new InsetUrl(icp);
233                 }
234         }
235         break;
236
237         default:
238                 break;
239         }
240
241         return 0;
242 }