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 "InsetIterator.h"
27 #include "LaTeXFeatures.h"
30 #include "OutputParams.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 Inset::DisplayType InsetNote::display() const
125 void InsetNote::write(ostream & os) const
128 InsetCollapsible::write(os);
132 void InsetNote::read(Lexer & lex)
135 InsetCollapsible::read(lex);
139 bool InsetNote::showInsetDialog(BufferView * bv) const
141 bv->showDialog("note", params2string(params()),
142 const_cast<InsetNote *>(this));
147 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
149 switch (cmd.action()) {
151 case LFUN_INSET_MODIFY: {
152 // Do not do anything if converting to the same type of Note.
153 // A quick break here is done instead of disabling the LFUN
154 // because disabling the LFUN would lead to a greyed out
155 // entry, which might confuse users.
156 // In the future, we might want to have a radio button for
157 // switching between notes.
158 InsetNoteParams params;
159 string2params(to_utf8(cmd.argument()), params);
160 if (params_.type == params.type)
163 cur.recordUndoInset(this);
164 string2params(to_utf8(cmd.argument()), params_);
166 // what we really want here is a TOC update, but that means
167 // a full buffer update
168 cur.forceBufferUpdate();
172 case LFUN_INSET_DIALOG_UPDATE:
173 cur.bv().updateDialog("note", params2string(params()));
177 InsetCollapsible::doDispatch(cur, cmd);
183 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
184 FuncStatus & flag) const
186 switch (cmd.action()) {
188 case LFUN_INSET_MODIFY:
189 if (cmd.getArg(0) == "note") {
190 InsetNoteParams params;
191 string2params(to_utf8(cmd.argument()), params);
192 flag.setOnOff(params_.type == params.type);
196 case LFUN_INSET_DIALOG_UPDATE:
197 flag.setEnabled(true);
201 return InsetCollapsible::getStatus(cur, cmd, flag);
206 bool InsetNote::isMacroScope() const
208 // LyX note has no latex output
209 if (params_.type == InsetNoteParams::Note)
212 return InsetCollapsible::isMacroScope();
216 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
218 if (params_.type == InsetNoteParams::Note)
221 OutputParams runparams(runparams_in);
222 if (params_.type == InsetNoteParams::Comment) {
223 runparams.inComment = true;
224 // Ignore files that are exported inside a comment
225 runparams.exportdata.reset(new ExportData);
228 // the space after the comment in 'a[comment] b' will be eaten by the
229 // comment environment since the space before b is ignored with the
230 // following latex output:
238 // Adding {} before ' b' fixes this.
239 // The {} will be automatically added, but only if needed, for all
240 // insets whose InsetLayout Display tag is false. This is achieved
241 // by telling otexstream to protect an immediately following space
242 // and is done for both comment and greyedout insets.
243 InsetCollapsible::latex(os, runparams);
245 runparams_in.encoding = runparams.encoding;
249 int InsetNote::plaintext(odocstringstream & os,
250 OutputParams const & runparams_in, size_t max_length) const
252 if (params_.type == InsetNoteParams::Note)
255 OutputParams runparams(runparams_in);
256 if (params_.type == InsetNoteParams::Comment) {
257 runparams.inComment = true;
258 // Ignore files that are exported inside a comment
259 runparams.exportdata.reset(new ExportData);
261 os << '[' << buffer().B_("note") << ":\n";
262 InsetText::plaintext(os, runparams, max_length);
265 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
269 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
271 if (params_.type == InsetNoteParams::Note)
274 OutputParams runparams(runparams_in);
275 if (params_.type == InsetNoteParams::Comment) {
277 runparams.inComment = true;
278 // Ignore files that are exported inside a comment
279 runparams.exportdata.reset(new ExportData);
282 int const n = InsetText::docbook(os, runparams);
284 if (params_.type == InsetNoteParams::Comment)
285 os << "\n</remark>\n";
287 // Return how many newlines we issued.
288 //return int(count(str.begin(), str.end(), '\n'));
293 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
295 if (params_.type == InsetNoteParams::Note)
298 return InsetCollapsible::xhtml(xs, rp);
302 void InsetNote::validate(LaTeXFeatures & features) const
304 switch (params_.type) {
305 case InsetNoteParams::Comment:
306 if (features.runparams().flavor == OutputParams::HTML)
307 // we do output this but set display to "none" by default,
308 // but people might want to use it.
309 InsetCollapsible::validate(features);
311 // Only do the requires
312 features.useInsetLayout(getLayout());
314 case InsetNoteParams::Greyedout:
315 InsetCollapsible::validate(features);
317 case InsetNoteParams::Note:
323 string InsetNote::contextMenuName() const
325 return "context-note";
328 bool InsetNote::allowSpellCheck() const
330 return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
333 FontInfo InsetNote::getFont() const
335 FontInfo font = getLayout().font();
336 // FIXME: This hardcoded color is a hack!
337 if (params_.type == InsetNoteParams::Greyedout
338 && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
339 ColorCode c = lcolor.getFromLyXName("notefontcolor");
347 string InsetNote::params2string(InsetNoteParams const & params)
350 data << "note" << ' ';
356 void InsetNote::string2params(string const & in, InsetNoteParams & params)
358 params = InsetNoteParams();
363 istringstream data(in);
366 lex.setContext("InsetNote::string2params");
368 // There are cases, such as when we are called via getStatus() from
369 // Dialog::canApply(), where we are just called with "note" rather
370 // than a full "note Note TYPE".