]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
The inset mailers' inset2string function now requires a Buffer const & arg.
[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
30
31 void InsetNote::init()
32 {
33         setInsetName("Note");
34         setButtonLabel();
35 }
36
37
38 InsetNote::InsetNote(BufferParams const & bp, string const & label)
39         : InsetCollapsable(bp)
40 {
41         params_.type = label;
42         init();
43         setLabel(label);
44 }
45
46
47 InsetNote::InsetNote(InsetNote const & in)
48         : InsetCollapsable(in), params_(in.params_)
49 {
50         init();
51 }
52
53
54 InsetNote::~InsetNote() // MV
55 {
56         InsetNoteMailer mailer("note", *this);
57         mailer.hideDialog();
58 }
59
60
61 InsetBase * InsetNote::clone() const
62 {
63         return new InsetNote(*this);
64 }
65
66
67 string const InsetNote::editMessage() const
68 {
69         return _("Opened Note Inset");
70 }
71
72
73 void InsetNote::write(Buffer const * buf, ostream & os) const
74 {
75         params_.write(os);
76         InsetCollapsable::write(buf, os);
77 }
78
79
80 void InsetNote::read(Buffer const * buf, LyXLex & lex)
81 {
82         InsetCollapsable::read(buf, lex);
83         setButtonLabel();
84 }
85
86
87 void InsetNote::setButtonLabel()
88 {
89         LyXFont font(LyXFont::ALL_SANE);
90         font.decSize();
91         font.decSize();
92
93         setLabel(params_.type);
94         if (params_.type == "Note" || params_.type == "Comment") {
95                 font.setColor(LColor::note);
96                 setBackgroundColor(LColor::notebg);
97         } else {
98                 font.setColor(LColor::red);
99                 setBackgroundColor(LColor::background);
100         }
101         setLabelFont(font);
102 }
103
104
105 bool InsetNote::showInsetDialog(BufferView * bv) const
106 {
107         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
108         return true;
109 }
110
111
112 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
113 {
114         BufferView * bv = cmd.view();
115
116         switch (cmd.action) {
117         case LFUN_INSET_MODIFY:
118                 {
119                 InsetNoteParams params;
120                 InsetNoteMailer::string2params(cmd.argument, params);
121                 params_.type = params.type;
122                 setButtonLabel();
123                 bv->updateInset(this);
124                 return DISPATCHED;
125                 }
126         case LFUN_INSET_EDIT:
127                 if (cmd.button() != mouse_button::button3) 
128                         return InsetCollapsable::localDispatch(cmd);
129                 return UNDISPATCHED;
130         case LFUN_INSET_DIALOG_UPDATE:
131                 InsetNoteMailer("note", *this).updateDialog(bv);
132                 return DISPATCHED;
133         case LFUN_MOUSE_RELEASE:
134                 if (cmd.button() == mouse_button::button3 && cmd.x < button_length 
135                                         && cmd.y >= button_top_y && cmd.y <= button_bottom_y) {
136                         InsetNoteMailer("note", *this).showDialog(bv);
137                         return DISPATCHED;
138                 }
139                 // fallthrough:
140         default:
141                 return InsetCollapsable::localDispatch(cmd);
142         }
143 }
144
145
146 int InsetNote::latex(Buffer const * buf, ostream & os,
147                                                 LatexRunParams const & runparams) const
148 {
149         string const pt = params_.type;
150
151         int i = 0;
152         if (pt == "Comment")
153                 os << "%\n\\begin{comment}\n"; // remember to validate
154         else if (pt == "Greyedout")
155                 os << "%\n\\textcolor[gray]{0.8}{";
156
157         if (pt != "Note")
158                 i = inset.latex(buf, os, runparams);
159
160         if (pt == "Comment") {
161                 os << "%\n\\end{comment}\n";
162                 i += 3;
163         } else if (pt == "Greyedout") { 
164                 os << "\\normalcolor%\n}";
165                 i += 2;
166         }
167         return i;
168 }
169
170
171 int InsetNote::linuxdoc(Buffer const *, std::ostream &) const
172
173         return 0; 
174 }
175
176
177 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
178 {
179         int i = 0;
180         string const pt = params_.type;
181         // incomplete, untested - MV
182         if (pt != "Note") 
183                 i = inset.docbook(buf, os, mixcont);
184         return i; 
185 }
186
187
188 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
189 {
190         int i = 0;
191         string const pt = params_.type;
192         if (pt != "Note") {
193                 os << "[";
194                 i = inset.ascii(buf, os, ll);
195                 os << "]";
196         }
197         return i;
198 }
199
200
201 void InsetNote::validate(LaTeXFeatures & features) const
202 {
203         if (params_.type == "Comment") 
204                 features.require("verbatim");
205         if (params_.type == "Greyedout")
206                 features.require("color");
207         inset.validate(features);
208 }
209
210
211
212 InsetNoteMailer::InsetNoteMailer(string const & name,
213                                                 InsetNote & inset)
214         : name_(name), inset_(inset)
215 {
216 }
217
218
219 string const InsetNoteMailer::inset2string(Buffer const &) const
220 {
221         return params2string(name_, inset_.params());
222 }
223
224
225 string const InsetNoteMailer::params2string(string const & name,
226                                 InsetNoteParams const & params)
227 {
228         ostringstream data;
229         data << name << ' ';
230         params.write(data);
231         return STRCONV(data.str());
232 }
233
234
235 void InsetNoteMailer::string2params(string const & in,
236                                      InsetNoteParams & params)
237 {
238         params = InsetNoteParams();
239
240         if (in.empty())
241                 return;
242
243         istringstream data(STRCONV(in));
244         LyXLex lex(0,0);
245         lex.setStream(data);
246         params.read(lex);
247 }
248
249
250 void InsetNoteParams::write(ostream & os) const
251 {
252         os << type << "\n";
253 }
254
255
256 void InsetNoteParams::read(LyXLex & lex)
257 {
258         if (lex.isOK()) {
259                 lex.next();
260                 string token = lex.getString();
261         }
262
263         if (lex.isOK()) {
264                 lex.next();
265                 type = lex.getString();
266         }
267 }
268