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<std::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 int InsetNote::latex(Buffer const & buf, odocstream & os,
222 OutputParams const & runparams_in) const
224 if (params_.type == InsetNoteParams::Note)
227 OutputParams runparams(runparams_in);
228 if (params_.type == InsetNoteParams::Comment) {
229 runparams.inComment = true;
230 // Ignore files that are exported inside a comment
231 runparams.exportdata.reset(new ExportData);
235 InsetCollapsable::latex(buf, ss, runparams);
236 // the space after the comment in 'a[comment] b' will be eaten by the
237 // comment environment since the space before b is ignored with the
238 // following latex output:
246 // Adding {} before ' b' fixes this.
247 if (params_.type == InsetNoteParams::Comment)
250 docstring const str = ss.str();
252 runparams_in.encoding = runparams.encoding;
253 // Return how many newlines we issued.
254 return int(std::count(str.begin(), str.end(), '\n'));
258 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
259 OutputParams const & runparams_in) const
261 if (params_.type == InsetNoteParams::Note)
264 OutputParams runparams(runparams_in);
265 if (params_.type == InsetNoteParams::Comment) {
266 runparams.inComment = true;
267 // Ignore files that are exported inside a comment
268 runparams.exportdata.reset(new ExportData);
270 os << '[' << buf.B_("note") << ":\n";
271 InsetText::plaintext(buf, os, runparams);
274 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
278 int InsetNote::docbook(Buffer const & buf, odocstream & os,
279 OutputParams const & runparams_in) const
281 if (params_.type == InsetNoteParams::Note)
284 OutputParams runparams(runparams_in);
285 if (params_.type == InsetNoteParams::Comment) {
287 runparams.inComment = true;
288 // Ignore files that are exported inside a comment
289 runparams.exportdata.reset(new ExportData);
292 int const n = InsetText::docbook(buf, os, runparams);
294 if (params_.type == InsetNoteParams::Comment)
295 os << "\n</remark>\n";
297 // Return how many newlines we issued.
298 //return int(count(str.begin(), str.end(), '\n'));
303 void InsetNote::validate(LaTeXFeatures & features) const
305 if (params_.type == InsetNoteParams::Comment)
306 features.require("verbatim");
307 if (params_.type == InsetNoteParams::Greyedout) {
308 features.require("color");
309 features.require("lyxgreyedout");
311 InsetText::validate(features);
316 string const InsetNoteMailer::name_("note");
318 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
323 string const InsetNoteMailer::inset2string(Buffer const &) const
325 return params2string(inset_.params());
329 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
332 data << name_ << ' ';
338 void InsetNoteMailer::string2params(string const & in,
339 InsetNoteParams & params)
341 params = InsetNoteParams();
346 istringstream data(in);
352 if (!lex || name != name_)
353 return print_mailer_error("InsetNoteMailer", in, 1, name_);
355 // This is part of the inset proper that is usually swallowed
356 // by Text::readInset
359 if (!lex || id != "Note")
360 return print_mailer_error("InsetBoxMailer", in, 2, "Note");