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(Buffer * buf, InsetCommandParams const & p)
50 : InsetCommand(buf, 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 docstring InsetNomencl::xhtml(odocstream &, OutputParams const &) const
107 int InsetNomencl::docbookGlossary(odocstream & os) const
109 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
111 << sgml::escapeString(getParam("symbol"))
113 << "<glossdef><para>"
114 << sgml::escapeString(getParam("description"))
115 << "</para></glossdef>\n"
121 void InsetNomencl::validate(LaTeXFeatures & features) const
123 features.require("nomencl");
127 /////////////////////////////////////////////////////////////////////
131 /////////////////////////////////////////////////////////////////////
133 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
134 : InsetCommand(buf, p, "nomencl_print")
138 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
140 // The symbol width is set via nomencl's \nomlabelwidth in
141 // InsetPrintNomencl::latex and not as optional parameter of
142 // \printnomenclature
143 static ParamInfo param_info_;
144 if (param_info_.empty()) {
145 // how is the width set?
146 // values: none|auto|custom
147 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
149 param_info_.add("width", ParamInfo::LYX_INTERNAL);
155 docstring InsetPrintNomencl::screenLabel() const
157 return _("Nomenclature");
161 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
163 switch (cmd.action) {
165 case LFUN_INSET_MODIFY: {
166 InsetCommandParams p(NOMENCL_PRINT_CODE);
168 InsetCommand::string2params("nomencl_print",
169 to_utf8(cmd.argument()), p);
170 if (p.getCmdName().empty()) {
179 InsetCommand::doDispatch(cur, cmd);
185 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
186 FuncStatus & status) const
188 switch (cmd.action) {
190 case LFUN_INSET_DIALOG_UPDATE:
191 case LFUN_INSET_MODIFY:
192 status.setEnabled(true);
196 return InsetCommand::getStatus(cur, cmd, status);
201 docstring InsetPrintNomencl::xhtml(odocstream &, OutputParams const &) const
207 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
209 os << "<glossary>\n";
211 InsetIterator it = inset_iterator_begin(buffer().inset());
213 if (it->lyxCode() == NOMENCL_CODE) {
214 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
216 } else if (!it->producesOutput()) {
217 // Ignore contents of insets that are not in output
218 size_t const depth = it.depth();
220 while (it.depth() > depth)
226 os << "</glossary>\n";
232 docstring nomenclWidest(Buffer const & buffer)
234 // nomenclWidest() determines and returns the widest used
235 // nomenclature symbol in the document
239 InsetNomencl const * nomencl = 0;
240 ParagraphList::const_iterator it = buffer.paragraphs().begin();
241 ParagraphList::const_iterator end = buffer.paragraphs().end();
243 for (; it != end; ++it) {
244 if (it->insetList().empty())
246 InsetList::const_iterator iit = it->insetList().begin();
247 InsetList::const_iterator eend = it->insetList().end();
248 for (; iit != eend; ++iit) {
249 Inset * inset = iit->inset;
250 if (inset->lyxCode() != NOMENCL_CODE)
252 nomencl = static_cast<InsetNomencl const *>(inset);
253 docstring const symbol =
254 nomencl->getParam("symbol");
255 // This is only an approximation,
256 // but the best we can get.
257 int const wx = use_gui ?
258 theFontMetrics(Font()).width(symbol) :
266 // return the widest (or an empty) string
272 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
275 if (getParam("set_width") == "auto") {
276 docstring widest = nomenclWidest(buffer());
277 // Set the label width via nomencl's command \nomlabelwidth.
278 // This must be output before the command \printnomenclature
279 if (!widest.empty()) {
280 os << "\\settowidth{\\nomlabelwidth}{"
285 } else if (getParam("set_width") == "custom") {
286 // custom length as optional arg of \printnomenclature
288 Length(to_ascii(getParam("width"))).asLatexString();
290 << from_ascii(getCmdName())
296 // output the command \printnomenclature
302 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
304 features.require("nomencl");
308 InsetCode InsetPrintNomencl::lyxCode() const
310 return NOMENCL_PRINT_CODE;
314 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
316 return from_ascii("context-nomenclprint");