]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
511d95a1112d0d055472ab3ee2e792f4317ff63c
[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 "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "DispatchResult.h"
22 #include "Exporter.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "LaTeXFeatures.h"
27 #include "Color.h"
28 #include "Lexer.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32
33 #include "support/lyxalgo.h"
34 #include "support/Translator.h"
35
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
286 int InsetNote::latex(Buffer const & buf, odocstream & os,
287                      OutputParams const & runparams_in) const
288 {
289         if (params_.type == InsetNoteParams::Note)
290                 return 0;
291
292         OutputParams runparams(runparams_in);
293         string type;
294         if (params_.type == InsetNoteParams::Comment) {
295                 type = "comment";
296                 runparams.inComment = true;
297                 // Ignore files that are exported inside a comment
298                 runparams.exportdata.reset(new ExportData);
299         } else if (params_.type == InsetNoteParams::Greyedout)
300                 type = "lyxgreyedout";
301         else if (params_.type == InsetNoteParams::Framed)
302                 type = "framed";
303         else if (params_.type == InsetNoteParams::Shaded)
304                 type = "shaded";
305
306         odocstringstream ss;
307         ss << "%\n\\begin{" << from_ascii(type) << "}\n";
308         InsetText::latex(buf, ss, runparams);
309         ss << "\n\\end{" << from_ascii(type) << "}\n";
310         // the space after the comment in 'a[comment] b' will be eaten by the
311         // comment environment since the space before b is ignored with the
312         // following latex output:
313         //
314         // a%
315         // \begin{comment}
316         // comment
317         // \end{comment}
318         //  b
319         //
320         // Adding {} before ' b' fixes this.
321         if (params_.type == InsetNoteParams::Comment)
322                 ss << "{}";
323
324         docstring const str = ss.str();
325         os << str;
326         runparams_in.encoding = runparams.encoding;
327         // Return how many newlines we issued.
328         return int(lyx::count(str.begin(), str.end(), '\n'));
329 }
330
331
332 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
333                          OutputParams const & runparams_in) const
334 {
335         if (params_.type == InsetNoteParams::Note)
336                 return 0;
337
338         OutputParams runparams(runparams_in);
339         if (params_.type == InsetNoteParams::Comment) {
340                 runparams.inComment = true;
341                 // Ignore files that are exported inside a comment
342                 runparams.exportdata.reset(new ExportData);
343         }
344         os << '[' << buf.B_("note") << ":\n";
345         InsetText::plaintext(buf, os, runparams);
346         os << "\n]";
347
348         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
349 }
350
351
352 int InsetNote::docbook(Buffer const & buf, odocstream & os,
353                        OutputParams const & runparams_in) const
354 {
355         if (params_.type == InsetNoteParams::Note)
356                 return 0;
357
358         OutputParams runparams(runparams_in);
359         if (params_.type == InsetNoteParams::Comment) {
360                 os << "<remark>\n";
361                 runparams.inComment = true;
362                 // Ignore files that are exported inside a comment
363                 runparams.exportdata.reset(new ExportData);
364         }
365
366         int const n = InsetText::docbook(buf, os, runparams);
367
368         if (params_.type == InsetNoteParams::Comment)
369                 os << "\n</remark>\n";
370
371         // Return how many newlines we issued.
372         //return int(count(str.begin(), str.end(), '\n'));
373         return n + 1 + 2;
374 }
375
376
377 void InsetNote::validate(LaTeXFeatures & features) const
378 {
379         if (params_.type == InsetNoteParams::Comment)
380                 features.require("verbatim");
381         if (params_.type == InsetNoteParams::Greyedout) {
382                 features.require("color");
383                 features.require("lyxgreyedout");
384         }
385         if (params_.type == InsetNoteParams::Shaded) {
386                 features.require("color");
387                 features.require("framed");
388         }
389         if (params_.type == InsetNoteParams::Framed)
390                 features.require("framed");
391         InsetText::validate(features);
392 }
393
394
395
396 string const InsetNoteMailer::name_("note");
397
398 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
399         : inset_(inset)
400 {}
401
402
403 string const InsetNoteMailer::inset2string(Buffer const &) const
404 {
405         return params2string(inset_.params());
406 }
407
408
409 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
410 {
411         ostringstream data;
412         data << name_ << ' ';
413         params.write(data);
414         return data.str();
415 }
416
417
418 void InsetNoteMailer::string2params(string const & in,
419                                     InsetNoteParams & params)
420 {
421         params = InsetNoteParams();
422
423         if (in.empty())
424                 return;
425
426         istringstream data(in);
427         Lexer lex(0,0);
428         lex.setStream(data);
429
430         string name;
431         lex >> name;
432         if (!lex || name != name_)
433                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
434
435         // This is part of the inset proper that is usually swallowed
436         // by Text::readInset
437         string id;
438         lex >> id;
439         if (!lex || id != "Note")
440                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
441
442         params.read(lex);
443 }
444
445
446 } // namespace lyx