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