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