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"
20 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "LaTeXFeatures.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
33 #include "TocBackend.h"
35 #include "frontends/FontMetrics.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
43 using namespace lyx::support;
48 /////////////////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////////
54 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
55 : InsetCommand(buf, p),
56 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
60 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
62 static ParamInfo param_info_;
63 if (param_info_.empty()) {
64 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
65 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
66 ParamInfo::HANDLING_LATEXIFY);
67 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
68 ParamInfo::HANDLING_LATEXIFY);
74 docstring InsetNomencl::screenLabel() const
76 size_t const maxLabelChars = 25;
78 docstring label = _("Nom: ") + getParam("symbol");
79 if (label.size() > maxLabelChars) {
80 label.erase(maxLabelChars - 3);
87 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
89 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
90 tip += _("Description: ") + "\t"
91 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
92 if (!getParam("prefix").empty())
93 tip += "\n" + _("Sorting: ") + getParam("prefix");
99 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
101 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
102 << sgml::escapeString(getParam("symbol"))
108 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
114 int InsetNomencl::docbookGlossary(odocstream & os) const
116 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
118 << sgml::escapeString(getParam("symbol"))
120 << "<glossdef><para>"
121 << sgml::escapeString(getParam("description"))
122 << "</para></glossdef>\n"
128 void InsetNomencl::validate(LaTeXFeatures & features) const
130 features.require("nomencl");
134 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active) const
136 docstring const str = getParam("symbol");
137 buffer().tocBackend().toc("nomencl").push_back(TocItem(cpit, 0, str, output_active));
141 /////////////////////////////////////////////////////////////////////
145 /////////////////////////////////////////////////////////////////////
147 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
148 : InsetCommand(buf, p)
152 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
154 // The symbol width is set via nomencl's \nomlabelwidth in
155 // InsetPrintNomencl::latex and not as optional parameter of
156 // \printnomenclature
157 static ParamInfo param_info_;
158 if (param_info_.empty()) {
159 // how is the width set?
160 // values: none|auto|custom
161 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
163 param_info_.add("width", ParamInfo::LYX_INTERNAL);
169 docstring InsetPrintNomencl::screenLabel() const
171 return _("Nomenclature");
175 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
177 switch (cmd.action()) {
179 case LFUN_INSET_MODIFY: {
180 InsetCommandParams p(NOMENCL_PRINT_CODE);
182 InsetCommand::string2params(to_utf8(cmd.argument()), p);
183 if (p.getCmdName().empty()) {
184 cur.noScreenUpdate();
194 InsetCommand::doDispatch(cur, cmd);
200 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
201 FuncStatus & status) const
203 switch (cmd.action()) {
205 case LFUN_INSET_DIALOG_UPDATE:
206 case LFUN_INSET_MODIFY:
207 status.setEnabled(true);
211 return InsetCommand::getStatus(cur, cmd, status);
216 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
222 // FIXME This should be changed to use the TOC. Perhaps
223 // that could be done when XHTML output is added.
224 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
226 os << "<glossary>\n";
228 InsetIterator it = inset_iterator_begin(buffer().inset());
230 if (it->lyxCode() == NOMENCL_CODE) {
231 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
233 } else if (!it->producesOutput()) {
234 // Ignore contents of insets that are not in output
235 size_t const depth = it.depth();
237 while (it.depth() > depth)
243 os << "</glossary>\n";
249 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
251 // nomenclWidest() determines and returns the widest used
252 // nomenclature symbol in the document
256 InsetNomencl const * nomencl = 0;
257 ParagraphList::const_iterator it = buffer.paragraphs().begin();
258 ParagraphList::const_iterator end = buffer.paragraphs().end();
260 for (; it != end; ++it) {
261 if (it->insetList().empty())
263 InsetList::const_iterator iit = it->insetList().begin();
264 InsetList::const_iterator eend = it->insetList().end();
265 for (; iit != eend; ++iit) {
266 Inset * inset = iit->inset;
267 if (inset->lyxCode() != NOMENCL_CODE)
269 nomencl = static_cast<InsetNomencl const *>(inset);
270 docstring const symbol =
271 nomencl->getParam("symbol");
272 // This is only an approximation,
273 // but the best we can get.
274 int const wx = use_gui ?
275 theFontMetrics(Font()).width(symbol) :
283 // return the widest (or an empty) string
287 // we have to encode the string properly
288 pair<docstring, docstring> latex_symb =
289 runparams.encoding->latexString(symb, runparams.dryrun);
290 if (!latex_symb.second.empty())
291 LYXERR0("Omitting uncodable characters '"
293 << "' in nomencl widest string!");
294 return latex_symb.first;
299 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
301 OutputParams runparams = runparams_in;
302 if (getParam("set_width") == "auto") {
303 docstring widest = nomenclWidest(buffer(), runparams);
304 // Set the label width via nomencl's command \nomlabelwidth.
305 // This must be output before the command \printnomenclature
306 if (!widest.empty()) {
307 os << "\\settowidth{\\nomlabelwidth}{"
311 } else if (getParam("set_width") == "custom") {
312 // custom length as optional arg of \printnomenclature
314 Length(to_ascii(getParam("width"))).asLatexString();
316 << from_ascii(getCmdName())
322 // output the command \printnomenclature
323 os << getCommand(runparams);
327 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
329 features.require("nomencl");
333 InsetCode InsetPrintNomencl::lyxCode() const
335 return NOMENCL_PRINT_CODE;
339 string InsetPrintNomencl::contextMenuName() const
341 return "context-nomenclprint";