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