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