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