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);
71 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
77 docstring InsetNomencl::screenLabel() const
79 size_t const maxLabelChars = 25;
80 docstring label = _("Nom: ") + getParam("symbol");
81 support::truncateWithEllipsis(label, maxLabelChars);
86 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
88 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
89 tip += _("Description: ") + "\t"
90 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
91 if (!getParam("prefix").empty())
92 tip += "\n" + _("Sorting: ") + getParam("prefix");
97 int InsetNomencl::plaintext(odocstringstream & os,
98 OutputParams const &, size_t) const
100 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
106 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
108 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
109 << sgml::escapeString(getParam("symbol"))
115 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
121 int InsetNomencl::docbookGlossary(odocstream & os) const
123 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
125 << sgml::escapeString(getParam("symbol"))
127 << "<glossdef><para>"
128 << sgml::escapeString(getParam("description"))
129 << "</para></glossdef>\n"
135 void InsetNomencl::validate(LaTeXFeatures & features) const
137 features.require("nomencl");
138 InsetCommand::validate(features);
142 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
143 UpdateType, TocBackend & backend) const
145 docstring const str = getParam("symbol");
146 TocBuilder & b = backend.builder("nomencl");
147 b.pushItem(cpit, str, output_active);
152 /////////////////////////////////////////////////////////////////////
156 /////////////////////////////////////////////////////////////////////
158 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
159 : InsetCommand(buf, p)
163 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
165 // The symbol width is set via nomencl's \nomlabelwidth in
166 // InsetPrintNomencl::latex and not as optional parameter of
167 // \printnomenclature
168 static ParamInfo param_info_;
169 if (param_info_.empty()) {
170 // how is the width set?
171 // values: none|auto|custom
172 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
174 param_info_.add("width", ParamInfo::LYX_INTERNAL);
180 docstring InsetPrintNomencl::screenLabel() const
182 return _("Nomenclature");
186 struct NomenclEntry {
187 NomenclEntry() : par(0) {}
188 NomenclEntry(docstring s, docstring d, Paragraph const * p)
189 : symbol(s), desc(d), par(p)
194 Paragraph const * par;
198 typedef map<docstring, NomenclEntry > EntryMap;
201 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
203 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
206 Toc::const_iterator it = toc->begin();
207 Toc::const_iterator const en = toc->end();
208 for (; it != en; ++it) {
209 DocIterator dit = it->dit();
210 Paragraph const & par = dit.innerParagraph();
211 Inset const * inset = par.getInset(dit.top().pos());
214 InsetCommand const * ic = inset->asInsetCommand();
218 // FIXME We need a link to the paragraph here, so we
219 // need some kind of struct.
220 docstring const symbol = ic->getParam("symbol");
221 docstring const desc = ic->getParam("description");
222 docstring const prefix = ic->getParam("prefix");
223 docstring const sortas = prefix.empty() ? symbol : prefix;
225 entries[sortas] = NomenclEntry(symbol, desc, &par);
231 // we'll use our own stream, because we are going to defer everything.
232 // that's how we deal with the fact that we're probably inside a standard
233 // paragraph, and we don't want to be.
234 odocstringstream ods;
237 InsetLayout const & il = getLayout();
238 string const & tag = il.htmltag();
239 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
240 op.local_font->language()->lang());
242 xs << html::StartTag("div", "class='nomencl'")
243 << html::StartTag(tag, "class='nomencl'")
247 << html::StartTag("dl")
250 EntryMap::const_iterator eit = entries.begin();
251 EntryMap::const_iterator const een = entries.end();
252 for (; eit != een; ++eit) {
253 NomenclEntry const & ne = eit->second;
254 string const parid = ne.par->magicLabel();
255 xs << html::StartTag("dt")
256 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
259 << html::EndTag("dt")
261 << html::StartTag("dd")
263 << html::EndTag("dd")
267 xs << html::EndTag("dl")
269 << html::EndTag("div")
276 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
278 switch (cmd.action()) {
280 case LFUN_INSET_MODIFY: {
281 InsetCommandParams p(NOMENCL_PRINT_CODE);
283 InsetCommand::string2params(to_utf8(cmd.argument()), p);
284 if (p.getCmdName().empty()) {
285 cur.noScreenUpdate();
295 InsetCommand::doDispatch(cur, cmd);
301 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
302 FuncStatus & status) const
304 switch (cmd.action()) {
306 case LFUN_INSET_DIALOG_UPDATE:
307 case LFUN_INSET_MODIFY:
308 status.setEnabled(true);
312 return InsetCommand::getStatus(cur, cmd, status);
317 // FIXME This should be changed to use the TOC. Perhaps
318 // that could be done when XHTML output is added.
319 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
321 os << "<glossary>\n";
323 InsetIterator it = inset_iterator_begin(buffer().inset());
325 if (it->lyxCode() == NOMENCL_CODE) {
326 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
328 } else if (!it->producesOutput()) {
329 // Ignore contents of insets that are not in output
330 size_t const depth = it.depth();
332 while (it.depth() > depth)
338 os << "</glossary>\n";
344 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
346 // nomenclWidest() determines and returns the widest used
347 // nomenclature symbol in the document
351 InsetNomencl const * nomencl = 0;
352 ParagraphList::const_iterator it = buffer.paragraphs().begin();
353 ParagraphList::const_iterator end = buffer.paragraphs().end();
355 for (; it != end; ++it) {
356 if (it->insetList().empty())
358 InsetList::const_iterator iit = it->insetList().begin();
359 InsetList::const_iterator eend = it->insetList().end();
360 for (; iit != eend; ++iit) {
361 Inset * inset = iit->inset;
362 if (inset->lyxCode() != NOMENCL_CODE)
364 nomencl = static_cast<InsetNomencl const *>(inset);
365 docstring const symbol =
366 nomencl->getParam("symbol");
367 // This is only an approximation,
368 // but the best we can get.
369 int const wx = use_gui ?
370 theFontMetrics(Font()).width(symbol) :
378 // return the widest (or an empty) string
382 // we have to encode the string properly
383 pair<docstring, docstring> latex_symb =
384 runparams.encoding->latexString(symb, runparams.dryrun);
385 if (!latex_symb.second.empty())
386 LYXERR0("Omitting uncodable characters '"
388 << "' in nomencl widest string!");
389 return latex_symb.first;
394 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
396 OutputParams runparams = runparams_in;
397 if (getParam("set_width") == "auto") {
398 docstring widest = nomenclWidest(buffer(), runparams);
399 // Set the label width via nomencl's command \nomlabelwidth.
400 // This must be output before the command \printnomenclature
401 if (!widest.empty()) {
402 os << "\\settowidth{\\nomlabelwidth}{"
406 } else if (getParam("set_width") == "custom") {
407 // custom length as optional arg of \printnomenclature
409 Length(to_ascii(getParam("width"))).asLatexString();
411 << from_ascii(getCmdName())
418 // output the command \printnomenclature
419 os << getCommand(runparams);
423 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
425 features.require("nomencl");
429 InsetCode InsetPrintNomencl::lyxCode() const
431 return NOMENCL_PRINT_CODE;
435 string InsetPrintNomencl::contextMenuName() const
437 return "context-nomenclprint";