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