]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Make all of InsetCollapsable's member variables (save 'inset') private.
[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 "funcrequest.h"
19 #include "gettext.h"
20 #include "LaTeXFeatures.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25
26 #include "support/std_sstream.h"
27
28 using std::auto_ptr;
29 using std::istringstream;
30 using std::ostream;
31 using std::ostringstream;
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 void InsetNote::metrics(MetricsInfo & mi, Dimension & dim) const
114 {
115         InsetCollapsable::metrics(mi, dim);
116         // Contrary to Greyedout, these cannot be construed as part of the
117         // running text: make them stand on their own
118         if (params_.type == "Note" || params_.type == "Comment")
119                 if (isOpen())
120                         dim.wid = mi.base.textwidth;
121         dim_ = dim;
122 }
123
124
125 bool InsetNote::showInsetDialog(BufferView * bv) const
126 {
127         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
128         return true;
129 }
130
131
132 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
133 {
134         BufferView * bv = cmd.view();
135
136         switch (cmd.action) {
137
138         case LFUN_INSET_MODIFY: {
139                 InsetNoteParams params;
140                 InsetNoteMailer::string2params(cmd.argument, params);
141                 params_.type = params.type;
142                 setButtonLabel();
143                 bv->updateInset(this);
144                 return DISPATCHED;
145         }
146
147         case LFUN_INSET_EDIT:
148                 if (cmd.button() == mouse_button::button3)
149                         return UNDISPATCHED;
150                 return InsetCollapsable::localDispatch(cmd);
151
152         case LFUN_INSET_DIALOG_UPDATE:
153                 InsetNoteMailer("note", *this).updateDialog(bv);
154                 return DISPATCHED;
155
156         case LFUN_MOUSE_RELEASE:
157                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
158                         InsetNoteMailer("note", *this).showDialog(bv);
159                         return DISPATCHED;
160                 }
161                 // fallthrough:
162
163         default:
164                 return InsetCollapsable::localDispatch(cmd);
165         }
166 }
167
168
169 int InsetNote::latex(Buffer const & buf, ostream & os,
170                                                 LatexRunParams const & runparams) const
171 {
172         string const pt = params_.type;
173
174         int i = 0;
175         if (pt == "Comment")
176                  // verbatim
177                 os << "%\n\\begin{comment}\n";
178         else if (pt == "Greyedout")
179                  // we roll our own macro
180                 os << "%\n\\begin{lyxgreyedout}\n";
181
182         if (pt != "Note")
183                 i = inset.latex(buf, os, runparams);
184
185         if (pt == "Comment") {
186                 os << "%\n\\end{comment}\n";
187                 i += 4;
188         } else if (pt == "Greyedout") {
189                 os << "%\n\\end{lyxgreyedout}\n";
190                 i += 4;
191         }
192         return i;
193 }
194
195
196 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os) const
197 {
198         string const pt = params_.type;
199
200         int i = 0;
201         if (pt == "Comment")
202                 os << "<comment>\n";
203
204         if (pt != "Note")
205                 i = inset.linuxdoc(buf, os);
206
207         if (pt == "Comment") {
208                 os << "\n</comment>\n";
209                 i += 3;
210         }
211         return i;
212 }
213
214
215 int InsetNote::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
216 {
217         string const pt = params_.type;
218
219         int i = 0;
220         if (pt == "Comment")
221                 os << "<remark>\n";
222
223         if (pt != "Note")
224                 i = inset.docbook(buf, os, mixcont);
225
226         if (pt == "Comment") {
227                 os << "\n</remark>\n";
228                 i += 3;
229         }
230         return i;
231 }
232
233
234 int InsetNote::ascii(Buffer const & buf, std::ostream & os, int ll) const
235 {
236         int i = 0;
237         string const pt = params_.type;
238         if (pt != "Note") {
239                 os << "[";
240                 i = inset.ascii(buf, os, ll);
241                 os << "]";
242         }
243         return i;
244 }
245
246
247 void InsetNote::validate(LaTeXFeatures & features) const
248 {
249         if (params_.type == "Comment")
250                 features.require("verbatim");
251         if (params_.type == "Greyedout") {
252                 features.require("color");
253                 features.require("lyxgreyedout");
254         }
255         inset.validate(features);
256 }
257
258
259
260 InsetNoteMailer::InsetNoteMailer(string const & name,
261                                                 InsetNote & inset)
262         : name_(name), inset_(inset)
263 {
264 }
265
266
267 string const InsetNoteMailer::inset2string(Buffer const &) const
268 {
269         return params2string(name_, inset_.params());
270 }
271
272
273 string const InsetNoteMailer::params2string(string const & name,
274                                 InsetNoteParams const & params)
275 {
276         ostringstream data;
277         data << name << ' ';
278         params.write(data);
279         return data.str();
280 }
281
282
283 void InsetNoteMailer::string2params(string const & in,
284                                      InsetNoteParams & params)
285 {
286         params = InsetNoteParams();
287
288         if (in.empty())
289                 return;
290
291         istringstream data(in);
292         LyXLex lex(0,0);
293         lex.setStream(data);
294         params.read(lex);
295 }
296
297
298 void InsetNoteParams::write(ostream & os) const
299 {
300         os << type << "\n";
301 }
302
303
304 void InsetNoteParams::read(LyXLex & lex)
305 {
306         if (lex.isOK()) {
307                 lex.next();
308                 string token = lex.getString();
309         }
310
311         if (lex.isOK()) {
312                 lex.next();
313                 type = lex.getString();
314         }
315 }