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