]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
'using namespace std' instead of 'using std::xxx'
[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
29 namespace lyx {
30
31 using support::escape;
32
33
34 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
35         : InsetCommand(p, "ref"), isLatex(buf.isLatex())
36 {}
37
38
39 InsetRef::InsetRef(InsetRef const & ir)
40         : InsetCommand(ir), isLatex(ir.isLatex)
41 {}
42
43
44 bool InsetRef::isCompatibleCommand(std::string const & s) {
45         //FIXME This is likely not the best way to handle this.
46         //But this stuff is hardcoded elsewhere already.
47         return s == "ref" 
48                 || s == "pageref"
49                 || s == "vref" 
50                 || s == "vpageref"
51                 || s == "prettyref"
52                 || s == "eqref";
53 }
54
55
56 CommandInfo const * InsetRef::findInfo(std::string const & /* cmdName */)
57 {
58         static const char * const paramnames[] = {"name", "reference", ""};
59         static const bool isoptional[] = {true, false};
60         static const CommandInfo info = {2, paramnames, isoptional};
61         return &info;
62 }
63
64
65 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
66 {
67         switch (cmd.action) {
68         case LFUN_MOUSE_RELEASE:
69                 // Eventually trigger dialog with button 3 not 1
70                 if (cmd.button() == mouse_button::button3)
71                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
72                                                   getParam("reference")));
73                 else
74                         InsetCommand::doDispatch(cur, cmd);
75                 break;
76
77         default:
78                 InsetCommand::doDispatch(cur, cmd);
79         }
80 }
81
82
83 docstring const InsetRef::getScreenLabel(Buffer const &) const
84 {
85         docstring temp;
86         for (int i = 0; !types[i].latex_name.empty(); ++i) {
87                 if (getCmdName() == types[i].latex_name) {
88                         temp = _(types[i].short_gui_name);
89                         break;
90                 }
91         }
92         temp += getParam("reference");
93
94         if (!isLatex && !getParam("name").empty()) {
95                 temp += "||";
96                 temp += getParam("name");
97         }
98         return temp;
99 }
100
101
102 int InsetRef::latex(Buffer const &, odocstream & os,
103                     OutputParams const &) const
104 {
105         // We don't want to output p_["name"], since that is only used 
106         // in docbook. So we construct new params, without it, and use that.
107         InsetCommandParams p(REF_CODE, getCmdName());
108         p["reference"] = getParam("reference");
109         os << escape(p.getCommand());
110         return 0;
111 }
112
113
114 int InsetRef::plaintext(Buffer const &, odocstream & os,
115                         OutputParams const &) const
116 {
117         docstring const str = getParam("reference");
118         os << '[' << str << ']';
119         return 2 + str.size();
120 }
121
122
123 int InsetRef::docbook(Buffer const & buf, odocstream & os,
124                       OutputParams const & runparams) const
125 {
126         docstring const & name = getParam("name");
127         if (name.empty()) {
128                 if (runparams.flavor == OutputParams::XML) {
129                         os << "<xref linkend=\""
130                            << sgml::cleanID(buf, runparams, getParam("reference"))
131                            << "\" />";
132                 } else {
133                         os << "<xref linkend=\""
134                            << sgml::cleanID(buf, runparams, getParam("reference"))
135                            << "\">";
136                 }
137         } else {
138                 os << "<link linkend=\""
139                    << sgml::cleanID(buf, runparams, getParam("reference"))
140                    << "\">"
141                    << getParam("name")
142                    << "</link>";
143         }
144
145         return 0;
146 }
147
148
149 int InsetRef::textString(Buffer const & buf, odocstream & os,
150                        OutputParams const & op) const
151 {
152         return plaintext(buf, os, op);
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