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