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 "TextClass.h"
35 #include "support/docstream.h"
36 #include "support/Translator.h"
47 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
48 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
50 NoteTranslator const init_notetranslator()
52 NoteTranslator translator("Note", InsetNoteParams::Note);
53 translator.addPair("Comment", InsetNoteParams::Comment);
54 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
59 NoteTranslatorLoc const init_notetranslator_loc()
61 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
62 translator.addPair(_("Comment"), InsetNoteParams::Comment);
63 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
68 NoteTranslator const & notetranslator()
70 static NoteTranslator translator = init_notetranslator();
75 NoteTranslatorLoc const & notetranslator_loc()
77 static NoteTranslatorLoc translator = init_notetranslator_loc();
86 InsetNoteParams::InsetNoteParams()
91 void InsetNoteParams::write(ostream & os) const
93 string const label = notetranslator().find(type);
94 os << "Note " << label << "\n";
98 void InsetNoteParams::read(Lexer & lex)
103 type = notetranslator().find(label);
107 InsetNote::InsetNote(BufferParams const & bp, string const & label)
108 : InsetCollapsable(bp)
110 params_.type = notetranslator().find(label);
114 InsetNote::InsetNote(InsetNote const & in)
115 : InsetCollapsable(in), params_(in.params_)
119 InsetNote::~InsetNote()
121 InsetNoteMailer(*this).hideDialog();
125 Inset * InsetNote::clone() const
127 return new InsetNote(*this);
131 docstring const InsetNote::editMessage() const
133 return _("Opened Note Inset");
137 docstring InsetNote::name() const
139 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
143 Inset::DisplayType InsetNote::display() const
149 void InsetNote::write(Buffer const & buf, ostream & os) const
152 InsetCollapsable::write(buf, os);
156 void InsetNote::read(Buffer const & buf, Lexer & lex)
159 InsetCollapsable::read(buf, lex);
163 void InsetNote::setButtonLabel()
165 docstring const label = notetranslator_loc().find(params_.type);
170 bool InsetNote::showInsetDialog(BufferView * bv) const
172 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
177 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
179 switch (cmd.action) {
181 case LFUN_INSET_MODIFY:
182 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
183 // get a bp from cur:
184 setLayout(cur.buffer().params());
187 case LFUN_INSET_DIALOG_UPDATE:
188 InsetNoteMailer(*this).updateDialog(&cur.bv());
191 InsetCollapsable::doDispatch(cur, cmd);
197 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
198 FuncStatus & flag) const
200 switch (cmd.action) {
202 case LFUN_INSET_MODIFY:
203 case LFUN_INSET_DIALOG_UPDATE:
208 return InsetCollapsable::getStatus(cur, cmd, flag);
212 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
214 TextClass const & tclass = buf.params().getTextClass();
215 Counters savecnt = tclass.counters();
216 InsetCollapsable::updateLabels(buf, it);
217 tclass.counters() = savecnt;
221 bool InsetNote::isMacroScope(Buffer const & buf) const
223 // LyX note has no latex output
224 if (params_.type == InsetNoteParams::Note)
227 return InsetCollapsable::isMacroScope(buf);
231 int InsetNote::latex(Buffer const & buf, odocstream & os,
232 OutputParams const & runparams_in) const
234 if (params_.type == InsetNoteParams::Note)
237 OutputParams runparams(runparams_in);
238 if (params_.type == InsetNoteParams::Comment) {
239 runparams.inComment = true;
240 // Ignore files that are exported inside a comment
241 runparams.exportdata.reset(new ExportData);
245 InsetCollapsable::latex(buf, ss, runparams);
246 // the space after the comment in 'a[comment] b' will be eaten by the
247 // comment environment since the space before b is ignored with the
248 // following latex output:
256 // Adding {} before ' b' fixes this.
257 if (params_.type == InsetNoteParams::Comment)
260 docstring const str = ss.str();
262 runparams_in.encoding = runparams.encoding;
263 // Return how many newlines we issued.
264 return int(count(str.begin(), str.end(), '\n'));
268 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
269 OutputParams const & runparams_in) const
271 if (params_.type == InsetNoteParams::Note)
274 OutputParams runparams(runparams_in);
275 if (params_.type == InsetNoteParams::Comment) {
276 runparams.inComment = true;
277 // Ignore files that are exported inside a comment
278 runparams.exportdata.reset(new ExportData);
280 os << '[' << buf.B_("note") << ":\n";
281 InsetText::plaintext(buf, os, runparams);
284 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
288 int InsetNote::docbook(Buffer const & buf, odocstream & os,
289 OutputParams const & runparams_in) const
291 if (params_.type == InsetNoteParams::Note)
294 OutputParams runparams(runparams_in);
295 if (params_.type == InsetNoteParams::Comment) {
297 runparams.inComment = true;
298 // Ignore files that are exported inside a comment
299 runparams.exportdata.reset(new ExportData);
302 int const n = InsetText::docbook(buf, os, runparams);
304 if (params_.type == InsetNoteParams::Comment)
305 os << "\n</remark>\n";
307 // Return how many newlines we issued.
308 //return int(count(str.begin(), str.end(), '\n'));
313 void InsetNote::validate(LaTeXFeatures & features) const
315 if (params_.type == InsetNoteParams::Comment)
316 features.require("verbatim");
317 if (params_.type == InsetNoteParams::Greyedout) {
318 features.require("color");
319 features.require("lyxgreyedout");
321 InsetText::validate(features);
326 string const InsetNoteMailer::name_("note");
328 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
333 string const InsetNoteMailer::inset2string(Buffer const &) const
335 return params2string(inset_.params());
339 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
342 data << name_ << ' ';
348 void InsetNoteMailer::string2params(string const & in,
349 InsetNoteParams & params)
351 params = InsetNoteParams();
356 istringstream data(in);
362 if (!lex || name != name_)
363 return print_mailer_error("InsetNoteMailer", in, 1, name_);
365 // This is part of the inset proper that is usually swallowed
366 // by Text::readInset
369 if (!lex || id != "Note")
370 return print_mailer_error("InsetBoxMailer", in, 2, "Note");