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"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetIterator.h"
24 #include "InsetList.h"
25 #include "LaTeXFeatures.h"
28 #include "MetricsInfo.h"
29 #include "output_xhtml.h"
32 #include "frontends/FontMetrics.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
39 using namespace lyx::support;
44 /////////////////////////////////////////////////////////////////////
48 /////////////////////////////////////////////////////////////////////
50 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
51 : InsetCommand(buf, p, "nomenclature"),
52 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
56 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
58 static ParamInfo param_info_;
59 if (param_info_.empty()) {
60 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
61 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
62 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
68 docstring InsetNomencl::screenLabel() const
70 size_t const maxLabelChars = 25;
72 docstring label = _("Nom: ") + getParam("symbol");
73 if (label.size() > maxLabelChars) {
74 label.erase(maxLabelChars - 3);
81 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
83 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
84 tip += _("Description: ") + "\t"
85 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
86 if (!getParam("prefix").empty())
87 tip += "\n" + _("Sorting: ") + getParam("prefix");
93 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
95 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
96 << sgml::escapeString(getParam("symbol"))
102 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
108 int InsetNomencl::docbookGlossary(odocstream & os) const
110 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
112 << sgml::escapeString(getParam("symbol"))
114 << "<glossdef><para>"
115 << sgml::escapeString(getParam("description"))
116 << "</para></glossdef>\n"
122 void InsetNomencl::validate(LaTeXFeatures & features) const
124 features.require("nomencl");
128 /////////////////////////////////////////////////////////////////////
132 /////////////////////////////////////////////////////////////////////
134 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
135 : InsetCommand(buf, p, "nomencl_print")
139 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
141 // The symbol width is set via nomencl's \nomlabelwidth in
142 // InsetPrintNomencl::latex and not as optional parameter of
143 // \printnomenclature
144 static ParamInfo param_info_;
145 if (param_info_.empty()) {
146 // how is the width set?
147 // values: none|auto|custom
148 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
150 param_info_.add("width", ParamInfo::LYX_INTERNAL);
156 docstring InsetPrintNomencl::screenLabel() const
158 return _("Nomenclature");
162 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
164 switch (cmd.action) {
166 case LFUN_INSET_MODIFY: {
167 InsetCommandParams p(NOMENCL_PRINT_CODE);
169 InsetCommand::string2params("nomencl_print",
170 to_utf8(cmd.argument()), p);
171 if (p.getCmdName().empty()) {
180 InsetCommand::doDispatch(cur, cmd);
186 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
187 FuncStatus & status) const
189 switch (cmd.action) {
191 case LFUN_INSET_DIALOG_UPDATE:
192 case LFUN_INSET_MODIFY:
193 status.setEnabled(true);
197 return InsetCommand::getStatus(cur, cmd, status);
202 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
208 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
210 os << "<glossary>\n";
212 InsetIterator it = inset_iterator_begin(buffer().inset());
214 if (it->lyxCode() == NOMENCL_CODE) {
215 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
217 } else if (!it->producesOutput()) {
218 // Ignore contents of insets that are not in output
219 size_t const depth = it.depth();
221 while (it.depth() > depth)
227 os << "</glossary>\n";
233 docstring nomenclWidest(Buffer const & buffer)
235 // nomenclWidest() determines and returns the widest used
236 // nomenclature symbol in the document
240 InsetNomencl const * nomencl = 0;
241 ParagraphList::const_iterator it = buffer.paragraphs().begin();
242 ParagraphList::const_iterator end = buffer.paragraphs().end();
244 for (; it != end; ++it) {
245 if (it->insetList().empty())
247 InsetList::const_iterator iit = it->insetList().begin();
248 InsetList::const_iterator eend = it->insetList().end();
249 for (; iit != eend; ++iit) {
250 Inset * inset = iit->inset;
251 if (inset->lyxCode() != NOMENCL_CODE)
253 nomencl = static_cast<InsetNomencl const *>(inset);
254 docstring const symbol =
255 nomencl->getParam("symbol");
256 // This is only an approximation,
257 // but the best we can get.
258 int const wx = use_gui ?
259 theFontMetrics(Font()).width(symbol) :
267 // return the widest (or an empty) string
273 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
276 if (getParam("set_width") == "auto") {
277 docstring widest = nomenclWidest(buffer());
278 // Set the label width via nomencl's command \nomlabelwidth.
279 // This must be output before the command \printnomenclature
280 if (!widest.empty()) {
281 os << "\\settowidth{\\nomlabelwidth}{"
286 } else if (getParam("set_width") == "custom") {
287 // custom length as optional arg of \printnomenclature
289 Length(to_ascii(getParam("width"))).asLatexString();
291 << from_ascii(getCmdName())
297 // output the command \printnomenclature
303 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
305 features.require("nomencl");
309 InsetCode InsetPrintNomencl::lyxCode() const
311 return NOMENCL_PRINT_CODE;
315 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
317 return from_ascii("context-nomenclprint");