]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Martin's changes to the Note inset.
[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         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 bool InsetNote::showInsetDialog(BufferView * bv) const
112 {
113         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
114         return true;
115 }
116
117
118 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
119 {
120         BufferView * bv = cmd.view();
121
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY:
124                 {
125                 InsetNoteParams params;
126                 InsetNoteMailer::string2params(cmd.argument, params);
127                 params_.type = params.type;
128                 setButtonLabel();
129                 bv->updateInset(this);
130                 return DISPATCHED;
131                 }
132         case LFUN_INSET_EDIT:
133                 if (cmd.button() != mouse_button::button3)
134                         return InsetCollapsable::localDispatch(cmd);
135                 return UNDISPATCHED;
136         case LFUN_INSET_DIALOG_UPDATE:
137                 InsetNoteMailer("note", *this).updateDialog(bv);
138                 return DISPATCHED;
139         case LFUN_MOUSE_RELEASE:
140                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
141                         InsetNoteMailer("note", *this).showDialog(bv);
142                         return DISPATCHED;
143                 }
144                 // fallthrough:
145         default:
146                 return InsetCollapsable::localDispatch(cmd);
147         }
148 }
149
150
151 int InsetNote::latex(Buffer const * buf, ostream & os,
152                                                 LatexRunParams const & runparams) const
153 {
154         string const pt = params_.type;
155
156         int i = 0;
157         if (pt == "Comment")
158                  // verbatim
159                 os << "%\n\\begin{comment}\n";
160         else if (pt == "Greyedout")
161                  // we roll our own macro
162                 os << "%\n\\begin{lyxgreyedout}\n";
163
164         if (pt != "Note")
165                 i = inset.latex(buf, os, runparams);
166
167         if (pt == "Comment") {
168                 os << "%\n\\end{comment}\n";
169                 i += 3;
170         } else if (pt == "Greyedout") {
171                 os << "%\n\\end{lyxgreyedout}\n";
172                 i += 2;
173         }
174         return i;
175 }
176
177
178 int InsetNote::linuxdoc(Buffer const * buf, std::ostream & os) const
179 {
180         string const pt = params_.type;
181
182         int i = 0;
183         if (pt == "Comment")
184                 os << "<comment>\n";
185
186         if (pt != "Note")
187                 i = inset.linuxdoc(buf, os);
188
189         if (pt == "Comment") {
190                 os << "\n</comment>\n";
191                 i += 3;
192         }
193         return i;
194 }
195
196
197 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
198 {
199         string const pt = params_.type;
200
201         int i = 0;
202         if (pt == "Comment")
203                 os << "<remark>\n";
204
205         if (pt != "Note")
206                 i = inset.docbook(buf, os, mixcont);
207
208         if (pt == "Comment") {
209                 os << "\n</remark>\n";
210                 i += 3;
211         }
212         return i;
213 }
214
215
216 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
217 {
218         int i = 0;
219         string const pt = params_.type;
220         if (pt != "Note") {
221                 os << "[";
222                 i = inset.ascii(buf, os, ll);
223                 os << "]";
224         }
225         return i;
226 }
227
228
229 void InsetNote::validate(LaTeXFeatures & features) const
230 {
231         if (params_.type == "Comment")
232                 features.require("verbatim");
233         if (params_.type == "Greyedout") {
234                 features.require("color");
235                 features.require("lyxgreyedout");
236         }
237         inset.validate(features);
238 }
239
240
241
242 InsetNoteMailer::InsetNoteMailer(string const & name,
243                                                 InsetNote & inset)
244         : name_(name), inset_(inset)
245 {
246 }
247
248
249 string const InsetNoteMailer::inset2string(Buffer const &) const
250 {
251         return params2string(name_, inset_.params());
252 }
253
254
255 string const InsetNoteMailer::params2string(string const & name,
256                                 InsetNoteParams const & params)
257 {
258         ostringstream data;
259         data << name << ' ';
260         params.write(data);
261         return STRCONV(data.str());
262 }
263
264
265 void InsetNoteMailer::string2params(string const & in,
266                                      InsetNoteParams & params)
267 {
268         params = InsetNoteParams();
269
270         if (in.empty())
271                 return;
272
273         istringstream data(STRCONV(in));
274         LyXLex lex(0,0);
275         lex.setStream(data);
276         params.read(lex);
277 }
278
279
280 void InsetNoteParams::write(ostream & os) const
281 {
282         os << type << "\n";
283 }
284
285
286 void InsetNoteParams::read(LyXLex & lex)
287 {
288         if (lex.isOK()) {
289                 lex.next();
290                 string token = lex.getString();
291         }
292
293         if (lex.isOK()) {
294                 lex.next();
295                 type = lex.getString();
296         }
297 }