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) const
137 docstring const str = getParam("symbol");
138 buffer().tocBackend().toc("nomencl").push_back(TocItem(cpit, 0, str, output_active));
142 /////////////////////////////////////////////////////////////////////
146 /////////////////////////////////////////////////////////////////////
148 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
149 : InsetCommand(buf, p)
153 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
155 // The symbol width is set via nomencl's \nomlabelwidth in
156 // InsetPrintNomencl::latex and not as optional parameter of
157 // \printnomenclature
158 static ParamInfo param_info_;
159 if (param_info_.empty()) {
160 // how is the width set?
161 // values: none|auto|custom
162 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
164 param_info_.add("width", ParamInfo::LYX_INTERNAL);
170 docstring InsetPrintNomencl::screenLabel() const
172 return _("Nomenclature");
176 struct NomenclEntry {
178 NomenclEntry(docstring s, docstring d, Paragraph const * p)
179 : symbol(s), desc(d), par(p)
184 Paragraph const * par;
188 typedef map<docstring, NomenclEntry > EntryMap;
191 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
193 Toc const & toc = buffer().tocBackend().toc("nomencl");
196 Toc::const_iterator it = toc.begin();
197 Toc::const_iterator const en = toc.end();
198 for (; it != en; ++it) {
199 DocIterator dit = it->dit();
200 Paragraph const & par = dit.innerParagraph();
201 Inset const * inset = par.getInset(dit.top().pos());
204 InsetCommand const * ic = inset->asInsetCommand();
208 // FIXME We need a link to the paragraph here, so we
209 // need some kind of struct.
210 docstring const symbol = ic->getParam("symbol");
211 docstring const desc = ic->getParam("description");
212 docstring const prefix = ic->getParam("prefix");
213 docstring const sortas = prefix.empty() ? symbol : prefix;
215 entries[sortas] = NomenclEntry(symbol, desc, &par);
221 // we'll use our own stream, because we are going to defer everything.
222 // that's how we deal with the fact that we're probably inside a standard
223 // paragraph, and we don't want to be.
224 odocstringstream ods;
227 InsetLayout const & il = getLayout();
228 string const & tag = il.htmltag();
229 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
230 op.local_font->language()->lang());
232 xs << html::StartTag("div", "class='nomencl'")
233 << html::StartTag(tag, "class='nomencl'")
237 << html::StartTag("dl")
240 EntryMap::const_iterator eit = entries.begin();
241 EntryMap::const_iterator const een = entries.end();
242 for (; eit != een; ++eit) {
243 NomenclEntry const & ne = eit->second;
244 string const parid = ne.par->magicLabel();
245 xs << html::StartTag("dt")
246 << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
249 << html::EndTag("dt")
251 << html::StartTag("dd")
253 << html::EndTag("dd")
257 xs << html::EndTag("dl")
259 << html::EndTag("div")
266 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
268 switch (cmd.action()) {
270 case LFUN_INSET_MODIFY: {
271 InsetCommandParams p(NOMENCL_PRINT_CODE);
273 InsetCommand::string2params(to_utf8(cmd.argument()), p);
274 if (p.getCmdName().empty()) {
275 cur.noScreenUpdate();
285 InsetCommand::doDispatch(cur, cmd);
291 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
292 FuncStatus & status) const
294 switch (cmd.action()) {
296 case LFUN_INSET_DIALOG_UPDATE:
297 case LFUN_INSET_MODIFY:
298 status.setEnabled(true);
302 return InsetCommand::getStatus(cur, cmd, status);
307 // FIXME This should be changed to use the TOC. Perhaps
308 // that could be done when XHTML output is added.
309 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
311 os << "<glossary>\n";
313 InsetIterator it = inset_iterator_begin(buffer().inset());
315 if (it->lyxCode() == NOMENCL_CODE) {
316 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
318 } else if (!it->producesOutput()) {
319 // Ignore contents of insets that are not in output
320 size_t const depth = it.depth();
322 while (it.depth() > depth)
328 os << "</glossary>\n";
334 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
336 // nomenclWidest() determines and returns the widest used
337 // nomenclature symbol in the document
341 InsetNomencl const * nomencl = 0;
342 ParagraphList::const_iterator it = buffer.paragraphs().begin();
343 ParagraphList::const_iterator end = buffer.paragraphs().end();
345 for (; it != end; ++it) {
346 if (it->insetList().empty())
348 InsetList::const_iterator iit = it->insetList().begin();
349 InsetList::const_iterator eend = it->insetList().end();
350 for (; iit != eend; ++iit) {
351 Inset * inset = iit->inset;
352 if (inset->lyxCode() != NOMENCL_CODE)
354 nomencl = static_cast<InsetNomencl const *>(inset);
355 docstring const symbol =
356 nomencl->getParam("symbol");
357 // This is only an approximation,
358 // but the best we can get.
359 int const wx = use_gui ?
360 theFontMetrics(Font()).width(symbol) :
368 // return the widest (or an empty) string
372 // we have to encode the string properly
373 pair<docstring, docstring> latex_symb =
374 runparams.encoding->latexString(symb, runparams.dryrun);
375 if (!latex_symb.second.empty())
376 LYXERR0("Omitting uncodable characters '"
378 << "' in nomencl widest string!");
379 return latex_symb.first;
384 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
386 OutputParams runparams = runparams_in;
387 if (getParam("set_width") == "auto") {
388 docstring widest = nomenclWidest(buffer(), runparams);
389 // Set the label width via nomencl's command \nomlabelwidth.
390 // This must be output before the command \printnomenclature
391 if (!widest.empty()) {
392 os << "\\settowidth{\\nomlabelwidth}{"
396 } else if (getParam("set_width") == "custom") {
397 // custom length as optional arg of \printnomenclature
399 Length(to_ascii(getParam("width"))).asLatexString();
401 << from_ascii(getCmdName())
407 // output the command \printnomenclature
408 os << getCommand(runparams);
412 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
414 features.require("nomencl");
418 InsetCode InsetPrintNomencl::lyxCode() const
420 return NOMENCL_PRINT_CODE;
424 string InsetPrintNomencl::contextMenuName() const
426 return "context-nomenclprint";