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"
31 #include "frontends/FontMetrics.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
38 using namespace lyx::support;
43 /////////////////////////////////////////////////////////////////////
47 /////////////////////////////////////////////////////////////////////
49 InsetNomencl::InsetNomencl(InsetCommandParams const & p)
50 : InsetCommand(p, "nomenclature"),
51 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
55 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
57 static ParamInfo param_info_;
58 if (param_info_.empty()) {
59 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
60 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
61 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
67 docstring InsetNomencl::screenLabel() const
69 size_t const maxLabelChars = 25;
71 docstring label = _("Nom: ") + getParam("symbol");
72 if (label.size() > maxLabelChars) {
73 label.erase(maxLabelChars - 3);
80 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
82 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
83 tip += _("Description: ") + "\t"
84 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
85 if (!getParam("prefix").empty())
86 tip += "\n" + _("Sorting: ") + getParam("prefix");
92 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
94 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
95 << sgml::escapeString(getParam("symbol"))
101 int InsetNomencl::docbookGlossary(odocstream & os) const
103 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
105 << sgml::escapeString(getParam("symbol"))
107 << "<glossdef><para>"
108 << sgml::escapeString(getParam("description"))
109 << "</para></glossdef>\n"
115 void InsetNomencl::validate(LaTeXFeatures & features) const
117 features.require("nomencl");
121 /////////////////////////////////////////////////////////////////////
125 /////////////////////////////////////////////////////////////////////
127 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
128 : InsetCommand(p, "nomencl_print")
132 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
134 // The symbol width is set via nomencl's \nomlabelwidth in
135 // InsetPrintNomencl::latex and not as optional parameter of
136 // \printnomenclature
137 static ParamInfo param_info_;
138 if (param_info_.empty()) {
139 // how is the width set?
140 // values: none|auto|custom
141 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
143 param_info_.add("width", ParamInfo::LYX_INTERNAL);
149 docstring InsetPrintNomencl::screenLabel() const
151 return _("Nomenclature");
155 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
157 switch (cmd.action) {
159 case LFUN_INSET_MODIFY: {
160 InsetCommandParams p(NOMENCL_PRINT_CODE);
162 InsetCommand::string2params("nomencl_print",
163 to_utf8(cmd.argument()), p);
164 if (p.getCmdName().empty()) {
173 InsetCommand::doDispatch(cur, cmd);
179 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
180 FuncStatus & status) const
182 switch (cmd.action) {
184 case LFUN_INSET_DIALOG_UPDATE:
185 case LFUN_INSET_MODIFY:
186 status.setEnabled(true);
190 return InsetCommand::getStatus(cur, cmd, status);
196 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
198 os << "<glossary>\n";
200 InsetIterator it = inset_iterator_begin(buffer().inset());
202 if (it->lyxCode() == NOMENCL_CODE) {
203 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
205 } else if (!it->producesOutput()) {
206 // Ignore contents of insets that are not in output
207 size_t const depth = it.depth();
209 while (it.depth() > depth)
215 os << "</glossary>\n";
221 docstring nomenclWidest(Buffer const & buffer)
223 // nomenclWidest() determines and returns the widest used
224 // nomenclature symbol in the document
228 InsetNomencl const * nomencl = 0;
229 ParagraphList::const_iterator it = buffer.paragraphs().begin();
230 ParagraphList::const_iterator end = buffer.paragraphs().end();
232 for (; it != end; ++it) {
233 if (it->insetList().empty())
235 InsetList::const_iterator iit = it->insetList().begin();
236 InsetList::const_iterator eend = it->insetList().end();
237 for (; iit != eend; ++iit) {
238 Inset * inset = iit->inset;
239 if (inset->lyxCode() != NOMENCL_CODE)
241 nomencl = static_cast<InsetNomencl const *>(inset);
242 docstring const symbol =
243 nomencl->getParam("symbol");
244 // This is only an approximation,
245 // but the best we can get.
246 int const wx = use_gui ?
247 theFontMetrics(Font()).width(symbol) :
255 // return the widest (or an empty) string
261 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
264 if (getParam("set_width") == "auto") {
265 docstring widest = nomenclWidest(buffer());
266 // Set the label width via nomencl's command \nomlabelwidth.
267 // This must be output before the command \printnomenclature
268 if (!widest.empty()) {
269 os << "\\settowidth{\\nomlabelwidth}{"
274 } else if (getParam("set_width") == "custom") {
275 // custom length as optional arg of \printnomenclature
277 Length(to_ascii(getParam("width"))).asLatexString();
279 << from_ascii(getCmdName())
285 // output the command \printnomenclature
291 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
293 features.require("nomencl");
297 InsetCode InsetPrintNomencl::lyxCode() const
299 return NOMENCL_PRINT_CODE;
303 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
305 return from_ascii("context-nomenclprint");