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