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