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"
20 #include "BufferParams.h"
24 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
29 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
34 #include "support/Translator.h"
44 using std::istringstream;
46 using std::ostringstream;
51 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
52 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
54 NoteTranslator const init_notetranslator()
56 NoteTranslator translator("Note", InsetNoteParams::Note);
57 translator.addPair("Comment", InsetNoteParams::Comment);
58 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
59 translator.addPair("Framed", InsetNoteParams::Framed);
60 translator.addPair("Shaded", InsetNoteParams::Shaded);
65 NoteTranslatorLoc const init_notetranslator_loc()
67 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
68 translator.addPair(_("Comment"), InsetNoteParams::Comment);
69 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
70 translator.addPair(_("Framed"), InsetNoteParams::Framed);
71 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
76 NoteTranslator const & notetranslator()
78 static NoteTranslator translator = init_notetranslator();
83 NoteTranslatorLoc const & notetranslator_loc()
85 static NoteTranslatorLoc translator = init_notetranslator_loc();
94 InsetNoteParams::InsetNoteParams()
99 void InsetNoteParams::write(ostream & os) const
101 string const label = notetranslator().find(type);
102 os << "Note " << label << "\n";
106 void InsetNoteParams::read(Lexer & lex)
111 type = notetranslator().find(label);
115 InsetNote::InsetNote(BufferParams const & bp, string const & label)
116 : InsetCollapsable(bp)
118 params_.type = notetranslator().find(label);
124 InsetNote::InsetNote(InsetNote const & in)
125 : InsetCollapsable(in), params_(in.params_)
131 InsetNote::~InsetNote()
133 InsetNoteMailer(*this).hideDialog();
137 auto_ptr<Inset> InsetNote::doClone() const
139 return auto_ptr<Inset>(new InsetNote(*this));
143 docstring const InsetNote::editMessage() const
145 return _("Opened Note Inset");
149 docstring InsetNote::name() const
151 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
155 Inset::DisplayType InsetNote::display() const
157 switch (params_.type) {
158 case InsetNoteParams::Framed:
159 case InsetNoteParams::Shaded:
167 void InsetNote::write(Buffer const & buf, ostream & os) const
170 InsetCollapsable::write(buf, os);
174 void InsetNote::read(Buffer const & buf, Lexer & lex)
177 InsetCollapsable::read(buf, lex);
178 setLayout(buf.params());
183 void InsetNote::setButtonLabel()
185 docstring const label = notetranslator_loc().find(params_.type);
187 // isn't this an identity? - MV
188 setLabelFont(layout_.labelfont);
192 Color_color InsetNote::backgroundColor() const
195 switch (params_.type) {
196 case InsetNoteParams::Note:
199 case InsetNoteParams::Comment:
200 c = Color::commentbg;
202 case InsetNoteParams::Greyedout:
203 c = Color::greyedoutbg;
205 case InsetNoteParams::Framed:
206 c = Color::greyedoutbg;
208 case InsetNoteParams::Shaded:
216 bool InsetNote::showInsetDialog(BufferView * bv) const
218 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
223 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
225 switch (cmd.action) {
227 case LFUN_INSET_MODIFY:
228 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
229 // get a bp from cur:
230 setLayout(cur.buffer().params());
234 case LFUN_INSET_DIALOG_UPDATE:
235 InsetNoteMailer(*this).updateDialog(&cur.bv());
238 case LFUN_MOUSE_RELEASE:
239 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
240 InsetNoteMailer(*this).showDialog(&cur.bv());
242 InsetCollapsable::doDispatch(cur, cmd);
246 InsetCollapsable::doDispatch(cur, cmd);
252 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
253 FuncStatus & flag) const
255 switch (cmd.action) {
257 case LFUN_INSET_MODIFY:
258 case LFUN_INSET_DIALOG_UPDATE:
263 return InsetCollapsable::getStatus(cur, cmd, flag);
267 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
269 TextClass const & tclass = buf.params().getTextClass();
270 Counters savecnt = tclass.counters();
271 InsetCollapsable::updateLabels(buf, it);
272 tclass.counters() = savecnt;
276 int InsetNote::latex(Buffer const & buf, odocstream & os,
277 OutputParams const & runparams_in) const
279 if (params_.type == InsetNoteParams::Note)
282 OutputParams runparams(runparams_in);
284 if (params_.type == InsetNoteParams::Comment) {
286 runparams.inComment = true;
287 // Ignore files that are exported inside a comment
288 runparams.exportdata.reset(new ExportData);
289 } else if (params_.type == InsetNoteParams::Greyedout)
290 type = "lyxgreyedout";
291 else if (params_.type == InsetNoteParams::Framed)
293 else if (params_.type == InsetNoteParams::Shaded)
297 ss << "%\n\\begin{" << from_ascii(type) << "}\n";
298 InsetText::latex(buf, ss, runparams);
299 ss << "\n\\end{" << from_ascii(type) << "}\n";
300 // the space after the comment in 'a[comment] b' will be eaten by the
301 // comment environment since the space before b is ignored with the
302 // following latex output:
310 // Adding {} before ' b' fixes this.
311 if (params_.type == InsetNoteParams::Comment)
314 docstring const str = ss.str();
316 runparams_in.encoding = runparams.encoding;
317 // Return how many newlines we issued.
318 return int(std::count(str.begin(), str.end(), '\n'));
322 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
323 OutputParams const & runparams_in) const
325 if (params_.type == InsetNoteParams::Note)
328 OutputParams runparams(runparams_in);
329 if (params_.type == InsetNoteParams::Comment) {
330 runparams.inComment = true;
331 // Ignore files that are exported inside a comment
332 runparams.exportdata.reset(new ExportData);
334 os << '[' << buf.B_("note") << ":\n";
335 InsetText::plaintext(buf, os, runparams);
338 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
342 int InsetNote::docbook(Buffer const & buf, odocstream & os,
343 OutputParams const & runparams_in) const
345 if (params_.type == InsetNoteParams::Note)
348 OutputParams runparams(runparams_in);
349 if (params_.type == InsetNoteParams::Comment) {
351 runparams.inComment = true;
352 // Ignore files that are exported inside a comment
353 runparams.exportdata.reset(new ExportData);
356 int const n = InsetText::docbook(buf, os, runparams);
358 if (params_.type == InsetNoteParams::Comment)
359 os << "\n</remark>\n";
361 // Return how many newlines we issued.
362 //return int(count(str.begin(), str.end(), '\n'));
367 void InsetNote::validate(LaTeXFeatures & features) const
369 if (params_.type == InsetNoteParams::Comment)
370 features.require("verbatim");
371 if (params_.type == InsetNoteParams::Greyedout) {
372 features.require("color");
373 features.require("lyxgreyedout");
375 if (params_.type == InsetNoteParams::Shaded) {
376 features.require("color");
377 features.require("framed");
379 if (params_.type == InsetNoteParams::Framed)
380 features.require("framed");
381 InsetText::validate(features);
386 string const InsetNoteMailer::name_("note");
388 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
393 string const InsetNoteMailer::inset2string(Buffer const &) const
395 return params2string(inset_.params());
399 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
402 data << name_ << ' ';
408 void InsetNoteMailer::string2params(string const & in,
409 InsetNoteParams & params)
411 params = InsetNoteParams();
416 istringstream data(in);
422 if (!lex || name != name_)
423 return print_mailer_error("InsetNoteMailer", in, 1, name_);
425 // This is part of the inset proper that is usually swallowed
426 // by Text::readInset
429 if (!lex || id != "Note")
430 return print_mailer_error("InsetBoxMailer", in, 2, "Note");