]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
convert lfun arguments to docstring
[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 "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "exporter.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
31
32 #include "support/lyxalgo.h"
33 #include "support/translator.h"
34
35 #include <sstream>
36
37 using std::string;
38 using std::auto_ptr;
39 using std::istringstream;
40 using std::ostream;
41 using std::ostringstream;
42
43
44 namespace {
45
46 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
47
48 NoteTranslator const init_notetranslator() {
49         NoteTranslator translator("Note", InsetNoteParams::Note);
50         translator.addPair("Comment", InsetNoteParams::Comment);
51         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
52         translator.addPair("Framed", InsetNoteParams::Framed);
53         translator.addPair("Shaded", InsetNoteParams::Shaded);
54         return translator;
55 }
56
57
58 NoteTranslator const init_notetranslator_loc() {
59         NoteTranslator translator(_("Note"), InsetNoteParams::Note);
60         translator.addPair(_("Comment"), InsetNoteParams::Comment);
61         translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
62         translator.addPair(_("Framed"), InsetNoteParams::Framed);
63         translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
64         return translator;
65 }
66
67
68 NoteTranslator const & notetranslator() {
69         static NoteTranslator translator = init_notetranslator();
70         return translator;
71 }
72
73
74 NoteTranslator const & notetranslator_loc() {
75         static NoteTranslator translator = init_notetranslator_loc();
76         return translator;
77 }
78
79 } // anon
80
81
82
83
84 InsetNoteParams::InsetNoteParams()
85         : type(Note)
86 {}
87
88
89 void InsetNoteParams::write(ostream & os) const
90 {
91         string const label = notetranslator().find(type);
92         os << "Note " << label << "\n";
93 }
94
95
96 void InsetNoteParams::read(LyXLex & lex)
97 {
98         string label;
99         lex >> label;
100         if (lex)
101                 type = notetranslator().find(label);
102 }
103
104
105 void InsetNote::init()
106 {
107         setInsetName("Note");
108         setButtonLabel();
109 }
110
111
112 InsetNote::InsetNote(BufferParams const & bp, string const & label)
113         : InsetCollapsable(bp)
114 {
115         params_.type = notetranslator().find(label);
116         init();
117 }
118
119
120 InsetNote::InsetNote(InsetNote const & in)
121         : InsetCollapsable(in), params_(in.params_)
122 {
123         init();
124 }
125
126
127 InsetNote::~InsetNote()
128 {
129         InsetNoteMailer(*this).hideDialog();
130 }
131
132
133 auto_ptr<InsetBase> InsetNote::doClone() const
134 {
135         return auto_ptr<InsetBase>(new InsetNote(*this));
136 }
137
138
139 string const InsetNote::editMessage() const
140 {
141         return _("Opened Note Inset");
142 }
143
144
145 void InsetNote::write(Buffer const & buf, ostream & os) const
146 {
147         params_.write(os);
148         InsetCollapsable::write(buf, os);
149 }
150
151
152 void InsetNote::read(Buffer const & buf, LyXLex & lex)
153 {
154         params_.read(lex);
155         InsetCollapsable::read(buf, lex);
156         setButtonLabel();
157 }
158
159
160 void InsetNote::setButtonLabel()
161 {
162         string const label = notetranslator_loc().find(params_.type);
163         setLabel(label);
164
165         LyXFont font(LyXFont::ALL_SANE);
166         font.decSize();
167         font.decSize();
168
169         switch (params_.type) {
170         case InsetNoteParams::Note:
171                 font.setColor(LColor::note);
172                 setBackgroundColor(LColor::notebg);
173                 break;
174         case InsetNoteParams::Comment:
175                 font.setColor(LColor::comment);
176                 setBackgroundColor(LColor::commentbg);
177                 break;
178         case InsetNoteParams::Greyedout:
179                 font.setColor(LColor::greyedout);
180                 setBackgroundColor(LColor::greyedoutbg);
181                 break;
182         case InsetNoteParams::Framed:
183                 font.setColor(LColor::greyedout);
184                 setBackgroundColor(LColor::greyedoutbg);
185                 break;
186         case InsetNoteParams::Shaded:
187                 font.setColor(LColor::greyedout);
188                 setBackgroundColor(LColor::shadedbg);
189                 break;
190         }
191         setLabelFont(font);
192 }
193
194
195 bool InsetNote::showInsetDialog(BufferView * bv) const
196 {
197         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
198         return true;
199 }
200
201
202 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
203 {
204         switch (cmd.action) {
205
206         case LFUN_INSET_MODIFY:
207                 InsetNoteMailer::string2params(lyx::to_utf8(cmd.argument()), params_);
208                 setButtonLabel();
209                 break;
210
211         case LFUN_INSET_DIALOG_UPDATE:
212                 InsetNoteMailer(*this).updateDialog(&cur.bv());
213                 break;
214
215         case LFUN_MOUSE_RELEASE:
216                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
217                         InsetNoteMailer(*this).showDialog(&cur.bv());
218                 else
219                         InsetCollapsable::doDispatch(cur, cmd);
220                 break;
221
222         default:
223                 InsetCollapsable::doDispatch(cur, cmd);
224                 break;
225         }
226 }
227
228
229 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
230                 FuncStatus & flag) const
231 {
232         switch (cmd.action) {
233
234         case LFUN_INSET_MODIFY:
235         case LFUN_INSET_DIALOG_UPDATE:
236                 flag.enabled(true);
237                 return true;
238
239         default:
240                 return InsetCollapsable::getStatus(cur, cmd, flag);
241         }
242 }
243
244
245 int InsetNote::latex(Buffer const & buf, ostream & os,
246                      OutputParams const & runparams_in) const
247 {
248         if (params_.type == InsetNoteParams::Note)
249                 return 0;
250
251         OutputParams runparams(runparams_in);
252         string type;
253         if (params_.type == InsetNoteParams::Comment) {
254                 type = "comment";
255                 runparams.inComment = true;
256                 // Ignore files that are exported inside a comment
257                 runparams.exportdata.reset(new ExportData);
258         } else if (params_.type == InsetNoteParams::Greyedout)
259                 type = "lyxgreyedout";
260         else if (params_.type == InsetNoteParams::Framed)
261                 type = "framed";
262         else if (params_.type == InsetNoteParams::Shaded)
263                 type = "shaded";
264
265         ostringstream ss;
266         ss << "%\n\\begin{" << type << "}\n";
267         InsetText::latex(buf, ss, runparams);
268         ss << "\n\\end{" << type << "}\n";
269         // the space after the comment in 'a[comment] b' will be eaten by the
270         // comment environment since the space before b is ignored with the
271         // following latex output:
272         //
273         // a%
274         // \begin{comment}
275         // comment
276         // \end{comment}
277         //  b
278         //
279         // Adding {} before ' b' fixes this.
280         if (params_.type == InsetNoteParams::Comment)
281                 ss << "{}";
282
283         string const str = ss.str();
284         os << str;
285         // Return how many newlines we issued.
286         return int(lyx::count(str.begin(), str.end(),'\n'));
287 }
288
289
290 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
291                        OutputParams const & runparams_in) const
292 {
293         if (params_.type == InsetNoteParams::Note)
294                 return 0;
295
296         OutputParams runparams(runparams_in);
297         ostringstream ss;
298         if (params_.type == InsetNoteParams::Comment) {
299                 ss << "<remark>\n";
300                 runparams.inComment = true;
301                 // Ignore files that are exported inside a comment
302                 runparams.exportdata.reset(new ExportData);
303         }
304
305         InsetText::docbook(buf, ss, runparams);
306
307         if (params_.type == InsetNoteParams::Comment)
308                 ss << "\n</remark>\n";
309
310         string const str = ss.str();
311         os << str;
312         // Return how many newlines we issued.
313         return int(lyx::count(str.begin(), str.end(),'\n'));
314 }
315
316
317 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
318                          OutputParams const & runparams_in) const
319 {
320         if (params_.type == InsetNoteParams::Note)
321                 return 0;
322
323         OutputParams runparams(runparams_in);
324         if (params_.type == InsetNoteParams::Comment) {
325                 runparams.inComment = true;
326                 // Ignore files that are exported inside a comment
327                 runparams.exportdata.reset(new ExportData);
328         }
329         ostringstream ss;
330         ss << "[";
331         InsetText::plaintext(buf, ss, runparams);
332         ss << "]";
333
334         string const str = ss.str();
335         os << str;
336         // Return how many newlines we issued.
337         return int(lyx::count(str.begin(), str.end(),'\n'));
338 }
339
340
341 void InsetNote::validate(LaTeXFeatures & features) const
342 {
343         if (params_.type == InsetNoteParams::Comment)
344                 features.require("verbatim");
345         if (params_.type == InsetNoteParams::Greyedout) {
346                 features.require("color");
347                 features.require("lyxgreyedout");
348         }
349         if (params_.type == InsetNoteParams::Shaded) {
350                 features.require("color");
351                 features.require("framed");
352         }               
353         if (params_.type == InsetNoteParams::Framed)
354                 features.require("framed");
355         InsetText::validate(features);
356 }
357
358
359
360 string const InsetNoteMailer::name_("note");
361
362 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
363         : inset_(inset)
364 {}
365
366
367 string const InsetNoteMailer::inset2string(Buffer const &) const
368 {
369         return params2string(inset_.params());
370 }
371
372
373 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
374 {
375         ostringstream data;
376         data << name_ << ' ';
377         params.write(data);
378         return data.str();
379 }
380
381
382 void InsetNoteMailer::string2params(string const & in,
383                                     InsetNoteParams & params)
384 {
385         params = InsetNoteParams();
386
387         if (in.empty())
388                 return;
389
390         istringstream data(in);
391         LyXLex lex(0,0);
392         lex.setStream(data);
393
394         string name;
395         lex >> name;
396         if (!lex || name != name_)
397                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
398
399         // This is part of the inset proper that is usually swallowed
400         // by LyXText::readInset
401         string id;
402         lex >> id;
403         if (!lex || id != "Note")
404                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
405
406         params.read(lex);
407 }