]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Clean-up of the Note, Branch mailer interface.
[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 "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26
27 #include "support/std_sstream.h"
28
29
30 using std::string;
31 using std::auto_ptr;
32 using std::istringstream;
33 using std::ostream;
34 using std::ostringstream;
35
36
37 void InsetNote::init()
38 {
39         setInsetName("Note");
40         setButtonLabel();
41 }
42
43
44 InsetNote::InsetNote(BufferParams const & bp, string const & label)
45         : InsetCollapsable(bp)
46 {
47         params_.type = label;
48         init();
49 }
50
51
52 InsetNote::InsetNote(InsetNote const & in)
53         : InsetCollapsable(in), params_(in.params_)
54 {
55         init();
56 }
57
58
59 InsetNote::~InsetNote()
60 {
61         InsetNoteMailer(*this).hideDialog();
62 }
63
64
65 auto_ptr<InsetBase> InsetNote::clone() const
66 {
67         return auto_ptr<InsetBase>(new InsetNote(*this));
68 }
69
70
71 string const InsetNote::editMessage() const
72 {
73         return _("Opened Note Inset");
74 }
75
76
77 void InsetNote::write(Buffer const & buf, ostream & os) const
78 {
79         params_.write(os);
80         InsetCollapsable::write(buf, os);
81 }
82
83
84 void InsetNote::read(Buffer const & buf, LyXLex & lex)
85 {
86         InsetCollapsable::read(buf, lex);
87         setButtonLabel();
88 }
89
90
91 void InsetNote::setButtonLabel()
92 {
93         LyXFont font(LyXFont::ALL_SANE);
94         font.decSize();
95         font.decSize();
96
97         if (params_.type == "Note") {
98                 setLabel(_("LyX Note"));
99                 font.setColor(LColor::note);
100                 setBackgroundColor(LColor::notebg);
101         } else if (params_.type == "Comment") {
102                 setLabel(_("Comment"));
103                 font.setColor(LColor::comment);
104                 setBackgroundColor(LColor::commentbg);
105         } else {
106                 setLabel(_("Greyed Out"));
107                 font.setColor(LColor::greyedout);
108                 setBackgroundColor(LColor::greyedoutbg);
109         }
110         setLabelFont(font);
111 }
112
113
114 bool InsetNote::showInsetDialog(BufferView * bv) const
115 {
116         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
117         return true;
118 }
119
120
121 DispatchResult
122 InsetNote::priv_dispatch(FuncRequest const & cmd,
123                          idx_type & idx, pos_type & pos)
124 {
125         BufferView * bv = cmd.view();
126
127         switch (cmd.action) {
128
129         case LFUN_INSET_MODIFY: {
130                 InsetNoteMailer::string2params(cmd.argument, params_);
131                 setButtonLabel();
132                 bv->update();
133                 return DispatchResult(true, true);
134         }
135
136         case LFUN_INSET_DIALOG_UPDATE:
137                 InsetNoteMailer(*this).updateDialog(bv);
138                 return DispatchResult(true, true);
139
140         case LFUN_MOUSE_RELEASE:
141                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
142                         InsetNoteMailer(*this).showDialog(bv);
143                         return DispatchResult(true, true);
144                 }
145                 // fallthrough:
146
147         default:
148                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
149         }
150 }
151
152
153 int InsetNote::latex(Buffer const & buf, ostream & os,
154                      OutputParams const & runparams) const
155 {
156         string const pt = params_.type;
157
158         int i = 0;
159         if (pt == "Comment")
160                  // verbatim
161                 os << "%\n\\begin{comment}\n";
162         else if (pt == "Greyedout")
163                  // we roll our own macro
164                 os << "%\n\\begin{lyxgreyedout}\n";
165
166         if (pt != "Note")
167                 i = inset.latex(buf, os, runparams);
168
169         if (pt == "Comment") {
170                 os << "%\n\\end{comment}\n";
171                 i += 4;
172         } else if (pt == "Greyedout") {
173                 os << "%\n\\end{lyxgreyedout}\n";
174                 i += 4;
175         }
176         return i;
177 }
178
179
180 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
181                         OutputParams const & runparams) const
182 {
183         string const pt = params_.type;
184
185         int i = 0;
186         if (pt == "Comment")
187                 os << "<comment>\n";
188
189         if (pt != "Note")
190                 i = inset.linuxdoc(buf, os, runparams);
191
192         if (pt == "Comment") {
193                 os << "\n</comment>\n";
194                 i += 3;
195         }
196         return i;
197 }
198
199
200 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
201                        OutputParams const & runparams) const
202 {
203         string const pt = params_.type;
204
205         int i = 0;
206         if (pt == "Comment")
207                 os << "<remark>\n";
208
209         if (pt != "Note")
210                 i = inset.docbook(buf, os, runparams);
211
212         if (pt == "Comment") {
213                 os << "\n</remark>\n";
214                 i += 3;
215         }
216         return i;
217 }
218
219
220 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
221                      OutputParams const & runparams) const
222 {
223         int i = 0;
224         string const pt = params_.type;
225         if (pt != "Note") {
226                 os << "[";
227                 i = inset.plaintext(buf, os, runparams);
228                 os << "]";
229         }
230         return i;
231 }
232
233
234 void InsetNote::validate(LaTeXFeatures & features) const
235 {
236         if (params_.type == "Comment")
237                 features.require("verbatim");
238         if (params_.type == "Greyedout") {
239                 features.require("color");
240                 features.require("lyxgreyedout");
241         }
242         inset.validate(features);
243 }
244
245
246
247 string const InsetNoteMailer:: name_("note");
248
249 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
250         : inset_(inset)
251 {}
252
253
254 string const InsetNoteMailer::inset2string(Buffer const &) const
255 {
256         return params2string(inset_.params());
257 }
258
259
260 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
261 {
262         ostringstream data;
263         data << name_ << ' ';
264         params.write(data);
265         return data.str();
266 }
267
268
269 void InsetNoteMailer::string2params(string const & in,
270                                     InsetNoteParams & params)
271 {
272         params = InsetNoteParams();
273
274         if (in.empty())
275                 return;
276
277         istringstream data(in);
278         LyXLex lex(0,0);
279         lex.setStream(data);
280         params.read(lex);
281 }
282
283
284 void InsetNoteParams::write(ostream & os) const
285 {
286         os << type << "\n";
287 }
288
289
290 void InsetNoteParams::read(LyXLex & lex)
291 {
292         if (lex.isOK()) {
293                 lex.next();
294                 string token = lex.getString();
295         }
296
297         if (lex.isOK()) {
298                 lex.next();
299                 type = lex.getString();
300         }
301 }