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