]> git.lyx.org Git - lyx.git/blob - src/insets/insetnote.C
fix longtabular ui (bug 1860)
[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                 cur.bv().update();
196                 break;
197
198         case LFUN_INSET_DIALOG_UPDATE:
199                 InsetNoteMailer(*this).updateDialog(&cur.bv());
200                 break;
201
202         case LFUN_MOUSE_RELEASE:
203                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
204                         InsetNoteMailer(*this).showDialog(&cur.bv());
205                 else
206                         InsetCollapsable::doDispatch(cur, cmd);
207                 break;
208
209         default:
210                 InsetCollapsable::doDispatch(cur, cmd);
211                 break;
212         }
213 }
214
215
216 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
217                 FuncStatus & flag) const
218 {
219         switch (cmd.action) {
220
221         case LFUN_INSET_MODIFY:
222         case LFUN_INSET_DIALOG_UPDATE:
223                 flag.enabled(true);
224                 return true;
225
226         default:
227                 return InsetCollapsable::getStatus(cur, cmd, flag);
228         }
229 }
230
231
232 int InsetNote::latex(Buffer const & buf, ostream & os,
233                      OutputParams const & runparams) const
234 {
235         if (params_.type == InsetNoteParams::Note)
236                 return 0;
237
238         string type;
239         if (params_.type == InsetNoteParams::Comment)
240                 type = "comment";
241         else if (params_.type == InsetNoteParams::Greyedout)
242                 type = "lyxgreyedout";
243
244         ostringstream ss;
245         ss << "%\n\\begin{" << type << "}\n";
246         InsetText::latex(buf, ss, runparams);
247         ss << "%\n\\end{" << type << "}\n";
248
249         string const str = ss.str();
250         os << str;
251         // Return how many newlines we issued.
252         return int(lyx::count(str.begin(), str.end(),'\n'));
253 }
254
255
256 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
257                         OutputParams const & runparams) const
258 {
259         if (params_.type == InsetNoteParams::Note)
260                 return 0;
261
262         ostringstream ss;
263         if (params_.type == InsetNoteParams::Comment)
264                 ss << "<comment>\n";
265
266         InsetText::linuxdoc(buf, ss, runparams);
267
268         if (params_.type == InsetNoteParams::Comment)
269                 ss << "\n</comment>\n";
270
271         string const str = ss.str();
272         os << str;
273         // Return how many newlines we issued.
274         return int(lyx::count(str.begin(), str.end(),'\n'));
275 }
276
277
278 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
279                        OutputParams const & runparams) const
280 {
281         if (params_.type == InsetNoteParams::Note)
282                 return 0;
283
284         ostringstream ss;
285         if (params_.type == InsetNoteParams::Comment)
286                 ss << "<remark>\n";
287
288         InsetText::docbook(buf, ss, runparams);
289
290         if (params_.type == InsetNoteParams::Comment)
291                 ss << "\n</remark>\n";
292
293         string const str = ss.str();
294         os << str;
295         // Return how many newlines we issued.
296         return int(lyx::count(str.begin(), str.end(),'\n'));
297 }
298
299
300 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
301                      OutputParams const & runparams) const
302 {
303         if (params_.type == InsetNoteParams::Note)
304                 return 0;
305
306         ostringstream ss;
307         ss << "[";
308         InsetText::plaintext(buf, ss, runparams);
309         ss << "]";
310
311         string const str = ss.str();
312         os << str;
313         // Return how many newlines we issued.
314         return int(lyx::count(str.begin(), str.end(),'\n'));
315 }
316
317
318 void InsetNote::validate(LaTeXFeatures & features) const
319 {
320         if (params_.type == InsetNoteParams::Comment)
321                 features.require("verbatim");
322         if (params_.type == InsetNoteParams::Greyedout) {
323                 features.require("color");
324                 features.require("lyxgreyedout");
325         }
326         InsetText::validate(features);
327 }
328
329
330
331 string const InsetNoteMailer::name_("note");
332
333 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
334         : inset_(inset)
335 {}
336
337
338 string const InsetNoteMailer::inset2string(Buffer const &) const
339 {
340         return params2string(inset_.params());
341 }
342
343
344 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
345 {
346         ostringstream data;
347         data << name_ << ' ';
348         params.write(data);
349         return data.str();
350 }
351
352
353 void InsetNoteMailer::string2params(string const & in,
354                                     InsetNoteParams & params)
355 {
356         params = InsetNoteParams();
357
358         if (in.empty())
359                 return;
360
361         istringstream data(in);
362         LyXLex lex(0,0);
363         lex.setStream(data);
364
365         string name;
366         lex >> name;
367         if (!lex || name != name_)
368                 return print_mailer_error("InsetNoteMailer", in, 1, name_);
369
370         // This is part of the inset proper that is usually swallowed
371         // by LyXText::readInset
372         string id;
373         lex >> id;
374         if (!lex || id != "Note")
375                 return print_mailer_error("InsetBoxMailer", in, 2, "Note");
376
377         params.read(lex);
378 }