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"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "support/lyxalgo.h"
29 #include "support/std_sstream.h"
30 #include "support/translator.h"
35 using std::istringstream;
37 using std::ostringstream;
42 typedef Translator<std::string, InsetNoteParams::Type> NoteTranslator;
44 NoteTranslator const init_notetranslator() {
45 NoteTranslator translator("Note", InsetNoteParams::Note);
46 translator.addPair("Comment", InsetNoteParams::Comment);
47 translator.addPair("Greyedout", InsetNoteParams::Greyedout);
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);
60 NoteTranslator const & notetranslator() {
61 static NoteTranslator translator = init_notetranslator();
66 NoteTranslator const & notetranslator_loc() {
67 static NoteTranslator translator = init_notetranslator_loc();
76 InsetNoteParams::InsetNoteParams()
81 void InsetNoteParams::write(ostream & os) const
83 string const label = notetranslator().find(type);
84 os << "Note " << label << "\n";
89 void InsetNoteParams::read(LyXLex & lex)
94 type = notetranslator().find(label);
98 void InsetNote::init()
100 setInsetName("Note");
105 InsetNote::InsetNote(BufferParams const & bp, string const & label)
106 : InsetCollapsable(bp)
108 params_.type = notetranslator().find(label);
113 InsetNote::InsetNote(InsetNote const & in)
114 : InsetCollapsable(in), params_(in.params_)
120 InsetNote::~InsetNote()
122 InsetNoteMailer(*this).hideDialog();
126 auto_ptr<InsetBase> InsetNote::clone() const
128 return auto_ptr<InsetBase>(new InsetNote(*this));
132 string const InsetNote::editMessage() const
134 return _("Opened Note Inset");
138 void InsetNote::write(Buffer const & buf, ostream & os) const
141 InsetCollapsable::write(buf, os);
145 void InsetNote::read(Buffer const & buf, LyXLex & lex)
148 InsetCollapsable::read(buf, lex);
153 void InsetNote::setButtonLabel()
155 string const label = notetranslator_loc().find(params_.type);
158 LyXFont font(LyXFont::ALL_SANE);
162 switch (params_.type) {
163 case InsetNoteParams::Note:
164 font.setColor(LColor::note);
165 setBackgroundColor(LColor::notebg);
167 case InsetNoteParams::Comment:
168 font.setColor(LColor::comment);
169 setBackgroundColor(LColor::commentbg);
171 case InsetNoteParams::Greyedout:
172 font.setColor(LColor::greyedout);
173 setBackgroundColor(LColor::greyedoutbg);
180 bool InsetNote::showInsetDialog(BufferView * bv) const
182 InsetNoteMailer(const_cast<InsetNote &>(*this)).showDialog(bv);
188 InsetNote::priv_dispatch(FuncRequest const & cmd,
189 idx_type & idx, pos_type & pos)
191 BufferView * bv = cmd.view();
193 switch (cmd.action) {
195 case LFUN_INSET_MODIFY: {
196 InsetNoteMailer::string2params(cmd.argument, params_);
199 return DispatchResult(true, true);
202 case LFUN_INSET_DIALOG_UPDATE:
203 InsetNoteMailer(*this).updateDialog(bv);
204 return DispatchResult(true, true);
206 case LFUN_MOUSE_RELEASE:
207 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
208 InsetNoteMailer(*this).showDialog(bv);
209 return DispatchResult(true, true);
214 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
219 int InsetNote::latex(Buffer const & buf, ostream & os,
220 OutputParams const & runparams) const
222 if (params_.type == InsetNoteParams::Note)
226 if (params_.type == InsetNoteParams::Comment)
228 else if (params_.type == InsetNoteParams::Greyedout)
229 type = "lyxgreyedout";
232 ss << "%\n\\begin{" << type << "}\n";
233 inset.latex(buf, ss, runparams);
234 ss << "%\n\\end{" << type << "}\n";
236 string const str = ss.str();
238 // Return how many newlines we issued.
239 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
243 int InsetNote::linuxdoc(Buffer const & buf, std::ostream & os,
244 OutputParams const & runparams) const
246 if (params_.type == InsetNoteParams::Note)
250 if (params_.type == InsetNoteParams::Comment)
253 inset.linuxdoc(buf, ss, runparams);
255 if (params_.type == InsetNoteParams::Comment)
256 ss << "\n</comment>\n";
258 string const str = ss.str();
260 // Return how many newlines we issued.
261 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
265 int InsetNote::docbook(Buffer const & buf, std::ostream & os,
266 OutputParams const & runparams) const
268 if (params_.type == InsetNoteParams::Note)
272 if (params_.type == InsetNoteParams::Comment)
275 inset.docbook(buf, ss, runparams);
277 if (params_.type == InsetNoteParams::Comment)
278 ss << "\n</remark>\n";
280 string const str = ss.str();
282 // Return how many newlines we issued.
283 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
287 int InsetNote::plaintext(Buffer const & buf, std::ostream & os,
288 OutputParams const & runparams) const
290 if (params_.type == InsetNoteParams::Note)
295 inset.plaintext(buf, ss, runparams);
298 string const str = ss.str();
300 // Return how many newlines we issued.
301 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
305 void InsetNote::validate(LaTeXFeatures & features) const
307 if (params_.type == InsetNoteParams::Comment)
308 features.require("verbatim");
309 if (params_.type == InsetNoteParams::Greyedout) {
310 features.require("color");
311 features.require("lyxgreyedout");
313 inset.validate(features);
318 string const InsetNoteMailer:: name_("note");
320 InsetNoteMailer::InsetNoteMailer(InsetNote & inset)
325 string const InsetNoteMailer::inset2string(Buffer const &) const
327 return params2string(inset_.params());
331 string const InsetNoteMailer::params2string(InsetNoteParams const & params)
334 data << name_ << ' ';
340 void InsetNoteMailer::string2params(string const & in,
341 InsetNoteParams & params)
343 params = InsetNoteParams();
348 istringstream data(in);
354 if (!lex || name != name_) {
355 lyxerr << "InsetNoteMailer::string2params(" << in << ")\n"
356 << "Missing identifier \"" << name_ << '"' << std::endl;
360 // This is part of the inset proper that is usually swallowed
361 // by LyXText::readInset
364 if (!lex || inset_id != "Note")