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