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