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