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