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 "InsetIterator.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/gettext.h"
39 #include "support/Translator.h"
41 #include "frontends/Application.h"
52 typedef Translator<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[[InsetNote]]"), 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 /////////////////////////////////////////////////////////////////////
116 /////////////////////////////////////////////////////////////////////
118 InsetNote::InsetNote(Buffer const & buf, string const & label)
119 : InsetCollapsable(buf)
121 params_.type = notetranslator().find(label);
125 InsetNote::~InsetNote()
127 hideDialogs("note", this);
131 docstring InsetNote::editMessage() const
133 return _("Opened Note Inset");
137 docstring InsetNote::name() const
139 return from_ascii("Note:" + notetranslator().find(params_.type));
143 Inset::DisplayType InsetNote::display() const
149 void InsetNote::write(ostream & os) const
152 InsetCollapsable::write(os);
156 void InsetNote::read(Lexer & lex)
159 InsetCollapsable::read(lex);
163 void InsetNote::setButtonLabel()
165 docstring const label = notetranslator_loc().find(params_.type);
170 bool InsetNote::showInsetDialog(BufferView * bv) const
172 bv->showDialog("note", params2string(params()),
173 const_cast<InsetNote *>(this));
178 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
180 switch (cmd.action) {
182 case LFUN_INSET_MODIFY:
183 string2params(to_utf8(cmd.argument()), params_);
186 case LFUN_INSET_DIALOG_UPDATE:
187 cur.bv().updateDialog("note", params2string(params()));
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 // disallow comment and greyed out in commands
204 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
205 cmd.getArg(2) == "Note");
206 if (cmd.getArg(0) == "note") {
207 InsetNoteParams params;
208 string2params(to_utf8(cmd.argument()), params);
209 flag.setOnOff(params_.type == params.type);
213 case LFUN_INSET_DIALOG_UPDATE:
214 flag.setEnabled(true);
218 return InsetCollapsable::getStatus(cur, cmd, flag);
223 void InsetNote::addToToc(DocIterator const & cpit)
225 DocIterator pit = cpit;
226 pit.push_back(CursorSlice(*this));
228 Toc & toc = buffer().tocBackend().toc("note");
230 str = notetranslator_loc().find(params_.type) + from_ascii(": ")
232 toc.push_back(TocItem(pit, 0, str));
233 // Proceed with the rest of the inset.
234 InsetCollapsable::addToToc(cpit);
238 bool InsetNote::isMacroScope() const
240 // LyX note has no latex output
241 if (params_.type == InsetNoteParams::Note)
244 return InsetCollapsable::isMacroScope();
248 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
250 if (params_.type == InsetNoteParams::Note)
253 OutputParams runparams(runparams_in);
254 if (params_.type == InsetNoteParams::Comment) {
255 runparams.inComment = true;
256 // Ignore files that are exported inside a comment
257 runparams.exportdata.reset(new ExportData);
261 InsetCollapsable::latex(ss, runparams);
262 // the space after the comment in 'a[comment] b' will be eaten by the
263 // comment environment since the space before b is ignored with the
264 // following latex output:
272 // Adding {} before ' b' fixes this.
273 if (params_.type == InsetNoteParams::Comment)
276 docstring const str = ss.str();
278 runparams_in.encoding = runparams.encoding;
279 // Return how many newlines we issued.
280 return int(count(str.begin(), str.end(), '\n'));
284 int InsetNote::plaintext(odocstream & os,
285 OutputParams const & runparams_in) const
287 if (params_.type == InsetNoteParams::Note)
290 OutputParams runparams(runparams_in);
291 if (params_.type == InsetNoteParams::Comment) {
292 runparams.inComment = true;
293 // Ignore files that are exported inside a comment
294 runparams.exportdata.reset(new ExportData);
296 os << '[' << buffer().B_("note") << ":\n";
297 InsetText::plaintext(os, runparams);
300 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
304 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
306 if (params_.type == InsetNoteParams::Note)
309 OutputParams runparams(runparams_in);
310 if (params_.type == InsetNoteParams::Comment) {
312 runparams.inComment = true;
313 // Ignore files that are exported inside a comment
314 runparams.exportdata.reset(new ExportData);
317 int const n = InsetText::docbook(os, runparams);
319 if (params_.type == InsetNoteParams::Comment)
320 os << "\n</remark>\n";
322 // Return how many newlines we issued.
323 //return int(count(str.begin(), str.end(), '\n'));
328 docstring InsetNote::xhtml(odocstream & os, OutputParams const & rp) const
330 if (params_.type == InsetNoteParams::Note)
333 return InsetCollapsable::xhtml(os, rp);
337 void InsetNote::validate(LaTeXFeatures & features) const
339 switch (params_.type) {
340 case InsetNoteParams::Comment:
341 features.require("verbatim");
343 case InsetNoteParams::Greyedout:
344 features.require("color");
345 features.require("lyxgreyedout");
346 InsetCollapsable::validate(features);
348 case InsetNoteParams::Note:
354 docstring InsetNote::contextMenu(BufferView const &, int, int) const
356 return from_ascii("context-note");
360 string InsetNote::params2string(InsetNoteParams const & params)
363 data << "note" << ' ';
369 void InsetNote::string2params(string const & in, InsetNoteParams & params)
371 params = InsetNoteParams();
376 istringstream data(in);
379 lex.setContext("InsetNote::string2params");
381 // There are cases, such as when we are called via getStatus() from
382 // Dialog::canApply(), where we are just called with "note" rather
383 // than a full "note Note TYPE".
392 bool mutateNotes(Cursor & cur, string const & source, string const & target)
394 InsetNoteParams::Type typeSrc = notetranslator().find(source);
395 InsetNoteParams::Type typeTrt = notetranslator().find(target);
396 // syntax check of arguments
397 string src = notetranslator().find(typeSrc);
398 string trt = notetranslator().find(typeTrt);
399 if (src != source || trt != target)
402 // did we found some conforming inset?
405 cur.beginUndoGroup();
406 Inset & inset = cur.buffer()->inset();
407 InsetIterator it = inset_iterator_begin(inset);
408 InsetIterator const end = inset_iterator_end(inset);
409 for (; it != end; ++it) {
410 if (it->lyxCode() == NOTE_CODE) {
411 InsetNote & ins = static_cast<InsetNote &>(*it);
412 if (ins.params().type == typeSrc) {
413 cur.buffer()->undo().recordUndo(it);
414 FuncRequest fr(LFUN_INSET_MODIFY, "note Note " + target);
415 ins.dispatch(cur, fr);