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"
33 #include "TextClass.h"
35 #include "support/docstream.h"
36 #include "support/Translator.h"
45 using std::istringstream;
47 using std::ostringstream;
52 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
53 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
55 NoteTranslator const init_notetranslator()
57 NoteTranslator translator("Note", InsetNoteParams::Note);
58 translator.addPair("Comment", InsetNoteParams::Comment);
59 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
60 translator.addPair("Framed", InsetNoteParams::Framed);
61 translator.addPair("Shaded", InsetNoteParams::Shaded);
66 NoteTranslatorLoc const init_notetranslator_loc()
68 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
69 translator.addPair(_("Comment"), InsetNoteParams::Comment);
70 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
71 translator.addPair(_("Framed"), InsetNoteParams::Framed);
72 translator.addPair(_("Shaded"), InsetNoteParams::Shaded);
77 NoteTranslator const & notetranslator()
79 static NoteTranslator translator = init_notetranslator();
84 NoteTranslatorLoc const & notetranslator_loc()
86 static NoteTranslatorLoc translator = init_notetranslator_loc();
95 InsetNoteParams::InsetNoteParams()
100 void InsetNoteParams::write(ostream & os) const
102 string const label = notetranslator().find(type);
103 os << "Note " << label << "\n";
107 void InsetNoteParams::read(Lexer & lex)
112 type = notetranslator().find(label);
116 InsetNote::InsetNote(BufferParams const & bp, string const & label)
117 : InsetCollapsable(bp)
119 params_.type = notetranslator().find(label);
123 InsetNote::InsetNote(InsetNote const & in)
124 : InsetCollapsable(in), params_(in.params_)
128 InsetNote::~InsetNote()
130 InsetNoteMailer(*this).hideDialog();
134 Inset * InsetNote::clone() const
136 return new InsetNote(*this);
140 docstring const InsetNote::editMessage() const
142 return _("Opened Note Inset");
146 docstring InsetNote::name() const
148 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
152 Inset::DisplayType InsetNote::display() const
154 switch (params_.type) {
155 case InsetNoteParams::Framed:
156 case InsetNoteParams::Shaded:
164 void InsetNote::write(Buffer const & buf, ostream & os) const
167 InsetCollapsable::write(buf, os);
171 void InsetNote::read(Buffer const & buf, Lexer & lex)
174 InsetCollapsable::read(buf, lex);
178 void InsetNote::setButtonLabel()
180 docstring const label = notetranslator_loc().find(params_.type);
185 bool InsetNote::showInsetDialog(BufferView * bv) const
187 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
192 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
194 switch (cmd.action) {
196 case LFUN_INSET_MODIFY:
197 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
198 // get a bp from cur:
199 setLayout(cur.buffer().params());
202 case LFUN_INSET_DIALOG_UPDATE:
203 InsetNoteMailer(*this).updateDialog(&cur.bv());
206 InsetCollapsable::doDispatch(cur, cmd);
212 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
213 FuncStatus & flag) const
215 switch (cmd.action) {
217 case LFUN_INSET_MODIFY:
218 case LFUN_INSET_DIALOG_UPDATE:
223 return InsetCollapsable::getStatus(cur, cmd, flag);
227 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
229 TextClass const & tclass = buf.params().getTextClass();
230 Counters savecnt = tclass.counters();
231 InsetCollapsable::updateLabels(buf, it);
232 tclass.counters() = savecnt;
236 int InsetNote::latex(Buffer const & buf, odocstream & os,
237 OutputParams const & runparams_in) const
239 if (params_.type == InsetNoteParams::Note)
242 OutputParams runparams(runparams_in);
243 if (params_.type == InsetNoteParams::Comment) {
244 runparams.inComment = true;
245 // Ignore files that are exported inside a comment
246 runparams.exportdata.reset(new ExportData);
250 InsetCollapsable::latex(buf, ss, runparams);
251 // the space after the comment in 'a[comment] b' will be eaten by the
252 // comment environment since the space before b is ignored with the
253 // following latex output:
261 // Adding {} before ' b' fixes this.
262 if (params_.type == InsetNoteParams::Comment)
265 docstring const str = ss.str();
267 runparams_in.encoding = runparams.encoding;
268 // Return how many newlines we issued.
269 return int(std::count(str.begin(), str.end(), '\n'));
273 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
274 OutputParams const & runparams_in) const
276 if (params_.type == InsetNoteParams::Note)
279 OutputParams runparams(runparams_in);
280 if (params_.type == InsetNoteParams::Comment) {
281 runparams.inComment = true;
282 // Ignore files that are exported inside a comment
283 runparams.exportdata.reset(new ExportData);
285 os << '[' << buf.B_("note") << ":\n";
286 InsetText::plaintext(buf, os, runparams);
289 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
293 int InsetNote::docbook(Buffer const & buf, odocstream & os,
294 OutputParams const & runparams_in) const
296 if (params_.type == InsetNoteParams::Note)
299 OutputParams runparams(runparams_in);
300 if (params_.type == InsetNoteParams::Comment) {
302 runparams.inComment = true;
303 // Ignore files that are exported inside a comment
304 runparams.exportdata.reset(new ExportData);
307 int const n = InsetText::docbook(buf, os, runparams);
309 if (params_.type == InsetNoteParams::Comment)
310 os << "\n</remark>\n";
312 // Return how many newlines we issued.
313 //return int(count(str.begin(), str.end(), '\n'));
318 void InsetNote::validate(LaTeXFeatures & features) const
320 if (params_.type == InsetNoteParams::Comment)
321 features.require("verbatim");
322 if (params_.type == InsetNoteParams::Greyedout) {
323 features.require("color");
324 features.require("lyxgreyedout");
326 if (params_.type == InsetNoteParams::Shaded) {
327 features.require("color");
328 features.require("framed");
330 if (params_.type == InsetNoteParams::Framed)
331 features.require("framed");
332 InsetText::validate(features);
337 string const InsetNoteMailer::name_("note");
339 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
344 string const InsetNoteMailer::inset2string(Buffer const &) const
346 return params2string(inset_.params());
350 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
353 data << name_ << ' ';
359 void InsetNoteMailer::string2params(string const & in,
360 InsetNoteParams & params)
362 params = InsetNoteParams();
367 istringstream data(in);
373 if (!lex || name != name_)
374 return print_mailer_error("InsetNoteMailer", in, 1, name_);
376 // This is part of the inset proper that is usually swallowed
377 // by Text::readInset
380 if (!lex || id != "Note")
381 return print_mailer_error("InsetBoxMailer", in, 2, "Note");