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"
22 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "OutputParams.h"
30 #include "output_docbook.h"
31 #include "ParIterator.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/Translator.h"
41 #include "frontends/Application.h"
52 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
54 NoteTranslator const init_notetranslator()
56 NoteTranslator translator("Note", InsetNoteParams::Note);
57 translator.addPair("Comment", InsetNoteParams::Comment);
58 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
63 NoteTranslator const & notetranslator()
65 static NoteTranslator const translator = init_notetranslator();
73 InsetNoteParams::InsetNoteParams()
78 void InsetNoteParams::write(ostream & os) const
80 string const label = notetranslator().find(type);
81 os << "Note " << label << "\n";
85 void InsetNoteParams::read(Lexer & lex)
90 type = notetranslator().find(label);
94 /////////////////////////////////////////////////////////////////////
98 /////////////////////////////////////////////////////////////////////
100 InsetNote::InsetNote(Buffer * buf, string const & label)
101 : InsetCollapsible(buf)
103 params_.type = notetranslator().find(label);
107 InsetNote::~InsetNote()
109 hideDialogs("note", this);
113 docstring InsetNote::layoutName() const
115 return from_ascii("Note:" + notetranslator().find(params_.type));
119 void InsetNote::write(ostream & os) const
122 InsetCollapsible::write(os);
126 void InsetNote::read(Lexer & lex)
129 InsetCollapsible::read(lex);
133 bool InsetNote::showInsetDialog(BufferView * bv) const
135 bv->showDialog("note", params2string(params()),
136 const_cast<InsetNote *>(this));
141 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
143 switch (cmd.action()) {
145 case LFUN_INSET_MODIFY: {
146 // Do not do anything if converting to the same type of Note.
147 // A quick break here is done instead of disabling the LFUN
148 // because disabling the LFUN would lead to a greyed out
149 // entry, which might confuse users.
150 // In the future, we might want to have a radio button for
151 // switching between notes.
152 InsetNoteParams params;
153 string2params(to_utf8(cmd.argument()), params);
154 if (params_.type == params.type)
157 cur.recordUndoInset(this);
158 string2params(to_utf8(cmd.argument()), params_);
160 // what we really want here is a TOC update, but that means
161 // a full buffer update
162 cur.forceBufferUpdate();
166 case LFUN_INSET_DIALOG_UPDATE:
167 cur.bv().updateDialog("note", params2string(params()));
171 InsetCollapsible::doDispatch(cur, cmd);
177 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
178 FuncStatus & flag) const
180 switch (cmd.action()) {
182 case LFUN_INSET_MODIFY:
183 if (cmd.getArg(0) == "note") {
184 InsetNoteParams params;
185 string2params(to_utf8(cmd.argument()), params);
186 flag.setOnOff(params_.type == params.type);
190 case LFUN_INSET_DIALOG_UPDATE:
191 flag.setEnabled(true);
195 return InsetCollapsible::getStatus(cur, cmd, flag);
200 bool InsetNote::isMacroScope() const
202 // LyX note has no latex output
203 if (params_.type == InsetNoteParams::Note)
206 return InsetCollapsible::isMacroScope();
210 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
212 if (params_.type == InsetNoteParams::Note)
215 OutputParams runparams(runparams_in);
216 if (params_.type == InsetNoteParams::Comment) {
217 runparams.inComment = true;
218 // Ignore files that are exported inside a comment
219 runparams.exportdata.reset(new ExportData);
222 // the space after the comment in 'a[comment] b' will be eaten by the
223 // comment environment since the space before b is ignored with the
224 // following latex output:
232 // Adding {} before ' b' fixes this.
233 // The {} will be automatically added, but only if needed, for all
234 // insets whose InsetLayout Display tag is false. This is achieved
235 // by telling otexstream to protect an immediately following space
236 // and is done for both comment and greyedout insets.
237 InsetCollapsible::latex(os, runparams);
239 runparams_in.encoding = runparams.encoding;
243 int InsetNote::plaintext(odocstringstream & os,
244 OutputParams const & runparams_in, size_t max_length) const
246 if (params_.type == InsetNoteParams::Note)
249 OutputParams runparams(runparams_in);
250 if (params_.type == InsetNoteParams::Comment) {
251 runparams.inComment = true;
252 // Ignore files that are exported inside a comment
253 runparams.exportdata.reset(new ExportData);
255 os << '[' << buffer().B_("note") << ":\n";
256 InsetText::plaintext(os, runparams, max_length);
259 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
263 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
265 if (params_.type == InsetNoteParams::Note)
268 OutputParams runparams(runparams_in);
269 if (params_.type == InsetNoteParams::Comment) {
270 xs << xml::StartTag("remark");
272 runparams.inComment = true;
273 // Ignore files that are exported inside a comment
274 runparams.exportdata.reset(new ExportData);
276 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
278 InsetText::docbook(xs, runparams);
280 if (params_.type == InsetNoteParams::Comment) {
282 xs << xml::EndTag("remark");
288 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
290 if (params_.type == InsetNoteParams::Note)
293 return InsetCollapsible::xhtml(xs, rp);
297 void InsetNote::validate(LaTeXFeatures & features) const
299 switch (params_.type) {
300 case InsetNoteParams::Comment:
301 if (features.runparams().flavor == OutputParams::HTML)
302 // we do output this but set display to "none" by default,
303 // but people might want to use it.
304 InsetCollapsible::validate(features);
306 // Only do the requires
307 features.useInsetLayout(getLayout());
309 case InsetNoteParams::Greyedout:
310 if (features.hasRTLLanguage())
311 features.require("environ");
312 InsetCollapsible::validate(features);
314 case InsetNoteParams::Note:
320 string InsetNote::contextMenuName() const
322 return "context-note";
325 bool InsetNote::allowSpellCheck() const
327 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
330 FontInfo InsetNote::getFont() const
332 FontInfo font = getLayout().font();
333 // FIXME: This hardcoded color is a hack!
334 if (params_.type == InsetNoteParams::Greyedout
335 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
336 ColorCode c = lcolor.getFromLyXName("notefontcolor");
344 string InsetNote::params2string(InsetNoteParams const & params)
347 data << "note" << ' ';
353 void InsetNote::string2params(string const & in, InsetNoteParams & params)
355 params = InsetNoteParams();
360 istringstream data(in);
363 lex.setContext("InsetNote::string2params");
365 // There are cases, such as when we are called via getStatus() from
366 // Dialog::canApply(), where we are just called with "note" rather
367 // than a full "note Note TYPE".