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"
27 #include "MetricsInfo.h"
30 #include "frontends/FontMetrics.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
37 using namespace lyx::support;
42 /////////////////////////////////////////////////////////////////////
46 /////////////////////////////////////////////////////////////////////
48 InsetNomencl::InsetNomencl(InsetCommandParams const & p)
49 : InsetCommand(p, "nomenclature"),
50 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
54 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
56 static ParamInfo param_info_;
57 if (param_info_.empty()) {
58 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
59 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
60 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
66 docstring InsetNomencl::screenLabel() const
68 size_t const maxLabelChars = 25;
70 docstring label = _("Nom: ") + getParam("symbol");
71 if (label.size() > maxLabelChars) {
72 label.erase(maxLabelChars - 3);
79 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
81 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
82 tip += _("Description: ") + "\t"
83 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
84 if (!getParam("prefix").empty())
85 tip += "\n" + _("Sorting: ") + getParam("prefix");
91 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
93 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
94 << sgml::escapeString(getParam("symbol"))
100 int InsetNomencl::docbookGlossary(odocstream & os) const
102 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
104 << sgml::escapeString(getParam("symbol"))
106 << "<glossdef><para>"
107 << sgml::escapeString(getParam("description"))
108 << "</para></glossdef>\n"
114 void InsetNomencl::validate(LaTeXFeatures & features) const
116 features.require("nomencl");
120 /////////////////////////////////////////////////////////////////////
124 /////////////////////////////////////////////////////////////////////
126 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
127 : InsetCommand(p, "nomencl_print")
131 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
133 // The symbol width is set via nomencl's \nomlabelwidth in
134 // InsetPrintNomencl::latex and not as optional parameter of
135 // \printnomenclature
136 static ParamInfo param_info_;
137 if (param_info_.empty()) {
138 // how is the width set?
139 // values: none|auto|custom
140 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
142 param_info_.add("width", ParamInfo::LYX_INTERNAL);
148 docstring InsetPrintNomencl::screenLabel() const
150 return _("Nomenclature");
154 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
156 switch (cmd.action) {
158 case LFUN_INSET_MODIFY: {
159 InsetCommandParams p(NOMENCL_PRINT_CODE);
161 InsetCommand::string2params("nomencl_print",
162 to_utf8(cmd.argument()), p);
163 if (p.getCmdName().empty()) {
172 InsetCommand::doDispatch(cur, cmd);
178 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & status) const
181 switch (cmd.action) {
183 case LFUN_INSET_DIALOG_UPDATE:
184 case LFUN_INSET_MODIFY:
185 status.setEnabled(true);
189 return InsetCommand::getStatus(cur, cmd, status);
195 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
197 os << "<glossary>\n";
199 InsetIterator it = inset_iterator_begin(buffer().inset());
201 if (it->lyxCode() == NOMENCL_CODE) {
202 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
204 } else if (!it->producesOutput()) {
205 // Ignore contents of insets that are not in output
206 size_t const depth = it.depth();
208 while (it.depth() > depth)
214 os << "</glossary>\n";
220 docstring nomenclWidest(Buffer const & buffer)
222 // nomenclWidest() determines and returns the widest used
223 // nomenclature symbol in the document
227 InsetNomencl const * nomencl = 0;
228 ParagraphList::const_iterator it = buffer.paragraphs().begin();
229 ParagraphList::const_iterator end = buffer.paragraphs().end();
231 for (; it != end; ++it) {
232 if (it->insetList().empty())
234 InsetList::const_iterator iit = it->insetList().begin();
235 InsetList::const_iterator eend = it->insetList().end();
236 for (; iit != eend; ++iit) {
237 Inset * inset = iit->inset;
238 if (inset->lyxCode() != NOMENCL_CODE)
240 nomencl = static_cast<InsetNomencl const *>(inset);
241 docstring const symbol =
242 nomencl->getParam("symbol");
243 // This is only an approximation,
244 // but the best we can get.
246 theFontMetrics(Font()).width(symbol);
253 // return the widest (or an empty) string
259 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
262 if (getParam("set_width") == "auto") {
263 docstring widest = nomenclWidest(buffer());
264 // Set the label width via nomencl's command \nomlabelwidth.
265 // This must be output before the command
266 // \printnomenclature
267 if (!widest.empty()) {
268 // assure that the width is never below the
269 // value predefined in nomencl.cfg
270 os << "\\newlength{\\symbwidth}\n";
271 os << "\\settowidth{\\symbwidth}{"
274 os << "\\ifthenelse{%\n";
275 os << "\\lengthtest{\\symbwidth < \\nomlabelwidth}}\n";
276 os << " {\\setlength{\\symbwidth}{\\nomlabelwidth}}\n";
280 } else if (getParam("set_width") == "custom") {
281 // custom length as optional arg of \printnomenclature
283 Length(to_ascii(getParam("width"))).asLatexString();
285 << from_ascii(getCmdName())
291 // output the command \printnomenclature
297 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
299 features.require("nomencl");
300 // needed for InsetPrintNomencl::latex
301 features.require("ifthen");
305 InsetCode InsetPrintNomencl::lyxCode() const
307 return NOMENCL_PRINT_CODE;
311 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
313 return from_ascii("context-nomenclprint");