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"
23 #include "DispatchResult.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32 #include "ParIterator.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/Translator.h"
49 typedef Translator<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);
61 NoteTranslatorLoc const init_notetranslator_loc()
63 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
64 translator.addPair(_("Comment"), InsetNoteParams::Comment);
65 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
70 NoteTranslator const & notetranslator()
72 static NoteTranslator translator = init_notetranslator();
77 NoteTranslatorLoc const & notetranslator_loc()
79 static NoteTranslatorLoc translator = init_notetranslator_loc();
88 InsetNoteParams::InsetNoteParams()
93 void InsetNoteParams::write(ostream & os) const
95 string const label = notetranslator().find(type);
96 os << "Note " << label << "\n";
100 void InsetNoteParams::read(Lexer & lex)
105 type = notetranslator().find(label);
109 /////////////////////////////////////////////////////////////////////
113 /////////////////////////////////////////////////////////////////////
115 InsetNote::InsetNote(Buffer const & buf, string const & label)
116 : InsetCollapsable(buf)
118 params_.type = notetranslator().find(label);
122 InsetNote::~InsetNote()
124 InsetNoteMailer(*this).hideDialog();
128 docstring InsetNote::editMessage() const
130 return _("Opened Note Inset");
134 docstring InsetNote::name() const
136 return from_ascii("Note:" + notetranslator().find(params_.type));
140 Inset::DisplayType InsetNote::display() const
146 void InsetNote::write(ostream & os) const
149 InsetCollapsable::write(os);
153 void InsetNote::read(Lexer & lex)
156 InsetCollapsable::read(lex);
160 void InsetNote::setButtonLabel()
162 docstring const label = notetranslator_loc().find(params_.type);
167 bool InsetNote::showInsetDialog(BufferView * bv) const
169 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
174 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
176 switch (cmd.action) {
178 case LFUN_INSET_MODIFY:
179 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
180 // get a bp from cur:
181 setLayout(cur.buffer().params());
184 case LFUN_INSET_DIALOG_UPDATE:
185 InsetNoteMailer(*this).updateDialog(&cur.bv());
188 InsetCollapsable::doDispatch(cur, cmd);
194 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
195 FuncStatus & flag) const
197 switch (cmd.action) {
199 case LFUN_INSET_MODIFY:
200 case LFUN_INSET_DIALOG_UPDATE:
205 return InsetCollapsable::getStatus(cur, cmd, flag);
209 void InsetNote::updateLabels(ParIterator const & it)
211 DocumentClass const & tclass = buffer().params().documentClass();
212 Counters savecnt = tclass.counters();
213 InsetCollapsable::updateLabels(it);
214 tclass.counters() = savecnt;
218 void InsetNote::addToToc(ParConstIterator const & cpit) const
220 ParConstIterator pit = cpit;
221 pit.push_back(*this);
223 Toc & toc = buffer().tocBackend().toc("note");
225 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
227 toc.push_back(TocItem(pit, 0, str));
231 bool InsetNote::isMacroScope() const
233 // LyX note has no latex output
234 if (params_.type == InsetNoteParams::Note)
237 return InsetCollapsable::isMacroScope();
241 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
243 if (params_.type == InsetNoteParams::Note)
246 OutputParams runparams(runparams_in);
247 if (params_.type == InsetNoteParams::Comment) {
248 runparams.inComment = true;
249 // Ignore files that are exported inside a comment
250 runparams.exportdata.reset(new ExportData);
254 InsetCollapsable::latex(ss, runparams);
255 // the space after the comment in 'a[comment] b' will be eaten by the
256 // comment environment since the space before b is ignored with the
257 // following latex output:
265 // Adding {} before ' b' fixes this.
266 if (params_.type == InsetNoteParams::Comment)
269 docstring const str = ss.str();
271 runparams_in.encoding = runparams.encoding;
272 // Return how many newlines we issued.
273 return int(count(str.begin(), str.end(), '\n'));
277 int InsetNote::plaintext(odocstream & os,
278 OutputParams const & runparams_in) const
280 if (params_.type == InsetNoteParams::Note)
283 OutputParams runparams(runparams_in);
284 if (params_.type == InsetNoteParams::Comment) {
285 runparams.inComment = true;
286 // Ignore files that are exported inside a comment
287 runparams.exportdata.reset(new ExportData);
289 os << '[' << buffer().B_("note") << ":\n";
290 InsetText::plaintext(os, runparams);
293 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
297 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
299 if (params_.type == InsetNoteParams::Note)
302 OutputParams runparams(runparams_in);
303 if (params_.type == InsetNoteParams::Comment) {
305 runparams.inComment = true;
306 // Ignore files that are exported inside a comment
307 runparams.exportdata.reset(new ExportData);
310 int const n = InsetText::docbook(os, runparams);
312 if (params_.type == InsetNoteParams::Comment)
313 os << "\n</remark>\n";
315 // Return how many newlines we issued.
316 //return int(count(str.begin(), str.end(), '\n'));
321 void InsetNote::validate(LaTeXFeatures & features) const
323 if (params_.type == InsetNoteParams::Comment)
324 features.require("verbatim");
325 if (params_.type == InsetNoteParams::Greyedout) {
326 features.require("color");
327 features.require("lyxgreyedout");
329 InsetText::validate(features);
334 string const InsetNoteMailer::name_("note");
336 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
341 string const InsetNoteMailer::inset2string(Buffer const &) const
343 return params2string(inset_.params());
347 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
350 data << name_ << ' ';
356 void InsetNoteMailer::string2params(string const & in,
357 InsetNoteParams & params)
359 params = InsetNoteParams();
364 istringstream data(in);
370 if (!lex || name != name_)
371 return print_mailer_error("InsetNoteMailer", in, 1, name_);
373 // This is part of the inset proper that is usually swallowed
374 // by Text::readInset
377 if (!lex || id != "Note")
378 return print_mailer_error("InsetBoxMailer", in, 2, "Note");