]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[lyx.git] / src / insets / InsetCommand.cpp
1 /**
2  * \file InsetCommand.cpp
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
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "support/gettext.h"
22 #include "Lexer.h"
23 #include "MetricsInfo.h"
24
25 #include <sstream>
26
27 using namespace std;
28
29 namespace lyx {
30
31
32 // FIXME Would it now be possible to use the InsetCode in 
33 // place of the mailer name and recover that information?
34 InsetCommand::InsetCommand(InsetCommandParams const & p,
35                            string const & mailer_name)
36         : p_(p),
37           mailer_name_(mailer_name),
38           mouse_hover_(false)
39 {}
40
41
42 InsetCommand::~InsetCommand()
43 {
44         if (!mailer_name_.empty())
45                 InsetCommandMailer(mailer_name_, *this).hideDialog();
46 }
47
48
49 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
50 {
51         button_.update(screenLabel(), editable() != NOT_EDITABLE);
52         button_.metrics(mi, dim);
53 }
54
55
56 bool InsetCommand::setMouseHover(bool mouse_hover)
57 {
58         mouse_hover_ = mouse_hover;
59         return true;
60 }
61
62
63 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
64 {
65         button_.setRenderState(mouse_hover_);
66         button_.draw(pi, x, y);
67 }
68
69
70 void InsetCommand::setParams(InsetCommandParams const & p)
71 {
72         p_ = p;
73         initView();
74 }
75
76
77 int InsetCommand::latex(odocstream & os, OutputParams const &) const
78 {
79         os << getCommand();
80         return 0;
81 }
82
83
84 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
85 {
86         docstring const str = "[" + buffer().B_("LaTeX Command: ")
87                 + from_utf8(getCmdName()) + "]";
88         os << str;
89         return str.size();
90 }
91
92
93 int InsetCommand::docbook(odocstream &, OutputParams const &) const
94 {
95         return 0;
96 }
97
98
99 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
100 {
101         switch (cmd.action) {
102         case LFUN_INSET_MODIFY: {
103                 InsetCommandParams p(p_.code());
104                 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
105                 if (p.getCmdName().empty())
106                         cur.noUpdate();
107                 else
108                         setParams(p);
109                 break;
110         }
111
112         case LFUN_INSET_DIALOG_UPDATE: {
113                 string const name = to_utf8(cmd.argument());
114                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
115                 break;
116         }
117
118         case LFUN_MOUSE_RELEASE: {
119                 if (!cur.selection() && cmd.button() != mouse_button::button3)
120                         edit(cur, true);
121                 break;
122         }
123
124         default:
125                 Inset::doDispatch(cur, cmd);
126                 break;
127         }
128
129 }
130
131
132 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
133         FuncStatus & status) const
134 {
135         switch (cmd.action) {
136         // suppress these
137         case LFUN_ERT_INSERT:
138                 status.enabled(false);
139                 return true;
140         // we handle these
141         case LFUN_INSET_MODIFY:
142         case LFUN_INSET_DIALOG_UPDATE:
143                 status.enabled(true);
144                 return true;
145         default:
146                 return Inset::getStatus(cur, cmd, status);
147         }
148 }
149
150
151 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
152 {
153         return from_ascii("context-") + from_ascii(mailer_name_);
154 }
155
156
157 void InsetCommand::edit(Cursor & cur, bool, EntryDirection)
158 {
159         if (!mailer_name_.empty())
160                 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
161 }
162
163
164 InsetCommandMailer::InsetCommandMailer(
165         string const & name, InsetCommand & inset)
166         : name_(name), inset_(inset)
167 {}
168
169
170 string const InsetCommandMailer::inset2string(Buffer const &) const
171 {
172         return params2string(name(), inset_.params());
173 }
174
175
176 //FIXME This could take an InsetCode instead of a string
177 bool InsetCommandMailer::string2params(
178         string const & name, string const & in, InsetCommandParams & params)
179 {
180         params.clear();
181         if (in.empty())
182                 return false;
183
184         istringstream data(in);
185         Lexer lex(0,0);
186         lex.setStream(data);
187
188         string n;
189         lex >> n;
190         if (!lex || n != name) {
191                 print_mailer_error("InsetCommandMailer", in, 1, name);
192                 return false;
193         }
194
195         // This is part of the inset proper that is usually swallowed
196         // by Text::readInset
197         string id;
198         lex >> id;
199         if (!lex || id != "CommandInset") {
200                 print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
201                 return false;
202         }
203
204         params.read(lex);
205         return true;
206 }
207
208
209 //FIXME This could take an InsetCode instead of a string
210 string const
211 InsetCommandMailer::params2string(string const & name,
212                                   InsetCommandParams const & params)
213 {
214         ostringstream data;
215         data << name << ' ';
216         params.write(data);
217         data << "\\end_inset\n";
218         return data.str();
219 }
220
221
222 } // namespace lyx