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