]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Small cleanup, removal of self-aggrandizing comment
[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 }
47
48
49 InsetNote::InsetNote(InsetNote const & in)
50         : InsetCollapsable(in), params_(in.params_)
51 {
52         init();
53 }
54
55
56 InsetNote::~InsetNote()
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 (isOpen())
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                 InsetNoteMailer::string2params(cmd.argument, params_);
139                 setButtonLabel();
140                 bv->updateInset(this);
141                 return DISPATCHED;
142         }
143
144         case LFUN_INSET_EDIT:
145                 if (cmd.button() == mouse_button::button3)
146                         return UNDISPATCHED;
147                 return InsetCollapsable::localDispatch(cmd);
148
149         case LFUN_INSET_DIALOG_UPDATE:
150                 InsetNoteMailer("note", *this).updateDialog(bv);
151                 return DISPATCHED;
152
153         case LFUN_MOUSE_RELEASE:
154                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
155                         InsetNoteMailer("note", *this).showDialog(bv);
156                         return DISPATCHED;
157                 }
158                 // fallthrough:
159
160         default:
161                 return InsetCollapsable::localDispatch(cmd);
162         }
163 }
164
165
166 int InsetNote::latex(Buffer const & buf, ostream & os,
167                                                 LatexRunParams const & runparams) const
168 {
169         string const pt = params_.type;
170
171         int i = 0;
172         if (pt == "Comment")
173                  // verbatim
174                 os << "%\n\\begin{comment}\n";
175         else if (pt == "Greyedout")
176                  // we roll our own macro
177                 os << "%\n\\begin{lyxgreyedout}\n";
178
179         if (pt != "Note")
180                 i = inset.latex(buf, os, runparams);
181
182         if (pt == "Comment") {
183                 os << "%\n\\end{comment}\n";
184                 i += 4;
185         } else if (pt == "Greyedout") {
186                 os << "%\n\\end{lyxgreyedout}\n";
187                 i += 4;
188         }
189         return i;
190 }
191
192
193 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os) const
194 {
195         string const pt = params_.type;
196
197         int i = 0;
198         if (pt == "Comment")
199                 os << "<comment>\n";
200
201         if (pt != "Note")
202                 i = inset.linuxdoc(buf, os);
203
204         if (pt == "Comment") {
205                 os << "\n</comment>\n";
206                 i += 3;
207         }
208         return i;
209 }
210
211
212 int InsetNote::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
213 {
214         string const pt = params_.type;
215
216         int i = 0;
217         if (pt == "Comment")
218                 os << "<remark>\n";
219
220         if (pt != "Note")
221                 i = inset.docbook(buf, os, mixcont);
222
223         if (pt == "Comment") {
224                 os << "\n</remark>\n";
225                 i += 3;
226         }
227         return i;
228 }
229
230
231 int InsetNote::ascii(Buffer const & buf, std::ostream & os, int ll) const
232 {
233         int i = 0;
234         string const pt = params_.type;
235         if (pt != "Note") {
236                 os << "[";
237                 i = inset.ascii(buf, os, ll);
238                 os << "]";
239         }
240         return i;
241 }
242
243
244 void InsetNote::validate(LaTeXFeatures & features) const
245 {
246         if (params_.type == "Comment")
247                 features.require("verbatim");
248         if (params_.type == "Greyedout") {
249                 features.require("color");
250                 features.require("lyxgreyedout");
251         }
252         inset.validate(features);
253 }
254
255
256
257 InsetNoteMailer::InsetNoteMailer(string const & name,
258                                                 InsetNote & inset)
259         : name_(name), inset_(inset)
260 {
261 }
262
263
264 string const InsetNoteMailer::inset2string(Buffer const &) const
265 {
266         return params2string(name_, inset_.params());
267 }
268
269
270 string const InsetNoteMailer::params2string(string const & name,
271                                 InsetNoteParams const & params)
272 {
273         ostringstream data;
274         data << name << ' ';
275         params.write(data);
276         return data.str();
277 }
278
279
280 void InsetNoteMailer::string2params(string const & in,
281                                      InsetNoteParams & params)
282 {
283         params = InsetNoteParams();
284
285         if (in.empty())
286                 return;
287
288         istringstream data(in);
289         LyXLex lex(0,0);
290         lex.setStream(data);
291         params.read(lex);
292 }
293
294
295 void InsetNoteParams::write(ostream & os) const
296 {
297         os << type << "\n";
298 }
299
300
301 void InsetNoteParams::read(LyXLex & lex)
302 {
303         if (lex.isOK()) {
304                 lex.next();
305                 string token = lex.getString();
306         }
307
308         if (lex.isOK()) {
309                 lex.next();
310                 type = lex.getString();
311         }
312 }