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