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"
28 #include "LaTeXFeatures.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
34 #include "TocBackend.h"
36 #include "frontends/FontMetrics.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
44 using namespace lyx::support;
49 /////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////
55 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
56 : InsetCommand(buf, p),
57 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
61 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
63 static ParamInfo param_info_;
64 if (param_info_.empty()) {
65 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
66 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
67 ParamInfo::HANDLING_LATEXIFY);
68 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
69 ParamInfo::HANDLING_LATEXIFY);
75 docstring InsetNomencl::screenLabel() const
77 size_t const maxLabelChars = 25;
78 docstring label = _("Nom: ") + getParam("symbol");
79 support::truncateWithEllipsis(label, maxLabelChars);
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 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
134 docstring const str = getParam("symbol");
135 buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, output_active));
139 /////////////////////////////////////////////////////////////////////
143 /////////////////////////////////////////////////////////////////////
145 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
146 : InsetCommand(buf, p)
150 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
152 // The symbol width is set via nomencl's \nomlabelwidth in
153 // InsetPrintNomencl::latex and not as optional parameter of
154 // \printnomenclature
155 static ParamInfo param_info_;
156 if (param_info_.empty()) {
157 // how is the width set?
158 // values: none|auto|custom
159 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
161 param_info_.add("width", ParamInfo::LYX_INTERNAL);
167 docstring InsetPrintNomencl::screenLabel() const
169 return _("Nomenclature");
173 struct NomenclEntry {
175 NomenclEntry(docstring s, docstring d, Paragraph const * p)
176 : symbol(s), desc(d), par(p)
181 Paragraph const * par;
185 typedef map<docstring, NomenclEntry > EntryMap;
188 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
190 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
193 Toc::const_iterator it = toc->begin();
194 Toc::const_iterator const en = toc->end();
195 for (; it != en; ++it) {
196 DocIterator dit = it->dit();
197 Paragraph const & par = dit.innerParagraph();
198 Inset const * inset = par.getInset(dit.top().pos());
201 InsetCommand const * ic = inset->asInsetCommand();
205 // FIXME We need a link to the paragraph here, so we
206 // need some kind of struct.
207 docstring const symbol = ic->getParam("symbol");
208 docstring const desc = ic->getParam("description");
209 docstring const prefix = ic->getParam("prefix");
210 docstring const sortas = prefix.empty() ? symbol : prefix;
212 entries[sortas] = NomenclEntry(symbol, desc, &par);
218 // we'll use our own stream, because we are going to defer everything.
219 // that's how we deal with the fact that we're probably inside a standard
220 // paragraph, and we don't want to be.
221 odocstringstream ods;
224 InsetLayout const & il = getLayout();
225 string const & tag = il.htmltag();
226 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
227 op.local_font->language()->lang());
229 xs << html::StartTag("div", "class='nomencl'")
230 << html::StartTag(tag, "class='nomencl'")
234 << html::StartTag("dl")
237 EntryMap::const_iterator eit = entries.begin();
238 EntryMap::const_iterator const een = entries.end();
239 for (; eit != een; ++eit) {
240 NomenclEntry const & ne = eit->second;
241 string const parid = ne.par->magicLabel();
242 xs << html::StartTag("dt")
243 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
246 << html::EndTag("dt")
248 << html::StartTag("dd")
250 << html::EndTag("dd")
254 xs << html::EndTag("dl")
256 << html::EndTag("div")
263 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
265 switch (cmd.action()) {
267 case LFUN_INSET_MODIFY: {
268 InsetCommandParams p(NOMENCL_PRINT_CODE);
270 InsetCommand::string2params(to_utf8(cmd.argument()), p);
271 if (p.getCmdName().empty()) {
272 cur.noScreenUpdate();
282 InsetCommand::doDispatch(cur, cmd);
288 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
289 FuncStatus & status) const
291 switch (cmd.action()) {
293 case LFUN_INSET_DIALOG_UPDATE:
294 case LFUN_INSET_MODIFY:
295 status.setEnabled(true);
299 return InsetCommand::getStatus(cur, cmd, status);
304 // FIXME This should be changed to use the TOC. Perhaps
305 // that could be done when XHTML output is added.
306 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
308 os << "<glossary>\n";
310 InsetIterator it = inset_iterator_begin(buffer().inset());
312 if (it->lyxCode() == NOMENCL_CODE) {
313 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
315 } else if (!it->producesOutput()) {
316 // Ignore contents of insets that are not in output
317 size_t const depth = it.depth();
319 while (it.depth() > depth)
325 os << "</glossary>\n";
331 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
333 // nomenclWidest() determines and returns the widest used
334 // nomenclature symbol in the document
338 InsetNomencl const * nomencl = 0;
339 ParagraphList::const_iterator it = buffer.paragraphs().begin();
340 ParagraphList::const_iterator end = buffer.paragraphs().end();
342 for (; it != end; ++it) {
343 if (it->insetList().empty())
345 InsetList::const_iterator iit = it->insetList().begin();
346 InsetList::const_iterator eend = it->insetList().end();
347 for (; iit != eend; ++iit) {
348 Inset * inset = iit->inset;
349 if (inset->lyxCode() != NOMENCL_CODE)
351 nomencl = static_cast<InsetNomencl const *>(inset);
352 docstring const symbol =
353 nomencl->getParam("symbol");
354 // This is only an approximation,
355 // but the best we can get.
356 int const wx = use_gui ?
357 theFontMetrics(Font()).width(symbol) :
365 // return the widest (or an empty) string
369 // we have to encode the string properly
370 pair<docstring, docstring> latex_symb =
371 runparams.encoding->latexString(symb, runparams.dryrun);
372 if (!latex_symb.second.empty())
373 LYXERR0("Omitting uncodable characters '"
375 << "' in nomencl widest string!");
376 return latex_symb.first;
381 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
383 OutputParams runparams = runparams_in;
384 if (getParam("set_width") == "auto") {
385 docstring widest = nomenclWidest(buffer(), runparams);
386 // Set the label width via nomencl's command \nomlabelwidth.
387 // This must be output before the command \printnomenclature
388 if (!widest.empty()) {
389 os << "\\settowidth{\\nomlabelwidth}{"
393 } else if (getParam("set_width") == "custom") {
394 // custom length as optional arg of \printnomenclature
396 Length(to_ascii(getParam("width"))).asLatexString();
398 << from_ascii(getCmdName())
404 // output the command \printnomenclature
405 os << getCommand(runparams);
409 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
411 features.require("nomencl");
415 InsetCode InsetPrintNomencl::lyxCode() const
417 return NOMENCL_PRINT_CODE;
421 string InsetPrintNomencl::contextMenuName() const
423 return "context-nomenclprint";