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"
17 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
23 #include "LaTeXFeatures.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #include "support/lyxalgo.h"
30 #include "support/translator.h"
36 using std::istringstream;
38 using std::ostringstream;
43 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
45 NoteTranslator const init_notetranslator() {
46 NoteTranslator translator("Note", InsetNoteParams::Note);
47 translator.addPair("Comment", InsetNoteParams::Comment);
48 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
53 NoteTranslator const init_notetranslator_loc() {
54 NoteTranslator translator(_("Note"), InsetNoteParams::Note);
55 translator.addPair(_("Comment"), InsetNoteParams::Comment);
56 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
61 NoteTranslator const & notetranslator() {
62 static NoteTranslator translator = init_notetranslator();
67 NoteTranslator const & notetranslator_loc() {
68 static NoteTranslator translator = init_notetranslator_loc();
77 InsetNoteParams::InsetNoteParams()
82 void InsetNoteParams::write(ostream & os) const
84 string const label = notetranslator().find(type);
85 os << "Note " << label << "\n";
89 void InsetNoteParams::read(LyXLex & lex)
94 type = notetranslator().find(label);
98 void InsetNote::init()
100 setInsetName("Note");
105 InsetNote::InsetNote(BufferParams const & bp, string const & label)
106 : InsetCollapsable(bp)
108 params_.type = notetranslator().find(label);
113 InsetNote::InsetNote(InsetNote const & in)
114 : InsetCollapsable(in), params_(in.params_)
120 InsetNote::~InsetNote()
122 InsetNoteMailer(*this).hideDialog();
126 auto_ptr<InsetBase> InsetNote::clone() const
128 return auto_ptr<InsetBase>(new InsetNote(*this));
132 string const InsetNote::editMessage() const
134 return _("Opened Note Inset");
138 void InsetNote::write(Buffer const & buf, ostream & os) const
141 InsetCollapsable::write(buf, os);
145 void InsetNote::read(Buffer const & buf, LyXLex & lex)
148 InsetCollapsable::read(buf, lex);
153 void InsetNote::setButtonLabel()
155 string const label = notetranslator_loc().find(params_.type);
158 LyXFont font(LyXFont::ALL_SANE);
162 switch (params_.type) {
163 case InsetNoteParams::Note:
164 font.setColor(LColor::note);
165 setBackgroundColor(LColor::notebg);
167 case InsetNoteParams::Comment:
168 font.setColor(LColor::comment);
169 setBackgroundColor(LColor::commentbg);
171 case InsetNoteParams::Greyedout:
172 font.setColor(LColor::greyedout);
173 setBackgroundColor(LColor::greyedoutbg);
180 bool InsetNote::showInsetDialog(BufferView * bv) const
182 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
187 void InsetNote::priv_dispatch(LCursor & cur, FuncRequest & cmd)
189 switch (cmd.action) {
191 case LFUN_INSET_MODIFY:
192 InsetNoteMailer::string2params(cmd.argument, params_);
197 case LFUN_INSET_DIALOG_UPDATE:
198 InsetNoteMailer(*this).updateDialog(&cur.bv());
201 case LFUN_MOUSE_RELEASE:
202 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
203 InsetNoteMailer(*this).showDialog(&cur.bv());
205 InsetCollapsable::priv_dispatch(cur, cmd);
209 InsetCollapsable::priv_dispatch(cur, cmd);
215 int InsetNote::latex(Buffer const & buf, ostream & os,
216 OutputParams const & runparams) const
218 if (params_.type == InsetNoteParams::Note)
222 if (params_.type == InsetNoteParams::Comment)
224 else if (params_.type == InsetNoteParams::Greyedout)
225 type = "lyxgreyedout";
228 ss << "%\n\\begin{" << type << "}\n";
229 InsetText::latex(buf, ss, runparams);
230 ss << "%\n\\end{" << type << "}\n";
232 string const str = ss.str();
234 // Return how many newlines we issued.
235 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
239 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
240 OutputParams const & runparams) const
242 if (params_.type == InsetNoteParams::Note)
246 if (params_.type == InsetNoteParams::Comment)
249 InsetText::linuxdoc(buf, ss, runparams);
251 if (params_.type == InsetNoteParams::Comment)
252 ss << "\n</comment>\n";
254 string const str = ss.str();
256 // Return how many newlines we issued.
257 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
261 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
262 OutputParams const & runparams) const
264 if (params_.type == InsetNoteParams::Note)
268 if (params_.type == InsetNoteParams::Comment)
271 InsetText::docbook(buf, ss, runparams);
273 if (params_.type == InsetNoteParams::Comment)
274 ss << "\n</remark>\n";
276 string const str = ss.str();
278 // Return how many newlines we issued.
279 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
283 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
284 OutputParams const & runparams) const
286 if (params_.type == InsetNoteParams::Note)
291 InsetText::plaintext(buf, ss, runparams);
294 string const str = ss.str();
296 // Return how many newlines we issued.
297 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
301 void InsetNote::validate(LaTeXFeatures & features) const
303 if (params_.type == InsetNoteParams::Comment)
304 features.require("verbatim");
305 if (params_.type == InsetNoteParams::Greyedout) {
306 features.require("color");
307 features.require("lyxgreyedout");
309 InsetText::validate(features);
314 string const InsetNoteMailer::name_("note");
316 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
321 string const InsetNoteMailer::inset2string(Buffer const &) const
323 return params2string(inset_.params());
327 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
330 data << name_ << ' ';
336 void InsetNoteMailer::string2params(string const & in,
337 InsetNoteParams & params)
339 params = InsetNoteParams();
344 istringstream data(in);
350 if (!lex || name != name_)
351 return print_mailer_error("InsetNoteMailer", in, 1, name_);
353 // This is part of the inset proper that is usually swallowed
354 // by LyXText::readInset
357 if (!lex || id != "Note")
358 return print_mailer_error("InsetBoxMailer", in, 2, "Note");