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