]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Cleanup included headers
[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 "OutputParams.h"
30 #include "output_docbook.h"
31 #include "TextClass.h"
32 #include "TocBackend.h"
33
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"
39
40 #include "frontends/Application.h"
41
42 #include <algorithm>
43 #include <sstream>
44
45 using namespace std;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
52
53 NoteTranslator const init_notetranslator()
54 {
55         NoteTranslator translator("Note", InsetNoteParams::Note);
56         translator.addPair("Comment", InsetNoteParams::Comment);
57         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
58         return translator;
59 }
60
61
62 NoteTranslator const & notetranslator()
63 {
64         static NoteTranslator const translator = init_notetranslator();
65         return translator;
66 }
67
68
69 } // namespace
70
71
72 InsetNoteParams::InsetNoteParams()
73         : type(Note)
74 {}
75
76
77 void InsetNoteParams::write(ostream & os) const
78 {
79         string const label = notetranslator().find(type);
80         os << "Note " << label << "\n";
81 }
82
83
84 void InsetNoteParams::read(Lexer & lex)
85 {
86         string label;
87         lex >> label;
88         if (lex)
89                 type = notetranslator().find(label);
90 }
91
92
93 /////////////////////////////////////////////////////////////////////
94 //
95 // InsetNote
96 //
97 /////////////////////////////////////////////////////////////////////
98
99 InsetNote::InsetNote(Buffer * buf, string const & label)
100         : InsetCollapsible(buf)
101 {
102         params_.type = notetranslator().find(label);
103 }
104
105
106 InsetNote::~InsetNote()
107 {
108         hideDialogs("note", this);
109 }
110
111
112 docstring InsetNote::layoutName() const
113 {
114         return from_ascii("Note:" + notetranslator().find(params_.type));
115 }
116
117
118 void InsetNote::write(ostream & os) const
119 {
120         params_.write(os);
121         InsetCollapsible::write(os);
122 }
123
124
125 void InsetNote::read(Lexer & lex)
126 {
127         params_.read(lex);
128         InsetCollapsible::read(lex);
129 }
130
131
132 bool InsetNote::showInsetDialog(BufferView * bv) const
133 {
134         bv->showDialog("note", params2string(params()),
135                 const_cast<InsetNote *>(this));
136         return true;
137 }
138
139
140 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
141 {
142         switch (cmd.action()) {
143
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)
154                   break;
155
156                 cur.recordUndoInset(this);
157                 string2params(to_utf8(cmd.argument()), params_);
158                 setButtonLabel();
159                 // what we really want here is a TOC update, but that means
160                 // a full buffer update
161                 cur.forceBufferUpdate();
162                 break;
163         }
164
165         case LFUN_INSET_DIALOG_UPDATE:
166                 cur.bv().updateDialog("note", params2string(params()));
167                 break;
168
169         default:
170                 InsetCollapsible::doDispatch(cur, cmd);
171                 break;
172         }
173 }
174
175
176 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
177                 FuncStatus & flag) const
178 {
179         switch (cmd.action()) {
180
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);
186                 }
187                 return true;
188
189         case LFUN_INSET_DIALOG_UPDATE:
190                 flag.setEnabled(true);
191                 return true;
192
193         default:
194                 return InsetCollapsible::getStatus(cur, cmd, flag);
195         }
196 }
197
198
199 bool InsetNote::isMacroScope() const
200 {
201         // LyX note has no latex output
202         if (params_.type == InsetNoteParams::Note)
203                 return true;
204
205         return InsetCollapsible::isMacroScope();
206 }
207
208
209 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
210 {
211         if (params_.type == InsetNoteParams::Note)
212                 return;
213
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);
219         }
220
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:
224         //
225         // a%
226         // \begin{comment}
227         // comment
228         // \end{comment}
229         //  b
230         //
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);
237
238         runparams_in.encoding = runparams.encoding;
239 }
240
241
242 int InsetNote::plaintext(odocstringstream & os,
243                          OutputParams const & runparams_in, size_t max_length) const
244 {
245         if (params_.type == InsetNoteParams::Note)
246                 return 0;
247
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);
253         }
254         os << '[' << buffer().B_("note") << ":\n";
255         InsetText::plaintext(os, runparams, max_length);
256         os << "\n]";
257
258         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
259 }
260
261
262 void InsetNote::docbook(XMLStream & xs, OutputParams const & runparams_in) const
263 {
264         if (params_.type == InsetNoteParams::Note)
265                 return;
266
267         OutputParams runparams(runparams_in);
268         if (params_.type == InsetNoteParams::Comment) {
269                 xs << xml::StartTag("remark");
270                 xs << xml::CR();
271                 runparams.inComment = true;
272                 // Ignore files that are exported inside a comment
273                 runparams.exportdata.reset(new ExportData);
274         }
275         // Greyed out text is output as such (no way to mark text as greyed out with DocBook).
276
277         InsetText::docbook(xs, runparams);
278
279         if (params_.type == InsetNoteParams::Comment) {
280                 xs << xml::CR();
281                 xs << xml::EndTag("remark");
282                 xs << xml::CR();
283         }
284 }
285
286
287 docstring InsetNote::xhtml(XMLStream & xs, OutputParams const & rp) const
288 {
289         if (params_.type == InsetNoteParams::Note)
290                 return docstring();
291
292         return InsetCollapsible::xhtml(xs, rp);
293 }
294
295
296 void InsetNote::validate(LaTeXFeatures & features) const
297 {
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);
304                 else
305                         // Only do the requires
306                         features.useInsetLayout(getLayout());
307                 break;
308         case InsetNoteParams::Greyedout:
309                 if (features.hasRTLLanguage())
310                         features.require("environ");
311                 InsetCollapsible::validate(features);
312                 break;
313         case InsetNoteParams::Note:
314                 break;
315         }
316 }
317
318
319 string InsetNote::contextMenuName() const
320 {
321         return "context-note";
322 }
323
324 bool InsetNote::allowSpellCheck() const
325 {
326         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
327 }
328
329 FontInfo InsetNote::getFont() const
330 {
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");
336                 if (c != Color_none)
337                         font.setColor(c);
338         }
339         return font;
340 }
341
342
343 string InsetNote::params2string(InsetNoteParams const & params)
344 {
345         ostringstream data;
346         data << "note" << ' ';
347         params.write(data);
348         return data.str();
349 }
350
351
352 void InsetNote::string2params(string const & in, InsetNoteParams & params)
353 {
354         params = InsetNoteParams();
355
356         if (in.empty())
357                 return;
358
359         istringstream data(in);
360         Lexer lex;
361         lex.setStream(data);
362         lex.setContext("InsetNote::string2params");
363         lex >> "note";
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".
367         if (!lex.isOK())
368                 return;
369         lex >> "Note";
370
371         params.read(lex);
372 }
373
374
375 } // namespace lyx