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