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