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"
40 #include "frontends/Application.h"
51 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
52 typedef Translator<docstring, InsetNoteParams::Type> NoteTranslatorLoc;
54 NoteTranslator const init_notetranslator()
56 NoteTranslator translator("Note", InsetNoteParams::Note);
57 translator.addPair("Comment", InsetNoteParams::Comment);
58 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
63 NoteTranslatorLoc const init_notetranslator_loc()
65 NoteTranslatorLoc translator(_("Note[[InsetNote]]"), InsetNoteParams::Note);
66 translator.addPair(_("Comment"), InsetNoteParams::Comment);
67 translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
72 NoteTranslator const & notetranslator()
74 static NoteTranslator translator = init_notetranslator();
79 NoteTranslatorLoc const & notetranslator_loc()
81 static NoteTranslatorLoc translator = init_notetranslator_loc();
90 InsetNoteParams::InsetNoteParams()
95 void InsetNoteParams::write(ostream & os) const
97 string const label = notetranslator().find(type);
98 os << "Note " << label << "\n";
102 void InsetNoteParams::read(Lexer & lex)
107 type = notetranslator().find(label);
111 /////////////////////////////////////////////////////////////////////
115 /////////////////////////////////////////////////////////////////////
117 InsetNote::InsetNote(Buffer const & buf, string const & label)
118 : InsetCollapsable(buf)
120 params_.type = notetranslator().find(label);
124 InsetNote::~InsetNote()
126 hideDialogs("note", this);
130 docstring InsetNote::editMessage() const
132 return _("Opened Note Inset");
136 docstring InsetNote::name() const
138 return from_ascii("Note:" + notetranslator().find(params_.type));
142 Inset::DisplayType InsetNote::display() const
148 void InsetNote::write(ostream & os) const
151 InsetCollapsable::write(os);
155 void InsetNote::read(Lexer & lex)
158 InsetCollapsable::read(lex);
162 void InsetNote::setButtonLabel()
164 docstring const label = notetranslator_loc().find(params_.type);
169 bool InsetNote::showInsetDialog(BufferView * bv) const
171 bv->showDialog("note", params2string(params()),
172 const_cast<InsetNote *>(this));
177 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
179 switch (cmd.action) {
181 case LFUN_INSET_MODIFY:
182 string2params(to_utf8(cmd.argument()), params_);
183 // get a bp from cur:
184 setLayout(cur.buffer().params());
187 case LFUN_INSET_DIALOG_UPDATE:
188 cur.bv().updateDialog("note", params2string(params()));
192 InsetCollapsable::doDispatch(cur, cmd);
198 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
199 FuncStatus & flag) const
201 switch (cmd.action) {
203 case LFUN_INSET_MODIFY:
204 // disallow comment and greyed out in commands
205 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
206 cmd.getArg(2) == "Note");
207 if (cmd.getArg(0) == "note") {
208 InsetNoteParams params;
209 string2params(to_utf8(cmd.argument()), params);
210 flag.setOnOff(params_.type == params.type);
214 case LFUN_INSET_DIALOG_UPDATE:
215 flag.setEnabled(true);
219 return InsetCollapsable::getStatus(cur, cmd, flag);
224 void InsetNote::addToToc(DocIterator const & cpit)
226 DocIterator pit = cpit;
227 pit.push_back(CursorSlice(*this));
229 Toc & toc = buffer().tocBackend().toc("note");
231 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
233 toc.push_back(TocItem(pit, 0, str));
237 bool InsetNote::isMacroScope() const
239 // LyX note has no latex output
240 if (params_.type == InsetNoteParams::Note)
243 return InsetCollapsable::isMacroScope();
247 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
249 if (params_.type == InsetNoteParams::Note)
252 OutputParams runparams(runparams_in);
253 if (params_.type == InsetNoteParams::Comment) {
254 runparams.inComment = true;
255 // Ignore files that are exported inside a comment
256 runparams.exportdata.reset(new ExportData);
260 InsetCollapsable::latex(ss, runparams);
261 // the space after the comment in 'a[comment] b' will be eaten by the
262 // comment environment since the space before b is ignored with the
263 // following latex output:
271 // Adding {} before ' b' fixes this.
272 if (params_.type == InsetNoteParams::Comment)
275 docstring const str = ss.str();
277 runparams_in.encoding = runparams.encoding;
278 // Return how many newlines we issued.
279 return int(count(str.begin(), str.end(), '\n'));
283 int InsetNote::plaintext(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) {
291 runparams.inComment = true;
292 // Ignore files that are exported inside a comment
293 runparams.exportdata.reset(new ExportData);
295 os << '[' << buffer().B_("note") << ":\n";
296 InsetText::plaintext(os, runparams);
299 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
303 int InsetNote::docbook(odocstream & os, 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(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);
339 docstring InsetNote::contextMenu(BufferView const &, int, int) const
341 return from_ascii("context-note");
345 string InsetNote::params2string(InsetNoteParams const & params)
348 data << "note" << ' ';
354 void InsetNote::string2params(string const & in, InsetNoteParams & params)
356 params = InsetNoteParams();
361 istringstream data(in);
364 lex.setContext("InsetNote::string2params");
365 lex >> "note" >> "Note";