]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
The forms resize patch, and small updates
[lyx.git] / src / frontends / xforms / FormCitation.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 #include <algorithm>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22
23 #include "Dialogs.h"
24 #include "FormCitation.h"
25 #include "LyXView.h"
26 #include "buffer.h"
27 #include "form_citation.h"
28 #include "lyxfunc.h"
29 #include "support/filetools.h"
30
31 using std::vector;
32 using std::pair;
33 using std::max;
34 using std::min;
35 using std::find;
36
37
38 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
39         : FormCommand(lv, d, _("Citation")), dialog_(0)
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->showCitation.connect(slot(this, &FormCitation::showInset));
45         d->createCitation.connect(slot(this, &FormCitation::createInset));
46 }
47
48
49 FormCitation::~FormCitation()
50 {
51         delete dialog_;
52 }
53
54
55 FL_FORM * FormCitation::form() const
56 {
57         if ( dialog_ ) return dialog_->form;
58         return 0;
59 }
60
61
62 void FormCitation::connect()
63 {
64         fl_set_form_maxsize( dialog_->form, 3*minw_, minh_ );
65         FormCommand::connect();
66 }
67
68
69 void FormCitation::disconnect()
70 {
71         citekeys.clear();
72         bibkeys.clear();
73         bibkeysInfo.clear();
74
75         FormCommand::disconnect();
76 }
77
78
79 void FormCitation::build()
80 {
81         dialog_ = build_citation();
82
83         // Workaround dumb xforms sizing bug
84         minw_ = form()->w;
85         minh_ = form()->h;
86 }
87
88
89 void FormCitation::update(bool switched)
90 {
91         if (switched) {
92                 hide();
93                 return;
94         }
95
96         bibkeys.clear();
97         bibkeysInfo.clear();
98
99         vector<pair<string,string> > blist =
100                 lv_->buffer()->getBibkeyList();
101
102         for( unsigned int i = 0; i < blist.size(); ++i ) {
103                 bibkeys.push_back(blist[i].first);
104                 bibkeysInfo.push_back(blist[i].second);
105         }
106         blist.clear();
107
108         citekeys.clear();
109         string tmp, keys( params.getContents() );
110         keys = frontStrip( split(keys, tmp, ',') );
111         while( !tmp.empty() ) {
112                 citekeys.push_back( tmp );
113                 keys = frontStrip( split(keys, tmp, ',') );
114         }
115
116         updateBrowser( dialog_->bibBrsr,  bibkeys );
117         updateBrowser( dialog_->citeBrsr, citekeys );
118         fl_clear_browser( dialog_->infoBrsr );
119
120         // No keys have been selected yet, so...
121         setBibButtons( OFF );
122         setCiteButtons( OFF );
123
124         int noKeys = static_cast<int>( max( bibkeys.size(), citekeys.size() ) );
125
126         // Place bounds, so that 4 <= noKeys <= 10
127         noKeys = max(4, min(10, noKeys) );
128
129         // Re-size the form to accommodate the new browser size
130         int size = 20 * noKeys;
131         bool bibPresent = ( bibkeys.size() > 0 );
132         setSize( size, bibPresent );
133
134         fl_set_input( dialog_->textAftr, params.getOptions().c_str());
135 }
136
137
138 void FormCitation::updateBrowser( FL_OBJECT * browser,
139                                   vector<string> const & keys ) const
140 {
141         fl_clear_browser( browser );
142
143         for( unsigned int i = 0; i < keys.size(); ++i )
144                 fl_add_browser_line( browser, keys[i].c_str());
145 }
146
147
148 void FormCitation::setBibButtons( State status ) const
149 {
150         switch (status) {
151         case ON:
152                 fl_activate_object( dialog_->addBtn );
153                 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
154                 break;
155
156         case OFF:
157                 fl_deactivate_object( dialog_->addBtn );
158                 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
159                 break;
160
161         default:
162                 break;
163         }
164 }
165
166
167 void FormCitation::setCiteButtons( State status ) const
168 {
169         switch( status ) {
170         case ON:
171         {
172                 fl_activate_object( dialog_->delBtn );
173                 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
174
175                 int sel = fl_get_browser( dialog_->citeBrsr );
176
177                 if ( sel != 1 ) {
178                         fl_activate_object( dialog_->upBtn );
179                         fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
180                 } else {
181                         fl_deactivate_object( dialog_->upBtn );
182                         fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
183                 }
184
185                 if ( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
186                         fl_activate_object( dialog_->downBtn );
187                         fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
188                 } else {
189                         fl_deactivate_object( dialog_->downBtn );
190                         fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
191                 }
192
193                 break;
194         }
195         case OFF:
196         {
197                 fl_deactivate_object( dialog_->delBtn );
198                 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
199
200                 fl_deactivate_object( dialog_->upBtn );
201                 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
202
203                 fl_deactivate_object( dialog_->downBtn );
204                 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
205         }
206         default:
207                 break;
208         }
209 }
210
211
212 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
213 {
214         bool const natbib = false; // will eventually be input
215         hbrsr = max( hbrsr, 175 ); // limit max size of cite/bib brsrs
216
217         // dh1, dh2, dh3 are the vertical separation between elements.
218         // These can be specified because the browser height is fixed
219         // so they are not changed by dynamic resizing
220         static int const dh1 = 30; // top of form to top of cite/bib brsrs;
221                                    // bottom of cite/bib brsrs to top of info;
222                                    // bottom of info to top next element;
223                                    // bottom of style to top textBefore;
224                                    // bottom of text to top ok/cancel buttons.
225         static int const dh2 = 10; // bottom of textBefore to top textAftr;
226                                    // bottom of ok/cancel buttons to bottom form
227         static int const dh3 = 5;  // spacing between add/delete/... buttons.
228
229         int const wbrsr  = dialog_->citeBrsr->w;
230         static int const hinfo  = dialog_->infoBrsr->h;
231         static int const hstyle = dialog_->style->h;
232         static int const htext  = dialog_->textAftr->h;
233         static int const hok    = dialog_->button_ok->h;
234
235         int hform = dh1 + hbrsr + dh1;
236         if ( bibPresent ) hform += hinfo + dh1;
237         if ( natbib ) hform += hstyle + dh1 + htext + dh2;
238         hform += htext + dh1 + hok + dh2;
239
240         if ( hform != minh_ ) {
241                 minh_ = hform;
242                 fl_set_form_size( dialog_->form, minw_, minh_ );
243         } else
244                 return;
245
246         int x = 0;
247         int y = 0;
248         fl_set_object_geometry( dialog_->box, x, y, minw_, minh_ );
249
250         x = dialog_->citeBrsr->x;
251         y += dh1; 
252         fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
253         x = dialog_->bibBrsr->x;
254         fl_set_object_geometry( dialog_->bibBrsr,  x, y, wbrsr, hbrsr );
255
256         x = dialog_->addBtn->x;
257         fl_set_object_position( dialog_->addBtn,  x, y );
258         y += dh3 + dialog_->addBtn->h;
259         fl_set_object_position( dialog_->delBtn,  x, y );
260         y += dh3 + dialog_->delBtn->h;
261         fl_set_object_position( dialog_->upBtn,   x, y );
262         y += dh3 + dialog_->upBtn->h;
263         fl_set_object_position( dialog_->downBtn, x, y );
264
265         y = dh1 + hbrsr + dh1; // in position for next element
266
267         if ( bibPresent ) {
268                 x = dialog_->infoBrsr->x;
269                 fl_set_object_position( dialog_->infoBrsr, x, y );
270                 fl_show_object( dialog_->infoBrsr );
271                 y += hinfo + dh1;
272         } else
273                 fl_hide_object( dialog_->infoBrsr );
274
275         if ( natbib ) {
276                 x = dialog_->style->x;
277                 fl_set_object_position( dialog_->style, x, y );
278                 fl_show_object( dialog_->style );
279                 x = dialog_->textBefore->x;
280                 y += hstyle + dh1;
281                 fl_set_object_position( dialog_->textBefore, x, y );
282                 fl_show_object( dialog_->textBefore );
283                 y += htext + dh2;
284         } else {
285                 fl_hide_object( dialog_->style );
286                 fl_hide_object( dialog_->textBefore );
287         }
288
289         x = dialog_->textAftr->x;
290         fl_set_object_position( dialog_->textAftr, x, y );
291
292         x = dialog_->button_ok->x;
293         y += htext + dh1;
294         fl_set_object_position( dialog_->button_ok,       x, y );
295         x = dialog_->button_cancel->x;
296         fl_set_object_position( dialog_->button_cancel,   x, y );
297 }
298
299
300 #ifdef WITH_WARNINGS
301 #warning convert this to use the buttoncontroller
302 #endif
303 bool FormCitation::input( FL_OBJECT *, long data )
304 {
305         State cb = static_cast<State>( data );
306
307         switch( cb ) {
308         case BIBBRSR:
309         {
310                 fl_deselect_browser( dialog_->citeBrsr );
311                 
312                 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
313                 if ( sel < 1 || sel > bibkeys.size() ) break;
314
315                 // Put into infoBrsr the additional info associated with
316                 // the selected bibBrsr key
317                 fl_clear_browser( dialog_->infoBrsr );
318                 fl_add_browser_line( dialog_->infoBrsr,
319                                      bibkeysInfo[sel - 1].c_str() );
320
321                 // Highlight the selected bibBrsr key in citeBrsr if present
322                 vector<string>::iterator it =
323                         find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
324
325                 if ( it != citekeys.end() ) {
326                         int n = static_cast<int>( it - citekeys.begin() );
327                         fl_select_browser_line( dialog_->citeBrsr, n+1 );
328                         fl_set_browser_topline( dialog_->citeBrsr, n+1 );
329                 }
330
331                 if ( !lv_->buffer()->isReadonly() ) {
332                         if ( it != citekeys.end() ) {
333                                 setBibButtons( OFF );
334                                 setCiteButtons( ON );
335                         } else {
336                                 setBibButtons( ON );
337                                 setCiteButtons( OFF );
338                         }
339                 }
340         }
341         break;
342         case CITEBRSR:
343         {
344                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
345                 if ( sel < 1 || sel > citekeys.size() ) break;
346
347                 if ( !lv_->buffer()->isReadonly() ) {
348                         setBibButtons( OFF );
349                         setCiteButtons( ON );
350                 }
351
352                 // Highlight the selected citeBrsr key in bibBrsr
353                 vector<string>::iterator it =
354                         find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
355
356                 if (it != bibkeys.end()) {
357                         int n = static_cast<int>( it - bibkeys.begin() );
358                         fl_select_browser_line( dialog_->bibBrsr, n+1 );
359                         fl_set_browser_topline( dialog_->bibBrsr, n+1 );
360
361                         // Put into infoBrsr the additional info associated with
362                         // the selected citeBrsr key
363                         fl_clear_browser( dialog_->infoBrsr );
364                         fl_add_browser_line( dialog_->infoBrsr,
365                                              bibkeysInfo[n].c_str() );
366                 }
367         }
368         break;
369         case ADD:
370         {
371                 if ( lv_->buffer()->isReadonly() ) break;
372
373                 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
374                 if ( sel < 1 || sel > bibkeys.size() ) break;
375
376                 // Add the selected bibBrsr key to citeBrsr
377                 fl_addto_browser( dialog_->citeBrsr,
378                                   bibkeys[sel-1].c_str() );
379                 citekeys.push_back( bibkeys[sel-1] );
380
381                 int n = static_cast<int>( citekeys.size() );
382                 fl_select_browser_line( dialog_->citeBrsr, n );
383
384                 setBibButtons( OFF );
385                 setCiteButtons( ON );
386         }
387         break;
388         case DELETE:
389         {
390                 if ( lv_->buffer()->isReadonly() ) break;
391
392                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
393                 if ( sel < 1 || sel > citekeys.size() ) break;
394
395                 // Remove the selected key from citeBrsr
396                 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
397                 citekeys.erase( citekeys.begin() + sel-1 );
398
399                 setBibButtons( ON );
400                 setCiteButtons( OFF );
401         }
402         break;
403         case UP:
404         {
405                 if ( lv_->buffer()->isReadonly() ) break;
406
407                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
408                 if ( sel < 2 || sel > citekeys.size() ) break;
409
410                 // Move the selected key up one line
411                 vector<string>::iterator it = citekeys.begin() + sel-1;
412                 string tmp = *it;
413
414                 fl_delete_browser_line( dialog_->citeBrsr, sel );
415                 citekeys.erase( it );
416
417                 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
418                 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
419                 citekeys.insert( it-1, tmp );
420                 setCiteButtons( ON );
421         }
422         break;
423         case DOWN:
424         {
425                 if ( lv_->buffer()->isReadonly() ) break;
426
427                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
428                 if ( sel < 1 || sel > citekeys.size()-1 ) break;
429
430                 // Move the selected key down one line
431                 vector<string>::iterator it = citekeys.begin() + sel-1;
432                 string tmp = *it;
433
434                 fl_delete_browser_line( dialog_->citeBrsr, sel );
435                 citekeys.erase( it );
436
437                 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
438                 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
439                 citekeys.insert( it+1, tmp );
440                 setCiteButtons( ON );
441         }
442         break;
443         default:
444                 break;
445         }
446         return true;
447 }
448
449
450 void FormCitation::apply()
451 {
452         if (lv_->buffer()->isReadonly()) return;
453
454         string contents;
455         for(unsigned int i = 0; i < citekeys.size(); ++i) {
456                 if (i > 0) contents += ", ";
457                 contents += citekeys[i];
458         }
459
460         params.setContents(contents);
461         params.setOptions(fl_get_input(dialog_->textAftr));
462
463         if (inset_ != 0) {
464                 // Only update if contents have changed
465                 if (params != inset_->params()) {
466                         inset_->setParams(params);
467                         lv_->view()->updateInset(inset_, true);
468                 }
469         } else {
470                 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
471                                             params.getAsString());
472         }
473 }