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"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
25 #include "LaTeXFeatures.h"
28 #include "metricsinfo.h"
29 #include "outputparams.h"
30 #include "paragraph.h"
32 #include "support/lyxalgo.h"
33 #include "support/translator.h"
42 using std::istringstream;
44 using std::ostringstream;
49 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
51 NoteTranslator const init_notetranslator()
53 NoteTranslator translator("Note", InsetNoteParams::Note);
54 translator.addPair("Comment", InsetNoteParams::Comment);
55 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
56 translator.addPair("Framed", InsetNoteParams::Framed);
57 translator.addPair("Shaded", InsetNoteParams::Shaded);
62 NoteTranslator const init_notetranslator_loc()
65 NoteTranslator translator(to_utf8(_("Note")), InsetNoteParams::Note);
66 translator.addPair(to_utf8(_("Comment")), InsetNoteParams::Comment);
67 translator.addPair(to_utf8(_("Greyed out")), InsetNoteParams::Greyedout);
68 translator.addPair(to_utf8(_("Framed")), InsetNoteParams::Framed);
69 translator.addPair(to_utf8(_("Shaded")), InsetNoteParams::Shaded);
74 NoteTranslator const & notetranslator()
76 static NoteTranslator translator = init_notetranslator();
81 NoteTranslator const & notetranslator_loc()
83 static NoteTranslator translator = init_notetranslator_loc();
92 InsetNoteParams::InsetNoteParams()
97 void InsetNoteParams::write(ostream & os) const
99 string const label = notetranslator().find(type);
100 os << "Note " << label << "\n";
104 void InsetNoteParams::read(LyXLex & lex)
109 type = notetranslator().find(label);
113 void InsetNote::init()
115 setInsetName("Note");
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<InsetBase> InsetNote::doClone() const
143 return auto_ptr<InsetBase>(new InsetNote(*this));
147 docstring const InsetNote::editMessage() const
149 return _("Opened Note Inset");
153 void InsetNote::write(Buffer const & buf, ostream & os) const
156 InsetCollapsable::write(buf, os);
160 void InsetNote::read(Buffer const & buf, LyXLex & lex)
163 InsetCollapsable::read(buf, lex);
168 void InsetNote::setButtonLabel()
171 docstring const label = from_utf8(notetranslator_loc().find(params_.type));
174 LyXFont font(LyXFont::ALL_SANE);
178 switch (params_.type) {
179 case InsetNoteParams::Note:
180 font.setColor(LColor::note);
181 setBackgroundColor(LColor::notebg);
183 case InsetNoteParams::Comment:
184 font.setColor(LColor::comment);
185 setBackgroundColor(LColor::commentbg);
187 case InsetNoteParams::Greyedout:
188 font.setColor(LColor::greyedout);
189 setBackgroundColor(LColor::greyedoutbg);
191 case InsetNoteParams::Framed:
192 font.setColor(LColor::greyedout);
193 setBackgroundColor(LColor::greyedoutbg);
195 case InsetNoteParams::Shaded:
196 font.setColor(LColor::greyedout);
197 setBackgroundColor(LColor::shadedbg);
204 bool InsetNote::showInsetDialog(BufferView * bv) const
206 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
211 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
213 switch (cmd.action) {
215 case LFUN_INSET_MODIFY:
216 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
220 case LFUN_INSET_DIALOG_UPDATE:
221 InsetNoteMailer(*this).updateDialog(&cur.bv());
224 case LFUN_MOUSE_RELEASE:
225 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
226 InsetNoteMailer(*this).showDialog(&cur.bv());
228 InsetCollapsable::doDispatch(cur, cmd);
232 InsetCollapsable::doDispatch(cur, cmd);
238 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
239 FuncStatus & flag) const
241 switch (cmd.action) {
243 case LFUN_INSET_MODIFY:
244 case LFUN_INSET_DIALOG_UPDATE:
249 return InsetCollapsable::getStatus(cur, cmd, flag);
254 int InsetNote::latex(Buffer const & buf, odocstream & os,
255 OutputParams const & runparams_in) const
257 if (params_.type == InsetNoteParams::Note)
260 OutputParams runparams(runparams_in);
262 if (params_.type == InsetNoteParams::Comment) {
264 runparams.inComment = true;
265 // Ignore files that are exported inside a comment
266 runparams.exportdata.reset(new ExportData);
267 } else if (params_.type == InsetNoteParams::Greyedout)
268 type = "lyxgreyedout";
269 else if (params_.type == InsetNoteParams::Framed)
271 else if (params_.type == InsetNoteParams::Shaded)
275 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
276 InsetText::latex(buf, ss, runparams);
277 ss << "\n\\end{" << from_ascii(type) << "}\n";
278 // the space after the comment in 'a[comment] b' will be eaten by the
279 // comment environment since the space before b is ignored with the
280 // following latex output:
288 // Adding {} before ' b' fixes this.
289 if (params_.type == InsetNoteParams::Comment)
292 docstring const str = ss.str();
294 // Return how many newlines we issued.
295 return int(lyx::count(str.begin(), str.end(), '\n'));
299 int InsetNote::docbook(Buffer const & buf, odocstream & os,
300 OutputParams const & runparams_in) const
302 if (params_.type == InsetNoteParams::Note)
305 OutputParams runparams(runparams_in);
306 if (params_.type == InsetNoteParams::Comment) {
308 runparams.inComment = true;
309 // Ignore files that are exported inside a comment
310 runparams.exportdata.reset(new ExportData);
313 int const n = InsetText::docbook(buf, os, runparams);
315 if (params_.type == InsetNoteParams::Comment)
316 os << "\n</remark>\n";
318 // Return how many newlines we issued.
319 //return int(count(str.begin(), str.end(), '\n'));
324 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
325 OutputParams const & runparams_in) const
327 if (params_.type == InsetNoteParams::Note)
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);
337 int const nlines = InsetText::plaintext(buf, os, runparams);
340 // Return how many newlines we issued.
345 void InsetNote::validate(LaTeXFeatures & features) const
347 if (params_.type == InsetNoteParams::Comment)
348 features.require("verbatim");
349 if (params_.type == InsetNoteParams::Greyedout) {
350 features.require("color");
351 features.require("lyxgreyedout");
353 if (params_.type == InsetNoteParams::Shaded) {
354 features.require("color");
355 features.require("framed");
357 if (params_.type == InsetNoteParams::Framed)
358 features.require("framed");
359 InsetText::validate(features);
364 string const InsetNoteMailer::name_("note");
366 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
371 string const InsetNoteMailer::inset2string(Buffer const &) const
373 return params2string(inset_.params());
377 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
380 data << name_ << ' ';
386 void InsetNoteMailer::string2params(string const & in,
387 InsetNoteParams & params)
389 params = InsetNoteParams();
394 istringstream data(in);
400 if (!lex || name != name_)
401 return print_mailer_error("InsetNoteMailer", in, 1, name_);
403 // This is part of the inset proper that is usually swallowed
404 // by LyXText::readInset
407 if (!lex || id != "Note")
408 return print_mailer_error("InsetBoxMailer", in, 2, "Note");