3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "InsetNote.h"
18 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
33 #include "support/lyxalgo.h"
34 #include "support/Translator.h"
43 using std::istringstream;
45 using std::ostringstream;
50 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
51 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
53 NoteTranslator const init_notetranslator()
55 NoteTranslator translator("Note", InsetNoteParams::Note);
56 translator.addPair("Comment", InsetNoteParams::Comment);
57 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
58 translator.addPair("Framed", InsetNoteParams::Framed);
59 translator.addPair("Shaded", InsetNoteParams::Shaded);
64 NoteTranslatorLoc const init_notetranslator_loc()
66 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
67 translator.addPair(_("Comment"), InsetNoteParams::Comment);
68 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
69 translator.addPair(_("Framed"), InsetNoteParams::Framed);
70 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
75 NoteTranslator const & notetranslator()
77 static NoteTranslator translator = init_notetranslator();
82 NoteTranslatorLoc const & notetranslator_loc()
84 static NoteTranslatorLoc translator = init_notetranslator_loc();
93 InsetNoteParams::InsetNoteParams()
98 void InsetNoteParams::write(ostream & os) const
100 string const label = notetranslator().find(type);
101 os << "Note " << label << "\n";
105 void InsetNoteParams::read(Lexer & lex)
110 type = notetranslator().find(label);
114 void InsetNote::init()
120 InsetNote::InsetNote(BufferParams const & bp, string const & label)
121 : InsetCollapsable(bp)
123 params_.type = notetranslator().find(label);
128 InsetNote::InsetNote(InsetNote const & in)
129 : InsetCollapsable(in), params_(in.params_)
135 InsetNote::~InsetNote()
137 InsetNoteMailer(*this).hideDialog();
141 auto_ptr<Inset> InsetNote::doClone() const
143 return auto_ptr<Inset>(new InsetNote(*this));
147 docstring const InsetNote::editMessage() const
149 return _("Opened Note Inset");
153 Inset::DisplayType InsetNote::display() const
155 switch (params_.type) {
156 case InsetNoteParams::Framed:
157 case InsetNoteParams::Shaded:
165 void InsetNote::write(Buffer const & buf, ostream & os) const
168 InsetCollapsable::write(buf, os);
172 void InsetNote::read(Buffer const & buf, Lexer & lex)
175 InsetCollapsable::read(buf, lex);
180 void InsetNote::setButtonLabel()
182 docstring const label = notetranslator_loc().find(params_.type);
185 Font font(Font::ALL_SANE);
189 switch (params_.type) {
190 case InsetNoteParams::Note:
191 font.setColor(Color::note);
192 setBackgroundColor(Color::notebg);
194 case InsetNoteParams::Comment:
195 font.setColor(Color::comment);
196 setBackgroundColor(Color::commentbg);
198 case InsetNoteParams::Greyedout:
199 font.setColor(Color::greyedout);
200 setBackgroundColor(Color::greyedoutbg);
202 case InsetNoteParams::Framed:
203 font.setColor(Color::greyedout);
204 setBackgroundColor(Color::greyedoutbg);
206 case InsetNoteParams::Shaded:
207 font.setColor(Color::greyedout);
208 setBackgroundColor(Color::shadedbg);
215 bool InsetNote::showInsetDialog(BufferView * bv) const
217 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
222 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
224 switch (cmd.action) {
226 case LFUN_INSET_MODIFY:
227 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
231 case LFUN_INSET_DIALOG_UPDATE:
232 InsetNoteMailer(*this).updateDialog(&cur.bv());
235 case LFUN_MOUSE_RELEASE:
236 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
237 InsetNoteMailer(*this).showDialog(&cur.bv());
239 InsetCollapsable::doDispatch(cur, cmd);
243 InsetCollapsable::doDispatch(cur, cmd);
249 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
250 FuncStatus & flag) const
252 switch (cmd.action) {
254 case LFUN_INSET_MODIFY:
255 case LFUN_INSET_DIALOG_UPDATE:
260 return InsetCollapsable::getStatus(cur, cmd, flag);
265 int InsetNote::latex(Buffer const & buf, odocstream & os,
266 OutputParams const & runparams_in) const
268 if (params_.type == InsetNoteParams::Note)
271 OutputParams runparams(runparams_in);
273 if (params_.type == InsetNoteParams::Comment) {
275 runparams.inComment = true;
276 // Ignore files that are exported inside a comment
277 runparams.exportdata.reset(new ExportData);
278 } else if (params_.type == InsetNoteParams::Greyedout)
279 type = "lyxgreyedout";
280 else if (params_.type == InsetNoteParams::Framed)
282 else if (params_.type == InsetNoteParams::Shaded)
286 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
287 InsetText::latex(buf, ss, runparams);
288 ss << "\n\\end{" << from_ascii(type) << "}\n";
289 // the space after the comment in 'a[comment] b' will be eaten by the
290 // comment environment since the space before b is ignored with the
291 // following latex output:
299 // Adding {} before ' b' fixes this.
300 if (params_.type == InsetNoteParams::Comment)
303 docstring const str = ss.str();
305 runparams_in.encoding = runparams.encoding;
306 // Return how many newlines we issued.
307 return int(lyx::count(str.begin(), str.end(), '\n'));
311 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
312 OutputParams const & runparams_in) const
314 if (params_.type == InsetNoteParams::Note)
317 OutputParams runparams(runparams_in);
318 if (params_.type == InsetNoteParams::Comment) {
319 runparams.inComment = true;
320 // Ignore files that are exported inside a comment
321 runparams.exportdata.reset(new ExportData);
323 os << '[' << buf.B_("note") << ":\n";
324 InsetText::plaintext(buf, os, runparams);
327 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
331 int InsetNote::docbook(Buffer const & buf, odocstream & os,
332 OutputParams const & runparams_in) const
334 if (params_.type == InsetNoteParams::Note)
337 OutputParams runparams(runparams_in);
338 if (params_.type == InsetNoteParams::Comment) {
340 runparams.inComment = true;
341 // Ignore files that are exported inside a comment
342 runparams.exportdata.reset(new ExportData);
345 int const n = InsetText::docbook(buf, os, runparams);
347 if (params_.type == InsetNoteParams::Comment)
348 os << "\n</remark>\n";
350 // Return how many newlines we issued.
351 //return int(count(str.begin(), str.end(), '\n'));
356 void InsetNote::validate(LaTeXFeatures & features) const
358 if (params_.type == InsetNoteParams::Comment)
359 features.require("verbatim");
360 if (params_.type == InsetNoteParams::Greyedout) {
361 features.require("color");
362 features.require("lyxgreyedout");
364 if (params_.type == InsetNoteParams::Shaded) {
365 features.require("color");
366 features.require("framed");
368 if (params_.type == InsetNoteParams::Framed)
369 features.require("framed");
370 InsetText::validate(features);
375 string const InsetNoteMailer::name_("note");
377 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
382 string const InsetNoteMailer::inset2string(Buffer const &) const
384 return params2string(inset_.params());
388 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
391 data << name_ << ' ';
397 void InsetNoteMailer::string2params(string const & in,
398 InsetNoteParams & params)
400 params = InsetNoteParams();
405 istringstream data(in);
411 if (!lex || name != name_)
412 return print_mailer_error("InsetNoteMailer", in, 1, name_);
414 // This is part of the inset proper that is usually swallowed
415 // by Text::readInset
418 if (!lex || id != "Note")
419 return print_mailer_error("InsetBoxMailer", in, 2, "Note");