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