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;
50 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
52 NoteTranslator const init_notetranslator()
54 NoteTranslator translator("Note", InsetNoteParams::Note);
55 translator.addPair("Comment", InsetNoteParams::Comment);
56 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
57 translator.addPair("Framed", InsetNoteParams::Framed);
58 translator.addPair("Shaded", InsetNoteParams::Shaded);
63 NoteTranslatorLoc const init_notetranslator_loc()
65 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
66 translator.addPair(_("Comment"), InsetNoteParams::Comment);
67 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
68 translator.addPair(_("Framed"), InsetNoteParams::Framed);
69 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
74 NoteTranslator const & notetranslator()
76 static NoteTranslator translator = init_notetranslator();
81 NoteTranslatorLoc const & notetranslator_loc()
83 static NoteTranslatorLoc 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(Lexer & lex)
109 type = notetranslator().find(label);
113 void InsetNote::init()
119 InsetNote::InsetNote(BufferParams const & bp, string const & label)
120 : InsetCollapsable(bp)
122 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 void InsetNote::write(Buffer const & buf, ostream & os) const
155 InsetCollapsable::write(buf, os);
159 void InsetNote::read(Buffer const & buf, Lexer & lex)
162 InsetCollapsable::read(buf, lex);
167 void InsetNote::setButtonLabel()
169 docstring const label = notetranslator_loc().find(params_.type);
172 Font font(Font::ALL_SANE);
176 switch (params_.type) {
177 case InsetNoteParams::Note:
178 font.setColor(Color::note);
179 setBackgroundColor(Color::notebg);
181 case InsetNoteParams::Comment:
182 font.setColor(Color::comment);
183 setBackgroundColor(Color::commentbg);
185 case InsetNoteParams::Greyedout:
186 font.setColor(Color::greyedout);
187 setBackgroundColor(Color::greyedoutbg);
189 case InsetNoteParams::Framed:
190 font.setColor(Color::greyedout);
191 setBackgroundColor(Color::greyedoutbg);
193 case InsetNoteParams::Shaded:
194 font.setColor(Color::greyedout);
195 setBackgroundColor(Color::shadedbg);
202 bool InsetNote::showInsetDialog(BufferView * bv) const
204 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
209 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
211 switch (cmd.action) {
213 case LFUN_INSET_MODIFY:
214 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
218 case LFUN_INSET_DIALOG_UPDATE:
219 InsetNoteMailer(*this).updateDialog(&cur.bv());
222 case LFUN_MOUSE_RELEASE:
223 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
224 InsetNoteMailer(*this).showDialog(&cur.bv());
226 InsetCollapsable::doDispatch(cur, cmd);
230 InsetCollapsable::doDispatch(cur, cmd);
236 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
237 FuncStatus & flag) const
239 switch (cmd.action) {
241 case LFUN_INSET_MODIFY:
242 case LFUN_INSET_DIALOG_UPDATE:
247 return InsetCollapsable::getStatus(cur, cmd, flag);
252 int InsetNote::latex(Buffer const & buf, odocstream & os,
253 OutputParams const & runparams_in) const
255 if (params_.type == InsetNoteParams::Note)
258 OutputParams runparams(runparams_in);
260 if (params_.type == InsetNoteParams::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)
269 else if (params_.type == InsetNoteParams::Shaded)
273 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
274 InsetText::latex(buf, ss, runparams);
275 ss << "\n\\end{" << from_ascii(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:
286 // Adding {} before ' b' fixes this.
287 if (params_.type == InsetNoteParams::Comment)
290 docstring const str = ss.str();
292 runparams_in.encoding = runparams.encoding;
293 // Return how many newlines we issued.
294 return int(lyx::count(str.begin(), str.end(), '\n'));
298 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
299 OutputParams const & runparams_in) const
301 if (params_.type == InsetNoteParams::Note)
304 OutputParams runparams(runparams_in);
305 if (params_.type == InsetNoteParams::Comment) {
306 runparams.inComment = true;
307 // Ignore files that are exported inside a comment
308 runparams.exportdata.reset(new ExportData);
310 os << '[' << _("note") << ":\n";
311 InsetText::plaintext(buf, os, runparams);
314 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
318 int InsetNote::docbook(Buffer const & buf, odocstream & os,
319 OutputParams const & runparams_in) const
321 if (params_.type == InsetNoteParams::Note)
324 OutputParams runparams(runparams_in);
325 if (params_.type == InsetNoteParams::Comment) {
327 runparams.inComment = true;
328 // Ignore files that are exported inside a comment
329 runparams.exportdata.reset(new ExportData);
332 int const n = InsetText::docbook(buf, os, runparams);
334 if (params_.type == InsetNoteParams::Comment)
335 os << "\n</remark>\n";
337 // Return how many newlines we issued.
338 //return int(count(str.begin(), str.end(), '\n'));
343 void InsetNote::validate(LaTeXFeatures & features) const
345 if (params_.type == InsetNoteParams::Comment)
346 features.require("verbatim");
347 if (params_.type == InsetNoteParams::Greyedout) {
348 features.require("color");
349 features.require("lyxgreyedout");
351 if (params_.type == InsetNoteParams::Shaded) {
352 features.require("color");
353 features.require("framed");
355 if (params_.type == InsetNoteParams::Framed)
356 features.require("framed");
357 InsetText::validate(features);
362 string const InsetNoteMailer::name_("note");
364 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
369 string const InsetNoteMailer::inset2string(Buffer const &) const
371 return params2string(inset_.params());
375 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
378 data << name_ << ' ';
384 void InsetNoteMailer::string2params(string const & in,
385 InsetNoteParams & params)
387 params = InsetNoteParams();
392 istringstream data(in);
398 if (!lex || name != name_)
399 return print_mailer_error("InsetNoteMailer", in, 1, name_);
401 // This is part of the inset proper that is usually swallowed
402 // by LyXText::readInset
405 if (!lex || id != "Note")
406 return print_mailer_error("InsetBoxMailer", in, 2, "Note");