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