]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
more cursor dispatch
[lyx.git] / src / insets / insetnote.C
1 /**
2  * \file insetnote.C
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 "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlex.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "support/lyxalgo.h"
30 #include "support/std_sstream.h"
31 #include "support/translator.h"
32
33
34 using std::string;
35 using std::auto_ptr;
36 using std::istringstream;
37 using std::ostream;
38 using std::ostringstream;
39
40
41 namespace {
42
43 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
44
45 NoteTranslator const init_notetranslator() {
46         NoteTranslator translator("Note", InsetNoteParams::Note);
47         translator.addPair("Comment", InsetNoteParams::Comment);
48         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
49         return translator;
50 }
51
52
53 NoteTranslator const init_notetranslator_loc() {
54         NoteTranslator translator(_("Note"), InsetNoteParams::Note);
55         translator.addPair(_("Comment"), InsetNoteParams::Comment);
56         translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
57         return translator;
58 }
59
60
61 NoteTranslator const & notetranslator() {
62         static NoteTranslator translator = init_notetranslator();
63         return translator;
64 }
65
66
67 NoteTranslator const & notetranslator_loc() {
68         static NoteTranslator translator = init_notetranslator_loc();
69         return translator;
70 }
71
72 } // anon
73
74
75
76
77 InsetNoteParams::InsetNoteParams()
78         : type(Note)
79 {}
80
81
82 void InsetNoteParams::write(ostream & os) const
83 {
84         string const label = notetranslator().find(type);
85         os << "Note " << label << "\n";
86 }
87
88
89 void InsetNoteParams::read(LyXLex & lex)
90 {
91         string label;
92         lex >> label;
93         if (lex)
94                 type = notetranslator().find(label);
95 }
96
97
98 void InsetNote::init()
99 {
100         setInsetName("Note");
101         setButtonLabel();
102 }
103
104
105 InsetNote::InsetNote(BufferParams const & bp, string const & label)
106         : InsetCollapsable(bp)
107 {
108         params_.type = notetranslator().find(label);
109         init();
110 }
111
112
113 InsetNote::InsetNote(InsetNote const & in)
114         : InsetCollapsable(in), params_(in.params_)
115 {
116         init();
117 }
118
119
120 InsetNote::~InsetNote()
121 {
122         InsetNoteMailer(*this).hideDialog();
123 }
124
125
126 auto_ptr<InsetBase> InsetNote::clone() const
127 {
128         return auto_ptr<InsetBase>(new InsetNote(*this));
129 }
130
131
132 string const InsetNote::editMessage() const
133 {
134         return _("Opened Note Inset");
135 }
136
137
138 void InsetNote::write(Buffer const & buf, ostream & os) const
139 {
140         params_.write(os);
141         InsetCollapsable::write(buf, os);
142 }
143
144
145 void InsetNote::read(Buffer const & buf, LyXLex & lex)
146 {
147         params_.read(lex);
148         InsetCollapsable::read(buf, lex);
149         setButtonLabel();
150 }
151
152
153 void InsetNote::setButtonLabel()
154 {
155         string const label = notetranslator_loc().find(params_.type);
156         setLabel(label);
157
158         LyXFont font(LyXFont::ALL_SANE);
159         font.decSize();
160         font.decSize();
161
162         switch (params_.type) {
163         case InsetNoteParams::Note:
164                 font.setColor(LColor::note);
165                 setBackgroundColor(LColor::notebg);
166                 break;
167         case InsetNoteParams::Comment:
168                 font.setColor(LColor::comment);
169                 setBackgroundColor(LColor::commentbg);
170                 break;
171         case InsetNoteParams::Greyedout:
172                 font.setColor(LColor::greyedout);
173                 setBackgroundColor(LColor::greyedoutbg);
174                 break;
175         }
176         setLabelFont(font);
177 }
178
179
180 bool InsetNote::showInsetDialog(BufferView * bv) const
181 {
182         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
183         return true;
184 }
185
186
187 DispatchResult
188 InsetNote::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
189 {
190         switch (cmd.action) {
191
192         case LFUN_INSET_MODIFY: {
193                 InsetNoteMailer::string2params(cmd.argument, params_);
194                 setButtonLabel();
195                 cur.bv().update();
196                 return DispatchResult(true, true);
197         }
198
199         case LFUN_INSET_DIALOG_UPDATE:
200                 InsetNoteMailer(*this).updateDialog(&cur.bv());
201                 return DispatchResult(true, true);
202
203         case LFUN_MOUSE_RELEASE:
204                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
205                         InsetNoteMailer(*this).showDialog(&cur.bv());
206                         return DispatchResult(true, true);
207                 }
208                 return InsetCollapsable::priv_dispatch(cur, cmd);
209
210         default:
211                 return InsetCollapsable::priv_dispatch(cur, cmd);
212         }
213 }
214
215
216 int InsetNote::latex(Buffer const & buf, ostream & os,
217                      OutputParams const & runparams) const
218 {
219         if (params_.type == InsetNoteParams::Note)
220                 return 0;
221
222         string type;
223         if (params_.type == InsetNoteParams::Comment)
224                 type = "comment";
225         else if (params_.type == InsetNoteParams::Greyedout)
226                 type = "lyxgreyedout";
227
228         ostringstream ss;
229         ss << "%\n\\begin{" << type << "}\n";
230         inset.latex(buf, ss, runparams);
231         ss << "%\n\\end{" << type << "}\n";
232
233         string const str = ss.str();
234         os << str;
235         // Return how many newlines we issued.
236         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
237 }
238
239
240 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
241                         OutputParams const & runparams) const
242 {
243         if (params_.type == InsetNoteParams::Note)
244                 return 0;
245
246         ostringstream ss;
247         if (params_.type == InsetNoteParams::Comment)
248                 ss << "<comment>\n";
249
250         inset.linuxdoc(buf, ss, runparams);
251
252         if (params_.type == InsetNoteParams::Comment)
253                 ss << "\n</comment>\n";
254
255         string const str = ss.str();
256         os << str;
257         // Return how many newlines we issued.
258         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
259 }
260
261
262 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
263                        OutputParams const & runparams) const
264 {
265         if (params_.type == InsetNoteParams::Note)
266                 return 0;
267
268         ostringstream ss;
269         if (params_.type == InsetNoteParams::Comment)
270                 ss << "<remark>\n";
271
272         inset.docbook(buf, ss, runparams);
273
274         if (params_.type == InsetNoteParams::Comment)
275                 ss << "\n</remark>\n";
276
277         string const str = ss.str();
278         os << str;
279         // Return how many newlines we issued.
280         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
281 }
282
283
284 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
285                      OutputParams const & runparams) const
286 {
287         if (params_.type == InsetNoteParams::Note)
288                 return 0;
289
290         ostringstream ss;
291         ss << "[";
292         inset.plaintext(buf, ss, runparams);
293         ss << "]";
294
295         string const str = ss.str();
296         os << str;
297         // Return how many newlines we issued.
298         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
299 }
300
301
302 void InsetNote::validate(LaTeXFeatures & features) const
303 {
304         if (params_.type == InsetNoteParams::Comment)
305                 features.require("verbatim");
306         if (params_.type == InsetNoteParams::Greyedout) {
307                 features.require("color");
308                 features.require("lyxgreyedout");
309         }
310         inset.validate(features);
311 }
312
313
314
315 string const InsetNoteMailer:: name_("note");
316
317 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
318         : inset_(inset)
319 {}
320
321
322 string const InsetNoteMailer::inset2string(Buffer const &) const
323 {
324         return params2string(inset_.params());
325 }
326
327
328 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
329 {
330         ostringstream data;
331         data << name_ << ' ';
332         params.write(data);
333         return data.str();
334 }
335
336
337 void InsetNoteMailer::string2params(string const & in,
338                                     InsetNoteParams & params)
339 {
340         params = InsetNoteParams();
341
342         if (in.empty())
343                 return;
344
345         istringstream data(in);
346         LyXLex lex(0,0);
347         lex.setStream(data);
348
349         string name;
350         lex >> name;
351         if (!lex || name != name_)
352                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
353
354         // This is part of the inset proper that is usually swallowed
355         // by LyXText::readInset
356         string id;
357         lex >> id;
358         if (!lex || id != "Note")
359                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
360
361         params.read(lex);
362 }