]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
fix #832
[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         lyxerr << "InsetCommand::localDispatch: " << cmd.action << "\n";
71         switch (cmd.action) {
72         case LFUN_INSET_MODIFY: {
73                 InsetCommandParams p;
74                 InsetCommandMailer::string2params(cmd.argument, p);
75                 if (p.getCmdName().empty())
76                         return UNDISPATCHED;
77
78                 setParams(p);
79                 cmd.view()->updateInset(this);
80                 return DISPATCHED;
81         }
82
83         case LFUN_INSET_DIALOG_UPDATE:
84                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
85                 return DISPATCHED;
86
87         case LFUN_MOUSE_RELEASE:
88                 return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
89
90         default:
91                 return UNDISPATCHED;
92         }
93
94 }
95
96
97 InsetCommandMailer::InsetCommandMailer(string const & name,
98                                        InsetCommand & inset)
99         : name_(name), inset_(inset)
100 {}
101
102
103 string const InsetCommandMailer::inset2string() const
104 {
105         return params2string(name(), inset_.params());
106 }
107
108
109 void InsetCommandMailer::string2params(string const & in,
110                                        InsetCommandParams & params)
111 {
112         params.setCmdName(string());
113         params.setContents(string());
114         params.setOptions(string());
115
116         if (in.empty())
117                 return;
118         
119         istringstream data(STRCONV(in));
120         LyXLex lex(0,0);
121         lex.setStream(data);
122
123         if (lex.isOK()) {
124                 lex.next();
125                 string const name = lex.getString();
126         }
127
128         // This is part of the inset proper that is usually swallowed
129         // by Buffer::readInset
130         if (lex.isOK()) {
131                 lex.next();
132                 string const token = lex.getString();
133                 if (token != "LatexCommand")
134                         return;
135         }
136         if (lex.isOK()) {
137                 params.read(lex);
138         }
139 }
140
141
142 string const InsetCommandMailer::params2string(string const & name,
143                                   InsetCommandParams const & params)
144 {
145         ostringstream data;
146         data << name << ' ';
147         params.write(data);
148         data << "\\end_inset\n";
149         return STRCONV(data.str());
150 }