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 "BufferParams.h"
19 #include "BufferView.h"
21 #include "BufferParams.h"
27 #include "DispatchResult.h"
29 #include "FuncRequest.h"
30 #include "FuncStatus.h"
32 #include "LaTeXFeatures.h"
34 #include "MetricsInfo.h"
35 #include "OutputParams.h"
37 #include "support/Translator.h"
47 using std::istringstream;
49 using std::ostringstream;
54 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
55 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
57 NoteTranslator const init_notetranslator()
59 NoteTranslator translator("Note", InsetNoteParams::Note);
60 translator.addPair("Comment", InsetNoteParams::Comment);
61 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
62 translator.addPair("Framed", InsetNoteParams::Framed);
63 translator.addPair("Shaded", InsetNoteParams::Shaded);
68 NoteTranslatorLoc const init_notetranslator_loc()
70 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
71 translator.addPair(_("Comment"), InsetNoteParams::Comment);
72 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
73 translator.addPair(_("Framed"), InsetNoteParams::Framed);
74 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
79 NoteTranslator const & notetranslator()
81 static NoteTranslator translator = init_notetranslator();
86 NoteTranslatorLoc const & notetranslator_loc()
88 static NoteTranslatorLoc translator = init_notetranslator_loc();
97 InsetNoteParams::InsetNoteParams()
102 void InsetNoteParams::write(ostream & os) const
104 string const label = notetranslator().find(type);
105 os << "Note " << label << "\n";
109 void InsetNoteParams::read(Lexer & lex)
114 type = notetranslator().find(label);
118 InsetNote::InsetNote(BufferParams const & bp, string const & label)
119 : InsetCollapsable(bp)
121 params_.type = notetranslator().find(label);
127 InsetNote::InsetNote(InsetNote const & in)
128 : InsetCollapsable(in), params_(in.params_)
134 InsetNote::~InsetNote()
136 InsetNoteMailer(*this).hideDialog();
140 auto_ptr<Inset> InsetNote::doClone() const
142 return auto_ptr<Inset>(new InsetNote(*this));
146 docstring const InsetNote::editMessage() const
148 return _("Opened Note Inset");
152 docstring InsetNote::name() const
154 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
158 Inset::DisplayType InsetNote::display() const
160 switch (params_.type) {
161 case InsetNoteParams::Framed:
162 case InsetNoteParams::Shaded:
170 void InsetNote::write(Buffer const & buf, ostream & os) const
173 InsetCollapsable::write(buf, os);
177 void InsetNote::read(Buffer const & buf, Lexer & lex)
180 InsetCollapsable::read(buf, lex);
181 setLayout(buf.params());
186 void InsetNote::setButtonLabel()
188 docstring const label = notetranslator_loc().find(params_.type);
190 setLabelFont(layout_.labelfont);
194 Color_color InsetNote::backgroundColor() const
197 switch (params_.type) {
198 case InsetNoteParams::Note:
201 case InsetNoteParams::Comment:
202 c = Color::commentbg;
204 case InsetNoteParams::Greyedout:
205 c = Color::greyedoutbg;
207 case InsetNoteParams::Framed:
208 c = Color::greyedoutbg;
210 case InsetNoteParams::Shaded:
218 bool InsetNote::showInsetDialog(BufferView * bv) const
220 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
225 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
227 switch (cmd.action) {
229 case LFUN_INSET_MODIFY:
230 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
231 // get a bp from cur:
232 setLayout(cur.buffer().params());
236 case LFUN_INSET_DIALOG_UPDATE:
237 InsetNoteMailer(*this).updateDialog(&cur.bv());
240 case LFUN_MOUSE_RELEASE:
241 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
242 InsetNoteMailer(*this).showDialog(&cur.bv());
244 InsetCollapsable::doDispatch(cur, cmd);
248 InsetCollapsable::doDispatch(cur, cmd);
254 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
255 FuncStatus & flag) const
257 switch (cmd.action) {
259 case LFUN_INSET_MODIFY:
260 case LFUN_INSET_DIALOG_UPDATE:
265 return InsetCollapsable::getStatus(cur, cmd, flag);
269 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
271 TextClass const & tclass = buf.params().getTextClass();
272 Counters savecnt = tclass.counters();
273 InsetCollapsable::updateLabels(buf, it);
274 tclass.counters() = savecnt;
278 int InsetNote::latex(Buffer const & buf, odocstream & os,
279 OutputParams const & runparams_in) const
281 if (params_.type == InsetNoteParams::Note)
284 OutputParams runparams(runparams_in);
286 if (params_.type == InsetNoteParams::Comment) {
288 runparams.inComment = true;
289 // Ignore files that are exported inside a comment
290 runparams.exportdata.reset(new ExportData);
291 } else if (params_.type == InsetNoteParams::Greyedout)
292 type = "lyxgreyedout";
293 else if (params_.type == InsetNoteParams::Framed)
295 else if (params_.type == InsetNoteParams::Shaded)
299 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
300 InsetText::latex(buf, ss, runparams);
301 ss << "\n\\end{" << from_ascii(type) << "}\n";
302 // the space after the comment in 'a[comment] b' will be eaten by the
303 // comment environment since the space before b is ignored with the
304 // following latex output:
312 // Adding {} before ' b' fixes this.
313 if (params_.type == InsetNoteParams::Comment)
316 docstring const str = ss.str();
318 runparams_in.encoding = runparams.encoding;
319 // Return how many newlines we issued.
320 return int(std::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);
336 os << '[' << buf.B_("note") << ":\n";
337 InsetText::plaintext(buf, os, runparams);
340 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
344 int InsetNote::docbook(Buffer const & buf, odocstream & os,
345 OutputParams const & runparams_in) const
347 if (params_.type == InsetNoteParams::Note)
350 OutputParams runparams(runparams_in);
351 if (params_.type == InsetNoteParams::Comment) {
353 runparams.inComment = true;
354 // Ignore files that are exported inside a comment
355 runparams.exportdata.reset(new ExportData);
358 int const n = InsetText::docbook(buf, os, runparams);
360 if (params_.type == InsetNoteParams::Comment)
361 os << "\n</remark>\n";
363 // Return how many newlines we issued.
364 //return int(count(str.begin(), str.end(), '\n'));
369 void InsetNote::validate(LaTeXFeatures & features) const
371 if (params_.type == InsetNoteParams::Comment)
372 features.require("verbatim");
373 if (params_.type == InsetNoteParams::Greyedout) {
374 features.require("color");
375 features.require("lyxgreyedout");
377 if (params_.type == InsetNoteParams::Shaded) {
378 features.require("color");
379 features.require("framed");
381 if (params_.type == InsetNoteParams::Framed)
382 features.require("framed");
383 InsetText::validate(features);
388 string const InsetNoteMailer::name_("note");
390 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
395 string const InsetNoteMailer::inset2string(Buffer const &) const
397 return params2string(inset_.params());
401 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
404 data << name_ << ' ';
410 void InsetNoteMailer::string2params(string const & in,
411 InsetNoteParams & params)
413 params = InsetNoteParams();
418 istringstream data(in);
424 if (!lex || name != name_)
425 return print_mailer_error("InsetNoteMailer", in, 1, name_);
427 // This is part of the inset proper that is usually swallowed
428 // by Text::readInset
431 if (!lex || id != "Note")
432 return print_mailer_error("InsetBoxMailer", in, 2, "Note");