]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
More pref work from Angus
[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 #include <algorithm>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19
20 #include "Dialogs.h"
21 #include "FormCitation.h"
22 #include "LyXView.h"
23 #include "buffer.h"
24 #include "form_citation.h"
25 #include "lyxfunc.h"
26 #include "support/filetools.h"
27
28 using std::vector;
29 using std::pair;
30 using std::max;
31 using std::min;
32 using std::find;
33
34
35 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
36         : FormCommand(lv, d, _("Citation"), new OkApplyCancelReadOnlyPolicy),
37           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->showCitation.connect(slot(this, &FormCitation::showInset));
43         d->createCitation.connect(slot(this, &FormCitation::createInset));
44 }
45
46
47 FormCitation::~FormCitation()
48 {
49         delete dialog_;
50 }
51
52
53 FL_FORM * FormCitation::form() const
54 {
55         if ( dialog_ ) return dialog_->form;
56         return 0;
57 }
58
59
60 void FormCitation::connect()
61 {
62         fl_set_form_maxsize( dialog_->form, 3*minw_, minh_ );
63         FormCommand::connect();
64 }
65
66
67 void FormCitation::disconnect()
68 {
69         citekeys.clear();
70         bibkeys.clear();
71         bibkeysInfo.clear();
72
73         FormCommand::disconnect();
74 }
75
76
77 void FormCitation::build()
78 {
79         dialog_ = build_citation();
80
81         // Workaround dumb xforms sizing bug
82         minw_ = form()->w;
83         minh_ = form()->h;
84
85         // manage the ok, apply and cancel/close buttons
86         bc_.setOK(dialog_->button_ok);
87         bc_.setApply(dialog_->button_apply);
88         bc_.setCancel(dialog_->button_cancel);
89         bc_.setUndoAll(dialog_->button_restore);
90         bc_.refresh();
91 }
92
93
94 void FormCitation::update()
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         y += htext + dh1;
293         x = dialog_->button_restore->x;
294         fl_set_object_position( dialog_->button_restore,     x, y );
295         x = dialog_->button_ok->x;
296         fl_set_object_position( dialog_->button_ok,     x, y );
297         x = dialog_->button_apply->x;
298         fl_set_object_position( dialog_->button_apply,  x, y );
299         x = dialog_->button_cancel->x;
300         fl_set_object_position( dialog_->button_cancel, x, y );
301 }
302
303
304 #ifdef WITH_WARNINGS
305 #warning convert this to use the buttoncontroller
306 #endif
307 bool FormCitation::input( FL_OBJECT *, long data )
308 {
309         bool activate = false;
310         State cb = static_cast<State>( data );
311
312         switch( cb ) {
313         case BIBBRSR:
314         {
315                 fl_deselect_browser( dialog_->citeBrsr );
316                 
317                 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
318                 if ( sel < 1 || sel > bibkeys.size() ) break;
319
320                 // Put into infoBrsr the additional info associated with
321                 // the selected bibBrsr key
322                 fl_clear_browser( dialog_->infoBrsr );
323                 fl_add_browser_line( dialog_->infoBrsr,
324                                      bibkeysInfo[sel - 1].c_str() );
325
326                 // Highlight the selected bibBrsr key in citeBrsr if present
327                 vector<string>::iterator it =
328                         find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
329
330                 if ( it != citekeys.end() ) {
331                         int n = static_cast<int>( it - citekeys.begin() );
332                         fl_select_browser_line( dialog_->citeBrsr, n+1 );
333                         fl_set_browser_topline( dialog_->citeBrsr, n+1 );
334                 }
335
336                 if ( !lv_->buffer()->isReadonly() ) {
337                         if ( it != citekeys.end() ) {
338                                 setBibButtons( OFF );
339                                 setCiteButtons( ON );
340                         } else {
341                                 setBibButtons( ON );
342                                 setCiteButtons( OFF );
343                         }
344                 }
345         }
346         break;
347         case CITEBRSR:
348         {
349                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
350                 if ( sel < 1 || sel > citekeys.size() ) break;
351
352                 if ( !lv_->buffer()->isReadonly() ) {
353                         setBibButtons( OFF );
354                         setCiteButtons( ON );
355                 }
356
357                 // Highlight the selected citeBrsr key in bibBrsr
358                 vector<string>::iterator it =
359                         find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
360
361                 if (it != bibkeys.end()) {
362                         int n = static_cast<int>( it - bibkeys.begin() );
363                         fl_select_browser_line( dialog_->bibBrsr, n+1 );
364                         fl_set_browser_topline( dialog_->bibBrsr, n+1 );
365
366                         // Put into infoBrsr the additional info associated with
367                         // the selected citeBrsr key
368                         fl_clear_browser( dialog_->infoBrsr );
369                         fl_add_browser_line( dialog_->infoBrsr,
370                                              bibkeysInfo[n].c_str() );
371                 }
372         }
373         break;
374         case ADD:
375         {
376                 if ( lv_->buffer()->isReadonly() ) break;
377
378                 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
379                 if ( sel < 1 || sel > bibkeys.size() ) break;
380
381                 // Add the selected bibBrsr key to citeBrsr
382                 fl_addto_browser( dialog_->citeBrsr,
383                                   bibkeys[sel-1].c_str() );
384                 citekeys.push_back( bibkeys[sel-1] );
385
386                 int n = static_cast<int>( citekeys.size() );
387                 fl_select_browser_line( dialog_->citeBrsr, n );
388
389                 setBibButtons( OFF );
390                 setCiteButtons( ON );
391                 activate = true;
392         }
393         break;
394         case DELETE:
395         {
396                 if ( lv_->buffer()->isReadonly() ) break;
397
398                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
399                 if ( sel < 1 || sel > citekeys.size() ) break;
400
401                 // Remove the selected key from citeBrsr
402                 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
403                 citekeys.erase( citekeys.begin() + sel-1 );
404
405                 setBibButtons( ON );
406                 setCiteButtons( OFF );
407                 activate = true;
408         }
409         break;
410         case UP:
411         {
412                 if ( lv_->buffer()->isReadonly() ) break;
413
414                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
415                 if ( sel < 2 || sel > citekeys.size() ) break;
416
417                 // Move the selected key up one line
418                 vector<string>::iterator it = citekeys.begin() + sel-1;
419                 string tmp = *it;
420
421                 fl_delete_browser_line( dialog_->citeBrsr, sel );
422                 citekeys.erase( it );
423
424                 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
425                 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
426                 citekeys.insert( it-1, tmp );
427                 setCiteButtons( ON );
428                 activate = true;
429         }
430         break;
431         case DOWN:
432         {
433                 if ( lv_->buffer()->isReadonly() ) break;
434
435                 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
436                 if ( sel < 1 || sel > citekeys.size()-1 ) break;
437
438                 // Move the selected key down one line
439                 vector<string>::iterator it = citekeys.begin() + sel-1;
440                 string tmp = *it;
441
442                 fl_delete_browser_line( dialog_->citeBrsr, sel );
443                 citekeys.erase( it );
444
445                 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
446                 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
447                 citekeys.insert( it+1, tmp );
448                 setCiteButtons( ON );
449                 activate = true;
450         }
451         break;
452         default:
453                 break;
454         }
455         return activate;
456 }
457
458
459 void FormCitation::apply()
460 {
461         if (lv_->buffer()->isReadonly()) return;
462
463         string contents;
464         for(unsigned int i = 0; i < citekeys.size(); ++i) {
465                 if (i > 0) contents += ", ";
466                 contents += citekeys[i];
467         }
468
469         params.setContents(contents);
470         params.setOptions(fl_get_input(dialog_->textAftr));
471
472         if (inset_ != 0) {
473                 // Only update if contents have changed
474                 if (params != inset_->params()) {
475                         inset_->setParams(params);
476                         lv_->view()->updateInset(inset_, true);
477                 }
478         } else {
479                 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
480                                             params.getAsString());
481         }
482 }