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;
79 docstring label = _("Nom: ") + getParam("symbol");
80 if (label.size() > maxLabelChars) {
81 label.erase(maxLabelChars - 3);
88 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
90 docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
91 tip += _("Description: ") + "\t"
92 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
93 if (!getParam("prefix").empty())
94 tip += "\n" + _("Sorting: ") + getParam("prefix");
100 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
102 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
103 << sgml::escapeString(getParam("symbol"))
109 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
115 int InsetNomencl::docbookGlossary(odocstream & os) const
117 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
119 << sgml::escapeString(getParam("symbol"))
121 << "<glossdef><para>"
122 << sgml::escapeString(getParam("description"))
123 << "</para></glossdef>\n"
129 void InsetNomencl::validate(LaTeXFeatures & features) const
131 features.require("nomencl");
135 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
138 docstring const str = getParam("symbol");
139 buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, output_active));
143 /////////////////////////////////////////////////////////////////////
147 /////////////////////////////////////////////////////////////////////
149 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
150 : InsetCommand(buf, p)
154 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
156 // The symbol width is set via nomencl's \nomlabelwidth in
157 // InsetPrintNomencl::latex and not as optional parameter of
158 // \printnomenclature
159 static ParamInfo param_info_;
160 if (param_info_.empty()) {
161 // how is the width set?
162 // values: none|auto|custom
163 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
165 param_info_.add("width", ParamInfo::LYX_INTERNAL);
171 docstring InsetPrintNomencl::screenLabel() const
173 return _("Nomenclature");
177 struct NomenclEntry {
179 NomenclEntry(docstring s, docstring d, Paragraph const * p)
180 : symbol(s), desc(d), par(p)
185 Paragraph const * par;
189 typedef map<docstring, NomenclEntry > EntryMap;
192 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
194 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
197 Toc::const_iterator it = toc->begin();
198 Toc::const_iterator const en = toc->end();
199 for (; it != en; ++it) {
200 DocIterator dit = it->dit();
201 Paragraph const & par = dit.innerParagraph();
202 Inset const * inset = par.getInset(dit.top().pos());
205 InsetCommand const * ic = inset->asInsetCommand();
209 // FIXME We need a link to the paragraph here, so we
210 // need some kind of struct.
211 docstring const symbol = ic->getParam("symbol");
212 docstring const desc = ic->getParam("description");
213 docstring const prefix = ic->getParam("prefix");
214 docstring const sortas = prefix.empty() ? symbol : prefix;
216 entries[sortas] = NomenclEntry(symbol, desc, &par);
222 // we'll use our own stream, because we are going to defer everything.
223 // that's how we deal with the fact that we're probably inside a standard
224 // paragraph, and we don't want to be.
225 odocstringstream ods;
228 InsetLayout const & il = getLayout();
229 string const & tag = il.htmltag();
230 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
231 op.local_font->language()->lang());
233 xs << html::StartTag("div", "class='nomencl'")
234 << html::StartTag(tag, "class='nomencl'")
238 << html::StartTag("dl")
241 EntryMap::const_iterator eit = entries.begin();
242 EntryMap::const_iterator const een = entries.end();
243 for (; eit != een; ++eit) {
244 NomenclEntry const & ne = eit->second;
245 string const parid = ne.par->magicLabel();
246 xs << html::StartTag("dt")
247 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
250 << html::EndTag("dt")
252 << html::StartTag("dd")
254 << html::EndTag("dd")
258 xs << html::EndTag("dl")
260 << html::EndTag("div")
267 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
269 switch (cmd.action()) {
271 case LFUN_INSET_MODIFY: {
272 InsetCommandParams p(NOMENCL_PRINT_CODE);
274 InsetCommand::string2params(to_utf8(cmd.argument()), p);
275 if (p.getCmdName().empty()) {
276 cur.noScreenUpdate();
286 InsetCommand::doDispatch(cur, cmd);
292 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
293 FuncStatus & status) const
295 switch (cmd.action()) {
297 case LFUN_INSET_DIALOG_UPDATE:
298 case LFUN_INSET_MODIFY:
299 status.setEnabled(true);
303 return InsetCommand::getStatus(cur, cmd, status);
308 // FIXME This should be changed to use the TOC. Perhaps
309 // that could be done when XHTML output is added.
310 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
312 os << "<glossary>\n";
314 InsetIterator it = inset_iterator_begin(buffer().inset());
316 if (it->lyxCode() == NOMENCL_CODE) {
317 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
319 } else if (!it->producesOutput()) {
320 // Ignore contents of insets that are not in output
321 size_t const depth = it.depth();
323 while (it.depth() > depth)
329 os << "</glossary>\n";
335 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
337 // nomenclWidest() determines and returns the widest used
338 // nomenclature symbol in the document
342 InsetNomencl const * nomencl = 0;
343 ParagraphList::const_iterator it = buffer.paragraphs().begin();
344 ParagraphList::const_iterator end = buffer.paragraphs().end();
346 for (; it != end; ++it) {
347 if (it->insetList().empty())
349 InsetList::const_iterator iit = it->insetList().begin();
350 InsetList::const_iterator eend = it->insetList().end();
351 for (; iit != eend; ++iit) {
352 Inset * inset = iit->inset;
353 if (inset->lyxCode() != NOMENCL_CODE)
355 nomencl = static_cast<InsetNomencl const *>(inset);
356 docstring const symbol =
357 nomencl->getParam("symbol");
358 // This is only an approximation,
359 // but the best we can get.
360 int const wx = use_gui ?
361 theFontMetrics(Font()).width(symbol) :
369 // return the widest (or an empty) string
373 // we have to encode the string properly
374 pair<docstring, docstring> latex_symb =
375 runparams.encoding->latexString(symb, runparams.dryrun);
376 if (!latex_symb.second.empty())
377 LYXERR0("Omitting uncodable characters '"
379 << "' in nomencl widest string!");
380 return latex_symb.first;
385 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
387 OutputParams runparams = runparams_in;
388 if (getParam("set_width") == "auto") {
389 docstring widest = nomenclWidest(buffer(), runparams);
390 // Set the label width via nomencl's command \nomlabelwidth.
391 // This must be output before the command \printnomenclature
392 if (!widest.empty()) {
393 os << "\\settowidth{\\nomlabelwidth}{"
397 } else if (getParam("set_width") == "custom") {
398 // custom length as optional arg of \printnomenclature
400 Length(to_ascii(getParam("width"))).asLatexString();
402 << from_ascii(getCmdName())
408 // output the command \printnomenclature
409 os << getCommand(runparams);
413 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
415 features.require("nomencl");
419 InsetCode InsetPrintNomencl::lyxCode() const
421 return NOMENCL_PRINT_CODE;
425 string InsetPrintNomencl::contextMenuName() const
427 return "context-nomenclprint";