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