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