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