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