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