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