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