]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
aa8d628c9af6cbcee95830bc62812b6b24f62a72
[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 "BufferParams.h"
16 #include "Cursor.h"
17 #include "DispatchResult.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")
39 {}
40
41
42 InsetRef::InsetRef(InsetRef const & ir)
43         : InsetCommand(ir)
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 == "formatted"
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         string const cmd = getCmdName();
75         docstring const ref = getParam("reference");
76         if (cmd != "formatted") {
77                 // We don't want to output p_["name"], since that is only used 
78                 // in docbook. So we construct new params, without it, and use that.
79                 InsetCommandParams p(REF_CODE, cmd);
80                 p["reference"] = ref;
81                 os << p.getCommand(runparams);
82                 return 0;
83         } 
84         
85         // so we're doing a formatted reference.
86         // the command may need to be escaped.
87         InsetCommandParams const & p = params();
88         ParamInfo const & pi = p.info();
89         ParamInfo::ParamData const & pd = pi["reference"];
90         docstring const data = 
91                         p.prepareCommand(runparams, ref, pd.handling());
92
93         if  (!buffer().params().use_refstyle) {
94                 os << "\\prettyref{" << data << '}';
95                 return 0;
96         }
97         
98         os << "\\lyxref{" << data << '}';
99         return 0;
100 }
101
102
103 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
104 {
105         docstring const str = getParam("reference");
106         os << '[' << str << ']';
107         return 2 + str.size();
108 }
109
110
111 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
112 {
113         docstring const & name = getParam("name");
114         if (name.empty()) {
115                 if (runparams.flavor == OutputParams::XML) {
116                         os << "<xref linkend=\""
117                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
118                            << "\" />";
119                 } else {
120                         os << "<xref linkend=\""
121                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
122                            << "\">";
123                 }
124         } else {
125                 os << "<link linkend=\""
126                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
127                    << "\">"
128                    << getParam("name")
129                    << "</link>";
130         }
131
132         return 0;
133 }
134
135
136 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
137 {
138         docstring const & ref = getParam("reference");
139         InsetLabel const * il = buffer().insetLabel(ref);
140         string const & cmd = params().getCmdName();
141         docstring display_string;
142
143         if (il && !il->counterValue().empty()) {
144                 // Try to construct a label from the InsetLabel we reference.
145                 docstring const & value = il->counterValue();
146                 if (cmd == "ref")
147                         display_string = value;
148                 else if (cmd == "vref")
149                         // normally, would be "ref on page #", but we have no pages
150                         display_string = value;
151                 else if (cmd == "pageref" || cmd == "vpageref")
152                         // normally would be "on page #", but we have no pages
153                         display_string = _("elsewhere");
154                 else if (cmd == "eqref")
155                         display_string = bformat(from_ascii("equation (%1$s)"), value);
156                 else if (cmd == "prettyref" 
157                          // we don't really have the ability to handle these 
158                          // properly in XHTML output
159                          || cmd == "nameref")
160                         display_string = il->prettyCounter();
161         } else 
162                         display_string = ref;
163
164         // FIXME What we'd really like to do is to be able to output some
165         // appropriate sort of text here. But to do that, we need to associate
166         // some sort of counter with the label, and we don't have that yet.
167         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
168         xs << html::StartTag("a", attr);
169         xs << display_string;
170         xs << html::EndTag("a");
171         return docstring();
172 }
173
174
175 void InsetRef::tocString(odocstream & os) const
176 {
177         plaintext(os, OutputParams(0));
178 }
179
180
181 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
182 {
183         docstring const & ref = getParam("reference");
184         // register this inset into the buffer reference cache.
185         buffer().references(ref).push_back(make_pair(this, it));
186
187         docstring label;
188         for (int i = 0; !types[i].latex_name.empty(); ++i) {
189                 if (getCmdName() == types[i].latex_name) {
190                         label = _(types[i].short_gui_name);
191                         break;
192                 }
193         }
194         label += ref;
195         
196         if (!buffer().isLatex() && !getParam("name").empty()) {
197                 label += "||";
198                 label += getParam("name");
199         }
200         
201         screen_label_ = label;
202         bool shortened = false;
203         unsigned int const maxLabelChars = 24;
204         if (screen_label_.size() > maxLabelChars) {
205                 screen_label_.erase(maxLabelChars - 3);
206                 screen_label_ += "...";
207                 shortened = true;
208         }
209         if (shortened)
210                 tooltip_ = label;
211         else 
212                 tooltip_ = from_ascii("");
213 }
214
215
216 void InsetRef::addToToc(DocIterator const & cpit)
217 {
218         docstring const & label = getParam("reference");
219         if (buffer().insetLabel(label))
220                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
221                 return;
222
223         // It seems that this reference does not point to any valid label.
224         screen_label_ = _("BROKEN: ") + screen_label_;
225         Toc & toc = buffer().tocBackend().toc("label");
226         toc.push_back(TocItem(cpit, 0, screen_label_));
227 }
228
229
230 void InsetRef::validate(LaTeXFeatures & features) const
231 {
232         string const cmd = getCmdName();
233         if (cmd == "vref" || cmd == "vpageref")
234                 features.require("varioref");
235         else if (getCmdName() == "formatted") {
236                 if (buffer().params().use_refstyle) {
237                         features.require("refstyle");
238                         features.require("ifthen");
239                 } else
240                         features.require("prettyref");
241         } else if (getCmdName() == "eqref" && !buffer().params().use_refstyle)
242                 // refstyle defines its own version
243                 features.require("amsmath");
244         else if (cmd == "nameref")
245                 features.require("nameref");
246 }
247
248
249 InsetRef::type_info InsetRef::types[] = {
250         { "ref",       N_("Standard"),              N_("Ref: ")},
251         { "eqref",     N_("Equation"),              N_("EqRef: ")},
252         { "pageref",   N_("Page Number"),           N_("Page: ")},
253         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
254         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
255         { "formatted", N_("Formatted"),             N_("Format: ")},
256         { "nameref",   N_("Reference to Name"),     N_("NameRef:")},
257         { "", "", "" }
258 };
259
260
261 int InsetRef::getType(string const & name)
262 {
263         for (int i = 0; !types[i].latex_name.empty(); ++i)
264                 if (name == types[i].latex_name)
265                         return i;
266         return 0;
267 }
268
269
270 string const & InsetRef::getName(int type)
271 {
272         return types[type].latex_name;
273 }
274
275
276 } // namespace lyx