]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
This optional argument to the InsetCollapsable constructor
[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 "LaTeXFeatures.h"
19 #include "LyXFunc.h"
20 #include "OutputParams.h"
21 #include "ParIterator.h"
22 #include "sgml.h"
23 #include "TocBackend.h"
24
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 using namespace lyx::support;
30 using namespace std;
31
32 namespace lyx {
33
34
35 InsetRef::InsetRef(Buffer const & buf, InsetCommandParams const & p)
36         : InsetCommand(p, "ref"), isLatex(buf.isLatex())
37 {}
38
39
40 InsetRef::InsetRef(InsetRef const & ir)
41         : InsetCommand(ir), isLatex(ir.isLatex)
42 {}
43
44
45 bool InsetRef::isCompatibleCommand(string const & s) {
46         //FIXME This is likely not the best way to handle this.
47         //But this stuff is hardcoded elsewhere already.
48         return s == "ref" 
49                 || s == "pageref"
50                 || s == "vref" 
51                 || s == "vpageref"
52                 || s == "prettyref"
53                 || s == "eqref";
54 }
55
56
57 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
58 {
59         static ParamInfo param_info_;
60         if (param_info_.empty()) {
61                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
62                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
63         }
64         return param_info_;
65 }
66
67
68 docstring InsetRef::screenLabel() const
69 {
70         return screen_label_;
71 }
72
73
74 int InsetRef::latex(odocstream & os, OutputParams const &) const
75 {
76         // We don't want to output p_["name"], since that is only used 
77         // in docbook. So we construct new params, without it, and use that.
78         InsetCommandParams p(REF_CODE, getCmdName());
79         p["reference"] = getParam("reference");
80         os << escape(p.getCommand());
81         return 0;
82 }
83
84
85 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
86 {
87         docstring const str = getParam("reference");
88         os << '[' << str << ']';
89         return 2 + str.size();
90 }
91
92
93 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
94 {
95         docstring const & name = getParam("name");
96         if (name.empty()) {
97                 if (runparams.flavor == OutputParams::XML) {
98                         os << "<xref linkend=\""
99                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
100                            << "\" />";
101                 } else {
102                         os << "<xref linkend=\""
103                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
104                            << "\">";
105                 }
106         } else {
107                 os << "<link linkend=\""
108                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
109                    << "\">"
110                    << getParam("name")
111                    << "</link>";
112         }
113
114         return 0;
115 }
116
117
118 void InsetRef::textString(odocstream & os) const
119 {
120         plaintext(os, OutputParams(0));
121 }
122
123
124 void InsetRef::updateLabels(ParIterator const & it)
125 {
126         docstring const & label = getParam("reference");
127         // register this inset into the buffer reference cache.
128         buffer().references(label).push_back(make_pair(this, it));
129
130         for (int i = 0; !types[i].latex_name.empty(); ++i) {
131                 if (getCmdName() == types[i].latex_name) {
132                         screen_label_ = _(types[i].short_gui_name);
133                         break;
134                 }
135         }
136         screen_label_ += getParam("reference");
137
138         if (!isLatex && !getParam("name").empty()) {
139                 screen_label_ += "||";
140                 screen_label_ += getParam("name");
141         }
142 }
143
144
145 void InsetRef::addToToc(DocIterator const & cpit)
146 {
147         docstring const & label = getParam("reference");
148         if (buffer().insetLabel(label))
149                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
150                 return;
151
152         // It seems that this reference does not point to any valid label.
153         screen_label_ = _("BROKEN: ") + screen_label_;
154         Toc & toc = buffer().tocBackend().toc("label");
155         toc.push_back(TocItem(cpit, 0, screen_label_));
156 }
157
158
159 void InsetRef::validate(LaTeXFeatures & features) const
160 {
161         if (getCmdName() == "vref" || getCmdName() == "vpageref")
162                 features.require("varioref");
163         else if (getCmdName() == "prettyref")
164                 features.require("prettyref");
165         else if (getCmdName() == "eqref")
166                 features.require("amsmath");
167 }
168
169
170 InsetRef::type_info InsetRef::types[] = {
171         { "ref",       N_("Standard"),              N_("Ref: ")},
172         { "eqref",     N_("Equation"),              N_("EqRef: ")},
173         { "pageref",   N_("Page Number"),           N_("Page: ")},
174         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
175         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
176         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
177         { "", "", "" }
178 };
179
180
181 int InsetRef::getType(string const & name)
182 {
183         for (int i = 0; !types[i].latex_name.empty(); ++i)
184                 if (name == types[i].latex_name)
185                         return i;
186         return 0;
187 }
188
189
190 string const & InsetRef::getName(int type)
191 {
192         return types[type].latex_name;
193 }
194
195
196 } // namespace lyx