]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
e7536783bf26bc11bf18a721f099cdaca39d7d18
[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 dispatch_result InsetNote::localDispatch(FuncRequest const & cmd)
106 {
107         BufferView * bv = cmd.view();
108
109         switch (cmd.action) {
110         case LFUN_INSET_MODIFY:
111                 {
112                 InsetNoteParams params;
113                 InsetNoteMailer::string2params(cmd.argument, params);
114                 params_.type = params.type;
115                 setButtonLabel();
116                 bv->updateInset(this);
117                 return DISPATCHED;
118                 }
119         case LFUN_INSET_EDIT:
120                 if (cmd.button() != mouse_button::button3) 
121                         return InsetCollapsable::localDispatch(cmd);
122                 return UNDISPATCHED;
123         case LFUN_INSET_DIALOG_UPDATE:
124                 InsetNoteMailer("note", *this).updateDialog(bv);
125                 return DISPATCHED;
126         case LFUN_MOUSE_RELEASE:
127                 if (cmd.button() == mouse_button::button3 && cmd.x < button_length 
128                                         && cmd.y >= button_top_y && cmd.y <= button_bottom_y) {
129                         InsetNoteMailer("note", *this).showDialog(bv);
130                         return DISPATCHED;
131                 }
132                 // fallthrough:
133         default:
134                 return InsetCollapsable::localDispatch(cmd);
135         }
136 }
137
138
139 int InsetNote::latex(Buffer const * buf, ostream & os,
140                                                 LatexRunParams const & runparams) const
141 {
142         string const pt = params_.type;
143
144         int i = 0;
145         if (pt == "Comment") os << "%\n\\begin{comment}\n"; // remember to validate
146         if (pt == "Greyedout") os << "%\n\\textcolor[gray]{0.8}{";
147         if (pt != "Note") {
148                 i = inset.latex(buf, os, runparams);
149         }
150         if (pt == "Comment") {
151                 os << "%\n\\end{comment}\n";
152                 i += 3;
153         }
154         if (pt == "Greyedout") { 
155                 os << "%\n}";
156                 i += 2;
157         }
158         return i;
159 }
160
161
162 int InsetNote::linuxdoc(Buffer const *, std::ostream &) const
163
164         return 0; 
165 }
166
167
168 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
169 {
170         int i = 0;
171         string const pt = params_.type;
172         // incomplete, untested - MV
173         if (pt != "Note") 
174                 i = inset.docbook(buf, os, mixcont);
175         return i; 
176 }
177
178
179 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
180 {
181         int i = 0;
182         string const pt = params_.type;
183         if (pt != "Note") {
184                 os << "[";
185                 i = inset.ascii(buf, os, ll);
186                 os << "]";
187         }
188         return i;
189 }
190
191
192 void InsetNote::validate(LaTeXFeatures & features) const
193 {
194         if (params_.type == "Comment") 
195                 features.require("verbatim");
196         if (params_.type == "Greyedout")
197                 features.require("color");
198         inset.validate(features);
199 }
200
201
202
203 InsetNoteMailer::InsetNoteMailer(string const & name,
204                                                 InsetNote & inset)
205         : name_(name), inset_(inset)
206 {
207 }
208
209
210 string const InsetNoteMailer::inset2string() const
211 {
212         return params2string(name_, inset_.params());
213 }
214
215
216 string const InsetNoteMailer::params2string(string const & name,
217                                 InsetNoteParams const & params)
218 {
219         ostringstream data;
220         data << name << ' ';
221         params.write(data);
222         return STRCONV(data.str());
223 }
224
225
226 void InsetNoteMailer::string2params(string const & in,
227                                      InsetNoteParams & params)
228 {
229         params = InsetNoteParams();
230
231         if (in.empty())
232                 return;
233
234         istringstream data(STRCONV(in));
235         LyXLex lex(0,0);
236         lex.setStream(data);
237         params.read(lex);
238 }
239
240
241 void InsetNoteParams::write(ostream & os) const
242 {
243         os << type << "\n";
244 }
245
246
247 void InsetNoteParams::read(LyXLex & lex)
248 {
249         if (lex.isOK()) {
250                 lex.next();
251                 string token = lex.getString();
252         }
253
254         if (lex.isOK()) {
255                 lex.next();
256                 type = lex.getString();
257         }
258 }
259