]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
update \author list
[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  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #include "Lsstream.h"
16
17 #include "insetnote.h"
18 #include "gettext.h"
19 #include "lyxfont.h"
20 #include "language.h"
21 #include "buffer.h"
22 #include "BufferView.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "insets/insettext.h"
26 #include "support/LOstream.h"
27 #include "support/lstrings.h"
28 #include "debug.h"
29
30 using std::ostream;
31 using std::auto_ptr;
32
33
34 void InsetNote::init()
35 {
36         setInsetName("Note");
37         setButtonLabel();
38 }
39
40
41 InsetNote::InsetNote(BufferParams const & bp, string const & label)
42         : InsetCollapsable(bp)
43 {
44         params_.type = label;
45         init();
46         setButtonLabel();
47 }
48
49
50 InsetNote::InsetNote(InsetNote const & in)
51         : InsetCollapsable(in), params_(in.params_)
52 {
53         init();
54 }
55
56
57 InsetNote::~InsetNote() // MV
58 {
59         InsetNoteMailer mailer("note", *this);
60         mailer.hideDialog();
61 }
62
63
64 auto_ptr<InsetBase> InsetNote::clone() const
65 {
66         return auto_ptr<InsetBase>(new InsetNote(*this));
67 }
68
69
70 string const InsetNote::editMessage() const
71 {
72         return _("Opened Note Inset");
73 }
74
75
76 void InsetNote::write(Buffer const * buf, ostream & os) const
77 {
78         params_.write(os);
79         InsetCollapsable::write(buf, os);
80 }
81
82
83 void InsetNote::read(Buffer const * buf, LyXLex & lex)
84 {
85         InsetCollapsable::read(buf, lex);
86         setButtonLabel();
87 }
88
89
90 void InsetNote::setButtonLabel()
91 {
92         LyXFont font(LyXFont::ALL_SANE);
93         font.decSize();
94         font.decSize();
95
96         if (params_.type == "Note") {
97                 setLabel(_("LyX Note"));
98                 font.setColor(LColor::note);
99                 setBackgroundColor(LColor::notebg);
100         } else if (params_.type == "Comment") {
101                 setLabel(_("Comment"));
102                 font.setColor(LColor::comment);
103                 setBackgroundColor(LColor::commentbg);
104         } else {
105                 setLabel(_("Greyed Out"));
106                 font.setColor(LColor::greyedout);
107                 setBackgroundColor(LColor::greyedoutbg);
108         }
109         setLabelFont(font);
110 }
111
112
113 bool InsetNote::showInsetDialog(BufferView * bv) const
114 {
115         InsetNoteMailer("note", const_cast<InsetNote &>(*this)).showDialog(bv);
116         return true;
117 }
118
119
120 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
121 {
122         BufferView * bv = cmd.view();
123
124         switch (cmd.action) {
125         case LFUN_INSET_MODIFY:
126                 {
127                 InsetNoteParams params;
128                 InsetNoteMailer::string2params(cmd.argument, params);
129                 params_.type = params.type;
130                 setButtonLabel();
131                 bv->updateInset(this);
132                 return DISPATCHED;
133                 }
134         case LFUN_INSET_EDIT:
135                 if (cmd.button() != mouse_button::button3)
136                         return InsetCollapsable::localDispatch(cmd);
137                 return UNDISPATCHED;
138         case LFUN_INSET_DIALOG_UPDATE:
139                 InsetNoteMailer("note", *this).updateDialog(bv);
140                 return DISPATCHED;
141         case LFUN_MOUSE_RELEASE:
142                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
143                         InsetNoteMailer("note", *this).showDialog(bv);
144                         return DISPATCHED;
145                 }
146                 // fallthrough:
147         default:
148                 return InsetCollapsable::localDispatch(cmd);
149         }
150 }
151
152
153 int InsetNote::latex(Buffer const * buf, ostream & os,
154                                                 LatexRunParams const & runparams) const
155 {
156         string const pt = params_.type;
157
158         int i = 0;
159         if (pt == "Comment")
160                  // verbatim
161                 os << "%\n\\begin{comment}\n";
162         else if (pt == "Greyedout")
163                  // we roll our own macro
164                 os << "%\n\\begin{lyxgreyedout}\n";
165
166         if (pt != "Note")
167                 i = inset.latex(buf, os, runparams);
168
169         if (pt == "Comment") {
170                 os << "%\n\\end{comment}\n";
171                 i += 3;
172         } else if (pt == "Greyedout") {
173                 os << "%\n\\end{lyxgreyedout}\n";
174                 i += 2;
175         }
176         return i;
177 }
178
179
180 int InsetNote::linuxdoc(Buffer const * buf, std::ostream & os) const
181 {
182         string const pt = params_.type;
183
184         int i = 0;
185         if (pt == "Comment")
186                 os << "<comment>\n";
187
188         if (pt != "Note")
189                 i = inset.linuxdoc(buf, os);
190
191         if (pt == "Comment") {
192                 os << "\n</comment>\n";
193                 i += 3;
194         }
195         return i;
196 }
197
198
199 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
200 {
201         string const pt = params_.type;
202
203         int i = 0;
204         if (pt == "Comment")
205                 os << "<remark>\n";
206
207         if (pt != "Note")
208                 i = inset.docbook(buf, os, mixcont);
209
210         if (pt == "Comment") {
211                 os << "\n</remark>\n";
212                 i += 3;
213         }
214         return i;
215 }
216
217
218 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
219 {
220         int i = 0;
221         string const pt = params_.type;
222         if (pt != "Note") {
223                 os << "[";
224                 i = inset.ascii(buf, os, ll);
225                 os << "]";
226         }
227         return i;
228 }
229
230
231 void InsetNote::validate(LaTeXFeatures & features) const
232 {
233         if (params_.type == "Comment")
234                 features.require("verbatim");
235         if (params_.type == "Greyedout") {
236                 features.require("color");
237                 features.require("lyxgreyedout");
238         }
239         inset.validate(features);
240 }
241
242
243
244 InsetNoteMailer::InsetNoteMailer(string const & name,
245                                                 InsetNote & inset)
246         : name_(name), inset_(inset)
247 {
248 }
249
250
251 string const InsetNoteMailer::inset2string(Buffer const &) const
252 {
253         return params2string(name_, inset_.params());
254 }
255
256
257 string const InsetNoteMailer::params2string(string const & name,
258                                 InsetNoteParams const & params)
259 {
260         ostringstream data;
261         data << name << ' ';
262         params.write(data);
263         return STRCONV(data.str());
264 }
265
266
267 void InsetNoteMailer::string2params(string const & in,
268                                      InsetNoteParams & params)
269 {
270         params = InsetNoteParams();
271
272         if (in.empty())
273                 return;
274
275         istringstream data(STRCONV(in));
276         LyXLex lex(0,0);
277         lex.setStream(data);
278         params.read(lex);
279 }
280
281
282 void InsetNoteParams::write(ostream & os) const
283 {
284         os << type << "\n";
285 }
286
287
288 void InsetNoteParams::read(LyXLex & lex)
289 {
290         if (lex.isOK()) {
291                 lex.next();
292                 string token = lex.getString();
293         }
294
295         if (lex.isOK()) {
296                 lex.next();
297                 type = lex.getString();
298         }
299 }