]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
a7a0dc16abf0fad7856075ad085271d6f600d724
[lyx.git] / src / insets / InsetNote.cpp
1 /**
2  * \file InsetNote.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetNote.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "ColorSet.h"
21 #include "Cursor.h"
22 #include "Exporter.h"
23 #include "FontInfo.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "LyXRC.h"
30 #include "output_docbook.h"
31 #include "output_latex.h"
32
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/Translator.h"
38
39 #include "frontends/Application.h"
40
41 #include <algorithm>
42 #include <sstream>
43
44 using namespace std;
45
46 namespace lyx {
47
48 namespace {
49
50 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
51
52 NoteTranslator const init_notetranslator()
53 {
54         NoteTranslator translator("Note", InsetNoteParams::Note);
55         translator.addPair("Comment", InsetNoteParams::Comment);
56         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
57         return translator;
58 }
59
60
61 NoteTranslator const & notetranslator()
62 {
63         static NoteTranslator const translator = init_notetranslator();
64         return translator;
65 }
66
67
68 } // namespace
69
70
71 InsetNoteParams::InsetNoteParams()
72         : type(Note)
73 {}
74
75
76 void InsetNoteParams::write(ostream & os) const
77 {
78         string const label = notetranslator().find(type);
79         os << "Note " << label << "\n";
80 }
81
82
83 void InsetNoteParams::read(Lexer & lex)
84 {
85         string label;
86         lex >> label;
87         if (lex)
88                 type = notetranslator().find(label);
89 }
90
91
92 /////////////////////////////////////////////////////////////////////
93 //
94 // InsetNote
95 //
96 /////////////////////////////////////////////////////////////////////
97
98 InsetNote::InsetNote(Buffer * buf, string const & label)
99         : InsetCollapsible(buf)
100 {
101         params_.type = notetranslator().find(label);
102 }
103
104
105 InsetNote::~InsetNote()
106 {
107         hideDialogs("note", this);
108 }
109
110
111 docstring InsetNote::layoutName() const
112 {
113         return from_ascii("Note:" + notetranslator().find(params_.type));
114 }
115
116
117 void InsetNote::write(ostream & os) const
118 {
119         params_.write(os);
120         InsetCollapsible::write(os);
121 }
122
123
124 void InsetNote::read(Lexer & lex)
125 {
126         params_.read(lex);
127         InsetCollapsible::read(lex);
128 }
129
130
131 bool InsetNote::showInsetDialog(BufferView * bv) const
132 {
133         bv->showDialog("note", params2string(params()),
134                 const_cast<InsetNote *>(this));
135         return true;
136 }
137
138
139 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
140 {
141         switch (cmd.action()) {
142
143         case LFUN_INSET_MODIFY: {
144                 // Do not do anything if converting to the same type of Note.
145                 // A quick break here is done instead of disabling the LFUN
146                 // because disabling the LFUN would lead to a greyed out
147                 // entry, which might confuse users.
148                 // In the future, we might want to have a radio button for
149                 // switching between notes.
150                 InsetNoteParams params;
151                 string2params(to_utf8(cmd.argument()), params);
152                 if (params_.type == params.type)
153                   break;
154
155                 cur.recordUndoInset(this);
156                 string2params(to_utf8(cmd.argument()), params_);
157                 setButtonLabel();
158                 // what we really want here is a TOC update, but that means
159                 // a full buffer update
160                 cur.forceBufferUpdate();
161                 break;
162         }
163
164         case LFUN_INSET_DIALOG_UPDATE:
165                 cur.bv().updateDialog("note", params2string(params()));
166                 break;
167
168         default:
169                 InsetCollapsible::doDispatch(cur, cmd);
170                 break;
171         }
172 }
173
174
175 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
176                 FuncStatus & flag) const
177 {
178         switch (cmd.action()) {
179
180         case LFUN_INSET_MODIFY:
181                 if (cmd.getArg(0) == "note") {
182                         InsetNoteParams params;
183                         string2params(to_utf8(cmd.argument()), params);
184                         flag.setOnOff(params_.type == params.type);
185                 }
186                 return true;
187
188         case LFUN_INSET_DIALOG_UPDATE:
189                 flag.setEnabled(true);
190                 return true;
191
192         default:
193                 return InsetCollapsible::getStatus(cur, cmd, flag);
194         }
195 }
196
197
198 bool InsetNote::isMacroScope() const
199 {
200         // LyX note has no latex output
201         if (params_.type == InsetNoteParams::Note)
202                 return true;
203
204         return InsetCollapsible::isMacroScope();
205 }
206
207
208 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
209 {
210         if (params_.type != InsetNoteParams::Greyedout
211             && runparams_in.find_effective()
212             && !runparams_in.find_with_non_output())
213                 return;
214
215         if (params_.type == InsetNoteParams::Note) {
216                 if (runparams_in.find_with_non_output()) {
217                         OutputParams runparams(runparams_in);
218                         InsetCollapsible::latex(os, runparams);
219                         runparams_in.encoding = runparams.encoding;
220                 }
221                 return;
222         }
223
224         OutputParams runparams(runparams_in);
225         if (params_.type == InsetNoteParams::Comment) {
226                 if (runparams_in.inComment) {
227                         // Nested comments should just output the contents.
228                         latexParagraphs(buffer(), text(), os, runparams);
229                         return;
230                 }
231
232                 runparams.inComment = true;
233                 // Ignore files that are exported inside a comment
234                 runparams.exportdata.reset(new ExportData);
235         }
236
237         // the space after the comment in 'a[comment] b' will be eaten by the
238         // comment environment since the space before b is ignored with the
239         // following latex output:
240         //
241         // a%
242         // \begin{comment}
243         // comment
244         // \end{comment}
245         //  b
246         //
247         // Adding {} before ' b' fixes this.
248         // The {} will be automatically added, but only if needed, for all
249         // insets whose InsetLayout Display tag is false. This is achieved
250         // by telling otexstream to protect an immediately following space
251         // and is done for both comment and greyedout insets.
252         InsetCollapsible::latex(os, runparams);
253
254         runparams_in.encoding = runparams.encoding;
255 }
256
257
258 int InsetNote::plaintext(odocstringstream & os,
259                          OutputParams const & runparams_in, size_t max_length) const
260 {
261         if (!runparams_in.find_with_non_output()) {
262                 if (params_.type == InsetNoteParams::Note)
263                         return 0;
264                 else if (params_.type == InsetNoteParams::Comment
265                     && runparams_in.find_effective())
266                         return 0;
267         }
268
269         OutputParams runparams(runparams_in);
270         if (params_.type != InsetNoteParams::Greyedout) {
271                 runparams.inComment = true;
272                 // Ignore files that are exported inside a comment
273                 runparams.exportdata.reset(new ExportData);
274         }
275         if (!runparams_in.find_with_non_output())
276                 os << '[' << buffer().B_("note") << ":\n";
277         InsetText::plaintext(os, runparams, max_length);
278         if (!runparams_in.find_with_non_output())
279                 os << "\n]";
280
281         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
282 }
283
284
285 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
286 {
287         if (params_.type == InsetNoteParams::Note)
288                 return;
289
290         OutputParams runparams(runparams_in);
291         if (params_.type == InsetNoteParams::Comment) {
292                 xs << xml::StartTag("remark");
293                 xs << xml::CR();
294                 runparams.inComment = true;
295                 // Ignore files that are exported inside a comment
296                 runparams.exportdata.reset(new ExportData);
297         }
298         // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
299
300         InsetText::docbook(xs, runparams);
301
302         if (params_.type == InsetNoteParams::Comment) {
303                 xs << xml::CR();
304                 xs << xml::EndTag("remark");
305                 xs << xml::CR();
306         }
307 }
308
309
310 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
311 {
312         if (params_.type == InsetNoteParams::Note)
313                 return docstring();
314
315         return InsetCollapsible::xhtml(xs, rp);
316 }
317
318
319 void InsetNote::validate(LaTeXFeatures & features) const
320 {
321         switch (params_.type) {
322         case InsetNoteParams::Comment:
323                 if (features.runparams().flavor == Flavor::Html)
324                         // we do output this but set display to "none" by default,
325                         // but people might want to use it.
326                         InsetCollapsible::validate(features);
327                 else
328                         // Only do the requires
329                         features.useInsetLayout(getLayout());
330                 break;
331         case InsetNoteParams::Greyedout:
332                 if (features.hasRTLLanguage())
333                         features.require("environ");
334                 InsetCollapsible::validate(features);
335                 break;
336         case InsetNoteParams::Note:
337                 break;
338         }
339 }
340
341
342 string InsetNote::contextMenuName() const
343 {
344         return "context-note";
345 }
346
347 bool InsetNote::allowSpellCheck() const
348 {
349         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
350 }
351
352 FontInfo InsetNote::getFont() const
353 {
354         FontInfo font = getLayout().font();
355         if (params_.type == InsetNoteParams::Greyedout
356             && buffer().params().isnotefontcolor) {
357                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
358                 if (c != Color_none)
359                         font.setColor(c);
360                 // This is the local color (not overridden by other documents)
361                 ColorCode lc = lcolor.getFromLyXName("notefontcolor@" + buffer().fileName().absFileName());
362                 if (lc != Color_none)
363                         font.setPaintColor(lc);
364         }
365         return font;
366 }
367
368
369 string InsetNote::params2string(InsetNoteParams const & params)
370 {
371         ostringstream data;
372         data << "note" << ' ';
373         params.write(data);
374         return data.str();
375 }
376
377
378 void InsetNote::string2params(string const & in, InsetNoteParams & params)
379 {
380         params = InsetNoteParams();
381
382         if (in.empty())
383                 return;
384
385         istringstream data(in);
386         Lexer lex;
387         lex.setStream(data);
388         lex.setContext("InsetNote::string2params");
389         lex >> "note";
390         // There are cases, such as when we are called via getStatus() from
391         // Dialog::canApply(), where we are just called with "note" rather
392         // than a full "note Note TYPE".
393         if (!lex.isOK())
394                 return;
395         lex >> "Note";
396
397         params.read(lex);
398 }
399
400
401 } // namespace lyx