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 "support/debug.h"
24 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "support/gettext.h"
29 #include "LaTeXFeatures.h"
31 #include "MetricsInfo.h"
32 #include "OutputParams.h"
33 #include "ParIterator.h"
34 #include "TextClass.h"
35 #include "TocBackend.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 InsetNote::InsetNote(BufferParams const & bp, string const & label)
110 : InsetCollapsable(bp)
112 params_.type = notetranslator().find(label);
116 InsetNote::InsetNote(InsetNote const & in)
117 : InsetCollapsable(in), params_(in.params_)
121 InsetNote::~InsetNote()
123 InsetNoteMailer(*this).hideDialog();
127 Inset * InsetNote::clone() const
129 return new InsetNote(*this);
133 docstring const InsetNote::editMessage() const
135 return _("Opened Note Inset");
139 docstring InsetNote::name() const
141 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
145 Inset::DisplayType InsetNote::display() const
151 void InsetNote::write(Buffer const & buf, ostream & os) const
154 InsetCollapsable::write(buf, os);
158 void InsetNote::read(Buffer const & buf, Lexer & lex)
161 InsetCollapsable::read(buf, lex);
165 void InsetNote::setButtonLabel()
167 docstring const label = notetranslator_loc().find(params_.type);
172 bool InsetNote::showInsetDialog(BufferView * bv) const
174 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
179 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
181 switch (cmd.action) {
183 case LFUN_INSET_MODIFY:
184 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
185 // get a bp from cur:
186 setLayout(cur.buffer().params());
189 case LFUN_INSET_DIALOG_UPDATE:
190 InsetNoteMailer(*this).updateDialog(&cur.bv());
193 InsetCollapsable::doDispatch(cur, cmd);
199 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
200 FuncStatus & flag) const
202 switch (cmd.action) {
204 case LFUN_INSET_MODIFY:
205 case LFUN_INSET_DIALOG_UPDATE:
210 return InsetCollapsable::getStatus(cur, cmd, flag);
214 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
216 TextClass const & tclass = buf.params().getTextClass();
217 Counters savecnt = tclass.counters();
218 InsetCollapsable::updateLabels(buf, it);
219 tclass.counters() = savecnt;
223 void InsetNote::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
225 ParConstIterator pit = par_const_iterator_begin(*this);
227 Toc & toc = toclist["note"];
228 // FIXME: we probably want the note type too.
230 str = getNewLabel(str);
231 toc.push_back(TocItem(pit, 0, str));
235 bool InsetNote::isMacroScope(Buffer const & buf) const
237 // LyX note has no latex output
238 if (params_.type == InsetNoteParams::Note)
241 return InsetCollapsable::isMacroScope(buf);
245 int InsetNote::latex(Buffer const & buf, odocstream & os,
246 OutputParams const & runparams_in) const
248 if (params_.type == InsetNoteParams::Note)
251 OutputParams runparams(runparams_in);
252 if (params_.type == InsetNoteParams::Comment) {
253 runparams.inComment = true;
254 // Ignore files that are exported inside a comment
255 runparams.exportdata.reset(new ExportData);
259 InsetCollapsable::latex(buf, ss, runparams);
260 // the space after the comment in 'a[comment] b' will be eaten by the
261 // comment environment since the space before b is ignored with the
262 // following latex output:
270 // Adding {} before ' b' fixes this.
271 if (params_.type == InsetNoteParams::Comment)
274 docstring const str = ss.str();
276 runparams_in.encoding = runparams.encoding;
277 // Return how many newlines we issued.
278 return int(count(str.begin(), str.end(), '\n'));
282 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
283 OutputParams const & runparams_in) const
285 if (params_.type == InsetNoteParams::Note)
288 OutputParams runparams(runparams_in);
289 if (params_.type == InsetNoteParams::Comment) {
290 runparams.inComment = true;
291 // Ignore files that are exported inside a comment
292 runparams.exportdata.reset(new ExportData);
294 os << '[' << buf.B_("note") << ":\n";
295 InsetText::plaintext(buf, os, runparams);
298 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
302 int InsetNote::docbook(Buffer const & buf, odocstream & os,
303 OutputParams const & runparams_in) const
305 if (params_.type == InsetNoteParams::Note)
308 OutputParams runparams(runparams_in);
309 if (params_.type == InsetNoteParams::Comment) {
311 runparams.inComment = true;
312 // Ignore files that are exported inside a comment
313 runparams.exportdata.reset(new ExportData);
316 int const n = InsetText::docbook(buf, os, runparams);
318 if (params_.type == InsetNoteParams::Comment)
319 os << "\n</remark>\n";
321 // Return how many newlines we issued.
322 //return int(count(str.begin(), str.end(), '\n'));
327 void InsetNote::validate(LaTeXFeatures & features) const
329 if (params_.type == InsetNoteParams::Comment)
330 features.require("verbatim");
331 if (params_.type == InsetNoteParams::Greyedout) {
332 features.require("color");
333 features.require("lyxgreyedout");
335 InsetText::validate(features);
340 string const InsetNoteMailer::name_("note");
342 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
347 string const InsetNoteMailer::inset2string(Buffer const &) const
349 return params2string(inset_.params());
353 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
356 data << name_ << ' ';
362 void InsetNoteMailer::string2params(string const & in,
363 InsetNoteParams & params)
365 params = InsetNoteParams();
370 istringstream data(in);
376 if (!lex || name != name_)
377 return print_mailer_error("InsetNoteMailer", in, 1, name_);
379 // This is part of the inset proper that is usually swallowed
380 // by Text::readInset
383 if (!lex || id != "Note")
384 return print_mailer_error("InsetBoxMailer", in, 2, "Note");