2 * \file InsetNomencl.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
19 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
25 #include "InsetList.h"
26 #include "LaTeXFeatures.h"
29 #include "MetricsInfo.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
34 #include "frontends/FontMetrics.h"
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
41 using namespace lyx::support;
46 /////////////////////////////////////////////////////////////////////
50 /////////////////////////////////////////////////////////////////////
52 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
53 : InsetCommand(buf, p, "nomenclature"),
54 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
58 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
60 static ParamInfo param_info_;
61 if (param_info_.empty()) {
62 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
63 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
64 ParamInfo::HANDLING_LATEXIFY);
65 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
66 ParamInfo::HANDLING_LATEXIFY);
72 docstring InsetNomencl::screenLabel() const
74 size_t const maxLabelChars = 25;
76 docstring label = _("Nom: ") + getParam("symbol");
77 if (label.size() > maxLabelChars) {
78 label.erase(maxLabelChars - 3);
85 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
87 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
88 tip += _("Description: ") + "\t"
89 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
90 if (!getParam("prefix").empty())
91 tip += "\n" + _("Sorting: ") + getParam("prefix");
97 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
99 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
100 << sgml::escapeString(getParam("symbol"))
106 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
112 int InsetNomencl::docbookGlossary(odocstream & os) const
114 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
116 << sgml::escapeString(getParam("symbol"))
118 << "<glossdef><para>"
119 << sgml::escapeString(getParam("description"))
120 << "</para></glossdef>\n"
126 void InsetNomencl::validate(LaTeXFeatures & features) const
128 features.require("nomencl");
132 /////////////////////////////////////////////////////////////////////
136 /////////////////////////////////////////////////////////////////////
138 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
139 : InsetCommand(buf, p, "nomencl_print")
143 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
145 // The symbol width is set via nomencl's \nomlabelwidth in
146 // InsetPrintNomencl::latex and not as optional parameter of
147 // \printnomenclature
148 static ParamInfo param_info_;
149 if (param_info_.empty()) {
150 // how is the width set?
151 // values: none|auto|custom
152 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
154 param_info_.add("width", ParamInfo::LYX_INTERNAL);
160 docstring InsetPrintNomencl::screenLabel() const
162 return _("Nomenclature");
166 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
168 switch (cmd.action) {
170 case LFUN_INSET_MODIFY: {
171 InsetCommandParams p(NOMENCL_PRINT_CODE);
173 InsetCommand::string2params("nomencl_print",
174 to_utf8(cmd.argument()), p);
175 if (p.getCmdName().empty()) {
184 InsetCommand::doDispatch(cur, cmd);
190 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & status) const
193 switch (cmd.action) {
195 case LFUN_INSET_DIALOG_UPDATE:
196 case LFUN_INSET_MODIFY:
197 status.setEnabled(true);
201 return InsetCommand::getStatus(cur, cmd, status);
206 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
212 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
214 os << "<glossary>\n";
216 InsetIterator it = inset_iterator_begin(buffer().inset());
218 if (it->lyxCode() == NOMENCL_CODE) {
219 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
221 } else if (!it->producesOutput()) {
222 // Ignore contents of insets that are not in output
223 size_t const depth = it.depth();
225 while (it.depth() > depth)
231 os << "</glossary>\n";
237 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
239 // nomenclWidest() determines and returns the widest used
240 // nomenclature symbol in the document
244 InsetNomencl const * nomencl = 0;
245 ParagraphList::const_iterator it = buffer.paragraphs().begin();
246 ParagraphList::const_iterator end = buffer.paragraphs().end();
248 for (; it != end; ++it) {
249 if (it->insetList().empty())
251 InsetList::const_iterator iit = it->insetList().begin();
252 InsetList::const_iterator eend = it->insetList().end();
253 for (; iit != eend; ++iit) {
254 Inset * inset = iit->inset;
255 if (inset->lyxCode() != NOMENCL_CODE)
257 nomencl = static_cast<InsetNomencl const *>(inset);
258 docstring const symbol =
259 nomencl->getParam("symbol");
260 // This is only an approximation,
261 // but the best we can get.
262 int const wx = use_gui ?
263 theFontMetrics(Font()).width(symbol) :
271 // return the widest (or an empty) string
273 docstring latex_symb;
274 for (size_t n = 0; n < symb.size(); ++n) {
276 latex_symb += runparams.encoding->latexChar(symb[n]);
277 } catch (EncodingException & /* e */) {
278 if (runparams.dryrun) {
279 latex_symb += "<" + _("LyX Warning: ")
280 + _("uncodable character") + " '";
281 latex_symb += docstring(1, symb[n]);
293 int InsetPrintNomencl::latex(odocstream & os, OutputParams const & runparams_in) const
295 OutputParams runparams = runparams_in;
297 if (getParam("set_width") == "auto") {
298 docstring widest = nomenclWidest(buffer(), runparams);
299 // Set the label width via nomencl's command \nomlabelwidth.
300 // This must be output before the command \printnomenclature
301 if (!widest.empty()) {
302 os << "\\settowidth{\\nomlabelwidth}{"
307 } else if (getParam("set_width") == "custom") {
308 // custom length as optional arg of \printnomenclature
310 Length(to_ascii(getParam("width"))).asLatexString();
312 << from_ascii(getCmdName())
318 // output the command \printnomenclature
319 os << getCommand(runparams);
324 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
326 features.require("nomencl");
330 InsetCode InsetPrintNomencl::lyxCode() const
332 return NOMENCL_PRINT_CODE;
336 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
338 return from_ascii("context-nomenclprint");