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