]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Update my email and status.
[lyx.git] / src / insets / InsetNomencl.cpp
1 /**
2  * \file InsetNomencl.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author O. U. Baran
8  * \author Uwe Stöhr
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13 #include <config.h>
14
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
17
18 #include "Buffer.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "Font.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "LaTeXFeatures.h"
28 #include "Length.h"
29 #include "LyX.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
32 #include "sgml.h"
33
34 #include "frontends/FontMetrics.h"
35
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46
47 /////////////////////////////////////////////////////////////////////
48 //
49 // InsetNomencl
50 //
51 /////////////////////////////////////////////////////////////////////
52
53 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
54         : InsetCommand(buf, p),
55           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
56 {}
57
58
59 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
60 {
61         static ParamInfo param_info_;
62         if (param_info_.empty()) {
63                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
64                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
65                                 ParamInfo::HANDLING_LATEXIFY);
66                 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
67                                 ParamInfo::HANDLING_LATEXIFY);
68         }
69         return param_info_;
70 }
71
72
73 docstring InsetNomencl::screenLabel() const
74 {
75         size_t const maxLabelChars = 25;
76
77         docstring label = _("Nom: ") + getParam("symbol");
78         if (label.size() > maxLabelChars) {
79                 label.erase(maxLabelChars - 3);
80                 label += "...";
81         }
82         return label;
83 }
84
85
86 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
87 {
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");
93         return tip;
94 }
95
96
97
98 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
99 {
100         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
101            << sgml::escapeString(getParam("symbol"))
102            << "</glossterm>";
103         return 0;
104 }
105
106
107 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
108 {
109         return docstring();
110 }
111
112
113 int InsetNomencl::docbookGlossary(odocstream & os) const
114 {
115         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
116            << "<glossterm>"
117            << sgml::escapeString(getParam("symbol"))
118            << "</glossterm>\n"
119            << "<glossdef><para>"
120            << sgml::escapeString(getParam("description"))
121            << "</para></glossdef>\n"
122            <<"</glossentry>\n";
123         return 4;
124 }
125
126
127 void InsetNomencl::validate(LaTeXFeatures & features) const
128 {
129         features.require("nomencl");
130 }
131
132
133 /////////////////////////////////////////////////////////////////////
134 //
135 // InsetPrintNomencl
136 //
137 /////////////////////////////////////////////////////////////////////
138
139 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
140         : InsetCommand(buf, p)
141 {}
142
143
144 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
145 {
146         // The symbol width is set via nomencl's \nomlabelwidth in 
147         // InsetPrintNomencl::latex and not as optional parameter of
148         // \printnomenclature
149         static ParamInfo param_info_;
150         if (param_info_.empty()) {
151                 // how is the width set?
152                 // values: none|auto|custom
153                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
154                 // custom width
155                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
156         }
157         return param_info_;
158 }
159
160
161 docstring InsetPrintNomencl::screenLabel() const
162 {
163         return _("Nomenclature");
164 }
165
166
167 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
168 {
169         switch (cmd.action()) {
170
171         case LFUN_INSET_MODIFY: {
172                 InsetCommandParams p(NOMENCL_PRINT_CODE);
173                 // FIXME UNICODE
174                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
175                 if (p.getCmdName().empty()) {
176                         cur.noScreenUpdate();
177                         break;
178                 }
179
180                 cur.recordUndo();
181                 setParams(p);
182                 break;
183         }
184
185         default:
186                 InsetCommand::doDispatch(cur, cmd);
187                 break;
188         }
189 }
190
191
192 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
193         FuncStatus & status) const
194 {
195         switch (cmd.action()) {
196
197         case LFUN_INSET_DIALOG_UPDATE:
198         case LFUN_INSET_MODIFY:
199                 status.setEnabled(true);
200                 return true;
201
202         default:
203                 return InsetCommand::getStatus(cur, cmd, status);
204         }
205 }
206
207
208 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
209 {
210         return docstring();
211 }
212
213
214 // FIXME This should be changed to use the TOC. Perhaps
215 // that could be done when XHTML output is added.
216 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
217 {
218         os << "<glossary>\n";
219         int newlines = 2;
220         InsetIterator it = inset_iterator_begin(buffer().inset());
221         while (it) {
222                 if (it->lyxCode() == NOMENCL_CODE) {
223                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
224                         ++it;
225                 } else if (!it->producesOutput()) {
226                         // Ignore contents of insets that are not in output
227                         size_t const depth = it.depth();
228                         ++it;
229                         while (it.depth() > depth)
230                                 ++it;
231                 } else {
232                         ++it;
233                 }
234         }
235         os << "</glossary>\n";
236         return newlines;
237 }
238
239
240 namespace {
241 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
242 {
243         // nomenclWidest() determines and returns the widest used
244         // nomenclature symbol in the document
245
246         int w = 0;
247         docstring symb;
248         InsetNomencl const * nomencl = 0;
249         ParagraphList::const_iterator it = buffer.paragraphs().begin();
250         ParagraphList::const_iterator end = buffer.paragraphs().end();
251
252         for (; it != end; ++it) {
253                 if (it->insetList().empty())
254                         continue;
255                 InsetList::const_iterator iit = it->insetList().begin();
256                 InsetList::const_iterator eend = it->insetList().end();
257                 for (; iit != eend; ++iit) {
258                         Inset * inset = iit->inset;
259                         if (inset->lyxCode() != NOMENCL_CODE)
260                                 continue;
261                         nomencl = static_cast<InsetNomencl const *>(inset);
262                         docstring const symbol =
263                                 nomencl->getParam("symbol");
264                         // This is only an approximation,
265                         // but the best we can get.
266                         int const wx = use_gui ?
267                                 theFontMetrics(Font()).width(symbol) :
268                                 symbol.size();
269                         if (wx > w) {
270                                 w = wx;
271                                 symb = symbol;
272                         }
273                 }
274         }
275         // return the widest (or an empty) string
276         if (symb.empty())
277                 return symb;
278
279         // we have to encode the string properly
280         pair<docstring, docstring> latex_symb =
281                 runparams.encoding->latexString(symb, runparams.dryrun);
282         if (!latex_symb.second.empty())
283                 LYXERR0("Omitting uncodable characters '"
284                         << latex_symb.second
285                         << "' in nomencl widest string!");
286         return latex_symb.first;
287 }
288 } // namespace anon
289
290
291 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
292 {
293         OutputParams runparams = runparams_in;
294         if (getParam("set_width") == "auto") {
295                 docstring widest = nomenclWidest(buffer(), runparams);
296                 // Set the label width via nomencl's command \nomlabelwidth.
297                 // This must be output before the command \printnomenclature
298                 if (!widest.empty()) {
299                         os << "\\settowidth{\\nomlabelwidth}{"
300                            << widest
301                            << "}\n";
302                 }
303         } else if (getParam("set_width") == "custom") {
304                 // custom length as optional arg of \printnomenclature
305                 string const width =
306                         Length(to_ascii(getParam("width"))).asLatexString();
307                 os << '\\'
308                    << from_ascii(getCmdName())
309                    << '['
310                    << from_ascii(width)
311                    << "]{}";
312                 return;
313         }
314         // output the command \printnomenclature
315         os << getCommand(runparams);
316 }
317
318
319 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
320 {
321         features.require("nomencl");
322 }
323
324
325 InsetCode InsetPrintNomencl::lyxCode() const
326 {
327         return NOMENCL_PRINT_CODE;
328 }
329
330
331 string InsetPrintNomencl::contextMenuName() const
332 {
333         return "context-nomenclprint";
334 }
335
336
337 } // namespace lyx