]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Change DispatchResult semantics a bit.
[lyx.git] / src / insets / insetnote.C
1 /**
2  * \file insetnote.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 Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetnote.h"
16
17 #include "BufferView.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26
27 #include "support/std_sstream.h"
28
29
30 using std::string;
31 using std::auto_ptr;
32 using std::istringstream;
33 using std::ostream;
34 using std::ostringstream;
35
36
37 void InsetNote::init()
38 {
39         setInsetName("Note");
40         setButtonLabel();
41 }
42
43
44 InsetNote::InsetNote(BufferParams const & bp, string const & label)
45         : InsetCollapsable(bp)
46 {
47         params_.type = label;
48         init();
49 }
50
51
52 InsetNote::InsetNote(InsetNote const & in)
53         : InsetCollapsable(in), params_(in.params_)
54 {
55         init();
56 }
57
58
59 InsetNote::~InsetNote()
60 {
61         InsetNoteMailer("note", *this).hideDialog();
62 }
63
64
65 auto_ptr<InsetBase> InsetNote::clone() const
66 {
67         return auto_ptr<InsetBase>(new InsetNote(*this));
68 }
69
70
71 string const InsetNote::editMessage() const
72 {
73         return _("Opened Note Inset");
74 }
75
76
77 void InsetNote::write(Buffer const & buf, ostream & os) const
78 {
79         params_.write(os);
80         InsetCollapsable::write(buf, os);
81 }
82
83
84 void InsetNote::read(Buffer const & buf, LyXLex & lex)
85 {
86         InsetCollapsable::read(buf, lex);
87         setButtonLabel();
88 }
89
90
91 void InsetNote::setButtonLabel()
92 {
93         LyXFont font(LyXFont::ALL_SANE);
94         font.decSize();
95         font.decSize();
96
97         if (params_.type == "Note") {
98                 setLabel(_("LyX Note"));
99                 font.setColor(LColor::note);
100                 setBackgroundColor(LColor::notebg);
101         } else if (params_.type == "Comment") {
102                 setLabel(_("Comment"));
103                 font.setColor(LColor::comment);
104                 setBackgroundColor(LColor::commentbg);
105         } else {
106                 setLabel(_("Greyed Out"));
107                 font.setColor(LColor::greyedout);
108                 setBackgroundColor(LColor::greyedoutbg);
109         }
110         setLabelFont(font);
111 }
112
113
114 void InsetNote::metrics(MetricsInfo & mi, Dimension & dim) const
115 {
116         InsetCollapsable::metrics(mi, dim);
117         // Contrary to Greyedout, these cannot be construed as part of the
118         // running text: make them stand on their own
119         if (params_.type == "Note" || params_.type == "Comment")
120                 if (isOpen())
121                         dim.wid = mi.base.textwidth;
122         dim_ = dim;
123 }
124
125
126 bool InsetNote::showInsetDialog(BufferView * bv) const
127 {
128         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
129         return true;
130 }
131
132
133 DispatchResult
134 InsetNote::priv_dispatch(FuncRequest const & cmd,
135                          idx_type & idx, pos_type & pos)
136 {
137         BufferView * bv = cmd.view();
138
139         switch (cmd.action) {
140
141         case LFUN_INSET_MODIFY: {
142                 InsetNoteMailer::string2params(cmd.argument, params_);
143                 setButtonLabel();
144                 bv->updateInset(this);
145                 return DispatchResult(true, true);
146         }
147
148         case LFUN_INSET_EDIT:
149                 if (cmd.button() == mouse_button::button3)
150                         return DispatchResult(false);
151                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
152
153         case LFUN_INSET_DIALOG_UPDATE:
154                 InsetNoteMailer("note", *this).updateDialog(bv);
155                 return DispatchResult(true, true);
156
157         case LFUN_MOUSE_RELEASE:
158                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
159                         InsetNoteMailer("note", *this).showDialog(bv);
160                         return DispatchResult(true, true);
161                 }
162                 // fallthrough:
163
164         default:
165                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
166         }
167 }
168
169
170 int InsetNote::latex(Buffer const & buf, ostream & os,
171                      LatexRunParams const & runparams) const
172 {
173         string const pt = params_.type;
174
175         int i = 0;
176         if (pt == "Comment")
177                  // verbatim
178                 os << "%\n\\begin{comment}\n";
179         else if (pt == "Greyedout")
180                  // we roll our own macro
181                 os << "%\n\\begin{lyxgreyedout}\n";
182
183         if (pt != "Note")
184                 i = inset.latex(buf, os, runparams);
185
186         if (pt == "Comment") {
187                 os << "%\n\\end{comment}\n";
188                 i += 4;
189         } else if (pt == "Greyedout") {
190                 os << "%\n\\end{lyxgreyedout}\n";
191                 i += 4;
192         }
193         return i;
194 }
195
196
197 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
198                         LatexRunParams const & runparams) const
199 {
200         string const pt = params_.type;
201
202         int i = 0;
203         if (pt == "Comment")
204                 os << "<comment>\n";
205
206         if (pt != "Note")
207                 i = inset.linuxdoc(buf, os, runparams);
208
209         if (pt == "Comment") {
210                 os << "\n</comment>\n";
211                 i += 3;
212         }
213         return i;
214 }
215
216
217 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
218                        LatexRunParams const & runparams) const
219 {
220         string const pt = params_.type;
221
222         int i = 0;
223         if (pt == "Comment")
224                 os << "<remark>\n";
225
226         if (pt != "Note")
227                 i = inset.docbook(buf, os, runparams);
228
229         if (pt == "Comment") {
230                 os << "\n</remark>\n";
231                 i += 3;
232         }
233         return i;
234 }
235
236
237 int InsetNote::ascii(Buffer const & buf, std::ostream & os,
238                      LatexRunParams const & runparams) const
239 {
240         int i = 0;
241         string const pt = params_.type;
242         if (pt != "Note") {
243                 os << "[";
244                 i = inset.ascii(buf, os, runparams);
245                 os << "]";
246         }
247         return i;
248 }
249
250
251 void InsetNote::validate(LaTeXFeatures & features) const
252 {
253         if (params_.type == "Comment")
254                 features.require("verbatim");
255         if (params_.type == "Greyedout") {
256                 features.require("color");
257                 features.require("lyxgreyedout");
258         }
259         inset.validate(features);
260 }
261
262
263
264 InsetNoteMailer::InsetNoteMailer(string const & name,
265                                                 InsetNote & inset)
266         : name_(name), inset_(inset)
267 {
268 }
269
270
271 string const InsetNoteMailer::inset2string(Buffer const &) const
272 {
273         return params2string(name_, inset_.params());
274 }
275
276
277 string const InsetNoteMailer::params2string(string const & name,
278                                 InsetNoteParams const & params)
279 {
280         ostringstream data;
281         data << name << ' ';
282         params.write(data);
283         return data.str();
284 }
285
286
287 void InsetNoteMailer::string2params(string const & in,
288                                      InsetNoteParams & params)
289 {
290         params = InsetNoteParams();
291
292         if (in.empty())
293                 return;
294
295         istringstream data(in);
296         LyXLex lex(0,0);
297         lex.setStream(data);
298         params.read(lex);
299 }
300
301
302 void InsetNoteParams::write(ostream & os) const
303 {
304         os << type << "\n";
305 }
306
307
308 void InsetNoteParams::read(LyXLex & lex)
309 {
310         if (lex.isOK()) {
311                 lex.next();
312                 string token = lex.getString();
313         }
314
315         if (lex.isOK()) {
316                 lex.next();
317                 type = lex.getString();
318         }
319 }