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