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