]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Rewrite the label numbering code.
[features.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 "Counters.h"
21 #include "Cursor.h"
22 #include "debug.h"
23 #include "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "MetricsInfo.h"
31 #include "OutputParams.h"
32
33 #include "support/Translator.h"
34
35 #include <algorithm>
36 #include <sstream>
37
38
39 namespace lyx {
40
41 using std::string;
42 using std::auto_ptr;
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 void InsetNote::init()
115 {
116         setButtonLabel();
117 }
118
119
120 InsetNote::InsetNote(BufferParams const & bp, string const & label)
121         : InsetCollapsable(bp)
122 {
123         params_.type = notetranslator().find(label);
124         init();
125 }
126
127
128 InsetNote::InsetNote(InsetNote const & in)
129         : InsetCollapsable(in), params_(in.params_)
130 {
131         init();
132 }
133
134
135 InsetNote::~InsetNote()
136 {
137         InsetNoteMailer(*this).hideDialog();
138 }
139
140
141 auto_ptr<Inset> InsetNote::doClone() const
142 {
143         return auto_ptr<Inset>(new InsetNote(*this));
144 }
145
146
147 docstring const InsetNote::editMessage() const
148 {
149         return _("Opened Note Inset");
150 }
151
152
153 Inset::DisplayType InsetNote::display() const
154 {
155         switch (params_.type) {
156         case InsetNoteParams::Framed:
157         case InsetNoteParams::Shaded:
158                 return AlignLeft;
159         default:
160                 return Inline;
161         }
162 }
163
164
165 void InsetNote::write(Buffer const & buf, ostream & os) const
166 {
167         params_.write(os);
168         InsetCollapsable::write(buf, os);
169 }
170
171
172 void InsetNote::read(Buffer const & buf, Lexer & lex)
173 {
174         params_.read(lex);
175         InsetCollapsable::read(buf, lex);
176         setButtonLabel();
177 }
178
179
180 void InsetNote::setButtonLabel()
181 {
182         docstring const label = notetranslator_loc().find(params_.type);
183         setLabel(label);
184
185         Font font(Font::ALL_SANE);
186         font.decSize();
187         font.decSize();
188
189         Color_color c;
190         switch (params_.type) {
191         case InsetNoteParams::Note:
192                 c = Color::note;
193                 break;
194         case InsetNoteParams::Comment:
195                 c = Color::comment;
196                 break;
197         case InsetNoteParams::Greyedout:
198                 c = Color::greyedout;
199                 break;
200         case InsetNoteParams::Framed:
201                 c = Color::greyedout;
202                 break;
203         case InsetNoteParams::Shaded:
204                 c = Color::greyedout;
205                 break;
206         }
207         font.setColor(c);
208         setLabelFont(font);
209 }
210
211
212 Color_color InsetNote::backgroundColor() const
213 {
214         Color_color c;
215         switch (params_.type) {
216         case InsetNoteParams::Note:
217                 c = Color::notebg;
218                 break;
219         case InsetNoteParams::Comment:
220                 c = Color::commentbg;
221                 break;
222         case InsetNoteParams::Greyedout:
223                 c = Color::greyedoutbg;
224                 break;
225         case InsetNoteParams::Framed:
226                 c = Color::greyedoutbg;
227                 break;
228         case InsetNoteParams::Shaded:
229                 c = Color::shadedbg;
230                 break;
231         }
232         return c;
233 }
234
235
236 bool InsetNote::showInsetDialog(BufferView * bv) const
237 {
238         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
239         return true;
240 }
241
242
243 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
244 {
245         switch (cmd.action) {
246
247         case LFUN_INSET_MODIFY:
248                 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
249                 setButtonLabel();
250                 break;
251
252         case LFUN_INSET_DIALOG_UPDATE:
253                 InsetNoteMailer(*this).updateDialog(&cur.bv());
254                 break;
255
256         case LFUN_MOUSE_RELEASE:
257                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
258                         InsetNoteMailer(*this).showDialog(&cur.bv());
259                 else
260                         InsetCollapsable::doDispatch(cur, cmd);
261                 break;
262
263         default:
264                 InsetCollapsable::doDispatch(cur, cmd);
265                 break;
266         }
267 }
268
269
270 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
271                 FuncStatus & flag) const
272 {
273         switch (cmd.action) {
274
275         case LFUN_INSET_MODIFY:
276         case LFUN_INSET_DIALOG_UPDATE:
277                 flag.enabled(true);
278                 return true;
279
280         default:
281                 return InsetCollapsable::getStatus(cur, cmd, flag);
282         }
283 }
284
285 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
286 {
287         TextClass const & tclass = buf.params().getTextClass();
288         Counters savecnt = tclass.counters();
289         InsetCollapsable::updateLabels(buf, it);
290         tclass.counters() = savecnt;
291 }
292
293
294 int InsetNote::latex(Buffer const & buf, odocstream & os,
295                      OutputParams const & runparams_in) const
296 {
297         if (params_.type == InsetNoteParams::Note)
298                 return 0;
299
300         OutputParams runparams(runparams_in);
301         string type;
302         if (params_.type == InsetNoteParams::Comment) {
303                 type = "comment";
304                 runparams.inComment = true;
305                 // Ignore files that are exported inside a comment
306                 runparams.exportdata.reset(new ExportData);
307         } else if (params_.type == InsetNoteParams::Greyedout)
308                 type = "lyxgreyedout";
309         else if (params_.type == InsetNoteParams::Framed)
310                 type = "framed";
311         else if (params_.type == InsetNoteParams::Shaded)
312                 type = "shaded";
313
314         odocstringstream ss;
315         ss << "%\n\\begin{" << from_ascii(type) << "}\n";
316         InsetText::latex(buf, ss, runparams);
317         ss << "\n\\end{" << from_ascii(type) << "}\n";
318         // the space after the comment in 'a[comment] b' will be eaten by the
319         // comment environment since the space before b is ignored with the
320         // following latex output:
321         //
322         // a%
323         // \begin{comment}
324         // comment
325         // \end{comment}
326         //  b
327         //
328         // Adding {} before ' b' fixes this.
329         if (params_.type == InsetNoteParams::Comment)
330                 ss << "{}";
331
332         docstring const str = ss.str();
333         os << str;
334         runparams_in.encoding = runparams.encoding;
335         // Return how many newlines we issued.
336         return int(std::count(str.begin(), str.end(), '\n'));
337 }
338
339
340 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
341                          OutputParams const & runparams_in) const
342 {
343         if (params_.type == InsetNoteParams::Note)
344                 return 0;
345
346         OutputParams runparams(runparams_in);
347         if (params_.type == InsetNoteParams::Comment) {
348                 runparams.inComment = true;
349                 // Ignore files that are exported inside a comment
350                 runparams.exportdata.reset(new ExportData);
351         }
352         os << '[' << buf.B_("note") << ":\n";
353         InsetText::plaintext(buf, os, runparams);
354         os << "\n]";
355
356         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
357 }
358
359
360 int InsetNote::docbook(Buffer const & buf, odocstream & os,
361                        OutputParams const & runparams_in) const
362 {
363         if (params_.type == InsetNoteParams::Note)
364                 return 0;
365
366         OutputParams runparams(runparams_in);
367         if (params_.type == InsetNoteParams::Comment) {
368                 os << "<remark>\n";
369                 runparams.inComment = true;
370                 // Ignore files that are exported inside a comment
371                 runparams.exportdata.reset(new ExportData);
372         }
373
374         int const n = InsetText::docbook(buf, os, runparams);
375
376         if (params_.type == InsetNoteParams::Comment)
377                 os << "\n</remark>\n";
378
379         // Return how many newlines we issued.
380         //return int(count(str.begin(), str.end(), '\n'));
381         return n + 1 + 2;
382 }
383
384
385 void InsetNote::validate(LaTeXFeatures & features) const
386 {
387         if (params_.type == InsetNoteParams::Comment)
388                 features.require("verbatim");
389         if (params_.type == InsetNoteParams::Greyedout) {
390                 features.require("color");
391                 features.require("lyxgreyedout");
392         }
393         if (params_.type == InsetNoteParams::Shaded) {
394                 features.require("color");
395                 features.require("framed");
396         }
397         if (params_.type == InsetNoteParams::Framed)
398                 features.require("framed");
399         InsetText::validate(features);
400 }
401
402
403
404 string const InsetNoteMailer::name_("note");
405
406 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
407         : inset_(inset)
408 {}
409
410
411 string const InsetNoteMailer::inset2string(Buffer const &) const
412 {
413         return params2string(inset_.params());
414 }
415
416
417 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
418 {
419         ostringstream data;
420         data << name_ << ' ';
421         params.write(data);
422         return data.str();
423 }
424
425
426 void InsetNoteMailer::string2params(string const & in,
427                                     InsetNoteParams & params)
428 {
429         params = InsetNoteParams();
430
431         if (in.empty())
432                 return;
433
434         istringstream data(in);
435         Lexer lex(0,0);
436         lex.setStream(data);
437
438         string name;
439         lex >> name;
440         if (!lex || name != name_)
441                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
442
443         // This is part of the inset proper that is usually swallowed
444         // by Text::readInset
445         string id;
446         lex >> id;
447         if (!lex || id != "Note")
448                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
449
450         params.read(lex);
451 }
452
453
454 } // namespace lyx