]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
Dekel's lyxrc.example; Angus's FormDocument; John's build-listerrors; POTFILES.in...
[lyx.git] / src / frontends / xforms / FormRef.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 2000 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include FORMS_H_LOCATION
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20
21 #include "Dialogs.h"
22 #include "FormRef.h"
23 #include "LyXView.h"
24 #include "buffer.h"
25 #include "form_ref.h"
26 #include "lyxfunc.h"
27
28 #include <algorithm>
29
30 using std::sort;
31 using std::vector;
32
33 static int formw;
34 static int formh;
35
36 FormRef::FormRef(LyXView * lv, Dialogs * d)
37         : FormCommand(lv, d, _("Reference")), toggle(GOBACK), dialog_(0)
38 {
39         // let the dialog be shown
40         // These are permanent connections so we won't bother
41         // storing a copy because we won't be disconnecting.
42         d->showRef.connect(slot(this, &FormRef::showInset));
43         d->createRef.connect(slot(this, &FormRef::createInset));
44 }
45
46
47 FormRef::~FormRef()
48 {
49         delete dialog_;
50 }
51
52
53 FL_FORM * FormRef::form() const
54 {
55         if ( dialog_ ) return dialog_->form;
56         return 0;
57 }
58
59
60 void FormRef::clearStore()
61 {
62         refs.clear();
63 }
64
65
66 void FormRef::build()
67 {
68         dialog_ = build_ref();
69
70         fl_addto_choice(dialog_->type,
71                         _(" Ref | Page | TextRef | TextPage | PrettyRef "));
72
73         // XFORMS bug workaround
74         // Define the min/max dimensions. Actually applied in update()
75         formw = form()->w, formh = form()->h;
76
77         // Name is irrelevant to LaTeX documents
78         if ( lv_->buffer()->isLatex() ) {
79                 fl_deactivate_object( dialog_->name );
80                 fl_set_object_lcol( dialog_->name, FL_INACTIVE );
81         }
82           
83         // Can change reference only through browser
84         fl_deactivate_object( dialog_->ref );
85
86         bc_.setOK( dialog_->button_ok );
87         bc_.setCancel( dialog_->button_cancel );
88         bc_.addReadOnly( dialog_->type );
89         bc_.refresh();
90 }
91
92
93 void FormRef::update()
94 {
95         fl_set_input(dialog_->ref,  params.getContents().c_str());
96         fl_set_input(dialog_->name, params.getOptions().c_str());
97
98         Type type = getType();
99         fl_set_choice( dialog_->type, type+1 );
100
101         toggle = GOBACK;
102         fl_set_object_label(dialog_->button_go, _("Goto reference"));
103
104         refs.clear();
105         if ( inset_ == 0 ) {
106                 refs = lv_->buffer()->getLabelList();
107                 updateBrowser( refs );
108                 showBrowser();
109         } else {
110                 hideBrowser();
111         }
112         bc_.readOnly( lv_->buffer()->isReadonly() );
113 }
114
115
116 void FormRef::updateBrowser( vector<string> keys ) const
117 {
118         if ( fl_get_button( dialog_->sort ) )
119                 sort( keys.begin(), keys.end() );
120
121         fl_clear_browser( dialog_->browser );
122         for( vector<string>::const_iterator it = keys.begin();
123              it != keys.end(); ++it )
124                 fl_add_browser_line( dialog_->browser, (*it).c_str());
125
126         if ( keys.empty() ) {
127                 fl_add_browser_line( dialog_->browser,
128                                      _("*** No labels found in document ***"));
129
130                 fl_deactivate_object( dialog_->browser );
131                 fl_deactivate_object( dialog_->button_update );
132                 fl_deactivate_object( dialog_->sort );
133                 fl_set_object_lcol( dialog_->browser, FL_INACTIVE );
134                 fl_set_object_lcol( dialog_->button_update, FL_INACTIVE );
135                 fl_set_object_lcol( dialog_->sort, FL_INACTIVE );
136         } else {
137                 fl_set_browser_topline( dialog_->browser, 1 );
138                 fl_activate_object( dialog_->browser );
139                 fl_set_object_lcol( dialog_->browser, FL_BLACK );
140                 fl_activate_object( dialog_->button_update );
141                 fl_set_object_lcol( dialog_->button_update, FL_BLACK );
142                 fl_activate_object( dialog_->sort );
143                 fl_set_object_lcol( dialog_->sort, FL_BLACK );
144         }
145 }
146
147
148 void FormRef::showBrowser() const
149 {
150         fl_show_object( dialog_->browser );
151         fl_show_object( dialog_->button_update );
152         fl_show_object( dialog_->sort );
153
154         setSize( formw, formh, 0 );
155
156         fl_deactivate_object( dialog_->type );
157         fl_set_object_lcol( dialog_->type, FL_INACTIVE );
158         fl_deactivate_object( dialog_->button_go );
159         fl_set_object_lcol( dialog_->button_go, FL_INACTIVE );
160         fl_set_object_lcol( dialog_->ref, FL_INACTIVE );
161         bc_.valid(false);
162 }
163
164
165 void FormRef::hideBrowser() const
166 {
167         fl_hide_object( dialog_->browser );
168         fl_hide_object( dialog_->button_update );
169         fl_hide_object( dialog_->sort );
170
171         setSize( 250, formh, 280 );
172
173         fl_activate_object( dialog_->type );
174         fl_set_object_lcol( dialog_->type, FL_BLACK );
175         fl_activate_object( dialog_->button_go );
176         fl_set_object_lcol( dialog_->button_go, FL_BLACK );
177         fl_set_object_lcol( dialog_->ref, FL_BLACK );
178         bc_.invalid();
179 }
180
181
182 void FormRef::setSize( int w, int h, int dx ) const
183 {
184         static int x1 = dialog_->name->x;
185         static int y1 = dialog_->name->y;
186         static int x2 = dialog_->ref->x;
187         static int y2 = dialog_->ref->y;
188         static int x3 = dialog_->type->x;
189         static int y3 = dialog_->type->y;
190         static int x4 = dialog_->button_go->x;
191         static int y4 = dialog_->button_go->y;
192         static int x5 = dialog_->button_ok->x;
193         static int y5 = dialog_->button_ok->y;
194         static int x6 = dialog_->button_cancel->x;
195         static int y6 = dialog_->button_cancel->y;
196
197         if ( form()->w != w )
198                 fl_set_form_size( form(), w, h );
199
200         fl_set_form_minsize( form(), w, h );
201         fl_set_form_maxsize( form(), 2*w, h );
202
203         if ( form()->w == w ) return;
204
205         fl_set_object_position( dialog_->name,   x1-dx, y1 );
206         fl_set_object_position( dialog_->ref,    x2-dx, y2 );
207         fl_set_object_position( dialog_->type,   x3-dx, y3 );
208         fl_set_object_position( dialog_->button_go,     x4-dx, y4 );
209         fl_set_object_position( dialog_->button_ok,     x5-dx, y5 );
210         fl_set_object_position( dialog_->button_cancel, x6-dx, y6 );
211
212         // These two must be reset apparently
213         // Name is irrelevant to LaTeX documents
214         if ( lv_->buffer()->isLatex() ) {
215                 fl_deactivate_object( dialog_->name );
216                 fl_set_object_lcol( dialog_->name, FL_INACTIVE );
217         }
218           
219         // Can change reference only through browser
220         fl_deactivate_object( dialog_->ref );
221 }
222
223
224 void FormRef::apply()
225 {
226         if (!lv_->view()->available())
227                 return;
228
229         Type const type = static_cast<Type>(fl_get_choice(dialog_->type) - 1);
230         params.setCmdName(getName(type));
231
232         params.setOptions(fl_get_input(dialog_->name));
233
234         if (inset_ != 0) {
235                 // Only update if contents have changed
236                 if (params != inset_->params()) {
237                         inset_->setParams(params);
238                         lv_->view()->updateInset(inset_, true);
239                 }
240         } else {
241                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
242                                             params.getAsString());
243         }
244 }
245
246
247 #ifdef WITH_WARNINGS
248 #warning check use of buttoncontroller
249 // Seems okay except that goref and goback shouldn't
250 // affect the status of ok.
251 #endif
252 bool FormRef::input( FL_OBJECT *, long data )
253 {
254         bool activate( true );
255         switch( data ) {
256         // goto reference / go back
257         case 1:
258         {
259                 toggle = static_cast<Goto>(toggle + 1);
260                 if ( toggle == GOFIRST ) toggle = GOREF;
261         
262                 switch (toggle) {
263                 case GOREF:
264                 {
265                         lv_->getLyXFunc()->
266                                 Dispatch(LFUN_REF_GOTO,
267                                          params.getContents());
268                         fl_set_object_label(dialog_->button_go, _("Go back"));
269                 }
270                 break;
271
272                 case GOBACK:
273                 {
274                         lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
275                         fl_set_object_label(dialog_->button_go, _("Goto reference"));
276                 }
277                 break;
278
279                 default:
280                         break;
281                 }
282         }
283         break;
284
285         // choose browser key
286         case 2:
287         {
288                 unsigned int sel = fl_get_browser( dialog_->browser );
289                 if ( sel < 1 || sel > refs.size() ) break;
290
291                 string s = fl_get_browser_line( dialog_->browser, sel );
292                 fl_set_input( dialog_->ref, s.c_str());
293                 params.setContents( s );
294
295                 toggle = GOBACK;
296                 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
297                 fl_set_object_label(dialog_->button_go, _("Goto reference"));
298
299                 fl_activate_object( dialog_->type );
300                 fl_set_object_lcol( dialog_->type, FL_BLACK );
301                 fl_activate_object( dialog_->button_go );
302                 fl_set_object_lcol( dialog_->button_go, FL_BLACK );
303                 fl_set_object_lcol( dialog_->ref, FL_BLACK );
304         }
305         break;
306
307         // update or sort
308         case 3:
309         {
310                 fl_freeze_form( form() );
311                 updateBrowser( refs );
312                 fl_unfreeze_form( form() );
313         }
314         break;
315
316         // changed reference type
317         case 4:
318         {
319                 Type type = static_cast<Type>( fl_get_choice(dialog_->type)-1 );
320                 if ( params.getCmdName() == getName( type )
321                     && inset_ ) {
322                         activate = false;
323                 }
324         }
325         break;
326
327         default:
328                 break;
329         }
330         return activate;
331 }
332
333
334 FormRef::Type FormRef::getType() const
335 {
336         Type type;
337
338         if ( params.getCmdName() == "ref" )
339                 type = REF;
340
341         else if ( params.getCmdName() == "pageref" )
342                 type = PAGEREF;
343
344         else if ( params.getCmdName() == "vref" )
345                 type = VREF;
346
347         else if ( params.getCmdName() == "vpageref" )
348                 type = VPAGEREF;
349
350         else
351                 type = PRETTYREF;
352         
353         return type;
354 }
355
356
357 string FormRef::getName( Type type ) const
358 {
359         string name;
360
361         switch( type ) {
362         case REF:
363                 name = "ref";
364                 break;
365         case PAGEREF:
366                 name = "pageref";
367                 break;
368         case VREF:
369                 name = "vref";
370                 break;
371         case VPAGEREF:
372                 name = "vpageref";
373                 break;
374         case PRETTYREF:
375                 name = "prettyref";
376                 break;
377         }
378         
379         return name;
380 }