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"
20 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
34 #include "frontends/FontMetrics.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
42 using namespace lyx::support;
47 /////////////////////////////////////////////////////////////////////
51 /////////////////////////////////////////////////////////////////////
53 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
54 : InsetCommand(buf, p),
55 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
59 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
61 static ParamInfo param_info_;
62 if (param_info_.empty()) {
63 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
64 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
65 ParamInfo::HANDLING_LATEXIFY);
66 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::HANDLING_LATEXIFY);
73 docstring InsetNomencl::screenLabel() const
75 size_t const maxLabelChars = 25;
77 docstring label = _("Nom: ") + getParam("symbol");
78 if (label.size() > maxLabelChars) {
79 label.erase(maxLabelChars - 3);
86 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
88 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
89 tip += _("Description: ") + "\t"
90 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
91 if (!getParam("prefix").empty())
92 tip += "\n" + _("Sorting: ") + getParam("prefix");
98 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
100 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
101 << sgml::escapeString(getParam("symbol"))
107 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
113 int InsetNomencl::docbookGlossary(odocstream & os) const
115 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
117 << sgml::escapeString(getParam("symbol"))
119 << "<glossdef><para>"
120 << sgml::escapeString(getParam("description"))
121 << "</para></glossdef>\n"
127 void InsetNomencl::validate(LaTeXFeatures & features) const
129 features.require("nomencl");
133 /////////////////////////////////////////////////////////////////////
137 /////////////////////////////////////////////////////////////////////
139 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
140 : InsetCommand(buf, p)
144 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
146 // The symbol width is set via nomencl's \nomlabelwidth in
147 // InsetPrintNomencl::latex and not as optional parameter of
148 // \printnomenclature
149 static ParamInfo param_info_;
150 if (param_info_.empty()) {
151 // how is the width set?
152 // values: none|auto|custom
153 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
155 param_info_.add("width", ParamInfo::LYX_INTERNAL);
161 docstring InsetPrintNomencl::screenLabel() const
163 return _("Nomenclature");
167 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
169 switch (cmd.action()) {
171 case LFUN_INSET_MODIFY: {
172 InsetCommandParams p(NOMENCL_PRINT_CODE);
174 InsetCommand::string2params(to_utf8(cmd.argument()), p);
175 if (p.getCmdName().empty()) {
176 cur.noScreenUpdate();
186 InsetCommand::doDispatch(cur, cmd);
192 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
193 FuncStatus & status) const
195 switch (cmd.action()) {
197 case LFUN_INSET_DIALOG_UPDATE:
198 case LFUN_INSET_MODIFY:
199 status.setEnabled(true);
203 return InsetCommand::getStatus(cur, cmd, status);
208 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
214 // FIXME This should be changed to use the TOC. Perhaps
215 // that could be done when XHTML output is added.
216 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
218 os << "<glossary>\n";
220 InsetIterator it = inset_iterator_begin(buffer().inset());
222 if (it->lyxCode() == NOMENCL_CODE) {
223 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
225 } else if (!it->producesOutput()) {
226 // Ignore contents of insets that are not in output
227 size_t const depth = it.depth();
229 while (it.depth() > depth)
235 os << "</glossary>\n";
241 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
243 // nomenclWidest() determines and returns the widest used
244 // nomenclature symbol in the document
248 InsetNomencl const * nomencl = 0;
249 ParagraphList::const_iterator it = buffer.paragraphs().begin();
250 ParagraphList::const_iterator end = buffer.paragraphs().end();
252 for (; it != end; ++it) {
253 if (it->insetList().empty())
255 InsetList::const_iterator iit = it->insetList().begin();
256 InsetList::const_iterator eend = it->insetList().end();
257 for (; iit != eend; ++iit) {
258 Inset * inset = iit->inset;
259 if (inset->lyxCode() != NOMENCL_CODE)
261 nomencl = static_cast<InsetNomencl const *>(inset);
262 docstring const symbol =
263 nomencl->getParam("symbol");
264 // This is only an approximation,
265 // but the best we can get.
266 int const wx = use_gui ?
267 theFontMetrics(Font()).width(symbol) :
275 // return the widest (or an empty) string
279 // we have to encode the string properly
280 pair<docstring, docstring> latex_symb =
281 runparams.encoding->latexString(symb, runparams.dryrun);
282 if (!latex_symb.second.empty())
283 LYXERR0("Omitting uncodable characters '"
285 << "' in nomencl widest string!");
286 return latex_symb.first;
291 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
293 OutputParams runparams = runparams_in;
294 if (getParam("set_width") == "auto") {
295 docstring widest = nomenclWidest(buffer(), runparams);
296 // Set the label width via nomencl's command \nomlabelwidth.
297 // This must be output before the command \printnomenclature
298 if (!widest.empty()) {
299 os << "\\settowidth{\\nomlabelwidth}{"
303 } else if (getParam("set_width") == "custom") {
304 // custom length as optional arg of \printnomenclature
306 Length(to_ascii(getParam("width"))).asLatexString();
308 << from_ascii(getCmdName())
314 // output the command \printnomenclature
315 os << getCommand(runparams);
319 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
321 features.require("nomencl");
325 InsetCode InsetPrintNomencl::lyxCode() const
327 return NOMENCL_PRINT_CODE;
331 string InsetPrintNomencl::contextMenuName() const
333 return "context-nomenclprint";