]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
2cccc2931f42492ad96216b3e6d91e08cc5adbdd
[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 "gettext.h"
19 #include "LaTeXFeatures.h"
20 #include "LyXFunc.h"
21 #include "OutputParams.h"
22 #include "sgml.h"
23
24 #include "support/lstrings.h"
25
26
27 namespace lyx {
28
29 using support::escape;
30
31 using std::string;
32 using std::ostream;
33
34
35 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
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(std::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 CommandInfo const * InsetRef::findInfo(std::string const & /* cmdName */)
58 {
59         static const char * const paramnames[] = {"name", "reference", ""};
60         static const bool isoptional[] = {true, false};
61         static const CommandInfo info = {2, paramnames, isoptional};
62         return &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 int InsetRef::textString(Buffer const & buf, odocstream & os,
151                        OutputParams const & op) const
152 {
153         return plaintext(buf, os, op);
154 }
155
156
157 void InsetRef::validate(LaTeXFeatures & features) const
158 {
159         if (getCmdName() == "vref" || getCmdName() == "vpageref")
160                 features.require("varioref");
161         else if (getCmdName() == "prettyref")
162                 features.require("prettyref");
163         else if (getCmdName() == "eqref")
164                 features.require("amsmath");
165 }
166
167
168 InsetRef::type_info InsetRef::types[] = {
169         { "ref",       N_("Standard"),              N_("Ref: ")},
170         { "eqref",     N_("Equation"),              N_("EqRef: ")},
171         { "pageref",   N_("Page Number"),           N_("Page: ")},
172         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
173         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
174         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
175         { "", "", "" }
176 };
177
178
179 int InsetRef::getType(string const & name)
180 {
181         for (int i = 0; !types[i].latex_name.empty(); ++i)
182                 if (name == types[i].latex_name)
183                         return i;
184         return 0;
185 }
186
187
188 string const & InsetRef::getName(int type)
189 {
190         return types[type].latex_name;
191 }
192
193
194 } // namespace lyx