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