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