]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
the clone auto_ptr patch
[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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "Lsstream.h"
14
15 #include "insetnote.h"
16 #include "gettext.h"
17 #include "lyxfont.h"
18 #include "language.h"
19 #include "buffer.h"
20 #include "BufferView.h"
21 #include "lyxlex.h"
22 #include "lyxtext.h"
23 #include "insets/insettext.h"
24 #include "support/LOstream.h"
25 #include "support/lstrings.h"
26 #include "debug.h"
27
28 using std::ostream;
29 using std::auto_ptr;
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         setLabel(label);
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         setLabel(params_.type);
95         if (params_.type == "Note" || params_.type == "Comment") {
96                 font.setColor(LColor::note);
97                 setBackgroundColor(LColor::notebg);
98         } else {
99                 font.setColor(LColor::red);
100                 setBackgroundColor(LColor::background);
101         }
102         setLabelFont(font);
103 }
104
105
106 bool InsetNote::showInsetDialog(BufferView * bv) const
107 {
108         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
109         return true;
110 }
111
112
113 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
114 {
115         BufferView * bv = cmd.view();
116
117         switch (cmd.action) {
118         case LFUN_INSET_MODIFY:
119                 {
120                 InsetNoteParams params;
121                 InsetNoteMailer::string2params(cmd.argument, params);
122                 params_.type = params.type;
123                 setButtonLabel();
124                 bv->updateInset(this);
125                 return DISPATCHED;
126                 }
127         case LFUN_INSET_EDIT:
128                 if (cmd.button() != mouse_button::button3)
129                         return InsetCollapsable::localDispatch(cmd);
130                 return UNDISPATCHED;
131         case LFUN_INSET_DIALOG_UPDATE:
132                 InsetNoteMailer("note", *this).updateDialog(bv);
133                 return DISPATCHED;
134         case LFUN_MOUSE_RELEASE:
135                 if (cmd.button() == mouse_button::button3 && cmd.x < button_length
136                                         && cmd.y >= button_top_y && cmd.y <= button_bottom_y) {
137                         InsetNoteMailer("note", *this).showDialog(bv);
138                         return DISPATCHED;
139                 }
140                 // fallthrough:
141         default:
142                 return InsetCollapsable::localDispatch(cmd);
143         }
144 }
145
146
147 int InsetNote::latex(Buffer const * buf, ostream & os,
148                                                 LatexRunParams const & runparams) const
149 {
150         string const pt = params_.type;
151
152         int i = 0;
153         if (pt == "Comment")
154                 os << "%\n\\begin{comment}\n"; // remember to validate
155         else if (pt == "Greyedout")
156                 os << "%\n\\textcolor[gray]{0.8}{";
157
158         if (pt != "Note")
159                 i = inset.latex(buf, os, runparams);
160
161         if (pt == "Comment") {
162                 os << "%\n\\end{comment}\n";
163                 i += 3;
164         } else if (pt == "Greyedout") {
165                 os << "\\normalcolor%\n}";
166                 i += 2;
167         }
168         return i;
169 }
170
171
172 int InsetNote::linuxdoc(Buffer const *, std::ostream &) const
173 {
174         return 0;
175 }
176
177
178 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
179 {
180         int i = 0;
181         string const pt = params_.type;
182         // incomplete, untested - MV
183         if (pt != "Note")
184                 i = inset.docbook(buf, os, mixcont);
185         return i;
186 }
187
188
189 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
190 {
191         int i = 0;
192         string const pt = params_.type;
193         if (pt != "Note") {
194                 os << "[";
195                 i = inset.ascii(buf, os, ll);
196                 os << "]";
197         }
198         return i;
199 }
200
201
202 void InsetNote::validate(LaTeXFeatures & features) const
203 {
204         if (params_.type == "Comment")
205                 features.require("verbatim");
206         if (params_.type == "Greyedout")
207                 features.require("color");
208         inset.validate(features);
209 }
210
211
212
213 InsetNoteMailer::InsetNoteMailer(string const & name,
214                                                 InsetNote & inset)
215         : name_(name), inset_(inset)
216 {
217 }
218
219
220 string const InsetNoteMailer::inset2string(Buffer const &) const
221 {
222         return params2string(name_, inset_.params());
223 }
224
225
226 string const InsetNoteMailer::params2string(string const & name,
227                                 InsetNoteParams const & params)
228 {
229         ostringstream data;
230         data << name << ' ';
231         params.write(data);
232         return STRCONV(data.str());
233 }
234
235
236 void InsetNoteMailer::string2params(string const & in,
237                                      InsetNoteParams & params)
238 {
239         params = InsetNoteParams();
240
241         if (in.empty())
242                 return;
243
244         istringstream data(STRCONV(in));
245         LyXLex lex(0,0);
246         lex.setStream(data);
247         params.read(lex);
248 }
249
250
251 void InsetNoteParams::write(ostream & os) const
252 {
253         os << type << "\n";
254 }
255
256
257 void InsetNoteParams::read(LyXLex & lex)
258 {
259         if (lex.isOK()) {
260                 lex.next();
261                 string token = lex.getString();
262         }
263
264         if (lex.isOK()) {
265                 lex.next();
266                 type = lex.getString();
267         }
268 }