]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
change to use ostreams instead of string when writing files. fiddling with insettext...
[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, signed char /*fragile*/) const
71 {
72         if(getOptions().empty())
73                 os << escape(getCommand());
74         else {
75                 string ns;
76                 InsetCommand clone = InsetCommand(getCmdName(),
77                                                   getContents(), ns);
78                 os << escape(clone.getCommand());
79         }
80         return 0;
81 }
82
83
84 #ifndef USE_OSTREAM_ONLY
85 int InsetRef::Latex(string & file, signed char /*fragile*/) const
86 {
87         if(getOptions().empty())
88                 file += escape(getCommand());
89         else {
90                 string ns;
91                 InsetCommand clone = InsetCommand(getCmdName(),
92                                                   getContents(), ns);
93                 file += escape(clone.getCommand());
94         }
95         return 0;
96 }
97
98
99 int InsetRef::Linuxdoc(string & file) const
100 {
101         file += "<ref id=\"" + getContents()
102                 + "\" name=\""+ getOptions() +"\" >" ;
103
104         return 0;
105 }
106
107
108 int InsetRef::DocBook(string & file) const
109 {
110         file += "<link linkend=\"" + getContents()
111                 + "\">"+ getOptions() +"</link>" ;
112
113         return 0;
114 }
115
116 #else
117
118 int InsetRef::Linuxdoc(ostream & os) const
119 {
120         os << "<ref id=\"" << getContents()
121            << "\" name=\"" << getOptions() << "\" >";
122         return 0;
123 }
124
125
126 int InsetRef::DocBook(ostream & os) const
127 {
128         os << "<link linkend=\"" << getContents()
129            << "\">" << getOptions() << "</link>";
130         return 0;
131 }
132 #endif
133
134
135 // This function escapes 8-bit characters and other problematic characters
136 // It's exactly the same code as in insetlabel.C.
137 string InsetRef::escape(string const & lab) const
138 {
139         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
140                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
141         string enc;
142         for (string::size_type i = 0; i < lab.length(); ++i) {
143                 unsigned char c= lab[i];
144                 if (c >= 128 || c == '=' || c == '%') {
145                         enc += '=';
146                         enc += hexdigit[c>>4];
147                         enc += hexdigit[c & 15];
148                 } else {
149                         enc += c;
150                 }
151         }
152         return enc;
153 }