]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Changes due to the removal of using directives from support/std_sstream.h.
[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
24 #include "support/std_sstream.h"
25
26 using std::auto_ptr;
27 using std::istringstream;
28 using std::ostream;
29 using std::ostringstream;
30
31
32 void InsetNote::init()
33 {
34         setInsetName("Note");
35         setButtonLabel();
36 }
37
38
39 InsetNote::InsetNote(BufferParams const & bp, string const & label)
40         : InsetCollapsable(bp)
41 {
42         params_.type = label;
43         init();
44         setButtonLabel();
45 }
46
47
48 InsetNote::InsetNote(InsetNote const & in)
49         : InsetCollapsable(in), params_(in.params_)
50 {
51         init();
52 }
53
54
55 InsetNote::~InsetNote() // MV
56 {
57         InsetNoteMailer mailer("note", *this);
58         mailer.hideDialog();
59 }
60
61
62 auto_ptr<InsetBase> InsetNote::clone() const
63 {
64         return auto_ptr<InsetBase>(new InsetNote(*this));
65 }
66
67
68 string const InsetNote::editMessage() const
69 {
70         return _("Opened Note Inset");
71 }
72
73
74 void InsetNote::write(Buffer const & buf, ostream & os) const
75 {
76         params_.write(os);
77         InsetCollapsable::write(buf, os);
78 }
79
80
81 void InsetNote::read(Buffer const & buf, LyXLex & lex)
82 {
83         InsetCollapsable::read(buf, lex);
84         setButtonLabel();
85 }
86
87
88 void InsetNote::setButtonLabel()
89 {
90         LyXFont font(LyXFont::ALL_SANE);
91         font.decSize();
92         font.decSize();
93
94         if (params_.type == "Note") {
95                 setLabel(_("LyX Note"));
96                 font.setColor(LColor::note);
97                 setBackgroundColor(LColor::notebg);
98         } else if (params_.type == "Comment") {
99                 setLabel(_("Comment"));
100                 font.setColor(LColor::comment);
101                 setBackgroundColor(LColor::commentbg);
102         } else {
103                 setLabel(_("Greyed Out"));
104                 font.setColor(LColor::greyedout);
105                 setBackgroundColor(LColor::greyedoutbg);
106         }
107         setLabelFont(font);
108 }
109
110
111 void InsetNote::metrics(MetricsInfo & mi, Dimension & dim) const
112 {
113         InsetCollapsable::metrics(mi, dim);
114         // Contrary to Greyedout, these cannot be construed as part of the
115         // running text: make them stand on their own
116         if (params_.type == "Note" || params_.type == "Comment")
117                 if (!collapsed_)
118                         dim.wid = mi.base.textwidth;
119         dim_ = dim;
120 }
121
122
123 bool InsetNote::showInsetDialog(BufferView * bv) const
124 {
125         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
126         return true;
127 }
128
129
130 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
131 {
132         BufferView * bv = cmd.view();
133
134         switch (cmd.action) {
135
136         case LFUN_INSET_MODIFY: {
137                 InsetNoteParams params;
138                 InsetNoteMailer::string2params(cmd.argument, params);
139                 params_.type = params.type;
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 STRCONV(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(STRCONV(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 }