]> git.lyx.org Git - features.git/blob - src/insets/InsetNote.cpp
Make the insets accept LFUN_INSET_SETTINGS. For these insets, LFUN_INSET_SETTINGS...
[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 "MetricsInfo.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 const & 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::editMessage() const
132 {
133         return _("Opened Note Inset");
134 }
135
136
137 docstring InsetNote::name() const 
138 {
139         return from_ascii("Note:" + notetranslator().find(params_.type));
140 }
141
142
143 Inset::DisplayType InsetNote::display() const
144 {
145         return Inline;
146 }
147
148
149 void InsetNote::write(ostream & os) const
150 {
151         params_.write(os);
152         InsetCollapsable::write(os);
153 }
154
155
156 void InsetNote::read(Lexer & lex)
157 {
158         params_.read(lex);
159         InsetCollapsable::read(lex);
160 }
161
162
163 void InsetNote::setButtonLabel()
164 {
165         docstring const label = notetranslator_loc().find(params_.type);
166         setLabel(label);
167 }
168
169
170 bool InsetNote::showInsetDialog(BufferView * bv) const
171 {
172         bv->showDialog("note", params2string(params()),
173                 const_cast<InsetNote *>(this));
174         return true;
175 }
176
177
178 void InsetNote::doDispatch(Cursor & cur, FuncRequest & cmd)
179 {
180         switch (cmd.action) {
181
182         case LFUN_INSET_MODIFY:
183                 string2params(to_utf8(cmd.argument()), params_);
184                 setLayout(buffer().params());
185                 break;
186
187         case LFUN_INSET_DIALOG_UPDATE:
188                 cur.bv().updateDialog("note", params2string(params()));
189                 break;
190
191         default:
192                 InsetCollapsable::doDispatch(cur, cmd);
193                 break;
194         }
195 }
196
197
198 bool InsetNote::getStatus(Cursor & cur, FuncRequest const & cmd,
199                 FuncStatus & flag) const
200 {
201         switch (cmd.action) {
202
203         case LFUN_INSET_MODIFY:
204                 // disallow comment and greyed out in commands
205                 flag.setEnabled(!cur.paragraph().layout().isCommand() ||
206                                 cmd.getArg(2) == "Note");
207                 if (cmd.getArg(0) == "note") {
208                         InsetNoteParams params;
209                         string2params(to_utf8(cmd.argument()), params);
210                         flag.setOnOff(params_.type == params.type);
211                 }
212                 return true;
213
214         case LFUN_INSET_SETTINGS:
215         case LFUN_INSET_DIALOG_UPDATE:
216                 flag.setEnabled(true);
217                 return true;
218
219         default:
220                 return InsetCollapsable::getStatus(cur, cmd, flag);
221         }
222 }
223
224
225 void InsetNote::addToToc(DocIterator const & cpit)
226 {
227         DocIterator pit = cpit;
228         pit.push_back(CursorSlice(*this));
229
230         Toc & toc = buffer().tocBackend().toc("note");
231         docstring str;
232         str = notetranslator_loc().find(params_.type) + from_ascii(": ")
233                 + getNewLabel(str);
234         toc.push_back(TocItem(pit, 0, str));
235         // Proceed with the rest of the inset.
236         InsetCollapsable::addToToc(cpit);
237 }
238
239
240 bool InsetNote::isMacroScope() const
241 {
242         // LyX note has no latex output
243         if (params_.type == InsetNoteParams::Note)
244                 return true;
245
246         return InsetCollapsable::isMacroScope();
247 }
248
249
250 int InsetNote::latex(odocstream & os, OutputParams const & runparams_in) const
251 {
252         if (params_.type == InsetNoteParams::Note)
253                 return 0;
254
255         OutputParams runparams(runparams_in);
256         if (params_.type == InsetNoteParams::Comment) {
257                 runparams.inComment = true;
258                 // Ignore files that are exported inside a comment
259                 runparams.exportdata.reset(new ExportData);
260         } 
261
262         odocstringstream ss;
263         InsetCollapsable::latex(ss, runparams);
264         // the space after the comment in 'a[comment] b' will be eaten by the
265         // comment environment since the space before b is ignored with the
266         // following latex output:
267         //
268         // a%
269         // \begin{comment}
270         // comment
271         // \end{comment}
272         //  b
273         //
274         // Adding {} before ' b' fixes this.
275         if (params_.type == InsetNoteParams::Comment)
276                 ss << "{}";
277
278         docstring const str = ss.str();
279         os << str;
280         runparams_in.encoding = runparams.encoding;
281         // Return how many newlines we issued.
282         return int(count(str.begin(), str.end(), '\n'));
283 }
284
285
286 int InsetNote::plaintext(odocstream & os,
287                          OutputParams const & runparams_in) const
288 {
289         if (params_.type == InsetNoteParams::Note)
290                 return 0;
291
292         OutputParams runparams(runparams_in);
293         if (params_.type == InsetNoteParams::Comment) {
294                 runparams.inComment = true;
295                 // Ignore files that are exported inside a comment
296                 runparams.exportdata.reset(new ExportData);
297         }
298         os << '[' << buffer().B_("note") << ":\n";
299         InsetText::plaintext(os, runparams);
300         os << "\n]";
301
302         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
303 }
304
305
306 int InsetNote::docbook(odocstream & os, OutputParams const & runparams_in) const
307 {
308         if (params_.type == InsetNoteParams::Note)
309                 return 0;
310
311         OutputParams runparams(runparams_in);
312         if (params_.type == InsetNoteParams::Comment) {
313                 os << "<remark>\n";
314                 runparams.inComment = true;
315                 // Ignore files that are exported inside a comment
316                 runparams.exportdata.reset(new ExportData);
317         }
318
319         int const n = InsetText::docbook(os, runparams);
320
321         if (params_.type == InsetNoteParams::Comment)
322                 os << "\n</remark>\n";
323
324         // Return how many newlines we issued.
325         //return int(count(str.begin(), str.end(), '\n'));
326         return n + 1 + 2;
327 }
328
329
330 void InsetNote::validate(LaTeXFeatures & features) const
331 {
332         switch (params_.type) {
333         case InsetNoteParams::Comment:
334                 features.require("verbatim");
335                 break;
336         case InsetNoteParams::Greyedout:
337                 features.require("color");
338                 features.require("lyxgreyedout");
339                 InsetCollapsable::validate(features);
340                 break;
341         case InsetNoteParams::Note:
342                 break;
343         }
344 }
345
346
347 docstring InsetNote::contextMenu(BufferView const &, int, int) const
348 {
349         return from_ascii("context-note");
350 }
351
352
353 string InsetNote::params2string(InsetNoteParams const & params)
354 {
355         ostringstream data;
356         data << "note" << ' ';
357         params.write(data);
358         return data.str();
359 }
360
361
362 void InsetNote::string2params(string const & in, InsetNoteParams & params)
363 {
364         params = InsetNoteParams();
365
366         if (in.empty())
367                 return;
368
369         istringstream data(in);
370         Lexer lex;
371         lex.setStream(data);
372         lex.setContext("InsetNote::string2params");
373         lex >> "note";
374         // There are cases, such as when we are called via getStatus() from
375         // Dialog::canApply(), where we are just called with "note" rather
376         // than a full "note Note TYPE".
377         if (!lex.isOK())
378                 return;
379         lex >> "Note";
380
381         params.read(lex);
382 }
383
384
385 bool mutateNotes(Cursor & cur, string const & source, string const & target)
386 {
387         InsetNoteParams::Type typeSrc = notetranslator().find(source);
388         InsetNoteParams::Type typeTrt = notetranslator().find(target);
389         // syntax check of arguments
390         string src = notetranslator().find(typeSrc);
391         string trt = notetranslator().find(typeTrt);
392         if (src != source || trt != target)
393                 return false;
394
395         // did we found some conforming inset?
396         bool ret = false;
397
398         cur.beginUndoGroup();
399         Inset & inset = cur.buffer()->inset();
400         InsetIterator it  = inset_iterator_begin(inset);
401         InsetIterator const end = inset_iterator_end(inset);
402         for (; it != end; ++it) {
403                 if (it->lyxCode() == NOTE_CODE) {
404                         InsetNote & ins = static_cast<InsetNote &>(*it);
405                         if (ins.params().type == typeSrc) {
406                                 cur.buffer()->undo().recordUndo(it);
407                                 FuncRequest fr(LFUN_INSET_MODIFY, "note Note " + target);
408                                 ins.dispatch(cur, fr);
409                                 ret = true;
410                         }
411                 }
412         }
413         cur.endUndoGroup();
414
415         return ret;
416 }
417
418 } // namespace lyx