]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
3e5256adc6621405b10957e3c4ab21cfb866cbec
[lyx.git] / src / insets / insetref.C
1 #include <config.h>
2
3 #include <cstdlib>
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include FORMS_H_LOCATION 
10 #include "insetref.h"
11 #include "buffer.h"
12 #include "debug.h"
13 #include "lyx_gui_misc.h" // CancelCloseBoxCB
14 #include "LyXView.h"
15 #include "lyxfunc.h"
16 #include "commandtags.h"
17 #include "gettext.h"
18
19 extern BufferView * current_view;
20
21
22 InsetRef::InsetRef(string const & cmd, Buffer * bf)
23         : master(bf)
24 {
25         scanCommand(cmd);
26         if (getCmdName() == "ref")
27                 flag = InsetRef::REF;
28         else
29                 flag = InsetRef::PAGE_REF;
30 }
31
32
33 InsetRef::InsetRef(InsetCommand const & inscmd, Buffer * bf)
34         : master(bf)
35 {
36         setCmdName(inscmd.getCmdName());
37         setContents(inscmd.getContents());
38         setOptions(inscmd.getOptions());
39         if (getCmdName() == "ref")
40                 flag = InsetRef::REF;
41         else
42                 flag = InsetRef::PAGE_REF;
43 }
44
45
46 void InsetRef::Edit(BufferView * bv, int, int, unsigned int)
47 {
48         bv->owner()->getLyXFunc()->
49                 Dispatch(LFUN_REFGOTO, getContents().c_str());
50 }
51
52
53 string InsetRef::getScreenLabel() const
54 {
55         string temp;
56         if (flag == InsetRef::PAGE_REF)
57                 temp += _("Page: ");
58         else 
59                 temp += _("Ref: ");
60         temp += getContents();
61         if(!current_view->buffer()->isLatex()
62            && !getOptions().empty()) {
63                 temp += "||";
64                 temp += getOptions();
65         }
66         return temp;
67 }
68
69
70 int InsetRef::Latex(ostream & os,
71                     signed char /*fragile*/, bool /*fs*/) const
72 {
73         if(getOptions().empty())
74                 os << escape(getCommand());
75         else {
76                 string ns;
77                 InsetCommand clone = InsetCommand(getCmdName(),
78                                                   getContents(), ns);
79                 os << escape(clone.getCommand());
80         }
81         return 0;
82 }
83
84
85 int InsetRef::Linuxdoc(ostream & os) const
86 {
87         os << "<ref id=\"" << getContents()
88            << "\" name=\"" << getOptions() << "\" >";
89         return 0;
90 }
91
92
93 int InsetRef::DocBook(ostream & os) const
94 {
95         os << "<link linkend=\"" << getContents()
96            << "\">" << getOptions() << "</link>";
97         return 0;
98 }
99
100
101 // This function escapes 8-bit characters and other problematic characters
102 // It's exactly the same code as in insetlabel.C.
103 string InsetRef::escape(string const & lab) const
104 {
105         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
106                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
107         string enc;
108         for (string::size_type i = 0; i < lab.length(); ++i) {
109                 unsigned char c= lab[i];
110                 if (c >= 128 || c == '=' || c == '%') {
111                         enc += '=';
112                         enc += hexdigit[c>>4];
113                         enc += hexdigit[c & 15];
114                 } else {
115                         enc += c;
116                 }
117         }
118         return enc;
119 }