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 "texstream.h"
35 #include "TocBackend.h"
37 #include "frontends/FontMetrics.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
45 using namespace lyx::support;
50 /////////////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////
56 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
57 : InsetCommand(buf, p),
58 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
62 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
64 static ParamInfo param_info_;
65 if (param_info_.empty()) {
66 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
67 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
68 ParamInfo::HANDLING_LATEXIFY);
69 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
70 ParamInfo::HANDLING_LATEXIFY);
76 docstring InsetNomencl::screenLabel() const
78 size_t const maxLabelChars = 25;
79 docstring label = _("Nom: ") + getParam("symbol");
80 support::truncateWithEllipsis(label, maxLabelChars);
85 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
87 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
88 tip += _("Description: ") + "\t"
89 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
90 if (!getParam("prefix").empty())
91 tip += "\n" + _("Sorting: ") + getParam("prefix");
96 int InsetNomencl::plaintext(odocstringstream & os,
97 OutputParams const &, size_t) const
99 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
105 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
107 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
108 << sgml::escapeString(getParam("symbol"))
114 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
120 int InsetNomencl::docbookGlossary(odocstream & os) const
122 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
124 << sgml::escapeString(getParam("symbol"))
126 << "<glossdef><para>"
127 << sgml::escapeString(getParam("description"))
128 << "</para></glossdef>\n"
134 void InsetNomencl::validate(LaTeXFeatures & features) const
136 features.require("nomencl");
140 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
143 docstring const str = getParam("symbol");
144 TocBuilder & b = buffer().tocBackend().builder("nomencl");
145 b.pushItem(cpit, str, output_active);
150 /////////////////////////////////////////////////////////////////////
154 /////////////////////////////////////////////////////////////////////
156 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
157 : InsetCommand(buf, p)
161 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
163 // The symbol width is set via nomencl's \nomlabelwidth in
164 // InsetPrintNomencl::latex and not as optional parameter of
165 // \printnomenclature
166 static ParamInfo param_info_;
167 if (param_info_.empty()) {
168 // how is the width set?
169 // values: none|auto|custom
170 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
172 param_info_.add("width", ParamInfo::LYX_INTERNAL);
178 docstring InsetPrintNomencl::screenLabel() const
180 return _("Nomenclature");
184 struct NomenclEntry {
185 NomenclEntry() : par(0) {}
186 NomenclEntry(docstring s, docstring d, Paragraph const * p)
187 : symbol(s), desc(d), par(p)
192 Paragraph const * par;
196 typedef map<docstring, NomenclEntry > EntryMap;
199 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
201 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
204 Toc::const_iterator it = toc->begin();
205 Toc::const_iterator const en = toc->end();
206 for (; it != en; ++it) {
207 DocIterator dit = it->dit();
208 Paragraph const & par = dit.innerParagraph();
209 Inset const * inset = par.getInset(dit.top().pos());
212 InsetCommand const * ic = inset->asInsetCommand();
216 // FIXME We need a link to the paragraph here, so we
217 // need some kind of struct.
218 docstring const symbol = ic->getParam("symbol");
219 docstring const desc = ic->getParam("description");
220 docstring const prefix = ic->getParam("prefix");
221 docstring const sortas = prefix.empty() ? symbol : prefix;
223 entries[sortas] = NomenclEntry(symbol, desc, &par);
229 // we'll use our own stream, because we are going to defer everything.
230 // that's how we deal with the fact that we're probably inside a standard
231 // paragraph, and we don't want to be.
232 odocstringstream ods;
235 InsetLayout const & il = getLayout();
236 string const & tag = il.htmltag();
237 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
238 op.local_font->language()->lang());
240 xs << html::StartTag("div", "class='nomencl'")
241 << html::StartTag(tag, "class='nomencl'")
245 << html::StartTag("dl")
248 EntryMap::const_iterator eit = entries.begin();
249 EntryMap::const_iterator const een = entries.end();
250 for (; eit != een; ++eit) {
251 NomenclEntry const & ne = eit->second;
252 string const parid = ne.par->magicLabel();
253 xs << html::StartTag("dt")
254 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
257 << html::EndTag("dt")
259 << html::StartTag("dd")
261 << html::EndTag("dd")
265 xs << html::EndTag("dl")
267 << html::EndTag("div")
274 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
276 switch (cmd.action()) {
278 case LFUN_INSET_MODIFY: {
279 InsetCommandParams p(NOMENCL_PRINT_CODE);
281 InsetCommand::string2params(to_utf8(cmd.argument()), p);
282 if (p.getCmdName().empty()) {
283 cur.noScreenUpdate();
293 InsetCommand::doDispatch(cur, cmd);
299 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
300 FuncStatus & status) const
302 switch (cmd.action()) {
304 case LFUN_INSET_DIALOG_UPDATE:
305 case LFUN_INSET_MODIFY:
306 status.setEnabled(true);
310 return InsetCommand::getStatus(cur, cmd, status);
315 // FIXME This should be changed to use the TOC. Perhaps
316 // that could be done when XHTML output is added.
317 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
319 os << "<glossary>\n";
321 InsetIterator it = inset_iterator_begin(buffer().inset());
323 if (it->lyxCode() == NOMENCL_CODE) {
324 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
326 } else if (!it->producesOutput()) {
327 // Ignore contents of insets that are not in output
328 size_t const depth = it.depth();
330 while (it.depth() > depth)
336 os << "</glossary>\n";
342 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
344 // nomenclWidest() determines and returns the widest used
345 // nomenclature symbol in the document
349 InsetNomencl const * nomencl = 0;
350 ParagraphList::const_iterator it = buffer.paragraphs().begin();
351 ParagraphList::const_iterator end = buffer.paragraphs().end();
353 for (; it != end; ++it) {
354 if (it->insetList().empty())
356 InsetList::const_iterator iit = it->insetList().begin();
357 InsetList::const_iterator eend = it->insetList().end();
358 for (; iit != eend; ++iit) {
359 Inset * inset = iit->inset;
360 if (inset->lyxCode() != NOMENCL_CODE)
362 nomencl = static_cast<InsetNomencl const *>(inset);
363 docstring const symbol =
364 nomencl->getParam("symbol");
365 // This is only an approximation,
366 // but the best we can get.
367 int const wx = use_gui ?
368 theFontMetrics(Font()).width(symbol) :
376 // return the widest (or an empty) string
380 // we have to encode the string properly
381 pair<docstring, docstring> latex_symb =
382 runparams.encoding->latexString(symb, runparams.dryrun);
383 if (!latex_symb.second.empty())
384 LYXERR0("Omitting uncodable characters '"
386 << "' in nomencl widest string!");
387 return latex_symb.first;
392 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
394 OutputParams runparams = runparams_in;
395 if (getParam("set_width") == "auto") {
396 docstring widest = nomenclWidest(buffer(), runparams);
397 // Set the label width via nomencl's command \nomlabelwidth.
398 // This must be output before the command \printnomenclature
399 if (!widest.empty()) {
400 os << "\\settowidth{\\nomlabelwidth}{"
404 } else if (getParam("set_width") == "custom") {
405 // custom length as optional arg of \printnomenclature
407 Length(to_ascii(getParam("width"))).asLatexString();
409 << from_ascii(getCmdName())
416 // output the command \printnomenclature
417 os << getCommand(runparams);
421 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
423 features.require("nomencl");
427 InsetCode InsetPrintNomencl::lyxCode() const
429 return NOMENCL_PRINT_CODE;
433 string InsetPrintNomencl::contextMenuName() const
435 return "context-nomenclprint";