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