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"
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);
64 NoteTranslatorLoc const init_notetranslator_loc()
66 NoteTranslatorLoc translator(_("Note"), InsetNoteParams::Note);
67 translator.addPair(_("Comment"), InsetNoteParams::Comment);
68 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
73 NoteTranslator const & notetranslator()
75 static NoteTranslator translator = init_notetranslator();
80 NoteTranslatorLoc const & notetranslator_loc()
82 static NoteTranslatorLoc translator = init_notetranslator_loc();
91 InsetNoteParams::InsetNoteParams()
96 void InsetNoteParams::write(ostream & os) const
98 string const label = notetranslator().find(type);
99 os << "Note " << label << "\n";
103 void InsetNoteParams::read(Lexer & lex)
108 type = notetranslator().find(label);
112 InsetNote::InsetNote(BufferParams const & bp, string const & label)
113 : InsetCollapsable(bp)
115 params_.type = notetranslator().find(label);
119 InsetNote::InsetNote(InsetNote const & in)
120 : InsetCollapsable(in), params_(in.params_)
124 InsetNote::~InsetNote()
126 InsetNoteMailer(*this).hideDialog();
130 Inset * InsetNote::clone() const
132 return new InsetNote(*this);
136 docstring const InsetNote::editMessage() const
138 return _("Opened Note Inset");
142 docstring InsetNote::name() const
144 return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
148 Inset::DisplayType InsetNote::display() const
154 void InsetNote::write(Buffer const & buf, ostream & os) const
157 InsetCollapsable::write(buf, os);
161 void InsetNote::read(Buffer const & buf, Lexer & lex)
164 InsetCollapsable::read(buf, lex);
168 void InsetNote::setButtonLabel()
170 docstring const label = notetranslator_loc().find(params_.type);
175 bool InsetNote::showInsetDialog(BufferView * bv) const
177 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
182 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
184 switch (cmd.action) {
186 case LFUN_INSET_MODIFY:
187 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
188 // get a bp from cur:
189 setLayout(cur.buffer().params());
192 case LFUN_INSET_DIALOG_UPDATE:
193 InsetNoteMailer(*this).updateDialog(&cur.bv());
196 InsetCollapsable::doDispatch(cur, cmd);
202 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
203 FuncStatus & flag) const
205 switch (cmd.action) {
207 case LFUN_INSET_MODIFY:
208 case LFUN_INSET_DIALOG_UPDATE:
213 return InsetCollapsable::getStatus(cur, cmd, flag);
217 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
219 TextClass const & tclass = buf.params().getTextClass();
220 Counters savecnt = tclass.counters();
221 InsetCollapsable::updateLabels(buf, it);
222 tclass.counters() = savecnt;
226 int InsetNote::latex(Buffer const & buf, odocstream & os,
227 OutputParams const & runparams_in) const
229 if (params_.type == InsetNoteParams::Note)
232 OutputParams runparams(runparams_in);
233 if (params_.type == InsetNoteParams::Comment) {
234 runparams.inComment = true;
235 // Ignore files that are exported inside a comment
236 runparams.exportdata.reset(new ExportData);
240 InsetCollapsable::latex(buf, ss, runparams);
241 // the space after the comment in 'a[comment] b' will be eaten by the
242 // comment environment since the space before b is ignored with the
243 // following latex output:
251 // Adding {} before ' b' fixes this.
252 if (params_.type == InsetNoteParams::Comment)
255 docstring const str = ss.str();
257 runparams_in.encoding = runparams.encoding;
258 // Return how many newlines we issued.
259 return int(std::count(str.begin(), str.end(), '\n'));
263 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
264 OutputParams const & runparams_in) const
266 if (params_.type == InsetNoteParams::Note)
269 OutputParams runparams(runparams_in);
270 if (params_.type == InsetNoteParams::Comment) {
271 runparams.inComment = true;
272 // Ignore files that are exported inside a comment
273 runparams.exportdata.reset(new ExportData);
275 os << '[' << buf.B_("note") << ":\n";
276 InsetText::plaintext(buf, os, runparams);
279 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
283 int InsetNote::docbook(Buffer const & buf, odocstream & os,
284 OutputParams const & runparams_in) const
286 if (params_.type == InsetNoteParams::Note)
289 OutputParams runparams(runparams_in);
290 if (params_.type == InsetNoteParams::Comment) {
292 runparams.inComment = true;
293 // Ignore files that are exported inside a comment
294 runparams.exportdata.reset(new ExportData);
297 int const n = InsetText::docbook(buf, os, runparams);
299 if (params_.type == InsetNoteParams::Comment)
300 os << "\n</remark>\n";
302 // Return how many newlines we issued.
303 //return int(count(str.begin(), str.end(), '\n'));
308 void InsetNote::validate(LaTeXFeatures & features) const
310 if (params_.type == InsetNoteParams::Comment)
311 features.require("verbatim");
312 if (params_.type == InsetNoteParams::Greyedout) {
313 features.require("color");
314 features.require("lyxgreyedout");
316 InsetText::validate(features);
321 string const InsetNoteMailer::name_("note");
323 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
328 string const InsetNoteMailer::inset2string(Buffer const &) const
330 return params2string(inset_.params());
334 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
337 data << name_ << ' ';
343 void InsetNoteMailer::string2params(string const & in,
344 InsetNoteParams & params)
346 params = InsetNoteParams();
351 istringstream data(in);
357 if (!lex || name != name_)
358 return print_mailer_error("InsetNoteMailer", in, 1, name_);
360 // This is part of the inset proper that is usually swallowed
361 // by Text::readInset
364 if (!lex || id != "Note")
365 return print_mailer_error("InsetBoxMailer", in, 2, "Note");