]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
fc2f68342689e428b3e6807fd1ac4fd903376c8f
[lyx.git] / src / insets / insetref.C
1 #include <config.h>
2
3 #include <stdlib.h>
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 // Temporarily disabled the GUI code. Reasons:
22 // - Only page-ref button works currently, IMO we should use a LyX action
23 //   instead, to toggle the kind of refs.
24 // - To change the label, IMO it's faster to delete the old one and insert
25 //   a new one.
26 // - To goto to the label, IMO it's much faster to just click on the
27 //   inset. That's how I've implemented it now, I hope you'll like it.
28 // - The more GUI code we can remove, the less work we'll have at
29 //   the toolkit switch.
30 //   (ale 970723)
31
32 #if 0
33
34 /* Header file generated with fdesign. */
35
36 /**** Callback routines ****/
37
38 static void ref_close_cb(FL_OBJECT *, long);
39 static void goto_label_cb(FL_OBJECT *, long);
40 static void label_change_cb(FL_OBJECT *, long);
41
42 /**** Forms and Objects ****/
43
44 typedef struct {
45         FL_FORM *ref;
46         void *vdata;
47         long ldata;
48         FL_OBJECT *pg_grp;
49         FL_OBJECT *flag1;
50         FL_OBJECT *flag2;
51 } FD_ref;
52
53 /* Form definition file generated with fdesign. */
54
55 static
56 FD_ref *create_form_ref(void)
57 {
58   FL_OBJECT *obj;
59   FD_ref *fdui = (FD_ref *) fl_calloc(1, sizeof(*fdui));
60
61   fdui->ref = fl_bgn_form(FL_NO_BOX, 210, 170);
62   obj = fl_add_box(FL_UP_BOX,0,0,210,170,"");
63   obj = fl_add_frame(FL_ENGRAVED_FRAME,10,20,130,60,"");
64   obj = fl_add_button(FL_RETURN_BUTTON,120,130,80,30,_("Close"));
65     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
66     fl_set_object_callback(obj,ref_close_cb,0);
67   obj = fl_add_text(FL_NORMAL_TEXT,20,10,110,20,_("Reference Type"));
68     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
69     fl_set_object_lalign(obj,FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
70   obj = fl_add_button(FL_NORMAL_BUTTON,10,130,100,30,_("Goto Label"));
71     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
72     fl_set_object_callback(obj,goto_label_cb,0);
73   obj = fl_add_button(FL_NORMAL_BUTTON,10,90,100,30,_("Change Label"));
74     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
75     fl_set_object_callback(obj,label_change_cb,0);
76
77   fdui->pg_grp = fl_bgn_group();
78   fdui->flag1 = obj = fl_add_checkbutton(FL_RADIO_BUTTON,20,30,20,20,_("Page Number"));
79     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
80   fdui->flag2 = obj = fl_add_checkbutton(FL_RADIO_BUTTON,20,50,20,20,_("Reference"));
81     fl_set_object_lsize(obj,FL_NORMAL_SIZE);
82   fl_end_group();
83
84   fl_end_form();
85
86   fdui->ref->fdui = fdui;
87
88   return fdui;
89 }
90 /*---------------------------------------*/
91
92
93 static FD_ref *form = 0;
94
95
96 static
97 void ref_close_cb(FL_OBJECT *, long)
98 {
99         InsetRef *inset = (InsetRef*)form->vdata;
100
101         if (fl_get_button(form->flag1)) {
102                 inset->setFlag(InsetRef::PAGE_REF);
103                 inset->setCmdName("pageref");
104         } else {
105                 inset->setFlag(InsetRef::REF);
106                 inset->setCmdName("ref");
107         }
108                 
109         fl_hide_form(form->ref);
110 }
111
112
113 static
114 void goto_label_cb(FL_OBJECT *, long)
115 {
116         // code yet to be written
117         InsetRef *inset = (InsetRef*)form->vdata;
118         inset->gotoLabel();
119 #warning MAKEME!
120 }
121
122
123 static
124 void label_change_cb(FL_OBJECT *, long)
125 {
126         // code yet to be written
127         InsetRef *inset = (InsetRef*)form->vdata;
128 #warning MAKEME!
129 }
130
131 #endif
132
133 InsetRef::InsetRef(string const & cmd, Buffer *bf)
134         : master(bf)
135 {
136         scanCommand(cmd);
137         if (getCmdName() == "ref")
138                 flag = InsetRef::REF;
139         else
140                 flag = InsetRef::PAGE_REF;
141 }
142
143
144 InsetRef::InsetRef(InsetCommand const &inscmd, Buffer *bf)
145         : master(bf)
146 {
147         setCmdName(inscmd.getCmdName());
148         setContents(inscmd.getContents());
149         setOptions(inscmd.getOptions());
150         if (getCmdName() == "ref")
151                 flag = InsetRef::REF;
152         else
153                 flag = InsetRef::PAGE_REF;
154 }
155
156
157 InsetRef::~InsetRef()
158 {
159 }
160
161
162 void InsetRef::Edit(int, int)
163 {
164         current_view->getOwner()->getLyXFunc()->Dispatch(LFUN_REFGOTO
165                                                          , getContents().c_str());
166 //        gotoLabel();
167 /*    
168         if (!form) { 
169                 form = create_form_ref();
170                 fl_set_form_atclose(form->ref, IgnoreCloseBoxCB, 0);
171         }
172         form->vdata = this; 
173     
174         fl_set_button(form->flag1, (flag == InsetRef::REF) ? 1 : 0);
175         fl_set_button(form->flag2, (flag == InsetRef::PAGE_REF) ? 1 : 0);
176         
177         if (form->ref->visible) {
178                 fl_raise_form(form->ref);
179         } else {
180                 fl_show_form(form->ref,FL_PLACE_MOUSE, FL_FULLBORDER,
181                              _("Cross-Reference"));
182         }
183  */
184 }
185
186
187 string InsetRef::getScreenLabel() const
188 {
189         string temp;
190         if (flag == InsetRef::PAGE_REF)
191                 temp += _("Page: ");
192         else 
193                 temp += _("Ref: ");
194         temp += getContents();
195         if(!current_view->currentBuffer()->isLatex() && !getOptions().empty()) {
196                 temp += "||";
197                 temp += getOptions();
198         }
199         return temp;
200 }
201
202
203 int InsetRef::Latex(FILE *file, signed char /*fragile*/)
204 {
205         if(getOptions().empty())
206                 fprintf(file, "%s", escape(getCommand()).c_str());
207         else {
208                 string ns;
209                 InsetCommand clone= InsetCommand(getCmdName(),getContents(),ns);
210                 fprintf(file, "%s", escape(clone.getCommand()).c_str());
211         }
212         return 0;
213 }
214
215
216 int InsetRef::Latex(string &file, signed char /*fragile*/)
217 {
218         if(getOptions().empty())
219                 file += escape(getCommand());
220         else {
221                 string ns;
222                 InsetCommand clone= InsetCommand(getCmdName(),getContents(),ns);
223                 file += escape(clone.getCommand());
224         }
225         return 0;
226 }
227
228
229 int InsetRef::Linuxdoc(string &file)
230 {
231         file += "<ref id=\"" + getContents() + "\" name=\""+ getOptions() +"\" >" ;
232
233         return 0;
234 }
235
236
237 int InsetRef::DocBook(string &file)
238 {
239         file += "<link linkend=\"" + getContents() + "\">"+ getOptions() +"</link>" ;
240
241         return 0;
242 }
243
244
245 // This function escapes 8-bit characters and other problematic characters
246 // It's exactly the same code as in insetlabel.C.
247 string InsetRef::escape(string const & lab) const {
248         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
249                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
250         string enc;
251         for (string::size_type i = 0; i < lab.length(); ++i) {
252                 unsigned char c=lab[i];
253                 if (c >= 128 || c=='=' || c=='%') {
254                         enc += '=';
255                         enc += hexdigit[c>>4];
256                         enc += hexdigit[c & 15];
257                 } else {
258                         enc += (char) c;
259                 }
260         }
261         return enc;
262 }