]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
InsetBox.cpp: only shaded boxes can have multiple paragraphs when there is no inner box
[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 }
57
58
59 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
60 {
61         static ParamInfo param_info_;
62         if (param_info_.empty()) {
63                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
64                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
65                                 ParamInfo::HANDLING_ESCAPE);
66         }
67         return param_info_;
68 }
69
70
71 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
72 {
73         // We don't want to output p_["name"], since that is only used 
74         // in docbook. So we construct new params, without it, and use that.
75         InsetCommandParams p(REF_CODE, getCmdName());
76         p["reference"] = getParam("reference");
77         os << p.getCommand(runparams);
78         return 0;
79 }
80
81
82 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
83 {
84         docstring const str = getParam("reference");
85         os << '[' << str << ']';
86         return 2 + str.size();
87 }
88
89
90 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
91 {
92         docstring const & name = getParam("name");
93         if (name.empty()) {
94                 if (runparams.flavor == OutputParams::XML) {
95                         os << "<xref linkend=\""
96                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
97                            << "\" />";
98                 } else {
99                         os << "<xref linkend=\""
100                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
101                            << "\">";
102                 }
103         } else {
104                 os << "<link linkend=\""
105                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
106                    << "\">"
107                    << getParam("name")
108                    << "</link>";
109         }
110
111         return 0;
112 }
113
114
115 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
116 {
117         docstring const & ref = getParam("reference");
118         InsetLabel const * il = buffer().insetLabel(ref);
119         string const & cmd = params().getCmdName();
120         docstring display_string;
121
122         if (il && !il->counterValue().empty()) {
123                 // Try to construct a label from the InsetLabel we reference.
124                 docstring const & value = il->counterValue();
125                 if (cmd == "ref")
126                         display_string = value;
127                 else if (cmd == "vref")
128                         // normally, would be "ref on page #", but we have no pages
129                         display_string = value;
130                 else if (cmd == "pageref" || cmd == "vpageref")
131                         // normally would be "on page #", but we have no pages
132                         display_string = _("elsewhere");
133                 else if (cmd == "eqref")
134                         display_string = bformat(from_ascii("equation (%1$s)"), value);
135                 else { // "prettyref"
136                         display_string = il->prettyCounter();
137                 }
138         } else 
139                         display_string = ref;
140
141         // FIXME What we'd really like to do is to be able to output some
142         // appropriate sort of text here. But to do that, we need to associate
143         // some sort of counter with the label, and we don't have that yet.
144         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
145         xs << html::StartTag("a", attr);
146         xs << display_string;
147         xs << html::EndTag("a");
148         return docstring();
149 }
150
151
152 void InsetRef::tocString(odocstream & os) const
153 {
154         plaintext(os, OutputParams(0));
155 }
156
157
158 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
159 {
160         docstring const & ref = getParam("reference");
161         // register this inset into the buffer reference cache.
162         buffer().references(ref).push_back(make_pair(this, it));
163
164         docstring label;
165         for (int i = 0; !types[i].latex_name.empty(); ++i) {
166                 if (getCmdName() == types[i].latex_name) {
167                         label = _(types[i].short_gui_name);
168                         break;
169                 }
170         }
171         label += ref;
172
173         if (!isLatex && !getParam("name").empty()) {
174                 label += "||";
175                 label += getParam("name");
176         }
177         
178         screen_label_ = label;
179         bool shortened = false;
180         unsigned int const maxLabelChars = 24;
181         if (screen_label_.size() > maxLabelChars) {
182                 screen_label_.erase(maxLabelChars - 3);
183                 screen_label_ += "...";
184                 shortened = true;
185         }
186         if (shortened)
187                 tooltip_ = label;
188         else 
189                 tooltip_ = from_ascii("");
190 }
191
192
193 void InsetRef::addToToc(DocIterator const & cpit)
194 {
195         docstring const & label = getParam("reference");
196         if (buffer().insetLabel(label))
197                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
198                 return;
199
200         // It seems that this reference does not point to any valid label.
201         screen_label_ = _("BROKEN: ") + screen_label_;
202         Toc & toc = buffer().tocBackend().toc("label");
203         toc.push_back(TocItem(cpit, 0, screen_label_));
204 }
205
206
207 void InsetRef::validate(LaTeXFeatures & features) const
208 {
209         if (getCmdName() == "vref" || getCmdName() == "vpageref")
210                 features.require("varioref");
211         else if (getCmdName() == "prettyref")
212                 features.require("prettyref");
213         else if (getCmdName() == "eqref")
214                 features.require("amsmath");
215 }
216
217
218 InsetRef::type_info InsetRef::types[] = {
219         { "ref",       N_("Standard"),              N_("Ref: ")},
220         { "eqref",     N_("Equation"),              N_("EqRef: ")},
221         { "pageref",   N_("Page Number"),           N_("Page: ")},
222         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
223         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
224         { "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
225         { "", "", "" }
226 };
227
228
229 int InsetRef::getType(string const & name)
230 {
231         for (int i = 0; !types[i].latex_name.empty(); ++i)
232                 if (name == types[i].latex_name)
233                         return i;
234         return 0;
235 }
236
237
238 string const & InsetRef::getName(int type)
239 {
240         return types[type].latex_name;
241 }
242
243
244 } // namespace lyx