]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
0d09af17f0180a65a2f46e8cca99067bccad1486
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "Cursor.h"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "InsetLabel.h"
19 #include "LaTeXFeatures.h"
20 #include "LyX.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "TocBackend.h"
26
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30
31 using namespace lyx::support;
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
38         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
39 {}
40
41
42 InsetRef::InsetRef(InsetRef const & ir)
43         : InsetCommand(ir), isLatex(ir.isLatex)
44 {}
45
46
47 bool InsetRef::isCompatibleCommand(string const & s) {
48         //FIXME This is likely not the best way to handle this.
49         //But this stuff is hardcoded elsewhere already.
50         return s == "ref" 
51                 || s == "pageref"
52                 || s == "vref" 
53                 || s == "vpageref"
54                 || s == "prettyref"
55                 || s == "eqref"
56                 || s == "nameref";
57 }
58
59
60 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
61 {
62         static ParamInfo param_info_;
63         if (param_info_.empty()) {
64                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
65                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
66                                 ParamInfo::HANDLING_ESCAPE);
67         }
68         return param_info_;
69 }
70
71
72 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
73 {
74         // We don't want to output p_["name"], since that is only used 
75         // in docbook. So we construct new params, without it, and use that.
76         InsetCommandParams p(REF_CODE, getCmdName());
77         p["reference"] = getParam("reference");
78         os << p.getCommand(runparams);
79         return 0;
80 }
81
82
83 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
84 {
85         docstring const str = getParam("reference");
86         os << '[' << str << ']';
87         return 2 + str.size();
88 }
89
90
91 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
92 {
93         docstring const & name = getParam("name");
94         if (name.empty()) {
95                 if (runparams.flavor == OutputParams::XML) {
96                         os << "<xref linkend=\""
97                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
98                            << "\" />";
99                 } else {
100                         os << "<xref linkend=\""
101                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
102                            << "\">";
103                 }
104         } else {
105                 os << "<link linkend=\""
106                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
107                    << "\">"
108                    << getParam("name")
109                    << "</link>";
110         }
111
112         return 0;
113 }
114
115
116 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
117 {
118         docstring const & ref = getParam("reference");
119         InsetLabel const * il = buffer().insetLabel(ref);
120         string const & cmd = params().getCmdName();
121         docstring display_string;
122
123         if (il && !il->counterValue().empty()) {
124                 // Try to construct a label from the InsetLabel we reference.
125                 docstring const & value = il->counterValue();
126                 if (cmd == "ref")
127                         display_string = value;
128                 else if (cmd == "vref")
129                         // normally, would be "ref on page #", but we have no pages
130                         display_string = value;
131                 else if (cmd == "pageref" || cmd == "vpageref")
132                         // normally would be "on page #", but we have no pages
133                         display_string = _("elsewhere");
134                 else if (cmd == "eqref")
135                         display_string = bformat(from_ascii("equation (%1$s)"), value);
136                 else if (cmd == "prettyref" 
137                          // we don't really have the ability to handle these 
138                          // properly in XHTML output
139                          || cmd == "nameref")
140                         display_string = il->prettyCounter();
141         } else 
142                         display_string = ref;
143
144         // FIXME What we'd really like to do is to be able to output some
145         // appropriate sort of text here. But to do that, we need to associate
146         // some sort of counter with the label, and we don't have that yet.
147         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
148         xs << html::StartTag("a", attr);
149         xs << display_string;
150         xs << html::EndTag("a");
151         return docstring();
152 }
153
154
155 void InsetRef::tocString(odocstream & os) const
156 {
157         plaintext(os, OutputParams(0));
158 }
159
160
161 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
162 {
163         docstring const & ref = getParam("reference");
164         // register this inset into the buffer reference cache.
165         buffer().references(ref).push_back(make_pair(this, it));
166
167         docstring label;
168         for (int i = 0; !types[i].latex_name.empty(); ++i) {
169                 if (getCmdName() == types[i].latex_name) {
170                         label = _(types[i].short_gui_name);
171                         break;
172                 }
173         }
174         label += ref;
175
176         if (!isLatex && !getParam("name").empty()) {
177                 label += "||";
178                 label += getParam("name");
179         }
180         
181         screen_label_ = label;
182         bool shortened = false;
183         unsigned int const maxLabelChars = 24;
184         if (screen_label_.size() > maxLabelChars) {
185                 screen_label_.erase(maxLabelChars - 3);
186                 screen_label_ += "...";
187                 shortened = true;
188         }
189         if (shortened)
190                 tooltip_ = label;
191         else 
192                 tooltip_ = from_ascii("");
193 }
194
195
196 void InsetRef::addToToc(DocIterator const & cpit)
197 {
198         docstring const & label = getParam("reference");
199         if (buffer().insetLabel(label))
200                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
201                 return;
202
203         // It seems that this reference does not point to any valid label.
204         screen_label_ = _("BROKEN: ") + screen_label_;
205         Toc & toc = buffer().tocBackend().toc("label");
206         toc.push_back(TocItem(cpit, 0, screen_label_));
207 }
208
209
210 void InsetRef::validate(LaTeXFeatures & features) const
211 {
212         string const cmd = getCmdName();
213         if (cmd == "vref" || cmd == "vpageref")
214                 features.require("varioref");
215         else if (cmd == "prettyref")
216                 features.require("prettyref");
217         else if (cmd == "eqref")
218                 features.require("amsmath");
219         else if (cmd == "nameref")
220                 features.require("nameref");
221 }
222
223
224 InsetRef::type_info InsetRef::types[] = {
225         { "ref",       N_("Standard"),              N_("Ref: ")},
226         { "eqref",     N_("Equation"),              N_("EqRef: ")},
227         { "pageref",   N_("Page Number"),           N_("Page: ")},
228         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
229         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
230         { "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
231         { "nameref",   N_("Reference to Name"),     N_("NameRef:")},
232         { "", "", "" }
233 };
234
235
236 int InsetRef::getType(string const & name)
237 {
238         for (int i = 0; !types[i].latex_name.empty(); ++i)
239                 if (name == types[i].latex_name)
240                         return i;
241         return 0;
242 }
243
244
245 string const & InsetRef::getName(int type)
246 {
247         return types[type].latex_name;
248 }
249
250
251 } // namespace lyx