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