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