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