]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
Context menu for InsetArgument
[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 "DispatchResult.h"
24 #include "Exporter.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "InsetIterator.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "LyXRC.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/gettext.h"
39 #include "support/Translator.h"
40
41 #include "frontends/Application.h"
42
43 #include <algorithm>
44 #include <sstream>
45
46 using namespace std;
47
48 namespace lyx {
49
50 namespace {
51
52 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
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 NoteTranslator const & notetranslator()
64 {
65         static NoteTranslator translator = init_notetranslator();
66         return translator;
67 }
68
69
70 } // anon
71
72
73 InsetNoteParams::InsetNoteParams()
74         : type(Note)
75 {}
76
77
78 void InsetNoteParams::write(ostream & os) const
79 {
80         string const label = notetranslator().find(type);
81         os << "Note " << label << "\n";
82 }
83
84
85 void InsetNoteParams::read(Lexer & lex)
86 {
87         string label;
88         lex >> label;
89         if (lex)
90                 type = notetranslator().find(label);
91 }
92
93
94 /////////////////////////////////////////////////////////////////////
95 //
96 // InsetNote
97 //
98 /////////////////////////////////////////////////////////////////////
99
100 InsetNote::InsetNote(Buffer * buf, string const & label)
101         : InsetCollapsable(buf)
102 {
103         params_.type = notetranslator().find(label);
104 }
105
106
107 InsetNote::~InsetNote()
108 {
109         hideDialogs("note", this);
110 }
111
112
113 docstring InsetNote::layoutName() const
114 {
115         return from_ascii("Note:" + notetranslator().find(params_.type));
116 }
117
118
119 Inset::DisplayType InsetNote::display() const
120 {
121         return Inline;
122 }
123
124
125 void InsetNote::write(ostream & os) const
126 {
127         params_.write(os);
128         InsetCollapsable::write(os);
129 }
130
131
132 void InsetNote::read(Lexer & lex)
133 {
134         params_.read(lex);
135         InsetCollapsable::read(lex);
136 }
137
138
139 bool InsetNote::showInsetDialog(BufferView * bv) const
140 {
141         bv->showDialog("note", params2string(params()),
142                 const_cast<InsetNote *>(this));
143         return true;
144 }
145
146
147 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
148 {
149         switch (cmd.action()) {
150
151         case LFUN_INSET_MODIFY:
152                 cur.recordUndoInset(ATOMIC_UNDO, this);
153                 string2params(to_utf8(cmd.argument()), params_);
154                 setButtonLabel();
155                 // what we really want here is a TOC update, but that means
156                 // a full buffer update
157                 cur.forceBufferUpdate();
158                 break;
159
160         case LFUN_INSET_DIALOG_UPDATE:
161                 cur.bv().updateDialog("note", params2string(params()));
162                 break;
163
164         default:
165                 InsetCollapsable::doDispatch(cur, cmd);
166                 break;
167         }
168 }
169
170
171 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
172                 FuncStatus & flag) const
173 {
174         switch (cmd.action()) {
175
176         case LFUN_INSET_MODIFY:
177                 // disallow comment and greyed out in commands
178                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
179                                 cmd.getArg(2) == "Note");
180                 if (cmd.getArg(0) == "note") {
181                         InsetNoteParams params;
182                         string2params(to_utf8(cmd.argument()), params);
183                         flag.setOnOff(params_.type == params.type);
184                 }
185                 return true;
186
187         case LFUN_INSET_DIALOG_UPDATE:
188                 flag.setEnabled(true);
189                 return true;
190
191         default:
192                 return InsetCollapsable::getStatus(cur, cmd, flag);
193         }
194 }
195
196
197 void InsetNote::addToToc(DocIterator const & cpit) const
198 {
199         DocIterator pit = cpit;
200         pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
201
202         Toc & toc = buffer().tocBackend().toc("note");
203         InsetLayout const & il = getLayout();
204         docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
205         text().forToc(str, TOC_ENTRY_LENGTH);
206         toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
207         // Proceed with the rest of the inset.
208         InsetCollapsable::addToToc(cpit);
209 }
210
211
212 bool InsetNote::isMacroScope() const
213 {
214         // LyX note has no latex output
215         if (params_.type == InsetNoteParams::Note)
216                 return true;
217
218         return InsetCollapsable::isMacroScope();
219 }
220
221
222 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
223 {
224         if (params_.type == InsetNoteParams::Note)
225                 return;
226
227         OutputParams runparams(runparams_in);
228         if (params_.type == InsetNoteParams::Comment) {
229                 runparams.inComment = true;
230                 // Ignore files that are exported inside a comment
231                 runparams.exportdata.reset(new ExportData);
232         } 
233
234         // the space after the comment in 'a[comment] b' will be eaten by the
235         // comment environment since the space before b is ignored with the
236         // following latex output:
237         //
238         // a%
239         // \begin{comment}
240         // comment
241         // \end{comment}
242         //  b
243         //
244         // Adding {} before ' b' fixes this.
245         // The {} will be automatically added, but only if needed, for all
246         // insets whose InsetLayout Display tag is false. This is achieved
247         // by telling otexstream to protect an immediately following space
248         // and is done for both comment and greyedout insets.
249         InsetCollapsable::latex(os, runparams);
250
251         runparams_in.encoding = runparams.encoding;
252 }
253
254
255 int InsetNote::plaintext(odocstream & os,
256                          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         os << '[' << buffer().B_("note") << ":\n";
268         InsetText::plaintext(os, runparams);
269         os << "\n]";
270
271         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
272 }
273
274
275 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
276 {
277         if (params_.type == InsetNoteParams::Note)
278                 return 0;
279
280         OutputParams runparams(runparams_in);
281         if (params_.type == InsetNoteParams::Comment) {
282                 os << "<remark>\n";
283                 runparams.inComment = true;
284                 // Ignore files that are exported inside a comment
285                 runparams.exportdata.reset(new ExportData);
286         }
287
288         int const n = InsetText::docbook(os, runparams);
289
290         if (params_.type == InsetNoteParams::Comment)
291                 os << "\n</remark>\n";
292
293         // Return how many newlines we issued.
294         //return int(count(str.begin(), str.end(), '\n'));
295         return n + 1 + 2;
296 }
297
298
299 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
300 {
301         if (params_.type == InsetNoteParams::Note)
302                 return docstring();
303
304         return InsetCollapsable::xhtml(xs, rp);
305 }
306
307
308 void InsetNote::validate(LaTeXFeatures & features) const
309 {
310         switch (params_.type) {
311         case InsetNoteParams::Comment:
312                 features.require("verbatim");
313                 if (features.runparams().flavor == OutputParams::HTML)
314                         // we do output this but set display to "none" by default,
315                         // but people might want to use it.
316                         InsetCollapsable::validate(features);
317                 break;
318         case InsetNoteParams::Greyedout:
319                 features.require("color");
320                 features.require("lyxgreyedout");
321                 InsetCollapsable::validate(features);
322                 break;
323         case InsetNoteParams::Note:
324                 break;
325         }
326 }
327
328
329 string InsetNote::contextMenuName() const
330 {
331         return "context-note";
332 }
333
334 bool InsetNote::allowSpellCheck() const
335 {
336         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
337 }
338
339
340 string InsetNote::params2string(InsetNoteParams const & params)
341 {
342         ostringstream data;
343         data << "note" << ' ';
344         params.write(data);
345         return data.str();
346 }
347
348
349 void InsetNote::string2params(string const & in, InsetNoteParams & params)
350 {
351         params = InsetNoteParams();
352
353         if (in.empty())
354                 return;
355
356         istringstream data(in);
357         Lexer lex;
358         lex.setStream(data);
359         lex.setContext("InsetNote::string2params");
360         lex >> "note";
361         // There are cases, such as when we are called via getStatus() from
362         // Dialog::canApply(), where we are just called with "note" rather
363         // than a full "note Note TYPE".
364         if (!lex.isOK())
365                 return;
366         lex >> "Note";
367
368         params.read(lex);
369 }
370
371
372 } // namespace lyx