]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Remove a whole heap of redundant functions from classes derived from
[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 "debug.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "support/lyxalgo.h"
29 #include "support/std_sstream.h"
30 #include "support/translator.h"
31
32
33 using std::string;
34 using std::auto_ptr;
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38
39
40 namespace {
41
42 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
43
44 NoteTranslator const init_notetranslator() {
45         NoteTranslator translator("Note", InsetNoteParams::Note);
46         translator.addPair("Comment", InsetNoteParams::Comment);
47         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
48         return translator;
49 }
50
51
52 NoteTranslator const init_notetranslator_loc() {
53         NoteTranslator translator(_("Note"), InsetNoteParams::Note);
54         translator.addPair(_("Comment"), InsetNoteParams::Comment);
55         translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
56         return translator;
57 }
58
59
60 NoteTranslator const & notetranslator() {
61         static NoteTranslator translator = init_notetranslator();
62         return translator;
63 }
64
65
66 NoteTranslator const & notetranslator_loc() {
67         static NoteTranslator translator = init_notetranslator_loc();
68         return translator;
69 }
70
71 } // anon
72
73
74
75
76 InsetNoteParams::InsetNoteParams()
77         : type(Note)
78 {}
79
80
81 void InsetNoteParams::write(ostream & os) const
82 {
83         string const label = notetranslator().find(type);
84         os << "Note " << label << "\n";
85         os << 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(FuncRequest const & cmd,
189                          idx_type & idx, pos_type & pos)
190 {
191         BufferView * bv = cmd.view();
192
193         switch (cmd.action) {
194
195         case LFUN_INSET_MODIFY: {
196                 InsetNoteMailer::string2params(cmd.argument, params_);
197                 setButtonLabel();
198                 bv->update();
199                 return DispatchResult(true, true);
200         }
201
202         case LFUN_INSET_DIALOG_UPDATE:
203                 InsetNoteMailer(*this).updateDialog(bv);
204                 return DispatchResult(true, true);
205
206         case LFUN_MOUSE_RELEASE:
207                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
208                         InsetNoteMailer(*this).showDialog(bv);
209                         return DispatchResult(true, true);
210                 }
211                 // fallthrough:
212
213         default:
214                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
215         }
216 }
217
218
219 int InsetNote::latex(Buffer const & buf, ostream & os,
220                      OutputParams const & runparams) const
221 {
222         if (params_.type == InsetNoteParams::Note)
223                 return 0;
224
225         string type;
226         if (params_.type == InsetNoteParams::Comment)
227                 type = "comment";
228         else if (params_.type == InsetNoteParams::Greyedout)
229                 type = "lyxgreyedout";
230
231         ostringstream ss;
232         ss << "%\n\\begin{" << type << "}\n";
233         inset.latex(buf, ss, runparams);
234         ss << "%\n\\end{" << type << "}\n";
235
236         string const str = ss.str();
237         os << str;
238         // Return how many newlines we issued.
239         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
240 }
241
242
243 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
244                         OutputParams const & runparams) const
245 {
246         if (params_.type == InsetNoteParams::Note)
247                 return 0;
248
249         ostringstream ss;
250         if (params_.type == InsetNoteParams::Comment)
251                 ss << "<comment>\n";
252
253         inset.linuxdoc(buf, ss, runparams);
254
255         if (params_.type == InsetNoteParams::Comment)
256                 ss << "\n</comment>\n";
257
258         string const str = ss.str();
259         os << str;
260         // Return how many newlines we issued.
261         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
262 }
263
264
265 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
266                        OutputParams const & runparams) const
267 {
268         if (params_.type == InsetNoteParams::Note)
269                 return 0;
270
271         ostringstream ss;
272         if (params_.type == InsetNoteParams::Comment)
273                 ss << "<remark>\n";
274
275         inset.docbook(buf, ss, runparams);
276
277         if (params_.type == InsetNoteParams::Comment)
278                 ss << "\n</remark>\n";
279
280         string const str = ss.str();
281         os << str;
282         // Return how many newlines we issued.
283         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
284 }
285
286
287 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
288                      OutputParams const & runparams) const
289 {
290         if (params_.type == InsetNoteParams::Note)
291                 return 0;
292
293         ostringstream ss;
294         ss << "[";
295         inset.plaintext(buf, ss, runparams);
296         ss << "]";
297
298         string const str = ss.str();
299         os << str;
300         // Return how many newlines we issued.
301         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
302 }
303
304
305 void InsetNote::validate(LaTeXFeatures & features) const
306 {
307         if (params_.type == InsetNoteParams::Comment)
308                 features.require("verbatim");
309         if (params_.type == InsetNoteParams::Greyedout) {
310                 features.require("color");
311                 features.require("lyxgreyedout");
312         }
313         inset.validate(features);
314 }
315
316
317
318 string const InsetNoteMailer:: name_("note");
319
320 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
321         : inset_(inset)
322 {}
323
324
325 string const InsetNoteMailer::inset2string(Buffer const &) const
326 {
327         return params2string(inset_.params());
328 }
329
330
331 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
332 {
333         ostringstream data;
334         data << name_ << ' ';
335         params.write(data);
336         return data.str();
337 }
338
339
340 void InsetNoteMailer::string2params(string const & in,
341                                     InsetNoteParams & params)
342 {
343         params = InsetNoteParams();
344
345         if (in.empty())
346                 return;
347
348         istringstream data(in);
349         LyXLex lex(0,0);
350         lex.setStream(data);
351
352         string name;
353         lex >> name;
354         if (!lex || name != name_)
355                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
356
357         // This is part of the inset proper that is usually swallowed
358         // by LyXText::readInset
359         string id;
360         lex >> id;
361         if (!lex || id != "Note")
362                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
363
364         params.read(lex);
365 }