]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
status tag patch from Georg
[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 "insetnote.h"
16
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "support/lyxalgo.h"
29 #include "support/std_sstream.h"
30 #include "support/translator.h"
31
32
33 using std::string;
34 using std::auto_ptr;
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38
39
40 namespace {
41
42 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
43
44 NoteTranslator const init_notetranslator() {
45         NoteTranslator translator("Note", InsetNoteParams::Note);
46         translator.addPair("Comment", InsetNoteParams::Comment);
47         translator.addPair("Greyedout", InsetNoteParams::Greyedout);
48         return translator;
49 }
50
51
52 NoteTranslator const init_notetranslator_loc() {
53         NoteTranslator translator(_("Note"), InsetNoteParams::Note);
54         translator.addPair(_("Comment"), InsetNoteParams::Comment);
55         translator.addPair(_("Greyed out"), InsetNoteParams::Greyedout);
56         return translator;
57 }
58
59
60 NoteTranslator const & notetranslator() {
61         static NoteTranslator translator = init_notetranslator();
62         return translator;
63 }
64
65
66 NoteTranslator const & notetranslator_loc() {
67         static NoteTranslator translator = init_notetranslator_loc();
68         return translator;
69 }
70
71 } // anon
72
73
74
75
76 InsetNoteParams::InsetNoteParams()
77         : type(Note)
78 {}
79
80
81 void InsetNoteParams::write(ostream & os) const
82 {
83         string const label = notetranslator().find(type);
84         os << "Note " << label << "\n";
85 }
86
87
88 void InsetNoteParams::read(LyXLex & lex)
89 {
90         string label;
91         lex >> label;
92         if (lex)
93                 type = notetranslator().find(label);
94 }
95
96
97 void InsetNote::init()
98 {
99         setInsetName("Note");
100         setButtonLabel();
101 }
102
103
104 InsetNote::InsetNote(BufferParams const & bp, string const & label)
105         : InsetCollapsable(bp)
106 {
107         params_.type = notetranslator().find(label);
108         init();
109 }
110
111
112 InsetNote::InsetNote(InsetNote const & in)
113         : InsetCollapsable(in), params_(in.params_)
114 {
115         init();
116 }
117
118
119 InsetNote::~InsetNote()
120 {
121         InsetNoteMailer(*this).hideDialog();
122 }
123
124
125 auto_ptr<InsetBase> InsetNote::clone() const
126 {
127         return auto_ptr<InsetBase>(new InsetNote(*this));
128 }
129
130
131 string const InsetNote::editMessage() const
132 {
133         return _("Opened Note Inset");
134 }
135
136
137 void InsetNote::write(Buffer const & buf, ostream & os) const
138 {
139         params_.write(os);
140         InsetCollapsable::write(buf, os);
141 }
142
143
144 void InsetNote::read(Buffer const & buf, LyXLex & lex)
145 {
146         params_.read(lex);
147         InsetCollapsable::read(buf, lex);
148         setButtonLabel();
149 }
150
151
152 void InsetNote::setButtonLabel()
153 {
154         string const label = notetranslator_loc().find(params_.type);
155         setLabel(label);
156
157         LyXFont font(LyXFont::ALL_SANE);
158         font.decSize();
159         font.decSize();
160
161         switch (params_.type) {
162         case InsetNoteParams::Note:
163                 font.setColor(LColor::note);
164                 setBackgroundColor(LColor::notebg);
165                 break;
166         case InsetNoteParams::Comment:
167                 font.setColor(LColor::comment);
168                 setBackgroundColor(LColor::commentbg);
169                 break;
170         case InsetNoteParams::Greyedout:
171                 font.setColor(LColor::greyedout);
172                 setBackgroundColor(LColor::greyedoutbg);
173                 break;
174         }
175         setLabelFont(font);
176 }
177
178
179 bool InsetNote::showInsetDialog(BufferView * bv) const
180 {
181         InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
182         return true;
183 }
184
185
186 DispatchResult
187 InsetNote::priv_dispatch(FuncRequest const & cmd,
188                          idx_type & idx, pos_type & pos)
189 {
190         BufferView * bv = cmd.view();
191
192         switch (cmd.action) {
193
194         case LFUN_INSET_MODIFY: {
195                 InsetNoteMailer::string2params(cmd.argument, params_);
196                 setButtonLabel();
197                 bv->update();
198                 return DispatchResult(true, true);
199         }
200
201         case LFUN_INSET_DIALOG_UPDATE:
202                 InsetNoteMailer(*this).updateDialog(bv);
203                 return DispatchResult(true, true);
204
205         case LFUN_MOUSE_RELEASE:
206                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
207                         InsetNoteMailer(*this).showDialog(bv);
208                         return DispatchResult(true, true);
209                 }
210                 // fallthrough:
211
212         default:
213                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
214         }
215 }
216
217
218 int InsetNote::latex(Buffer const & buf, ostream & os,
219                      OutputParams const & runparams) const
220 {
221         if (params_.type == InsetNoteParams::Note)
222                 return 0;
223
224         string type;
225         if (params_.type == InsetNoteParams::Comment)
226                 type = "comment";
227         else if (params_.type == InsetNoteParams::Greyedout)
228                 type = "lyxgreyedout";
229
230         ostringstream ss;
231         ss << "%\n\\begin{" << type << "}\n";
232         inset.latex(buf, ss, runparams);
233         ss << "%\n\\end{" << type << "}\n";
234
235         string const str = ss.str();
236         os << str;
237         // Return how many newlines we issued.
238         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
239 }
240
241
242 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
243                         OutputParams const & runparams) const
244 {
245         if (params_.type == InsetNoteParams::Note)
246                 return 0;
247
248         ostringstream ss;
249         if (params_.type == InsetNoteParams::Comment)
250                 ss << "<comment>\n";
251
252         inset.linuxdoc(buf, ss, runparams);
253
254         if (params_.type == InsetNoteParams::Comment)
255                 ss << "\n</comment>\n";
256
257         string const str = ss.str();
258         os << str;
259         // Return how many newlines we issued.
260         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
261 }
262
263
264 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
265                        OutputParams const & runparams) const
266 {
267         if (params_.type == InsetNoteParams::Note)
268                 return 0;
269
270         ostringstream ss;
271         if (params_.type == InsetNoteParams::Comment)
272                 ss << "<remark>\n";
273
274         inset.docbook(buf, ss, runparams);
275
276         if (params_.type == InsetNoteParams::Comment)
277                 ss << "\n</remark>\n";
278
279         string const str = ss.str();
280         os << str;
281         // Return how many newlines we issued.
282         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
283 }
284
285
286 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
287                      OutputParams const & runparams) const
288 {
289         if (params_.type == InsetNoteParams::Note)
290                 return 0;
291
292         ostringstream ss;
293         ss << "[";
294         inset.plaintext(buf, ss, runparams);
295         ss << "]";
296
297         string const str = ss.str();
298         os << str;
299         // Return how many newlines we issued.
300         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
301 }
302
303
304 void InsetNote::validate(LaTeXFeatures & features) const
305 {
306         if (params_.type == InsetNoteParams::Comment)
307                 features.require("verbatim");
308         if (params_.type == InsetNoteParams::Greyedout) {
309                 features.require("color");
310                 features.require("lyxgreyedout");
311         }
312         inset.validate(features);
313 }
314
315
316
317 string const InsetNoteMailer:: name_("note");
318
319 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
320         : inset_(inset)
321 {}
322
323
324 string const InsetNoteMailer::inset2string(Buffer const &) const
325 {
326         return params2string(inset_.params());
327 }
328
329
330 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
331 {
332         ostringstream data;
333         data << name_ << ' ';
334         params.write(data);
335         return data.str();
336 }
337
338
339 void InsetNoteMailer::string2params(string const & in,
340                                     InsetNoteParams & params)
341 {
342         params = InsetNoteParams();
343
344         if (in.empty())
345                 return;
346
347         istringstream data(in);
348         LyXLex lex(0,0);
349         lex.setStream(data);
350
351         string name;
352         lex >> name;
353         if (!lex || name != name_)
354                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
355
356         // This is part of the inset proper that is usually swallowed
357         // by LyXText::readInset
358         string id;
359         lex >> id;
360         if (!lex || id != "Note")
361                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
362
363         params.read(lex);
364 }