]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Some context menu work:
[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                 // disallow comment and greyed out in commands
201                 flag.enabled(!cur.paragraph().layout().isCommand() ||
202                                 cmd.getArg(2) == "Note");
203                 if (cmd.getArg(0) == "note") {
204                         InsetNoteParams params;
205                         InsetNoteMailer::string2params(to_utf8(cmd.argument()), params);
206                         flag.setOnOff(params_.type == params.type);
207                 }
208                 return true;
209
210         case LFUN_INSET_DIALOG_UPDATE:
211                 flag.enabled(true);
212                 return true;
213
214         default:
215                 return InsetCollapsable::getStatus(cur, cmd, flag);
216         }
217 }
218
219
220 void InsetNote::updateLabels(ParIterator const & it)
221 {
222         DocumentClass const & tclass = buffer().params().documentClass();
223         Counters savecnt = tclass.counters();
224         InsetCollapsable::updateLabels(it);
225         tclass.counters() = savecnt;
226 }
227
228
229 void InsetNote::addToToc(ParConstIterator const & cpit) const
230 {
231         ParConstIterator pit = cpit;
232         pit.push_back(*this);
233
234         Toc & toc = buffer().tocBackend().toc("note");
235         docstring str;
236         str = notetranslator_loc().find(params_.type) + from_ascii(": ")
237                 + getNewLabel(str);
238         toc.push_back(TocItem(pit, 0, str));
239 }
240
241
242 bool InsetNote::isMacroScope() const
243 {
244         // LyX note has no latex output
245         if (params_.type == InsetNoteParams::Note)
246                 return true;
247
248         return InsetCollapsable::isMacroScope();
249 }
250
251
252 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
253 {
254         if (params_.type == InsetNoteParams::Note)
255                 return 0;
256
257         OutputParams runparams(runparams_in);
258         if (params_.type == InsetNoteParams::Comment) {
259                 runparams.inComment = true;
260                 // Ignore files that are exported inside a comment
261                 runparams.exportdata.reset(new ExportData);
262         } 
263
264         odocstringstream ss;
265         InsetCollapsable::latex(ss, runparams);
266         // the space after the comment in 'a[comment] b' will be eaten by the
267         // comment environment since the space before b is ignored with the
268         // following latex output:
269         //
270         // a%
271         // \begin{comment}
272         // comment
273         // \end{comment}
274         //  b
275         //
276         // Adding {} before ' b' fixes this.
277         if (params_.type == InsetNoteParams::Comment)
278                 ss << "{}";
279
280         docstring const str = ss.str();
281         os << str;
282         runparams_in.encoding = runparams.encoding;
283         // Return how many newlines we issued.
284         return int(count(str.begin(), str.end(), '\n'));
285 }
286
287
288 int InsetNote::plaintext(odocstream & os,
289                          OutputParams const & runparams_in) const
290 {
291         if (params_.type == InsetNoteParams::Note)
292                 return 0;
293
294         OutputParams runparams(runparams_in);
295         if (params_.type == InsetNoteParams::Comment) {
296                 runparams.inComment = true;
297                 // Ignore files that are exported inside a comment
298                 runparams.exportdata.reset(new ExportData);
299         }
300         os << '[' << buffer().B_("note") << ":\n";
301         InsetText::plaintext(os, runparams);
302         os << "\n]";
303
304         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
305 }
306
307
308 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
309 {
310         if (params_.type == InsetNoteParams::Note)
311                 return 0;
312
313         OutputParams runparams(runparams_in);
314         if (params_.type == InsetNoteParams::Comment) {
315                 os << "<remark>\n";
316                 runparams.inComment = true;
317                 // Ignore files that are exported inside a comment
318                 runparams.exportdata.reset(new ExportData);
319         }
320
321         int const n = InsetText::docbook(os, runparams);
322
323         if (params_.type == InsetNoteParams::Comment)
324                 os << "\n</remark>\n";
325
326         // Return how many newlines we issued.
327         //return int(count(str.begin(), str.end(), '\n'));
328         return n + 1 + 2;
329 }
330
331
332 void InsetNote::validate(LaTeXFeatures & features) const
333 {
334         if (params_.type == InsetNoteParams::Comment)
335                 features.require("verbatim");
336         if (params_.type == InsetNoteParams::Greyedout) {
337                 features.require("color");
338                 features.require("lyxgreyedout");
339         }
340         InsetText::validate(features);
341 }
342
343
344 docstring InsetNote::contextMenu(BufferView const &, int, int) const
345 {
346         return from_ascii("context-note");
347 }
348
349
350 string const InsetNoteMailer::name_("note");
351
352 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
353         : inset_(inset)
354 {}
355
356
357 string const InsetNoteMailer::inset2string(Buffer const &) const
358 {
359         return params2string(inset_.params());
360 }
361
362
363 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
364 {
365         ostringstream data;
366         data << name_ << ' ';
367         params.write(data);
368         return data.str();
369 }
370
371
372 void InsetNoteMailer::string2params(string const & in,
373                                     InsetNoteParams & params)
374 {
375         params = InsetNoteParams();
376
377         if (in.empty())
378                 return;
379
380         istringstream data(in);
381         Lexer lex(0,0);
382         lex.setStream(data);
383
384         string name;
385         lex >> name;
386         if (!lex || name != name_)
387                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
388
389         // This is part of the inset proper that is usually swallowed
390         // by Text::readInset
391         string id;
392         lex >> id;
393         if (!lex || id != "Note")
394                 return print_mailer_error("InsetNoteMailer", in, 2, "Note");
395
396         params.read(lex);
397 }
398
399
400 } // namespace lyx