]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Allow customization of label string for note insets. We actually had
[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 "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 label = getLayout().labelstring();
160         if (label.empty())
161                 label = notetranslator_loc().find(params_.type);
162         else
163                 label = translateIfPossible(label);
164         setLabel(label);
165 }
166
167
168 bool InsetNote::showInsetDialog(BufferView * bv) const
169 {
170         bv->showDialog("note", params2string(params()),
171                 const_cast<InsetNote *>(this));
172         return true;
173 }
174
175
176 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
177 {
178         switch (cmd.action()) {
179
180         case LFUN_INSET_MODIFY:
181                 cur.recordUndoInset(ATOMIC_UNDO, this);
182                 string2params(to_utf8(cmd.argument()), params_);
183                 setButtonLabel();
184                 // what we really want here is a TOC update, but that means
185                 // a full buffer update
186                 cur.forceBufferUpdate();
187                 break;
188
189         case LFUN_INSET_DIALOG_UPDATE:
190                 cur.bv().updateDialog("note", params2string(params()));
191                 break;
192
193         default:
194                 InsetCollapsable::doDispatch(cur, cmd);
195                 break;
196         }
197 }
198
199
200 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
201                 FuncStatus & flag) const
202 {
203         switch (cmd.action()) {
204
205         case LFUN_INSET_MODIFY:
206                 // disallow comment and greyed out in commands
207                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
208                                 cmd.getArg(2) == "Note");
209                 if (cmd.getArg(0) == "note") {
210                         InsetNoteParams params;
211                         string2params(to_utf8(cmd.argument()), params);
212                         flag.setOnOff(params_.type == params.type);
213                 }
214                 return true;
215
216         case LFUN_INSET_DIALOG_UPDATE:
217                 flag.setEnabled(true);
218                 return true;
219
220         default:
221                 return InsetCollapsable::getStatus(cur, cmd, flag);
222         }
223 }
224
225
226 void InsetNote::addToToc(DocIterator const & cpit)
227 {
228         DocIterator pit = cpit;
229         pit.push_back(CursorSlice(*this));
230
231         Toc & toc = buffer().tocBackend().toc("note");
232         docstring str;
233         str = notetranslator_loc().find(params_.type) + from_ascii(": ")
234                 + text().getPar(0).asString();
235         toc.push_back(TocItem(pit, 0, str, toolTipText()));
236         // Proceed with the rest of the inset.
237         InsetCollapsable::addToToc(cpit);
238 }
239
240
241 bool InsetNote::isMacroScope() const
242 {
243         // LyX note has no latex output
244         if (params_.type == InsetNoteParams::Note)
245                 return true;
246
247         return InsetCollapsable::isMacroScope();
248 }
249
250
251 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
252 {
253         if (params_.type == InsetNoteParams::Note)
254                 return 0;
255
256         OutputParams runparams(runparams_in);
257         if (params_.type == InsetNoteParams::Comment) {
258                 runparams.inComment = true;
259                 // Ignore files that are exported inside a comment
260                 runparams.exportdata.reset(new ExportData);
261         } 
262
263         odocstringstream ss;
264         InsetCollapsable::latex(ss, runparams);
265         // the space after the comment in 'a[comment] b' will be eaten by the
266         // comment environment since the space before b is ignored with the
267         // following latex output:
268         //
269         // a%
270         // \begin{comment}
271         // comment
272         // \end{comment}
273         //  b
274         //
275         // Adding {} before ' b' fixes this.
276         if (params_.type == InsetNoteParams::Comment)
277                 ss << "{}";
278
279         docstring const str = ss.str();
280         os << str;
281         runparams_in.encoding = runparams.encoding;
282         // Return how many newlines we issued.
283         return int(count(str.begin(), str.end(), '\n'));
284 }
285
286
287 int InsetNote::plaintext(odocstream & os,
288                          OutputParams const & runparams_in) const
289 {
290         if (params_.type == InsetNoteParams::Note)
291                 return 0;
292
293         OutputParams runparams(runparams_in);
294         if (params_.type == InsetNoteParams::Comment) {
295                 runparams.inComment = true;
296                 // Ignore files that are exported inside a comment
297                 runparams.exportdata.reset(new ExportData);
298         }
299         os << '[' << buffer().B_("note") << ":\n";
300         InsetText::plaintext(os, runparams);
301         os << "\n]";
302
303         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
304 }
305
306
307 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
308 {
309         if (params_.type == InsetNoteParams::Note)
310                 return 0;
311
312         OutputParams runparams(runparams_in);
313         if (params_.type == InsetNoteParams::Comment) {
314                 os << "<remark>\n";
315                 runparams.inComment = true;
316                 // Ignore files that are exported inside a comment
317                 runparams.exportdata.reset(new ExportData);
318         }
319
320         int const n = InsetText::docbook(os, runparams);
321
322         if (params_.type == InsetNoteParams::Comment)
323                 os << "\n</remark>\n";
324
325         // Return how many newlines we issued.
326         //return int(count(str.begin(), str.end(), '\n'));
327         return n + 1 + 2;
328 }
329
330
331 docstring InsetNote::xhtml(XHTMLStream & xs, OutputParams const & rp) const
332 {
333         if (params_.type == InsetNoteParams::Note)
334                 return docstring();
335
336         return InsetCollapsable::xhtml(xs, rp);
337 }
338
339
340 void InsetNote::validate(LaTeXFeatures & features) const
341 {
342         switch (params_.type) {
343         case InsetNoteParams::Comment:
344                 features.require("verbatim");
345                 break;
346         case InsetNoteParams::Greyedout:
347                 features.require("color");
348                 features.require("lyxgreyedout");
349                 InsetCollapsable::validate(features);
350                 break;
351         case InsetNoteParams::Note:
352                 break;
353         }
354 }
355
356
357 docstring InsetNote::contextMenuName() const
358 {
359         return from_ascii("context-note");
360 }
361
362 bool InsetNote::allowSpellCheck() const
363 {
364         return (params_.type == InsetNoteParams::Greyedout || lyxrc.spellcheck_notes);
365 }
366
367
368 string InsetNote::params2string(InsetNoteParams const & params)
369 {
370         ostringstream data;
371         data << "note" << ' ';
372         params.write(data);
373         return data.str();
374 }
375
376
377 void InsetNote::string2params(string const & in, InsetNoteParams & params)
378 {
379         params = InsetNoteParams();
380
381         if (in.empty())
382                 return;
383
384         istringstream data(in);
385         Lexer lex;
386         lex.setStream(data);
387         lex.setContext("InsetNote::string2params");
388         lex >> "note";
389         // There are cases, such as when we are called via getStatus() from
390         // Dialog::canApply(), where we are just called with "note" rather
391         // than a full "note Note TYPE".
392         if (!lex.isOK())
393                 return;
394         lex >> "Note";
395
396         params.read(lex);
397 }
398
399
400 } // namespace lyx