]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Enable the external inset to handle unknown templates gracefully.
[lyx.git] / src / insets / insetcommand.C
1 /**
2  * \file insetcommand.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "insetcommand.h"
15 #include "BufferView.h"
16 #include "debug.h"
17 #include "funcrequest.h"
18 #include "lyxlex.h"
19
20 #include "frontends/Painter.h"
21
22 #include "support/lstrings.h"
23
24 #include "Lsstream.h"
25
26 using std::ostream;
27
28
29 InsetCommand::InsetCommand(InsetCommandParams const & p)
30         : p_(p.getCmdName(), p.getContents(), p.getOptions())
31 {}
32
33
34 InsetCommand::InsetCommand(InsetCommand const & ic)
35         : p_(ic.p_)
36 {
37 }
38
39
40 void InsetCommand::setParams(InsetCommandParams const & p)
41 {
42         p_.setCmdName(p.getCmdName());
43         p_.setContents(p.getContents());
44         p_.setOptions(p.getOptions());
45 }
46
47
48 int InsetCommand::latex(Buffer const *, ostream & os,
49                         LatexRunParams const &) const
50 {
51         os << getCommand();
52         return 0;
53 }
54
55
56 int InsetCommand::ascii(Buffer const *, ostream &, int) const
57 {
58         return 0;
59 }
60
61
62 int InsetCommand::linuxdoc(Buffer const *, ostream &) const
63 {
64         return 0;
65 }
66
67
68 int InsetCommand::docbook(Buffer const *, ostream &, bool) const
69 {
70         return 0;
71 }
72
73
74 dispatch_result InsetCommand::localDispatch(FuncRequest const & cmd)
75 {
76         switch (cmd.action) {
77         case LFUN_INSET_MODIFY: {
78                 InsetCommandParams p;
79                 InsetCommandMailer::string2params(cmd.argument, p);
80                 if (p.getCmdName().empty())
81                         return UNDISPATCHED;
82
83                 setParams(p);
84                 cmd.view()->updateInset(this);
85                 return DISPATCHED;
86         }
87
88         case LFUN_INSET_DIALOG_UPDATE:
89                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
90                 return DISPATCHED;
91
92         case LFUN_MOUSE_RELEASE:
93                 return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
94
95         default:
96                 return UNDISPATCHED;
97         }
98
99 }
100
101
102 InsetCommandMailer::InsetCommandMailer(string const & name,
103                                        InsetCommand & inset)
104         : name_(name), inset_(inset)
105 {}
106
107
108 string const InsetCommandMailer::inset2string() const
109 {
110         return params2string(name(), inset_.params());
111 }
112
113
114 void InsetCommandMailer::string2params(string const & in,
115                                        InsetCommandParams & params)
116 {
117         params.setCmdName(string());
118         params.setContents(string());
119         params.setOptions(string());
120
121         if (in.empty())
122                 return;
123
124         istringstream data(STRCONV(in));
125         LyXLex lex(0,0);
126         lex.setStream(data);
127
128         if (lex.isOK()) {
129                 lex.next();
130                 string const name = lex.getString();
131         }
132
133         // This is part of the inset proper that is usually swallowed
134         // by Buffer::readInset
135         if (lex.isOK()) {
136                 lex.next();
137                 string const token = lex.getString();
138                 if (token != "LatexCommand")
139                         return;
140         }
141         if (lex.isOK()) {
142                 params.read(lex);
143         }
144 }
145
146
147 string const InsetCommandMailer::params2string(string const & name,
148                                   InsetCommandParams const & params)
149 {
150         ostringstream data;
151         data << name << ' ';
152         params.write(data);
153         data << "\\end_inset\n";
154         return STRCONV(data.str());
155 }