]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNote.cpp
Links between citations and bibliography entries were broken in XHTML
[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/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 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                 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, bool output_active) 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, output_active, toolTipText(docstring(), 3, 60)));
208
209         // Proceed with the rest of the inset.
210         bool doing_output = output_active && producesOutput();
211         InsetCollapsable::addToToc(cpit, doing_output);
212 }
213
214
215 bool InsetNote::isMacroScope() const
216 {
217         // LyX note has no latex output
218         if (params_.type == InsetNoteParams::Note)
219                 return true;
220
221         return InsetCollapsable::isMacroScope();
222 }
223
224
225 void InsetNote::latex(otexstream & os, OutputParams const & runparams_in) const
226 {
227         if (params_.type == InsetNoteParams::Note)
228                 return;
229
230         OutputParams runparams(runparams_in);
231         if (params_.type == InsetNoteParams::Comment) {
232                 runparams.inComment = true;
233                 // Ignore files that are exported inside a comment
234                 runparams.exportdata.reset(new ExportData);
235         } 
236
237         // the space after the comment in 'a[comment] b' will be eaten by the
238         // comment environment since the space before b is ignored with the
239         // following latex output:
240         //
241         // a%
242         // \begin{comment}
243         // comment
244         // \end{comment}
245         //  b
246         //
247         // Adding {} before ' b' fixes this.
248         // The {} will be automatically added, but only if needed, for all
249         // insets whose InsetLayout Display tag is false. This is achieved
250         // by telling otexstream to protect an immediately following space
251         // and is done for both comment and greyedout insets.
252         InsetCollapsable::latex(os, runparams);
253
254         runparams_in.encoding = runparams.encoding;
255 }
256
257
258 int InsetNote::plaintext(odocstringstream & os,
259                          OutputParams const & runparams_in, size_t max_length) const
260 {
261         if (params_.type == InsetNoteParams::Note)
262                 return 0;
263
264         OutputParams runparams(runparams_in);
265         if (params_.type == InsetNoteParams::Comment) {
266                 runparams.inComment = true;
267                 // Ignore files that are exported inside a comment
268                 runparams.exportdata.reset(new ExportData);
269         }
270         os << '[' << buffer().B_("note") << ":\n";
271         InsetText::plaintext(os, runparams, max_length);
272         os << "\n]";
273
274         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
275 }
276
277
278 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
279 {
280         if (params_.type == InsetNoteParams::Note)
281                 return 0;
282
283         OutputParams runparams(runparams_in);
284         if (params_.type == InsetNoteParams::Comment) {
285                 os << "<remark>\n";
286                 runparams.inComment = true;
287                 // Ignore files that are exported inside a comment
288                 runparams.exportdata.reset(new ExportData);
289         }
290
291         int const n = InsetText::docbook(os, runparams);
292
293         if (params_.type == InsetNoteParams::Comment)
294                 os << "\n</remark>\n";
295
296         // Return how many newlines we issued.
297         //return int(count(str.begin(), str.end(), '\n'));
298         return n + 1 + 2;
299 }
300
301
302 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
303 {
304         if (params_.type == InsetNoteParams::Note)
305                 return docstring();
306
307         return InsetCollapsable::xhtml(xs, rp);
308 }
309
310
311 void InsetNote::validate(LaTeXFeatures & features) const
312 {
313         switch (params_.type) {
314         case InsetNoteParams::Comment:
315                 features.require("verbatim");
316                 if (features.runparams().flavor == OutputParams::HTML)
317                         // we do output this but set display to "none" by default,
318                         // but people might want to use it.
319                         InsetCollapsable::validate(features);
320                 break;
321         case InsetNoteParams::Greyedout:
322                 features.require("color");
323                 features.require("lyxgreyedout");
324                 InsetCollapsable::validate(features);
325                 break;
326         case InsetNoteParams::Note:
327                 break;
328         }
329 }
330
331
332 string InsetNote::contextMenuName() const
333 {
334         return "context-note";
335 }
336
337 bool InsetNote::allowSpellCheck() const
338 {
339         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
340 }
341
342 FontInfo InsetNote::getFont() const
343 {
344         FontInfo font = getLayout().font();
345         // FIXME: This hardcoded color is a hack!
346         if (params_.type == InsetNoteParams::Greyedout
347             && buffer().params().notefontcolor != lyx::rgbFromHexName("#cccccc")) {
348                 ColorCode c = lcolor.getFromLyXName("notefontcolor");
349                 if (c != Color_none)
350                         font.setColor(c);
351         }
352         return font;
353 }
354
355
356 string InsetNote::params2string(InsetNoteParams const & params)
357 {
358         ostringstream data;
359         data << "note" << ' ';
360         params.write(data);
361         return data.str();
362 }
363
364
365 void InsetNote::string2params(string const & in, InsetNoteParams & params)
366 {
367         params = InsetNoteParams();
368
369         if (in.empty())
370                 return;
371
372         istringstream data(in);
373         Lexer lex;
374         lex.setStream(data);
375         lex.setContext("InsetNote::string2params");
376         lex >> "note";
377         // There are cases, such as when we are called via getStatus() from
378         // Dialog::canApply(), where we are just called with "note" rather
379         // than a full "note Note TYPE".
380         if (!lex.isOK())
381                 return;
382         lex >> "Note";
383
384         params.read(lex);
385 }
386
387
388 } // namespace lyx