]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Enhancements and bugfixes to the TOCs
[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 "ColorSet.h"
22 #include "Counters.h"
23 #include "Cursor.h"
24 #include "DispatchResult.h"
25 #include "Exporter.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "InsetIterator.h"
29 #include "LaTeXFeatures.h"
30 #include "Lexer.h"
31 #include "LyXRC.h"
32 #include "OutputParams.h"
33 #include "ParIterator.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
36
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/Translator.h"
41
42 #include "frontends/Application.h"
43
44 #include <algorithm>
45 #include <sstream>
46
47 using namespace std;
48
49 namespace lyx {
50
51 namespace {
52
53 typedef Translator<string, InsetNoteParams::Type> NoteTranslator;
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 const 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::layoutName() 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                 // Do not do anything if converting to the same type of Note.
154                 // A quick break here is done instead of disabling the LFUN
155                 // because disabling the LFUN would lead to a greyed out
156                 // entry, which might confuse users.
157                 // In the future, we might want to have a radio button for
158                 // switching between notes.
159                 InsetNoteParams params;
160                 string2params(to_utf8(cmd.argument()), params);
161                 if (params_.type == params.type)
162                   break;
163
164                 cur.recordUndoInset(this);
165                 string2params(to_utf8(cmd.argument()), params_);
166                 setButtonLabel();
167                 // what we really want here is a TOC update, but that means
168                 // a full buffer update
169                 cur.forceBufferUpdate();
170                 break;
171         }
172
173         case LFUN_INSET_DIALOG_UPDATE:
174                 cur.bv().updateDialog("note", params2string(params()));
175                 break;
176
177         default:
178                 InsetCollapsable::doDispatch(cur, cmd);
179                 break;
180         }
181 }
182
183
184 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
185                 FuncStatus & flag) const
186 {
187         switch (cmd.action()) {
188
189         case LFUN_INSET_MODIFY:
190                 // disallow comment and greyed out in commands
191                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
192                                 cmd.getArg(2) == "Note");
193                 if (cmd.getArg(0) == "note") {
194                         InsetNoteParams params;
195                         string2params(to_utf8(cmd.argument()), params);
196                         flag.setOnOff(params_.type == params.type);
197                 }
198                 return true;
199
200         case LFUN_INSET_DIALOG_UPDATE:
201                 flag.setEnabled(true);
202                 return true;
203
204         default:
205                 return InsetCollapsable::getStatus(cur, cmd, flag);
206         }
207 }
208
209
210 void InsetNote::addToToc(DocIterator const & cpit, bool output_active,
211                                                  UpdateType utype) const
212 {
213         DocIterator pit = cpit;
214         pit.push_back(CursorSlice(const_cast<InsetNote &>(*this)));
215         
216         shared_ptr<Toc> toc = buffer().tocBackend().toc("note");
217         InsetLayout const & il = getLayout();
218         docstring str = translateIfPossible(il.labelstring()) + from_ascii(": ");
219         text().forOutliner(str, TOC_ENTRY_LENGTH);
220         toc->push_back(TocItem(pit, 0, str, output_active, toolTipText(docstring(), 3, 60)));
221
222         // Proceed with the rest of the inset.
223         bool doing_output = output_active && producesOutput();
224         InsetCollapsable::addToToc(cpit, doing_output, utype);
225 }
226
227
228 bool InsetNote::isMacroScope() const
229 {
230         // LyX note has no latex output
231         if (params_.type == InsetNoteParams::Note)
232                 return true;
233
234         return InsetCollapsable::isMacroScope();
235 }
236
237
238 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
239 {
240         if (params_.type == InsetNoteParams::Note)
241                 return;
242
243         OutputParams runparams(runparams_in);
244         if (params_.type == InsetNoteParams::Comment) {
245                 runparams.inComment = true;
246                 // Ignore files that are exported inside a comment
247                 runparams.exportdata.reset(new ExportData);
248         } 
249
250         // the space after the comment in 'a[comment] b' will be eaten by the
251         // comment environment since the space before b is ignored with the
252         // following latex output:
253         //
254         // a%
255         // \begin{comment}
256         // comment
257         // \end{comment}
258         //  b
259         //
260         // Adding {} before ' b' fixes this.
261         // The {} will be automatically added, but only if needed, for all
262         // insets whose InsetLayout Display tag is false. This is achieved
263         // by telling otexstream to protect an immediately following space
264         // and is done for both comment and greyedout insets.
265         InsetCollapsable::latex(os, runparams);
266
267         runparams_in.encoding = runparams.encoding;
268 }
269
270
271 int InsetNote::plaintext(odocstringstream & os,
272                          OutputParams const & runparams_in, size_t max_length) const
273 {
274         if (params_.type == InsetNoteParams::Note)
275                 return 0;
276
277         OutputParams runparams(runparams_in);
278         if (params_.type == InsetNoteParams::Comment) {
279                 runparams.inComment = true;
280                 // Ignore files that are exported inside a comment
281                 runparams.exportdata.reset(new ExportData);
282         }
283         os << '[' << buffer().B_("note") << ":\n";
284         InsetText::plaintext(os, runparams, max_length);
285         os << "\n]";
286
287         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
288 }
289
290
291 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
292 {
293         if (params_.type == InsetNoteParams::Note)
294                 return 0;
295
296         OutputParams runparams(runparams_in);
297         if (params_.type == InsetNoteParams::Comment) {
298                 os << "<remark>\n";
299                 runparams.inComment = true;
300                 // Ignore files that are exported inside a comment
301                 runparams.exportdata.reset(new ExportData);
302         }
303
304         int const n = InsetText::docbook(os, runparams);
305
306         if (params_.type == InsetNoteParams::Comment)
307                 os << "\n</remark>\n";
308
309         // Return how many newlines we issued.
310         //return int(count(str.begin(), str.end(), '\n'));
311         return n + 1 + 2;
312 }
313
314
315 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
316 {
317         if (params_.type == InsetNoteParams::Note)
318                 return docstring();
319
320         return InsetCollapsable::xhtml(xs, rp);
321 }
322
323
324 void InsetNote::validate(LaTeXFeatures & features) const
325 {
326         switch (params_.type) {
327         case InsetNoteParams::Comment:
328                 features.require("verbatim");
329                 if (features.runparams().flavor == OutputParams::HTML)
330                         // we do output this but set display to "none" by default,
331                         // but people might want to use it.
332                         InsetCollapsable::validate(features);
333                 break;
334         case InsetNoteParams::Greyedout:
335                 features.require("color");
336                 features.require("lyxgreyedout");
337                 InsetCollapsable::validate(features);
338                 break;
339         case InsetNoteParams::Note:
340                 break;
341         }
342 }
343
344
345 string InsetNote::contextMenuName() const
346 {
347         return "context-note";
348 }
349
350 bool InsetNote::allowSpellCheck() const
351 {
352         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
353 }
354
355 FontInfo InsetNote::getFont() const
356 {
357         FontInfo font = getLayout().font();
358         // FIXME: This hardcoded color is a hack!
359         if (params_.type == InsetNoteParams::Greyedout
360             && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
361                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
362                 if (c != Color_none)
363                         font.setColor(c);
364         }
365         return font;
366 }
367
368
369 string InsetNote::params2string(InsetNoteParams const & params)
370 {
371         ostringstream data;
372         data << "note" << ' ';
373         params.write(data);
374         return data.str();
375 }
376
377
378 void InsetNote::string2params(string const & in, InsetNoteParams & params)
379 {
380         params = InsetNoteParams();
381
382         if (in.empty())
383                 return;
384
385         istringstream data(in);
386         Lexer lex;
387         lex.setStream(data);
388         lex.setContext("InsetNote::string2params");
389         lex >> "note";
390         // There are cases, such as when we are called via getStatus() from
391         // Dialog::canApply(), where we are just called with "note" rather
392         // than a full "note Note TYPE".
393         if (!lex.isOK())
394                 return;
395         lex >> "Note";
396
397         params.read(lex);
398 }
399
400
401 } // namespace lyx