]> git.lyx.org Git - features.git/blob - src/insets/insetref.C
da32aa0c4abc1d94891b94fd51c500304a12c830
[features.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 "BufferView.h"
16 #include "dispatchresult.h"
17 #include "funcrequest.h"
18 #include "gettext.h"
19 #include "LaTeXFeatures.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "support/lstrings.h"
24
25
26 using lyx::support::escape;
27
28 using std::string;
29 using std::ostream;
30
31
32 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
33         : InsetCommand(p, "ref"), isLatex(buf.isLatex())
34 {}
35
36
37 InsetRef::InsetRef(InsetRef const & ir)
38         : InsetCommand(ir), isLatex(ir.isLatex)
39 {}
40
41
42 DispatchResult
43 InsetRef::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
44 {
45         switch (cmd.action) {
46         case LFUN_MOUSE_PRESS:
47                 // Eventually trigger dialog with button 3 not 1
48                 if (cmd.button() == mouse_button::button3)
49                         bv.owner()->dispatch(FuncRequest(LFUN_REF_GOTO, getContents()));
50                 else
51                         InsetCommandMailer("ref", *this).showDialog(&bv);
52                 return DispatchResult(true, true);
53
54         default:
55                 return InsetCommand::priv_dispatch(bv, cmd);
56         }
57 }
58
59
60 string const InsetRef::getScreenLabel(Buffer const &) const
61 {
62         string temp;
63         for (int i = 0; !types[i].latex_name.empty(); ++ i)
64                 if (getCmdName() == types[i].latex_name) {
65                         temp = _(types[i].short_gui_name);
66                         break;
67                 }
68         temp += getContents();
69
70         if (!isLatex
71            && !getOptions().empty()) {
72                 temp += "||";
73                 temp += getOptions();
74         }
75         return temp;
76 }
77
78
79 int InsetRef::latex(Buffer const &, ostream & os,
80                     OutputParams const &) const
81 {
82         if (getOptions().empty())
83                 os << escape(getCommand());
84         else {
85                 InsetCommandParams p(getCmdName(), getContents(), "");
86                 os << escape(p.getCommand());
87         }
88         return 0;
89 }
90
91
92 int InsetRef::plaintext(Buffer const &, ostream & os,
93                     OutputParams const &) const
94 {
95         os << '[' << getContents() << ']';
96         return 0;
97 }
98
99
100 int InsetRef::linuxdoc(Buffer const &, ostream & os,
101                        OutputParams const &) const
102 {
103         os << "<ref id=\"" << getContents()
104            << "\" name=\"" << getOptions() << "\" >";
105         return 0;
106 }
107
108
109 int InsetRef::docbook(Buffer const &, ostream & os,
110                       OutputParams const &) const
111 {
112         if (getOptions().empty()) {
113                 os << "<xref linkend=\"" << getContents() << "\">";
114         } else {
115                 os << "<link linkend=\"" << getContents()
116                    << "\">" << getOptions() << "</link>";
117         }
118
119         return 0;
120 }
121
122
123 void InsetRef::validate(LaTeXFeatures & features) const
124 {
125         if (getCmdName() == "vref" || getCmdName() == "vpageref")
126                 features.require("varioref");
127         else if (getCmdName() == "prettyref")
128                 features.require("prettyref");
129         else if (getCmdName() == "eqref")
130                 features.require("amsmath");
131 }
132
133
134 InsetRef::type_info InsetRef::types[] = {
135         { "ref",       N_("Standard"),              N_("Ref: ")},
136         { "eqref",     N_("Equation"),              N_("EqRef: ")},
137         { "pageref",   N_("Page Number"),           N_("Page: ")},
138         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
139         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
140         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
141         { "", "", "" }
142 };
143
144
145 int InsetRef::getType(string const & name)
146 {
147         for (int i = 0; !types[i].latex_name.empty(); ++i)
148                 if (name == types[i].latex_name)
149                         return i;
150         return 0;
151 }
152
153
154 string const & InsetRef::getName(int type)
155 {
156         return types[type].latex_name;
157 }