]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
86203f397bb319ff83363fe91a839e42c64305cc
[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, bool)
30         : p_(p.getCmdName(), p.getContents(), p.getOptions())
31 {}
32
33
34 void InsetCommand::setParams(InsetCommandParams const & p)
35 {
36         p_.setCmdName(p.getCmdName());
37         p_.setContents(p.getContents());
38         p_.setOptions(p.getOptions());
39 }
40
41
42 int InsetCommand::latex(Buffer const *, ostream & os,
43                         bool /*fragile*/, bool/*fs*/) const
44 {
45         os << getCommand();
46         return 0;
47 }
48
49
50 int InsetCommand::ascii(Buffer const *, ostream &, int) const
51 {
52         return 0;
53 }
54
55
56 int InsetCommand::linuxdoc(Buffer const *, ostream &) const
57 {
58         return 0;
59 }
60
61
62 int InsetCommand::docbook(Buffer const *, ostream &, bool) const
63 {
64         return 0;
65 }
66
67
68 dispatch_result InsetCommand::localDispatch(FuncRequest const & cmd)
69 {
70         dispatch_result result = UNDISPATCHED;
71
72         switch (cmd.action) {
73         case LFUN_INSET_MODIFY: {
74                 InsetCommandParams p;
75                 InsetCommandMailer::string2params(cmd.argument, p);
76                 if (p.getCmdName().empty())
77                         break;
78
79                 setParams(p);
80                 cmd.view()->updateInset(this);
81                 result = DISPATCHED;
82         }
83         break;
84
85         case LFUN_INSET_DIALOG_UPDATE: {
86                 InsetCommandMailer mailer(cmd.argument, *this);
87                 mailer.updateDialog(cmd.view());
88                 result = DISPATCHED;
89         }
90         break;
91
92         case LFUN_MOUSE_RELEASE:
93                 edit(cmd.view(), cmd.x, cmd.y, cmd.button());
94                 result = DISPATCHED;
95                 break;
96
97         default:
98                 break;
99         }
100
101         return result;
102 }
103
104
105 InsetCommandMailer::InsetCommandMailer(string const & name,
106                                        InsetCommand & inset)
107         : name_(name), inset_(inset)
108 {}
109
110
111 string const InsetCommandMailer::inset2string() const
112 {
113         return params2string(name(), inset_.params());
114 }
115
116
117 void InsetCommandMailer::string2params(string const & in,
118                                        InsetCommandParams & params)
119 {
120         params.setCmdName(string());
121         params.setContents(string());
122         params.setOptions(string());
123
124         if (in.empty())
125                 return;
126         
127         istringstream data(STRCONV(in));
128         LyXLex lex(0,0);
129         lex.setStream(data);
130
131         if (lex.isOK()) {
132                 lex.next();
133                 string const name = lex.getString();
134         }
135
136         // This is part of the inset proper that is usually swallowed
137         // by Buffer::readInset
138         if (lex.isOK()) {
139                 lex.next();
140                 string const token = lex.getString();
141                 if (token != "LatexCommand")
142                         return;
143         }
144         if (lex.isOK()) {
145                 params.read(lex);
146         }
147 }
148
149
150 string const InsetCommandMailer::params2string(string const & name,
151                                   InsetCommandParams const & params)
152 {
153         ostringstream data;
154         data << name << ' ';
155         params.write(data);
156         data << "\\end_inset\n";
157         return STRCONV(data.str());
158 }