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