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 "TextClass.h"
32 #include "TocBackend.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/Translator.h"
40 #include "frontends/Application.h"
51 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
53 NoteTranslator const init_notetranslator()
55 NoteTranslator translator("Note", InsetNoteParams::Note);
56 translator.addPair("Comment", InsetNoteParams::Comment);
57 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
62 NoteTranslator const & notetranslator()
64 static NoteTranslator const translator = init_notetranslator();
72 InsetNoteParams::InsetNoteParams()
77 void InsetNoteParams::write(ostream & os) const
79 string const label = notetranslator().find(type);
80 os << "Note " << label << "\n";
84 void InsetNoteParams::read(Lexer & lex)
89 type = notetranslator().find(label);
93 /////////////////////////////////////////////////////////////////////
97 /////////////////////////////////////////////////////////////////////
99 InsetNote::InsetNote(Buffer * buf, string const & label)
100 : InsetCollapsible(buf)
102 params_.type = notetranslator().find(label);
106 InsetNote::~InsetNote()
108 hideDialogs("note", this);
112 docstring InsetNote::layoutName() const
114 return from_ascii("Note:" + notetranslator().find(params_.type));
118 void InsetNote::write(ostream & os) const
121 InsetCollapsible::write(os);
125 void InsetNote::read(Lexer & lex)
128 InsetCollapsible::read(lex);
132 bool InsetNote::showInsetDialog(BufferView * bv) const
134 bv->showDialog("note", params2string(params()),
135 const_cast<InsetNote *>(this));
140 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
142 switch (cmd.action()) {
144 case LFUN_INSET_MODIFY: {
145 // Do not do anything if converting to the same type of Note.
146 // A quick break here is done instead of disabling the LFUN
147 // because disabling the LFUN would lead to a greyed out
148 // entry, which might confuse users.
149 // In the future, we might want to have a radio button for
150 // switching between notes.
151 InsetNoteParams params;
152 string2params(to_utf8(cmd.argument()), params);
153 if (params_.type == params.type)
156 cur.recordUndoInset(this);
157 string2params(to_utf8(cmd.argument()), params_);
159 // what we really want here is a TOC update, but that means
160 // a full buffer update
161 cur.forceBufferUpdate();
165 case LFUN_INSET_DIALOG_UPDATE:
166 cur.bv().updateDialog("note", params2string(params()));
170 InsetCollapsible::doDispatch(cur, cmd);
176 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
177 FuncStatus & flag) const
179 switch (cmd.action()) {
181 case LFUN_INSET_MODIFY:
182 if (cmd.getArg(0) == "note") {
183 InsetNoteParams params;
184 string2params(to_utf8(cmd.argument()), params);
185 flag.setOnOff(params_.type == params.type);
189 case LFUN_INSET_DIALOG_UPDATE:
190 flag.setEnabled(true);
194 return InsetCollapsible::getStatus(cur, cmd, flag);
199 bool InsetNote::isMacroScope() const
201 // LyX note has no latex output
202 if (params_.type == InsetNoteParams::Note)
205 return InsetCollapsible::isMacroScope();
209 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
211 if (params_.type == InsetNoteParams::Note)
214 OutputParams runparams(runparams_in);
215 if (params_.type == InsetNoteParams::Comment) {
216 runparams.inComment = true;
217 // Ignore files that are exported inside a comment
218 runparams.exportdata.reset(new ExportData);
221 // the space after the comment in 'a[comment] b' will be eaten by the
222 // comment environment since the space before b is ignored with the
223 // following latex output:
231 // Adding {} before ' b' fixes this.
232 // The {} will be automatically added, but only if needed, for all
233 // insets whose InsetLayout Display tag is false. This is achieved
234 // by telling otexstream to protect an immediately following space
235 // and is done for both comment and greyedout insets.
236 InsetCollapsible::latex(os, runparams);
238 runparams_in.encoding = runparams.encoding;
242 int InsetNote::plaintext(odocstringstream & os,
243 OutputParams const & runparams_in, size_t max_length) const
245 if (params_.type == InsetNoteParams::Note)
248 OutputParams runparams(runparams_in);
249 if (params_.type == InsetNoteParams::Comment) {
250 runparams.inComment = true;
251 // Ignore files that are exported inside a comment
252 runparams.exportdata.reset(new ExportData);
254 os << '[' << buffer().B_("note") << ":\n";
255 InsetText::plaintext(os, runparams, max_length);
258 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
262 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
264 if (params_.type == InsetNoteParams::Note)
267 OutputParams runparams(runparams_in);
268 if (params_.type == InsetNoteParams::Comment) {
269 xs << xml::StartTag("remark");
271 runparams.inComment = true;
272 // Ignore files that are exported inside a comment
273 runparams.exportdata.reset(new ExportData);
275 // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
277 InsetText::docbook(xs, runparams);
279 if (params_.type == InsetNoteParams::Comment) {
281 xs << xml::EndTag("remark");
287 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
289 if (params_.type == InsetNoteParams::Note)
292 return InsetCollapsible::xhtml(xs, rp);
296 void InsetNote::validate(LaTeXFeatures & features) const
298 switch (params_.type) {
299 case InsetNoteParams::Comment:
300 if (features.runparams().flavor == OutputParams::HTML)
301 // we do output this but set display to "none" by default,
302 // but people might want to use it.
303 InsetCollapsible::validate(features);
305 // Only do the requires
306 features.useInsetLayout(getLayout());
308 case InsetNoteParams::Greyedout:
309 if (features.hasRTLLanguage())
310 features.require("environ");
311 InsetCollapsible::validate(features);
313 case InsetNoteParams::Note:
319 string InsetNote::contextMenuName() const
321 return "context-note";
324 bool InsetNote::allowSpellCheck() const
326 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
329 FontInfo InsetNote::getFont() const
331 FontInfo font = getLayout().font();
332 // FIXME: This hardcoded color is a hack!
333 if (params_.type == InsetNoteParams::Greyedout
334 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
335 ColorCode c = lcolor.getFromLyXName("notefontcolor");
343 string InsetNote::params2string(InsetNoteParams const & params)
346 data << "note" << ' ';
352 void InsetNote::string2params(string const & in, InsetNoteParams & params)
354 params = InsetNoteParams();
359 istringstream data(in);
362 lex.setContext("InsetNote::string2params");
364 // There are cases, such as when we are called via getStatus() from
365 // Dialog::canApply(), where we are just called with "note" rather
366 // than a full "note Note TYPE".