3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "insetnote.h"
17 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
24 #include "LaTeXFeatures.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
30 #include "support/lyxalgo.h"
31 #include "support/translator.h"
37 using std::istringstream;
39 using std::ostringstream;
44 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
46 NoteTranslator const init_notetranslator() {
47 NoteTranslator translator("Note", InsetNoteParams::Note);
48 translator.addPair("Comment", InsetNoteParams::Comment);
49 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
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);
62 NoteTranslator const & notetranslator() {
63 static NoteTranslator translator = init_notetranslator();
68 NoteTranslator const & notetranslator_loc() {
69 static NoteTranslator translator = init_notetranslator_loc();
78 InsetNoteParams::InsetNoteParams()
83 void InsetNoteParams::write(ostream & os) const
85 string const label = notetranslator().find(type);
86 os << "Note " << label << "\n";
90 void InsetNoteParams::read(LyXLex & lex)
95 type = notetranslator().find(label);
99 void InsetNote::init()
101 setInsetName("Note");
106 InsetNote::InsetNote(BufferParams const & bp, string const & label)
107 : InsetCollapsable(bp)
109 params_.type = notetranslator().find(label);
114 InsetNote::InsetNote(InsetNote const & in)
115 : InsetCollapsable(in), params_(in.params_)
121 InsetNote::~InsetNote()
123 InsetNoteMailer(*this).hideDialog();
127 auto_ptr<InsetBase> InsetNote::doClone() const
129 return auto_ptr<InsetBase>(new InsetNote(*this));
133 string const InsetNote::editMessage() const
135 return _("Opened Note Inset");
139 void InsetNote::write(Buffer const & buf, ostream & os) const
142 InsetCollapsable::write(buf, os);
146 void InsetNote::read(Buffer const & buf, LyXLex & lex)
149 InsetCollapsable::read(buf, lex);
154 void InsetNote::setButtonLabel()
156 string const label = notetranslator_loc().find(params_.type);
159 LyXFont font(LyXFont::ALL_SANE);
163 switch (params_.type) {
164 case InsetNoteParams::Note:
165 font.setColor(LColor::note);
166 setBackgroundColor(LColor::notebg);
168 case InsetNoteParams::Comment:
169 font.setColor(LColor::comment);
170 setBackgroundColor(LColor::commentbg);
172 case InsetNoteParams::Greyedout:
173 font.setColor(LColor::greyedout);
174 setBackgroundColor(LColor::greyedoutbg);
181 bool InsetNote::showInsetDialog(BufferView * bv) const
183 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
188 void InsetNote::doDispatch(LCursor & cur, FuncRequest & cmd)
190 switch (cmd.action) {
192 case LFUN_INSET_MODIFY:
193 InsetNoteMailer::string2params(cmd.argument, params_);
197 case LFUN_INSET_DIALOG_UPDATE:
198 InsetNoteMailer(*this).updateDialog(&cur.bv());
201 case LFUN_MOUSE_RELEASE:
202 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
203 InsetNoteMailer(*this).showDialog(&cur.bv());
205 InsetCollapsable::doDispatch(cur, cmd);
209 InsetCollapsable::doDispatch(cur, cmd);
215 bool InsetNote::getStatus(LCursor & cur, FuncRequest const & cmd,
216 FuncStatus & flag) const
218 switch (cmd.action) {
220 case LFUN_INSET_MODIFY:
221 case LFUN_INSET_DIALOG_UPDATE:
226 return InsetCollapsable::getStatus(cur, cmd, flag);
231 int InsetNote::latex(Buffer const & buf, ostream & os,
232 OutputParams const & runparams) const
234 if (params_.type == InsetNoteParams::Note)
238 if (params_.type == InsetNoteParams::Comment)
240 else if (params_.type == InsetNoteParams::Greyedout)
241 type = "lyxgreyedout";
244 ss << "%\n\\begin{" << type << "}\n";
245 InsetText::latex(buf, ss, runparams);
246 ss << "%\n\\end{" << type << "}\n";
248 string const str = ss.str();
250 // Return how many newlines we issued.
251 return int(lyx::count(str.begin(), str.end(),'\n'));
255 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
256 OutputParams const & runparams) const
258 if (params_.type == InsetNoteParams::Note)
262 if (params_.type == InsetNoteParams::Comment)
265 InsetText::linuxdoc(buf, ss, runparams);
267 if (params_.type == InsetNoteParams::Comment)
268 ss << "\n</comment>\n";
270 string const str = ss.str();
272 // Return how many newlines we issued.
273 return int(lyx::count(str.begin(), str.end(),'\n'));
277 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
278 OutputParams const & runparams) const
280 if (params_.type == InsetNoteParams::Note)
284 if (params_.type == InsetNoteParams::Comment)
287 InsetText::docbook(buf, ss, runparams);
289 if (params_.type == InsetNoteParams::Comment)
290 ss << "\n</remark>\n";
292 string const str = ss.str();
294 // Return how many newlines we issued.
295 return int(lyx::count(str.begin(), str.end(),'\n'));
299 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
300 OutputParams const & runparams) const
302 if (params_.type == InsetNoteParams::Note)
307 InsetText::plaintext(buf, ss, runparams);
310 string const str = ss.str();
312 // Return how many newlines we issued.
313 return int(lyx::count(str.begin(), str.end(),'\n'));
317 void InsetNote::validate(LaTeXFeatures & features) const
319 if (params_.type == InsetNoteParams::Comment)
320 features.require("verbatim");
321 if (params_.type == InsetNoteParams::Greyedout) {
322 features.require("color");
323 features.require("lyxgreyedout");
325 InsetText::validate(features);
330 string const InsetNoteMailer::name_("note");
332 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
337 string const InsetNoteMailer::inset2string(Buffer const &) const
339 return params2string(inset_.params());
343 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
346 data << name_ << ' ';
352 void InsetNoteMailer::string2params(string const & in,
353 InsetNoteParams & params)
355 params = InsetNoteParams();
360 istringstream data(in);
366 if (!lex || name != name_)
367 return print_mailer_error("InsetNoteMailer", in, 1, name_);
369 // This is part of the inset proper that is usually swallowed
370 // by LyXText::readInset
373 if (!lex || id != "Note")
374 return print_mailer_error("InsetBoxMailer", in, 2, "Note");