]> git.lyx.org Git - features.git/blob - src/insets/insetref.C
d0331fb6cfbee219d270c51c252a524750b3c67c
[features.git] / src / insets / insetref.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "insetref.h"
8 #include "buffer.h"
9 #include "commandtags.h"
10 #include "debug.h"
11 #include "gettext.h"
12 #include "LaTeXFeatures.h"
13 #include "LyXView.h"
14 #include "frontends/Dialogs.h"
15 #include "lyxfunc.h"
16 #include "BufferView.h"
17
18 using std::ostream;
19
20 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
21         : InsetCommand(p), isLatex(buf.isLatex())
22 {}
23
24 void InsetRef::Edit(BufferView * bv, int, int, unsigned int button)
25 {
26         // Eventually trigger dialog with button 3 not 1
27         if (button == 3 )
28                 bv->owner()->getLyXFunc()->
29                         Dispatch(LFUN_REF_GOTO, getContents());
30         else if (button == 1 )
31                 bv->owner()->getDialogs()->showRef( this );
32 }
33
34
35 string const InsetRef::getScreenLabel() const
36 {
37         string temp;
38         for (int i = 0; !types[i].latex_name.empty(); ++ i)
39                 if (getCmdName() == types[i].latex_name) {
40                         temp = _(types[i].short_gui_name);
41                         break;
42                 }
43         temp += getContents();
44
45         if (!isLatex
46            && !getOptions().empty()) {
47                 temp += "||";
48                 temp += getOptions();
49         }
50         return temp;
51 }
52
53
54 int InsetRef::Latex(Buffer const *, ostream & os,
55                     bool /*fragile*/, bool /*fs*/) const
56 {
57         if (getOptions().empty())
58                 os << escape(getCommand());
59         else {
60                 InsetCommandParams p( getCmdName(), getContents(), "" );
61                 os << escape(p.getCommand());
62         }
63         return 0;
64 }
65
66
67 int InsetRef::Ascii(Buffer const *, ostream & os, int) const
68 {
69         os << "[" << getContents() << "]";
70         return 0;
71 }
72
73
74 int InsetRef::Linuxdoc(Buffer const *, ostream & os) const
75 {
76         os << "<ref id=\"" << getContents()
77            << "\" name=\"" << getOptions() << "\" >";
78         return 0;
79 }
80
81
82 int InsetRef::DocBook(Buffer const *, ostream & os) const
83 {
84         os << "<link linkend=\"" << getContents()
85            << "\">" << getOptions() << "</link>";
86         return 0;
87 }
88
89
90 // This function escapes 8-bit characters and other problematic characters
91 // It's exactly the same code as in insetlabel.C.
92 string const InsetRef::escape(string const & lab) const
93 {
94         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
95                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
96         string enc;
97         for (string::size_type i = 0; i < lab.length(); ++i) {
98                 unsigned char c= lab[i];
99                 if (c >= 128 || c == '=' || c == '%') {
100                         enc += '=';
101                         enc += hexdigit[c>>4];
102                         enc += hexdigit[c & 15];
103                 } else {
104                         enc += c;
105                 }
106         }
107         return enc;
108 }
109
110
111 void InsetRef::Validate(LaTeXFeatures & features) const
112 {
113         if (getCmdName() == "vref" || getCmdName() == "vpageref")
114                 features.varioref = true;
115         else if (getCmdName() == "prettyref")
116                 features.prettyref = true;
117 }
118
119 InsetRef::type_info InsetRef::types[] = {
120         { "ref",        N_("Standard"),                 N_("Ref: ")},
121         { "pageref",    N_("Page Number"),              N_("Page: ")},
122         { "vpageref",   N_("Textual Page Number"),      N_("TextPage: ")},
123         { "vref",       N_("Standard+Textual Page"),    N_("Ref+Text: ")},
124         { "prettyref",  N_("PrettyRef"),                N_("PrettyRef: ")},
125         { "", "", "" }
126 };
127
128
129 int InsetRef::getType(string const & name)
130 {
131         for (int i = 0; !types[i].latex_name.empty(); ++i)
132                 if (name == types[i].latex_name)
133                         return i;
134         return 0;
135 }
136
137
138 string const & InsetRef::getName(int type)
139 {
140         return types[type].latex_name;
141 }