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