]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
revert change from previous commit and remove the FontMetrics access completely in...
[lyx.git] / src / insets / insetref.C
1 /**
2  * \file insetref.C
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 using lyx::docstring;
28 using lyx::support::escape;
29
30 using std::string;
31 using std::ostream;
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 void InsetRef::doDispatch(LCursor & cur, FuncRequest & cmd)
45 {
46         switch (cmd.action) {
47         case LFUN_MOUSE_PRESS:
48                 // Eventually trigger dialog with button 3 not 1
49                 if (cmd.button() == mouse_button::button3)
50                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, getContents()));
51                 else {
52                         InsetCommandMailer("ref", *this).showDialog(&cur.bv());
53                         cur.undispatched();
54                 }
55                 return;
56
57         case LFUN_MOUSE_RELEASE:
58                 return;
59
60         default:
61                 return InsetCommand::doDispatch(cur, cmd);
62         }
63 }
64
65
66 docstring const InsetRef::getScreenLabel(Buffer const &) const
67 {
68         docstring temp;
69         for (int i = 0; !types[i].latex_name.empty(); ++i) {
70                 if (getCmdName() == types[i].latex_name) {
71                         temp = _(types[i].short_gui_name);
72                         break;
73                 }
74         }
75         // FIXME UNIOCDE
76         temp += lyx::from_utf8(getContents());
77
78         if (!isLatex && !getOptions().empty()) {
79                 temp += "||";
80                 // FIXME UNIOCDE
81                 temp += lyx::from_utf8(getOptions());
82         }
83         return temp;
84 }
85
86
87 int InsetRef::latex(Buffer const &, ostream & os,
88                     OutputParams const &) const
89 {
90         if (getOptions().empty())
91                 os << escape(getCommand());
92         else {
93                 InsetCommandParams p(getCmdName(), getContents(), "");
94                 os << escape(p.getCommand());
95         }
96         return 0;
97 }
98
99
100 int InsetRef::plaintext(Buffer const &, lyx::odocstream & os,
101                     OutputParams const &) const
102 {
103         // FIXME UNIOCDE
104         os << '[' << lyx::from_utf8(getContents()) << ']';
105         return 0;
106 }
107
108
109 int InsetRef::docbook(Buffer const & buf, ostream & os,
110                       OutputParams const & runparams) const
111 {
112         if (getOptions().empty() && runparams.flavor == OutputParams::XML) {
113                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" />";
114         } else if (getOptions().empty()) {
115                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\">";
116         } else {
117                 os << "<link linkend=\"" << sgml::cleanID(buf, runparams, getContents())
118                    << "\">" << getOptions() << "</link>";
119         }
120
121         return 0;
122 }
123
124
125 int InsetRef::textString(Buffer const & buf, lyx::odocstream & os,
126                        OutputParams const & op) const
127 {
128         return plaintext(buf, os, op);
129 }
130
131
132 void InsetRef::validate(LaTeXFeatures & features) const
133 {
134         if (getCmdName() == "vref" || getCmdName() == "vpageref")
135                 features.require("varioref");
136         else if (getCmdName() == "prettyref")
137                 features.require("prettyref");
138         else if (getCmdName() == "eqref")
139                 features.require("amsmath");
140 }
141
142
143 InsetRef::type_info InsetRef::types[] = {
144         { "ref",       N_("Standard"),              N_("Ref: ")},
145         { "eqref",     N_("Equation"),              N_("EqRef: ")},
146         { "pageref",   N_("Page Number"),           N_("Page: ")},
147         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
148         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
149         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
150         { "", "", "" }
151 };
152
153
154 int InsetRef::getType(string const & name)
155 {
156         for (int i = 0; !types[i].latex_name.empty(); ++i)
157                 if (name == types[i].latex_name)
158                         return i;
159         return 0;
160 }
161
162
163 string const & InsetRef::getName(int type)
164 {
165         return types[type].latex_name;
166 }