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