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