]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
Remove color dependency of framed note, fix bug 3598
[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         switch (params_.type) {
190         case InsetNoteParams::Note:
191                 font.setColor(Color::note);
192                 setBackgroundColor(Color::notebg);
193                 break;
194         case InsetNoteParams::Comment:
195                 font.setColor(Color::comment);
196                 setBackgroundColor(Color::commentbg);
197                 break;
198         case InsetNoteParams::Greyedout:
199                 font.setColor(Color::greyedout);
200                 setBackgroundColor(Color::greyedoutbg);
201                 break;
202         case InsetNoteParams::Framed:
203                 font.setColor(Color::greyedout);
204                 setBackgroundColor(Color::greyedoutbg);
205                 break;
206         case InsetNoteParams::Shaded:
207                 font.setColor(Color::greyedout);
208                 setBackgroundColor(Color::shadedbg);
209                 break;
210         }
211         setLabelFont(font);
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                 setButtonLabel();
229                 break;
230
231         case LFUN_INSET_DIALOG_UPDATE:
232                 InsetNoteMailer(*this).updateDialog(&cur.bv());
233                 break;
234
235         case LFUN_MOUSE_RELEASE:
236                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
237                         InsetNoteMailer(*this).showDialog(&cur.bv());
238                 else
239                         InsetCollapsable::doDispatch(cur, cmd);
240                 break;
241
242         default:
243                 InsetCollapsable::doDispatch(cur, cmd);
244                 break;
245         }
246 }
247
248
249 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
250                 FuncStatus & flag) const
251 {
252         switch (cmd.action) {
253
254         case LFUN_INSET_MODIFY:
255         case LFUN_INSET_DIALOG_UPDATE:
256                 flag.enabled(true);
257                 return true;
258
259         default:
260                 return InsetCollapsable::getStatus(cur, cmd, flag);
261         }
262 }
263
264
265 int InsetNote::latex(Buffer const & buf, odocstream & os,
266                      OutputParams const & runparams_in) const
267 {
268         if (params_.type == InsetNoteParams::Note)
269                 return 0;
270
271         OutputParams runparams(runparams_in);
272         string type;
273         if (params_.type == InsetNoteParams::Comment) {
274                 type = "comment";
275                 runparams.inComment = true;
276                 // Ignore files that are exported inside a comment
277                 runparams.exportdata.reset(new ExportData);
278         } else if (params_.type == InsetNoteParams::Greyedout)
279                 type = "lyxgreyedout";
280         else if (params_.type == InsetNoteParams::Framed)
281                 type = "framed";
282         else if (params_.type == InsetNoteParams::Shaded)
283                 type = "shaded";
284
285         odocstringstream ss;
286         ss << "%\n\\begin{" << from_ascii(type) << "}\n";
287         InsetText::latex(buf, ss, runparams);
288         ss << "\n\\end{" << from_ascii(type) << "}\n";
289         // the space after the comment in 'a[comment] b' will be eaten by the
290         // comment environment since the space before b is ignored with the
291         // following latex output:
292         //
293         // a%
294         // \begin{comment}
295         // comment
296         // \end{comment}
297         //  b
298         //
299         // Adding {} before ' b' fixes this.
300         if (params_.type == InsetNoteParams::Comment)
301                 ss << "{}";
302
303         docstring const str = ss.str();
304         os << str;
305         runparams_in.encoding = runparams.encoding;
306         // Return how many newlines we issued.
307         return int(lyx::count(str.begin(), str.end(), '\n'));
308 }
309
310
311 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
312                          OutputParams const & runparams_in) const
313 {
314         if (params_.type == InsetNoteParams::Note)
315                 return 0;
316
317         OutputParams runparams(runparams_in);
318         if (params_.type == InsetNoteParams::Comment) {
319                 runparams.inComment = true;
320                 // Ignore files that are exported inside a comment
321                 runparams.exportdata.reset(new ExportData);
322         }
323         os << '[' << buf.B_("note") << ":\n";
324         InsetText::plaintext(buf, os, runparams);
325         os << "\n]";
326
327         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
328 }
329
330
331 int InsetNote::docbook(Buffer const & buf, odocstream & os,
332                        OutputParams const & runparams_in) const
333 {
334         if (params_.type == InsetNoteParams::Note)
335                 return 0;
336
337         OutputParams runparams(runparams_in);
338         if (params_.type == InsetNoteParams::Comment) {
339                 os << "<remark>\n";
340                 runparams.inComment = true;
341                 // Ignore files that are exported inside a comment
342                 runparams.exportdata.reset(new ExportData);
343         }
344
345         int const n = InsetText::docbook(buf, os, runparams);
346
347         if (params_.type == InsetNoteParams::Comment)
348                 os << "\n</remark>\n";
349
350         // Return how many newlines we issued.
351         //return int(count(str.begin(), str.end(), '\n'));
352         return n + 1 + 2;
353 }
354
355
356 void InsetNote::validate(LaTeXFeatures & features) const
357 {
358         if (params_.type == InsetNoteParams::Comment)
359                 features.require("verbatim");
360         if (params_.type == InsetNoteParams::Greyedout) {
361                 features.require("color");
362                 features.require("lyxgreyedout");
363         }
364         if (params_.type == InsetNoteParams::Shaded) {
365                 features.require("color");
366                 features.require("framed");
367         }
368         if (params_.type == InsetNoteParams::Framed)
369                 features.require("framed");
370         InsetText::validate(features);
371 }
372
373
374
375 string const InsetNoteMailer::name_("note");
376
377 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
378         : inset_(inset)
379 {}
380
381
382 string const InsetNoteMailer::inset2string(Buffer const &) const
383 {
384         return params2string(inset_.params());
385 }
386
387
388 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
389 {
390         ostringstream data;
391         data << name_ << ' ';
392         params.write(data);
393         return data.str();
394 }
395
396
397 void InsetNoteMailer::string2params(string const & in,
398                                     InsetNoteParams & params)
399 {
400         params = InsetNoteParams();
401
402         if (in.empty())
403                 return;
404
405         istringstream data(in);
406         Lexer lex(0,0);
407         lex.setStream(data);
408
409         string name;
410         lex >> name;
411         if (!lex || name != name_)
412                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
413
414         // This is part of the inset proper that is usually swallowed
415         // by Text::readInset
416         string id;
417         lex >> id;
418         if (!lex || id != "Note")
419                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
420
421         params.read(lex);
422 }
423
424
425 } // namespace lyx