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