]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
Hope I *now* get it right...
[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\\color[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 * buf, std::ostream & os) const
173 {
174         string const pt = params_.type;
175
176         int i = 0;
177         if (pt == "Comment")
178                 os << "<comment>\n";
179
180         if (pt != "Note")
181                 i = inset.linuxdoc(buf, os);
182
183         if (pt == "Comment") {
184                 os << "\n</comment>\n";
185                 i += 3;
186         }
187         return i;
188 }
189
190
191 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
192 {
193         string const pt = params_.type;
194
195         int i = 0;
196         if (pt == "Comment")
197                 os << "<remark>\n";
198
199         if (pt != "Note")
200                 i = inset.docbook(buf, os, mixcont);
201
202         if (pt == "Comment") {
203                 os << "\n</remark>\n";
204                 i += 3;
205         }
206         return i;
207 }
208
209
210 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
211 {
212         int i = 0;
213         string const pt = params_.type;
214         if (pt != "Note") {
215                 os << "[";
216                 i = inset.ascii(buf, os, ll);
217                 os << "]";
218         }
219         return i;
220 }
221
222
223 void InsetNote::validate(LaTeXFeatures & features) const
224 {
225         if (params_.type == "Comment")
226                 features.require("verbatim");
227         if (params_.type == "Greyedout")
228                 features.require("color");
229         inset.validate(features);
230 }
231
232
233
234 InsetNoteMailer::InsetNoteMailer(string const & name,
235                                                 InsetNote & inset)
236         : name_(name), inset_(inset)
237 {
238 }
239
240
241 string const InsetNoteMailer::inset2string(Buffer const &) const
242 {
243         return params2string(name_, inset_.params());
244 }
245
246
247 string const InsetNoteMailer::params2string(string const & name,
248                                 InsetNoteParams const & params)
249 {
250         ostringstream data;
251         data << name << ' ';
252         params.write(data);
253         return STRCONV(data.str());
254 }
255
256
257 void InsetNoteMailer::string2params(string const & in,
258                                      InsetNoteParams & params)
259 {
260         params = InsetNoteParams();
261
262         if (in.empty())
263                 return;
264
265         istringstream data(STRCONV(in));
266         LyXLex lex(0,0);
267         lex.setStream(data);
268         params.read(lex);
269 }
270
271
272 void InsetNoteParams::write(ostream & os) const
273 {
274         os << type << "\n";
275 }
276
277
278 void InsetNoteParams::read(LyXLex & lex)
279 {
280         if (lex.isOK()) {
281                 lex.next();
282                 string token = lex.getString();
283         }
284
285         if (lex.isOK()) {
286                 lex.next();
287                 type = lex.getString();
288         }
289 }