]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
showInsetDialog() for note
[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") os << "%\n\\begin{comment}\n"; // remember to validate
153         if (pt == "Greyedout") os << "%\n\\textcolor[gray]{0.8}{";
154         if (pt != "Note") {
155                 i = inset.latex(buf, os, runparams);
156         }
157         if (pt == "Comment") {
158                 os << "%\n\\end{comment}\n";
159                 i += 3;
160         }
161         if (pt == "Greyedout") { 
162                 os << "%\n}";
163                 i += 2;
164         }
165         return i;
166 }
167
168
169 int InsetNote::linuxdoc(Buffer const *, std::ostream &) const
170
171         return 0; 
172 }
173
174
175 int InsetNote::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
176 {
177         int i = 0;
178         string const pt = params_.type;
179         // incomplete, untested - MV
180         if (pt != "Note") 
181                 i = inset.docbook(buf, os, mixcont);
182         return i; 
183 }
184
185
186 int InsetNote::ascii(Buffer const * buf, std::ostream & os, int ll) const
187 {
188         int i = 0;
189         string const pt = params_.type;
190         if (pt != "Note") {
191                 os << "[";
192                 i = inset.ascii(buf, os, ll);
193                 os << "]";
194         }
195         return i;
196 }
197
198
199 void InsetNote::validate(LaTeXFeatures & features) const
200 {
201         if (params_.type == "Comment") 
202                 features.require("verbatim");
203         if (params_.type == "Greyedout")
204                 features.require("color");
205         inset.validate(features);
206 }
207
208
209
210 InsetNoteMailer::InsetNoteMailer(string const & name,
211                                                 InsetNote & inset)
212         : name_(name), inset_(inset)
213 {
214 }
215
216
217 string const InsetNoteMailer::inset2string() const
218 {
219         return params2string(name_, inset_.params());
220 }
221
222
223 string const InsetNoteMailer::params2string(string const & name,
224                                 InsetNoteParams const & params)
225 {
226         ostringstream data;
227         data << name << ' ';
228         params.write(data);
229         return STRCONV(data.str());
230 }
231
232
233 void InsetNoteMailer::string2params(string const & in,
234                                      InsetNoteParams & params)
235 {
236         params = InsetNoteParams();
237
238         if (in.empty())
239                 return;
240
241         istringstream data(STRCONV(in));
242         LyXLex lex(0,0);
243         lex.setStream(data);
244         params.read(lex);
245 }
246
247
248 void InsetNoteParams::write(ostream & os) const
249 {
250         os << type << "\n";
251 }
252
253
254 void InsetNoteParams::read(LyXLex & lex)
255 {
256         if (lex.isOK()) {
257                 lex.next();
258                 string token = lex.getString();
259         }
260
261         if (lex.isOK()) {
262                 lex.next();
263                 type = lex.getString();
264         }
265 }
266