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