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