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"
20 #include "FuncRequest.h"
21 #include "InsetIterator.h"
22 #include "InsetList.h"
23 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
32 using namespace lyx::support;
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
43 InsetNomencl::InsetNomencl(InsetCommandParams const & p)
44 : InsetCommand(p, "nomenclature"),
45 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
49 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
51 static ParamInfo param_info_;
52 if (param_info_.empty()) {
53 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
54 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
55 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
61 docstring InsetNomencl::screenLabel() const
63 size_t const maxLabelChars = 25;
65 docstring label = _("Nom: ") + getParam("symbol");
66 if (label.size() > maxLabelChars) {
67 label.erase(maxLabelChars - 3);
74 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
76 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
77 tip += _("Description: ") + "\t"
78 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
79 if (!getParam("prefix").empty())
80 tip += "\n" + _("Sorting: ") + getParam("prefix");
86 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
88 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
89 << sgml::escapeString(getParam("symbol"))
95 int InsetNomencl::docbookGlossary(odocstream & os) const
97 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
99 << sgml::escapeString(getParam("symbol"))
101 << "<glossdef><para>"
102 << sgml::escapeString(getParam("description"))
103 << "</para></glossdef>\n"
109 void InsetNomencl::validate(LaTeXFeatures & features) const
111 features.require("nomencl");
115 /////////////////////////////////////////////////////////////////////
119 /////////////////////////////////////////////////////////////////////
121 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
122 : InsetCommand(p, "printnomenclature")
126 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
128 // The symbol width is set via nomencl's \nomlabelwidth in
129 // InsetPrintNomencl::latex and not as optional parameter of
130 // \printnomenclature
131 static ParamInfo param_info_;
132 if (param_info_.empty()) {
133 // how is the width set?
135 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
141 docstring InsetPrintNomencl::screenLabel() const
143 return _("Nomenclature");
147 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
149 os << "<glossary>\n";
151 InsetIterator it = inset_iterator_begin(buffer().inset());
153 if (it->lyxCode() == NOMENCL_CODE) {
154 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
156 } else if (!it->producesOutput()) {
157 // Ignore contents of insets that are not in output
158 size_t const depth = it.depth();
160 while (it.depth() > depth)
166 os << "</glossary>\n";
172 docstring nomenclWidest(Buffer const & buffer)
174 // nomenclWidest() determines and returns the widest used
175 // nomenclature symbol in the document
179 InsetNomencl const * nomencl = 0;
180 ParagraphList::const_iterator it = buffer.paragraphs().begin();
181 ParagraphList::const_iterator end = buffer.paragraphs().end();
183 for (; it != end; ++it) {
184 if (it->insetList().empty())
186 InsetList::const_iterator iit = it->insetList().begin();
187 InsetList::const_iterator eend = it->insetList().end();
188 for (; iit != eend; ++iit) {
189 Inset * inset = iit->inset;
190 if (inset->lyxCode() != NOMENCL_CODE)
192 nomencl = static_cast<InsetNomencl const *>(inset);
193 docstring const symbol = nomencl->getParam("symbol");
194 // we can only check for the number of characters, since it is
195 // impossible to get the info that "iiiii" is smaller than "WW"
196 // we therefore output w times "W" as string ("W" is always the
198 int const wx = symbol.size();
203 // return the widest symbol as w times a "W"
204 for (int n = 1; n <= w; ++n)
211 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
214 if (getParam("set_width") == "auto") {
215 docstring widest = nomenclWidest(buffer());
216 // set the label width via nomencl's command
217 // \nomlabelwidth. This must be output before the command
218 // \printnomenclature
219 if (!widest.empty()) {
220 // assure that the width is never below the
221 // predefined value of 1 cm
222 os << "\\settowidth{\\nomlabelwidth}{"
225 os << "\\ifthenelse{%\n";
226 os << "\\lengthtest{\\nomlabelwidth < 1cm}}\n";
227 os << " {\\setlength{\\nomlabelwidth}{1cm}}\n";
232 // output the command \printnomenclature
238 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
240 features.require("nomencl");
241 // needed for InsetPrintNomencl::latex
242 features.require("ifthen");
246 InsetCode InsetPrintNomencl::lyxCode() const
248 return NOMENCL_PRINT_CODE;