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");
95 int InsetNomencl::plaintext(odocstringstream & os,
96 OutputParams const &, size_t) const
98 docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
104 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
106 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
107 << sgml::escapeString(getParam("symbol"))
113 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
119 int InsetNomencl::docbookGlossary(odocstream & os) const
121 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
123 << sgml::escapeString(getParam("symbol"))
125 << "<glossdef><para>"
126 << sgml::escapeString(getParam("description"))
127 << "</para></glossdef>\n"
133 void InsetNomencl::validate(LaTeXFeatures & features) const
135 features.require("nomencl");
139 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
142 docstring const str = getParam("symbol");
143 buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, output_active));
147 /////////////////////////////////////////////////////////////////////
151 /////////////////////////////////////////////////////////////////////
153 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
154 : InsetCommand(buf, p)
158 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
160 // The symbol width is set via nomencl's \nomlabelwidth in
161 // InsetPrintNomencl::latex and not as optional parameter of
162 // \printnomenclature
163 static ParamInfo param_info_;
164 if (param_info_.empty()) {
165 // how is the width set?
166 // values: none|auto|custom
167 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
169 param_info_.add("width", ParamInfo::LYX_INTERNAL);
175 docstring InsetPrintNomencl::screenLabel() const
177 return _("Nomenclature");
181 struct NomenclEntry {
183 NomenclEntry(docstring s, docstring d, Paragraph const * p)
184 : symbol(s), desc(d), par(p)
189 Paragraph const * par;
193 typedef map<docstring, NomenclEntry > EntryMap;
196 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
198 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
201 Toc::const_iterator it = toc->begin();
202 Toc::const_iterator const en = toc->end();
203 for (; it != en; ++it) {
204 DocIterator dit = it->dit();
205 Paragraph const & par = dit.innerParagraph();
206 Inset const * inset = par.getInset(dit.top().pos());
209 InsetCommand const * ic = inset->asInsetCommand();
213 // FIXME We need a link to the paragraph here, so we
214 // need some kind of struct.
215 docstring const symbol = ic->getParam("symbol");
216 docstring const desc = ic->getParam("description");
217 docstring const prefix = ic->getParam("prefix");
218 docstring const sortas = prefix.empty() ? symbol : prefix;
220 entries[sortas] = NomenclEntry(symbol, desc, &par);
226 // we'll use our own stream, because we are going to defer everything.
227 // that's how we deal with the fact that we're probably inside a standard
228 // paragraph, and we don't want to be.
229 odocstringstream ods;
232 InsetLayout const & il = getLayout();
233 string const & tag = il.htmltag();
234 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
235 op.local_font->language()->lang());
237 xs << html::StartTag("div", "class='nomencl'")
238 << html::StartTag(tag, "class='nomencl'")
242 << html::StartTag("dl")
245 EntryMap::const_iterator eit = entries.begin();
246 EntryMap::const_iterator const een = entries.end();
247 for (; eit != een; ++eit) {
248 NomenclEntry const & ne = eit->second;
249 string const parid = ne.par->magicLabel();
250 xs << html::StartTag("dt")
251 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
254 << html::EndTag("dt")
256 << html::StartTag("dd")
258 << html::EndTag("dd")
262 xs << html::EndTag("dl")
264 << html::EndTag("div")
271 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
273 switch (cmd.action()) {
275 case LFUN_INSET_MODIFY: {
276 InsetCommandParams p(NOMENCL_PRINT_CODE);
278 InsetCommand::string2params(to_utf8(cmd.argument()), p);
279 if (p.getCmdName().empty()) {
280 cur.noScreenUpdate();
290 InsetCommand::doDispatch(cur, cmd);
296 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
297 FuncStatus & status) const
299 switch (cmd.action()) {
301 case LFUN_INSET_DIALOG_UPDATE:
302 case LFUN_INSET_MODIFY:
303 status.setEnabled(true);
307 return InsetCommand::getStatus(cur, cmd, status);
312 // FIXME This should be changed to use the TOC. Perhaps
313 // that could be done when XHTML output is added.
314 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
316 os << "<glossary>\n";
318 InsetIterator it = inset_iterator_begin(buffer().inset());
320 if (it->lyxCode() == NOMENCL_CODE) {
321 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
323 } else if (!it->producesOutput()) {
324 // Ignore contents of insets that are not in output
325 size_t const depth = it.depth();
327 while (it.depth() > depth)
333 os << "</glossary>\n";
339 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
341 // nomenclWidest() determines and returns the widest used
342 // nomenclature symbol in the document
346 InsetNomencl const * nomencl = 0;
347 ParagraphList::const_iterator it = buffer.paragraphs().begin();
348 ParagraphList::const_iterator end = buffer.paragraphs().end();
350 for (; it != end; ++it) {
351 if (it->insetList().empty())
353 InsetList::const_iterator iit = it->insetList().begin();
354 InsetList::const_iterator eend = it->insetList().end();
355 for (; iit != eend; ++iit) {
356 Inset * inset = iit->inset;
357 if (inset->lyxCode() != NOMENCL_CODE)
359 nomencl = static_cast<InsetNomencl const *>(inset);
360 docstring const symbol =
361 nomencl->getParam("symbol");
362 // This is only an approximation,
363 // but the best we can get.
364 int const wx = use_gui ?
365 theFontMetrics(Font()).width(symbol) :
373 // return the widest (or an empty) string
377 // we have to encode the string properly
378 pair<docstring, docstring> latex_symb =
379 runparams.encoding->latexString(symb, runparams.dryrun);
380 if (!latex_symb.second.empty())
381 LYXERR0("Omitting uncodable characters '"
383 << "' in nomencl widest string!");
384 return latex_symb.first;
389 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
391 OutputParams runparams = runparams_in;
392 if (getParam("set_width") == "auto") {
393 docstring widest = nomenclWidest(buffer(), runparams);
394 // Set the label width via nomencl's command \nomlabelwidth.
395 // This must be output before the command \printnomenclature
396 if (!widest.empty()) {
397 os << "\\settowidth{\\nomlabelwidth}{"
401 } else if (getParam("set_width") == "custom") {
402 // custom length as optional arg of \printnomenclature
404 Length(to_ascii(getParam("width"))).asLatexString();
406 << from_ascii(getCmdName())
412 // output the command \printnomenclature
413 os << getCommand(runparams);
417 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
419 features.require("nomencl");
423 InsetCode InsetPrintNomencl::lyxCode() const
425 return NOMENCL_PRINT_CODE;
429 string InsetPrintNomencl::contextMenuName() const
431 return "context-nomenclprint";