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