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