]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Refactor OutputParams
[features.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 "DispatchResult.h"
23 #include "Exporter.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "LyXRC.h"
29 #include "output_docbook.h"
30 #include "TextClass.h"
31 #include "TocBackend.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::Note)
211                 return;
212
213         OutputParams runparams(runparams_in);
214         if (params_.type == InsetNoteParams::Comment) {
215                 runparams.inComment = true;
216                 // Ignore files that are exported inside a comment
217                 runparams.exportdata.reset(new ExportData);
218         }
219
220         // the space after the comment in 'a[comment] b' will be eaten by the
221         // comment environment since the space before b is ignored with the
222         // following latex output:
223         //
224         // a%
225         // \begin{comment}
226         // comment
227         // \end{comment}
228         //  b
229         //
230         // Adding {} before ' b' fixes this.
231         // The {} will be automatically added, but only if needed, for all
232         // insets whose InsetLayout Display tag is false. This is achieved
233         // by telling otexstream to protect an immediately following space
234         // and is done for both comment and greyedout insets.
235         InsetCollapsible::latex(os, runparams);
236
237         runparams_in.encoding = runparams.encoding;
238 }
239
240
241 int InsetNote::plaintext(odocstringstream & os,
242                          OutputParams const & runparams_in, size_t max_length) const
243 {
244         if (params_.type == InsetNoteParams::Note)
245                 return 0;
246
247         OutputParams runparams(runparams_in);
248         if (params_.type == InsetNoteParams::Comment) {
249                 runparams.inComment = true;
250                 // Ignore files that are exported inside a comment
251                 runparams.exportdata.reset(new ExportData);
252         }
253         os << '[' << buffer().B_("note") << ":\n";
254         InsetText::plaintext(os, runparams, max_length);
255         os << "\n]";
256
257         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
258 }
259
260
261 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
262 {
263         if (params_.type == InsetNoteParams::Note)
264                 return;
265
266         OutputParams runparams(runparams_in);
267         if (params_.type == InsetNoteParams::Comment) {
268                 xs << xml::StartTag("remark");
269                 xs << xml::CR();
270                 runparams.inComment = true;
271                 // Ignore files that are exported inside a comment
272                 runparams.exportdata.reset(new ExportData);
273         }
274         // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
275
276         InsetText::docbook(xs, runparams);
277
278         if (params_.type == InsetNoteParams::Comment) {
279                 xs << xml::CR();
280                 xs << xml::EndTag("remark");
281                 xs << xml::CR();
282         }
283 }
284
285
286 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
287 {
288         if (params_.type == InsetNoteParams::Note)
289                 return docstring();
290
291         return InsetCollapsible::xhtml(xs, rp);
292 }
293
294
295 void InsetNote::validate(LaTeXFeatures & features) const
296 {
297         switch (params_.type) {
298         case InsetNoteParams::Comment:
299                 if (features.runparams().flavor == FLAVOR::HTML)
300                         // we do output this but set display to "none" by default,
301                         // but people might want to use it.
302                         InsetCollapsible::validate(features);
303                 else
304                         // Only do the requires
305                         features.useInsetLayout(getLayout());
306                 break;
307         case InsetNoteParams::Greyedout:
308                 if (features.hasRTLLanguage())
309                         features.require("environ");
310                 InsetCollapsible::validate(features);
311                 break;
312         case InsetNoteParams::Note:
313                 break;
314         }
315 }
316
317
318 string InsetNote::contextMenuName() const
319 {
320         return "context-note";
321 }
322
323 bool InsetNote::allowSpellCheck() const
324 {
325         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
326 }
327
328 FontInfo InsetNote::getFont() const
329 {
330         FontInfo font = getLayout().font();
331         // FIXME: This hardcoded color is a hack!
332         if (params_.type == InsetNoteParams::Greyedout
333             && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
334                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
335                 if (c != Color_none)
336                         font.setColor(c);
337         }
338         return font;
339 }
340
341
342 string InsetNote::params2string(InsetNoteParams const & params)
343 {
344         ostringstream data;
345         data << "note" << ' ';
346         params.write(data);
347         return data.str();
348 }
349
350
351 void InsetNote::string2params(string const & in, InsetNoteParams & params)
352 {
353         params = InsetNoteParams();
354
355         if (in.empty())
356                 return;
357
358         istringstream data(in);
359         Lexer lex;
360         lex.setStream(data);
361         lex.setContext("InsetNote::string2params");
362         lex >> "note";
363         // There are cases, such as when we are called via getStatus() from
364         // Dialog::canApply(), where we are just called with "note" rather
365         // than a full "note Note TYPE".
366         if (!lex.isOK())
367                 return;
368         lex >> "Note";
369
370         params.read(lex);
371 }
372
373
374 } // namespace lyx