]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
* InsetCollapsable:
[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         setButtonLabel();
120 }
121
122
123 InsetNote::InsetNote(InsetNote const & in)
124         : InsetCollapsable(in), params_(in.params_)
125 {
126         setButtonLabel();
127 }
128
129
130 InsetNote::~InsetNote()
131 {
132         InsetNoteMailer(*this).hideDialog();
133 }
134
135
136 Inset * InsetNote::clone() const
137 {
138         return new InsetNote(*this);
139 }
140
141
142 docstring const InsetNote::editMessage() const
143 {
144         return _("Opened Note Inset");
145 }
146
147
148 docstring InsetNote::name() const 
149 {
150         return from_ascii(string("Note") + string(":") + string(notetranslator().find(params_.type)));
151 }
152
153
154 Inset::DisplayType InsetNote::display() const
155 {
156         switch (params_.type) {
157         case InsetNoteParams::Framed:
158         case InsetNoteParams::Shaded:
159                 return AlignLeft;
160         default:
161                 return Inline;
162         }
163 }
164
165
166 void InsetNote::write(Buffer const & buf, ostream & os) const
167 {
168         params_.write(os);
169         InsetCollapsable::write(buf, os);
170 }
171
172
173 void InsetNote::read(Buffer const & buf, Lexer & lex)
174 {
175         params_.read(lex);
176         InsetCollapsable::read(buf, lex);
177 }
178
179
180 void InsetNote::setButtonLabel()
181 {
182         docstring const label = notetranslator_loc().find(params_.type);
183         setLabel(label);
184 }
185
186
187 bool InsetNote::showInsetDialog(BufferView * bv) const
188 {
189         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
190         return true;
191 }
192
193
194 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
195 {
196         switch (cmd.action) {
197
198         case LFUN_INSET_MODIFY:
199                 InsetNoteMailer::string2params(to_utf8(cmd.argument()), params_);
200                 // get a bp from cur:
201                 setLayout(cur.buffer().params());
202                 break;
203
204         case LFUN_INSET_DIALOG_UPDATE:
205                 InsetNoteMailer(*this).updateDialog(&cur.bv());
206                 break;
207         default:
208                 InsetCollapsable::doDispatch(cur, cmd);
209                 break;
210         }
211 }
212
213
214 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
215                 FuncStatus & flag) const
216 {
217         switch (cmd.action) {
218
219         case LFUN_INSET_MODIFY:
220         case LFUN_INSET_DIALOG_UPDATE:
221                 flag.enabled(true);
222                 return true;
223
224         default:
225                 return InsetCollapsable::getStatus(cur, cmd, flag);
226         }
227 }
228
229 void InsetNote::updateLabels(Buffer const & buf, ParIterator const & it)
230 {
231         TextClass const & tclass = buf.params().getTextClass();
232         Counters savecnt = tclass.counters();
233         InsetCollapsable::updateLabels(buf, it);
234         tclass.counters() = savecnt;
235 }
236
237
238 int InsetNote::latex(Buffer const & buf, odocstream & os,
239                      OutputParams const & runparams_in) const
240 {
241         if (params_.type == InsetNoteParams::Note)
242                 return 0;
243
244         OutputParams runparams(runparams_in);
245         if (params_.type == InsetNoteParams::Comment) {
246                 runparams.inComment = true;
247                 // Ignore files that are exported inside a comment
248                 runparams.exportdata.reset(new ExportData);
249         } 
250
251         odocstringstream ss;
252         InsetCollapsable::latex(buf, ss, runparams);
253         // the space after the comment in 'a[comment] b' will be eaten by the
254         // comment environment since the space before b is ignored with the
255         // following latex output:
256         //
257         // a%
258         // \begin{comment}
259         // comment
260         // \end{comment}
261         //  b
262         //
263         // Adding {} before ' b' fixes this.
264         if (params_.type == InsetNoteParams::Comment)
265                 ss << "{}";
266
267         docstring const str = ss.str();
268         os << str;
269         runparams_in.encoding = runparams.encoding;
270         // Return how many newlines we issued.
271         return int(std::count(str.begin(), str.end(), '\n'));
272 }
273
274
275 int InsetNote::plaintext(Buffer const & buf, odocstream & os,
276                          OutputParams const & runparams_in) const
277 {
278         if (params_.type == InsetNoteParams::Note)
279                 return 0;
280
281         OutputParams runparams(runparams_in);
282         if (params_.type == InsetNoteParams::Comment) {
283                 runparams.inComment = true;
284                 // Ignore files that are exported inside a comment
285                 runparams.exportdata.reset(new ExportData);
286         }
287         os << '[' << buf.B_("note") << ":\n";
288         InsetText::plaintext(buf, os, runparams);
289         os << "\n]";
290
291         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
292 }
293
294
295 int InsetNote::docbook(Buffer const & buf, odocstream & os,
296                        OutputParams const & runparams_in) const
297 {
298         if (params_.type == InsetNoteParams::Note)
299                 return 0;
300
301         OutputParams runparams(runparams_in);
302         if (params_.type == InsetNoteParams::Comment) {
303                 os << "<remark>\n";
304                 runparams.inComment = true;
305                 // Ignore files that are exported inside a comment
306                 runparams.exportdata.reset(new ExportData);
307         }
308
309         int const n = InsetText::docbook(buf, os, runparams);
310
311         if (params_.type == InsetNoteParams::Comment)
312                 os << "\n</remark>\n";
313
314         // Return how many newlines we issued.
315         //return int(count(str.begin(), str.end(), '\n'));
316         return n + 1 + 2;
317 }
318
319
320 void InsetNote::validate(LaTeXFeatures & features) const
321 {
322         if (params_.type == InsetNoteParams::Comment)
323                 features.require("verbatim");
324         if (params_.type == InsetNoteParams::Greyedout) {
325                 features.require("color");
326                 features.require("lyxgreyedout");
327         }
328         if (params_.type == InsetNoteParams::Shaded) {
329                 features.require("color");
330                 features.require("framed");
331         }
332         if (params_.type == InsetNoteParams::Framed)
333                 features.require("framed");
334         InsetText::validate(features);
335 }
336
337
338
339 string const InsetNoteMailer::name_("note");
340
341 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
342         : inset_(inset)
343 {}
344
345
346 string const InsetNoteMailer::inset2string(Buffer const &) const
347 {
348         return params2string(inset_.params());
349 }
350
351
352 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
353 {
354         ostringstream data;
355         data << name_ << ' ';
356         params.write(data);
357         return data.str();
358 }
359
360
361 void InsetNoteMailer::string2params(string const & in,
362                                     InsetNoteParams & params)
363 {
364         params = InsetNoteParams();
365
366         if (in.empty())
367                 return;
368
369         istringstream data(in);
370         Lexer lex(0,0);
371         lex.setStream(data);
372
373         string name;
374         lex >> name;
375         if (!lex || name != name_)
376                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
377
378         // This is part of the inset proper that is usually swallowed
379         // by Text::readInset
380         string id;
381         lex >> id;
382         if (!lex || id != "Note")
383                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
384
385         params.read(lex);
386 }
387
388
389 } // namespace lyx