]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
setFont rework + some code simplification
[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 "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), isLatex(buf.isLatex())
34 {}
35
36
37 InsetRef::InsetRef(InsetRef const & ir)
38         : InsetCommand(ir), isLatex(ir.isLatex)
39 {
40 }
41
42
43 InsetRef::~InsetRef()
44 {
45         InsetCommandMailer("ref", *this).hideDialog();
46 }
47
48
49 DispatchResult
50 InsetRef::priv_dispatch(FuncRequest const & cmd,
51                         idx_type & idx, pos_type & pos)
52 {
53         switch (cmd.action) {
54         case LFUN_MOUSE_PRESS:
55                 // Eventually trigger dialog with button 3 not 1
56                 if (cmd.button() == mouse_button::button3)
57                         cmd.view()->owner()->
58                                 dispatch(FuncRequest(LFUN_REF_GOTO, getContents()));
59                 else
60                         InsetCommandMailer("ref", *this).showDialog(cmd.view());
61                 return DispatchResult(true, true);
62
63         default:
64                 return InsetCommand::priv_dispatch(cmd, idx, pos);
65         }
66 }
67
68
69 string const InsetRef::getScreenLabel(Buffer const &) const
70 {
71         string temp;
72         for (int i = 0; !types[i].latex_name.empty(); ++ i)
73                 if (getCmdName() == types[i].latex_name) {
74                         temp = _(types[i].short_gui_name);
75                         break;
76                 }
77         temp += getContents();
78
79         if (!isLatex
80            && !getOptions().empty()) {
81                 temp += "||";
82                 temp += getOptions();
83         }
84         return temp;
85 }
86
87
88 int InsetRef::latex(Buffer const &, ostream & os,
89                     OutputParams const &) const
90 {
91         if (getOptions().empty())
92                 os << escape(getCommand());
93         else {
94                 InsetCommandParams p(getCmdName(), getContents(), "");
95                 os << escape(p.getCommand());
96         }
97         return 0;
98 }
99
100
101 int InsetRef::plaintext(Buffer const &, ostream & os,
102                     OutputParams const &) const
103 {
104         os << '[' << getContents() << ']';
105         return 0;
106 }
107
108
109 int InsetRef::linuxdoc(Buffer const &, ostream & os,
110                        OutputParams const &) const
111 {
112         os << "<ref id=\"" << getContents()
113            << "\" name=\"" << getOptions() << "\" >";
114         return 0;
115 }
116
117
118 int InsetRef::docbook(Buffer const &, ostream & os,
119                       OutputParams const &) const
120 {
121         if (getOptions().empty()) {
122                 os << "<xref linkend=\"" << getContents() << "\">";
123         } else {
124                 os << "<link linkend=\"" << getContents()
125                    << "\">" << getOptions() << "</link>";
126         }
127
128         return 0;
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 }