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