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