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