]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
InsetXXX.cpp: remove unused and duplicated includes
[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 "InsetIterator.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "LyXRC.h"
30 #include "OutputParams.h"
31 #include "ParIterator.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
34
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"
40
41 #include "frontends/Application.h"
42
43 #include <algorithm>
44 #include <sstream>
45
46 using namespace std;
47
48 namespace lyx {
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 Inset::DisplayType InsetNote::display() const
120 {
121         return Inline;
122 }
123
124
125 void InsetNote::write(ostream & os) const
126 {
127         params_.write(os);
128         InsetCollapsible::write(os);
129 }
130
131
132 void InsetNote::read(Lexer & lex)
133 {
134         params_.read(lex);
135         InsetCollapsible::read(lex);
136 }
137
138
139 bool InsetNote::showInsetDialog(BufferView * bv) const
140 {
141         bv->showDialog("note", params2string(params()),
142                 const_cast<InsetNote *>(this));
143         return true;
144 }
145
146
147 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
148 {
149         switch (cmd.action()) {
150
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)
161                   break;
162
163                 cur.recordUndoInset(this);
164                 string2params(to_utf8(cmd.argument()), params_);
165                 setButtonLabel();
166                 // what we really want here is a TOC update, but that means
167                 // a full buffer update
168                 cur.forceBufferUpdate();
169                 break;
170         }
171
172         case LFUN_INSET_DIALOG_UPDATE:
173                 cur.bv().updateDialog("note", params2string(params()));
174                 break;
175
176         default:
177                 InsetCollapsible::doDispatch(cur, cmd);
178                 break;
179         }
180 }
181
182
183 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
184                 FuncStatus & flag) const
185 {
186         switch (cmd.action()) {
187
188         case LFUN_INSET_MODIFY:
189                 // disallow comment and greyed out in commands
190                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
191                                 cmd.getArg(2) == "Note");
192                 if (cmd.getArg(0) == "note") {
193                         InsetNoteParams params;
194                         string2params(to_utf8(cmd.argument()), params);
195                         flag.setOnOff(params_.type == params.type);
196                 }
197                 return true;
198
199         case LFUN_INSET_DIALOG_UPDATE:
200                 flag.setEnabled(true);
201                 return true;
202
203         default:
204                 return InsetCollapsible::getStatus(cur, cmd, flag);
205         }
206 }
207
208
209 bool InsetNote::isMacroScope() const
210 {
211         // LyX note has no latex output
212         if (params_.type == InsetNoteParams::Note)
213                 return true;
214
215         return InsetCollapsible::isMacroScope();
216 }
217
218
219 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
220 {
221         if (params_.type == InsetNoteParams::Note)
222                 return;
223
224         OutputParams runparams(runparams_in);
225         if (params_.type == InsetNoteParams::Comment) {
226                 runparams.inComment = true;
227                 // Ignore files that are exported inside a comment
228                 runparams.exportdata.reset(new ExportData);
229         }
230
231         // the space after the comment in 'a[comment] b' will be eaten by the
232         // comment environment since the space before b is ignored with the
233         // following latex output:
234         //
235         // a%
236         // \begin{comment}
237         // comment
238         // \end{comment}
239         //  b
240         //
241         // Adding {} before ' b' fixes this.
242         // The {} will be automatically added, but only if needed, for all
243         // insets whose InsetLayout Display tag is false. This is achieved
244         // by telling otexstream to protect an immediately following space
245         // and is done for both comment and greyedout insets.
246         InsetCollapsible::latex(os, runparams);
247
248         runparams_in.encoding = runparams.encoding;
249 }
250
251
252 int InsetNote::plaintext(odocstringstream & os,
253                          OutputParams const & runparams_in, size_t max_length) const
254 {
255         if (params_.type == InsetNoteParams::Note)
256                 return 0;
257
258         OutputParams runparams(runparams_in);
259         if (params_.type == InsetNoteParams::Comment) {
260                 runparams.inComment = true;
261                 // Ignore files that are exported inside a comment
262                 runparams.exportdata.reset(new ExportData);
263         }
264         os << '[' << buffer().B_("note") << ":\n";
265         InsetText::plaintext(os, runparams, max_length);
266         os << "\n]";
267
268         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
269 }
270
271
272 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
273 {
274         if (params_.type == InsetNoteParams::Note)
275                 return 0;
276
277         OutputParams runparams(runparams_in);
278         if (params_.type == InsetNoteParams::Comment) {
279                 os << "<remark>\n";
280                 runparams.inComment = true;
281                 // Ignore files that are exported inside a comment
282                 runparams.exportdata.reset(new ExportData);
283         }
284
285         int const n = InsetText::docbook(os, runparams);
286
287         if (params_.type == InsetNoteParams::Comment)
288                 os << "\n</remark>\n";
289
290         // Return how many newlines we issued.
291         //return int(count(str.begin(), str.end(), '\n'));
292         return n + 1 + 2;
293 }
294
295
296 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
297 {
298         if (params_.type == InsetNoteParams::Note)
299                 return docstring();
300
301         return InsetCollapsible::xhtml(xs, rp);
302 }
303
304
305 void InsetNote::validate(LaTeXFeatures & features) const
306 {
307         switch (params_.type) {
308         case InsetNoteParams::Comment:
309                 if (features.runparams().flavor == OutputParams::HTML)
310                         // we do output this but set display to "none" by default,
311                         // but people might want to use it.
312                         InsetCollapsible::validate(features);
313                 else
314                         // Only do the requires
315                         features.useInsetLayout(getLayout());
316                 break;
317         case InsetNoteParams::Greyedout:
318                 InsetCollapsible::validate(features);
319                 break;
320         case InsetNoteParams::Note:
321                 break;
322         }
323 }
324
325
326 string InsetNote::contextMenuName() const
327 {
328         return "context-note";
329 }
330
331 bool InsetNote::allowSpellCheck() const
332 {
333         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
334 }
335
336 FontInfo InsetNote::getFont() const
337 {
338         FontInfo font = getLayout().font();
339         // FIXME: This hardcoded color is a hack!
340         if (params_.type == InsetNoteParams::Greyedout
341             && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
342                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
343                 if (c != Color_none)
344                         font.setColor(c);
345         }
346         return font;
347 }
348
349
350 string InsetNote::params2string(InsetNoteParams const & params)
351 {
352         ostringstream data;
353         data << "note" << ' ';
354         params.write(data);
355         return data.str();
356 }
357
358
359 void InsetNote::string2params(string const & in, InsetNoteParams & params)
360 {
361         params = InsetNoteParams();
362
363         if (in.empty())
364                 return;
365
366         istringstream data(in);
367         Lexer lex;
368         lex.setStream(data);
369         lex.setContext("InsetNote::string2params");
370         lex >> "note";
371         // There are cases, such as when we are called via getStatus() from
372         // Dialog::canApply(), where we are just called with "note" rather
373         // than a full "note Note TYPE".
374         if (!lex.isOK())
375                 return;
376         lex >> "Note";
377
378         params.read(lex);
379 }
380
381
382 } // namespace lyx