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"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
25 #include "InsetList.h"
26 #include "LaTeXFeatures.h"
29 #include "OutputParams.h"
30 #include "output_xhtml.h"
33 #include "frontends/FontMetrics.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
40 using namespace lyx::support;
45 /////////////////////////////////////////////////////////////////////
49 /////////////////////////////////////////////////////////////////////
51 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
52 : InsetCommand(buf, p),
53 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
57 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
59 static ParamInfo param_info_;
60 if (param_info_.empty()) {
61 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
62 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
63 ParamInfo::HANDLING_LATEXIFY);
64 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
65 ParamInfo::HANDLING_LATEXIFY);
71 docstring InsetNomencl::screenLabel() const
73 size_t const maxLabelChars = 25;
75 docstring label = _("Nom: ") + getParam("symbol");
76 if (label.size() > maxLabelChars) {
77 label.erase(maxLabelChars - 3);
84 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
86 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
87 tip += _("Description: ") + "\t"
88 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
89 if (!getParam("prefix").empty())
90 tip += "\n" + _("Sorting: ") + getParam("prefix");
96 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
98 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
99 << sgml::escapeString(getParam("symbol"))
105 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
111 int InsetNomencl::docbookGlossary(odocstream & os) const
113 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
115 << sgml::escapeString(getParam("symbol"))
117 << "<glossdef><para>"
118 << sgml::escapeString(getParam("description"))
119 << "</para></glossdef>\n"
125 void InsetNomencl::validate(LaTeXFeatures & features) const
127 features.require("nomencl");
131 /////////////////////////////////////////////////////////////////////
135 /////////////////////////////////////////////////////////////////////
137 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
138 : InsetCommand(buf, p)
142 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
144 // The symbol width is set via nomencl's \nomlabelwidth in
145 // InsetPrintNomencl::latex and not as optional parameter of
146 // \printnomenclature
147 static ParamInfo param_info_;
148 if (param_info_.empty()) {
149 // how is the width set?
150 // values: none|auto|custom
151 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
153 param_info_.add("width", ParamInfo::LYX_INTERNAL);
159 docstring InsetPrintNomencl::screenLabel() const
161 return _("Nomenclature");
165 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
167 switch (cmd.action()) {
169 case LFUN_INSET_MODIFY: {
170 InsetCommandParams p(NOMENCL_PRINT_CODE);
172 InsetCommand::string2params(to_utf8(cmd.argument()), p);
173 if (p.getCmdName().empty()) {
174 cur.noScreenUpdate();
182 InsetCommand::doDispatch(cur, cmd);
188 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
189 FuncStatus & status) const
191 switch (cmd.action()) {
193 case LFUN_INSET_DIALOG_UPDATE:
194 case LFUN_INSET_MODIFY:
195 status.setEnabled(true);
199 return InsetCommand::getStatus(cur, cmd, status);
204 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
210 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
212 os << "<glossary>\n";
214 InsetIterator it = inset_iterator_begin(buffer().inset());
216 if (it->lyxCode() == NOMENCL_CODE) {
217 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
219 } else if (!it->producesOutput()) {
220 // Ignore contents of insets that are not in output
221 size_t const depth = it.depth();
223 while (it.depth() > depth)
229 os << "</glossary>\n";
235 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
237 // nomenclWidest() determines and returns the widest used
238 // nomenclature symbol in the document
242 InsetNomencl const * nomencl = 0;
243 ParagraphList::const_iterator it = buffer.paragraphs().begin();
244 ParagraphList::const_iterator end = buffer.paragraphs().end();
246 for (; it != end; ++it) {
247 if (it->insetList().empty())
249 InsetList::const_iterator iit = it->insetList().begin();
250 InsetList::const_iterator eend = it->insetList().end();
251 for (; iit != eend; ++iit) {
252 Inset * inset = iit->inset;
253 if (inset->lyxCode() != NOMENCL_CODE)
255 nomencl = static_cast<InsetNomencl const *>(inset);
256 docstring const symbol =
257 nomencl->getParam("symbol");
258 // This is only an approximation,
259 // but the best we can get.
260 int const wx = use_gui ?
261 theFontMetrics(Font()).width(symbol) :
269 // return the widest (or an empty) string
273 // we have to encode the string properly
274 docstring latex_symb;
275 for (size_t n = 0; n < symb.size(); ++n) {
277 latex_symb += runparams.encoding->latexChar(symb[n]);
278 } catch (EncodingException & /* e */) {
279 if (runparams.dryrun) {
280 latex_symb += "<" + _("LyX Warning: ")
281 + _("uncodable character") + " '";
282 latex_symb += docstring(1, symb[n]);
292 int InsetPrintNomencl::latex(odocstream & os, OutputParams const & runparams_in) const
294 OutputParams runparams = runparams_in;
296 if (getParam("set_width") == "auto") {
297 docstring widest = nomenclWidest(buffer(), runparams);
298 // Set the label width via nomencl's command \nomlabelwidth.
299 // This must be output before the command \printnomenclature
300 if (!widest.empty()) {
301 os << "\\settowidth{\\nomlabelwidth}{"
306 } else if (getParam("set_width") == "custom") {
307 // custom length as optional arg of \printnomenclature
309 Length(to_ascii(getParam("width"))).asLatexString();
311 << from_ascii(getCmdName())
317 // output the command \printnomenclature
318 os << getCommand(runparams);
323 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
325 features.require("nomencl");
329 InsetCode InsetPrintNomencl::lyxCode() const
331 return NOMENCL_PRINT_CODE;
335 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
337 return from_ascii("context-nomenclprint");