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