]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
implement getLabelList
[lyx.git] / src / frontends / xforms / FormCitation.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file FormCitation.C
11  * \author Angus Leeming, a.leeming@ic.ac.uk
12  */
13
14 #include <config.h>
15 #include <algorithm>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "xformsBC.h"
22 #include "ControlCitation.h"
23 #include "FormCitation.h"
24 #include "form_citation.h"
25 #include "gettext.h"
26 #include "support/lstrings.h"
27 #include "biblio.h"
28 #include "helper_funcs.h"
29 #include "xforms_helpers.h"
30
31 using std::find;
32 using std::max;
33 using std::min;
34 using std::pair;
35 using std::sort;
36 using std::vector;
37
38 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
39
40 FormCitation::FormCitation(ControlCitation & c)
41         : base_class(c, _("Citation"))
42 {}
43
44
45 void FormCitation::apply()
46 {
47         controller().params().setCmdName("cite");
48         controller().params().setContents(getStringFromVector(citekeys));
49
50         string const after  = fl_get_input(dialog_->input_after);
51         controller().params().setOptions(after);
52 }
53
54
55 void FormCitation::hide()
56 {
57         citekeys.clear();
58         bibkeys.clear();
59
60         FormBase::hide();
61 }
62
63
64 void FormCitation::build()
65 {
66         dialog_.reset(build_citation());
67
68         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
69         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
70
71         fl_set_button(dialog_->button_search_case, 0);
72         fl_set_button(dialog_->button_search_type, 0);
73
74         // Manage the ok, apply, restore and cancel/close buttons
75         bc().setOK(dialog_->button_ok);
76         bc().setApply(dialog_->button_apply);
77         bc().setCancel(dialog_->button_cancel);
78         bc().setRestore(dialog_->button_restore);
79
80         bc().addReadOnly(dialog_->button_add);
81         bc().addReadOnly(dialog_->button_del);
82         bc().addReadOnly(dialog_->button_up);
83         bc().addReadOnly(dialog_->button_down);
84         bc().addReadOnly(dialog_->choice_style);
85         bc().addReadOnly(dialog_->input_before);
86         bc().addReadOnly(dialog_->input_after);
87
88 }
89
90
91 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
92 {
93         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
94
95         biblio::InfoMap const & theMap = controller().bibkeysInfo();
96
97         if (ob == dialog_->browser_bib) {
98                 fl_deselect_browser(dialog_->browser_cite);
99
100                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
101                 if (sel < 1 || sel > bibkeys.size())
102                         return ButtonPolicy::SMI_NOOP;
103
104                 // Put into browser_info the additional info associated with
105                 // the selected browser_bib key
106                 fl_clear_browser(dialog_->browser_info);
107
108                 string const tmp = formatted(biblio::getInfo(theMap,
109                                                              bibkeys[sel-1]),
110                                               dialog_->browser_info->w-10 );
111                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
112
113                 // Highlight the selected browser_bib key in browser_cite if
114                 // present
115                 vector<string>::const_iterator cit =
116                         find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
117
118                 if (cit != citekeys.end()) {
119                         int const n = int(cit - citekeys.begin());
120                         fl_select_browser_line(dialog_->browser_cite, n+1);
121                         fl_set_browser_topline(dialog_->browser_cite, n+1);
122                 }
123
124                 if (!controller().isReadonly()) {
125                         if (cit != citekeys.end()) {
126                                 setBibButtons(OFF);
127                                 setCiteButtons(ON);
128                         } else {
129                                 setBibButtons(ON);
130                                 setCiteButtons(OFF);
131                         }
132                 }
133
134         } else if (ob == dialog_->browser_cite) {
135                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
136                 if (sel < 1 || sel > citekeys.size())
137                         return ButtonPolicy::SMI_NOOP;
138
139                 if (!controller().isReadonly()) {
140                         setBibButtons(OFF);
141                         setCiteButtons(ON);
142                 }
143
144                 // Highlight the selected browser_cite key in browser_bib
145                 vector<string>::const_iterator cit =
146                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
147
148                 if (cit != bibkeys.end()) {
149                         int const n = int(cit - bibkeys.begin());
150                         fl_select_browser_line(dialog_->browser_bib, n+1);
151                         fl_set_browser_topline(dialog_->browser_bib, n+1);
152
153                         // Put into browser_info the additional info associated
154                         // with the selected browser_cite key
155                         fl_clear_browser(dialog_->browser_info);
156                         string const tmp =
157                                 formatted(biblio::getInfo(theMap,
158                                                           bibkeys[sel-1]),
159                                           dialog_->browser_info->w-10);
160                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
161                 }
162
163         } else if (ob == dialog_->button_add) {
164                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
165                 if (sel < 1 || sel > bibkeys.size())
166                         return ButtonPolicy::SMI_NOOP;
167
168                 // Add the selected browser_bib key to browser_cite
169                 fl_addto_browser(dialog_->browser_cite,
170                                   bibkeys[sel-1].c_str());
171                 citekeys.push_back(bibkeys[sel-1]);
172
173                 int const n = int(citekeys.size());
174                 fl_select_browser_line(dialog_->browser_cite, n);
175
176                 setBibButtons(OFF);
177                 setCiteButtons(ON);
178                 activate = ButtonPolicy::SMI_VALID;
179
180         } else if (ob == dialog_->button_del) {
181                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
182                 if (sel < 1 || sel > citekeys.size())
183                         return ButtonPolicy::SMI_NOOP;
184
185                 // Remove the selected key from browser_cite
186                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
187                 citekeys.erase(citekeys.begin() + sel-1);
188
189                 setBibButtons(ON);
190                 setCiteButtons(OFF);
191                 activate = ButtonPolicy::SMI_VALID;
192
193         } else if (ob == dialog_->button_up) {
194                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
195                 if (sel < 2 || sel > citekeys.size())
196                         return ButtonPolicy::SMI_NOOP;
197
198                 // Move the selected key up one line
199                 vector<string>::iterator it = citekeys.begin() + sel-1;
200                 string const tmp = *it;
201
202                 fl_delete_browser_line(dialog_->browser_cite, sel);
203                 citekeys.erase(it);
204
205                 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
206                 fl_select_browser_line(dialog_->browser_cite, sel-1);
207                 citekeys.insert(it-1, tmp);
208                 setCiteButtons(ON);
209                 activate = ButtonPolicy::SMI_VALID;
210
211         } else if (ob == dialog_->button_down) {
212                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
213                 if (sel < 1 || sel > citekeys.size()-1)
214                         return ButtonPolicy::SMI_NOOP;
215
216                 // Move the selected key down one line
217                 vector<string>::iterator it = citekeys.begin() + sel-1;
218                 string const tmp = *it;
219
220                 fl_delete_browser_line(dialog_->browser_cite, sel);
221                 citekeys.erase(it);
222
223                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
224                 fl_select_browser_line(dialog_->browser_cite, sel+1);
225                 citekeys.insert(it+1, tmp);
226                 setCiteButtons(ON);
227                 activate = ButtonPolicy::SMI_VALID;
228
229         } else if (ob == dialog_->button_previous ||
230                    ob == dialog_->button_next) {
231
232                 string const str = fl_get_input(dialog_->input_search);
233
234                 biblio::Direction const dir =
235                         (ob == dialog_->button_previous) ?
236                         biblio::BACKWARD : biblio::FORWARD;
237
238                 biblio::Search const type =
239                         fl_get_button(dialog_->button_search_type) ?
240                         biblio::REGEX : biblio::SIMPLE;
241
242                 vector<string>::const_iterator start = bibkeys.begin();
243                 int const sel = fl_get_browser(dialog_->browser_bib);
244                 if (sel >= 1 && sel <= int(bibkeys.size()))
245                         start += sel-1;
246
247                 // Find the NEXT instance...
248                 if (dir == biblio::FORWARD)
249                         start += 1;
250                 else
251                         start -= 1;
252
253                 bool const caseSensitive =
254                         fl_get_button(dialog_->button_search_case);
255
256                 vector<string>::const_iterator const cit =
257                         biblio::searchKeys(theMap, bibkeys, str,
258                                            start, type, dir, caseSensitive);
259
260                 if (cit == bibkeys.end())
261                         return ButtonPolicy::SMI_NOOP;
262
263                 int const found = int(cit - bibkeys.begin()) + 1;
264                 if (found == sel)
265                         return ButtonPolicy::SMI_NOOP;
266
267                 // Update the display
268                 int const top = max(found-5, 1);
269                 fl_set_browser_topline(dialog_->browser_bib, top);
270                 fl_select_browser_line(dialog_->browser_bib, found);
271                 input(dialog_->browser_bib, 0);
272
273         } else if (ob == dialog_->choice_style ||
274                    ob == dialog_->input_before ||
275                    ob == dialog_->input_after) {
276                 activate = ButtonPolicy::SMI_VALID;
277         }
278
279         return activate;
280 }
281
282
283 void FormCitation::update()
284 {
285         // Make the list of all available bibliography keys
286         bibkeys = biblio::getKeys(controller().bibkeysInfo());
287         updateBrowser(dialog_->browser_bib, bibkeys);
288
289         // Ditto for the keys cited in this inset
290         citekeys = getVectorFromString(controller().params().getContents());
291         updateBrowser(dialog_->browser_cite, citekeys);
292
293         // No keys have been selected yet, so...
294         fl_clear_browser(dialog_->browser_info);
295         setBibButtons(OFF);
296         setCiteButtons(OFF);
297
298         int noKeys = int(max(bibkeys.size(), citekeys.size()));
299
300         // Place bounds, so that 4 <= noKeys <= 10
301         noKeys = max(4, min(10, noKeys));
302
303         // Re-size the form to accommodate the new browser size
304         int const size = 20 * noKeys;
305         bool const bibPresent = (bibkeys.size() > 0);
306         setSize(size, bibPresent);
307
308         fl_set_input(dialog_->input_after,
309                      controller().params().getOptions().c_str());
310 }
311
312
313 void FormCitation::updateBrowser(FL_OBJECT * browser,
314                                  vector<string> const & keys) const
315 {
316         fl_clear_browser(browser);
317
318         for (vector<string>::const_iterator it = keys.begin();
319              it < keys.end(); ++it) {
320                 string key = frontStrip(strip(*it));
321                 if (!key.empty())
322                         fl_add_browser_line(browser, key.c_str());
323         }
324 }
325
326
327 void FormCitation::setBibButtons(State status) const
328 {
329         setEnabled(dialog_->button_add, (status == ON));
330 }
331
332
333 void FormCitation::setCiteButtons(State status) const
334 {
335         int const sel     = fl_get_browser(dialog_->browser_cite);
336         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
337         bool const activate      = (status == ON);
338         bool const activate_up   = (activate && sel != 1);
339         bool const activate_down = (activate && sel != maxline);
340
341         setEnabled(dialog_->button_del,  activate);
342         setEnabled(dialog_->button_up,   activate_up);
343         setEnabled(dialog_->button_down, activate_down);
344 }
345
346
347 void FormCitation::setSize(int hbrsr, bool bibPresent) const
348 {
349         bool const natbib = false; // will eventually be input
350         hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
351
352         // dh1, dh2, dh3 are the vertical separation between elements.
353         // These can be specified because the browser height is fixed
354         // so they are not changed by dynamic resizing
355         static int const dh1 = 30; // top of form to top of cite/bib brsrs;
356                                    // bottom of cite/bib brsrs to top of info;
357                                    // bottom of info to top search frame;
358                                    // bottom of search frame to top next elemnt;
359                                    // bottom of style to top input_before;
360                                    // bottom of text to top ok/cancel buttons.
361         static int const dh2 = 10; // bottom of input_before to top input_after;
362                                    // bottom of ok/cancel buttons to bottom form
363         static int const dh3 = 5;  // spacing between add/delete/... buttons.
364
365         int const wbrsr  = dialog_->browser_cite->w;
366         static int const hinfo  = dialog_->browser_info->h;
367         static int const hframe = dialog_->frame_search->h;
368         static int const hstyle = dialog_->choice_style->h;
369         static int const htext  = dialog_->input_after->h;
370         static int const hok    = dialog_->button_ok->h;
371
372         int hform = dh1 + hbrsr + dh1 + hframe + dh1;
373         if (bibPresent) hform += hinfo + dh1;
374         if (natbib) hform += hstyle + dh1 + htext + dh2;
375         hform += htext + dh1 + hok + dh2;
376
377         if (hform != minh_) {
378                 minh_ = hform;
379                 fl_set_form_size(dialog_->form, minw_, minh_);
380         } else
381                 return;
382
383         int x = 0;
384         int y = 0;
385         fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
386
387         x = dialog_->browser_cite->x;
388         y += dh1; 
389         fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
390         x = dialog_->browser_bib->x;
391         fl_set_object_geometry(dialog_->browser_bib,  x, y, wbrsr, hbrsr);
392
393         x = dialog_->button_add->x;
394         fl_set_object_position(dialog_->button_add,  x, y);
395         y += dh3 + dialog_->button_add->h;
396         fl_set_object_position(dialog_->button_del,  x, y);
397         y += dh3 + dialog_->button_del->h;
398         fl_set_object_position(dialog_->button_up,   x, y);
399         y += dh3 + dialog_->button_up->h;
400         fl_set_object_position(dialog_->button_down, x, y);
401
402         y = dh1 + hbrsr + dh1; // in position for next element
403
404         if (bibPresent) {
405                 x = dialog_->browser_info->x;
406                 fl_set_object_position(dialog_->browser_info, x, y);
407                 fl_show_object(dialog_->browser_info);
408                 y += hinfo + dh1;
409         } else
410                 fl_hide_object(dialog_->browser_info);
411
412         x = dialog_->frame_search->x;
413         // ??? The frame height seems to be reduced. Use geometry to enforce it.
414         fl_set_object_geometry(dialog_->frame_search, x, y,
415                                dialog_->frame_search->w, hframe);
416         //fl_set_object_position(dialog_->frame_search, x, y);
417
418         x = dialog_->input_search->x;
419         y += 15;
420         fl_set_object_position(dialog_->input_search, x, y);
421
422         x = dialog_->button_previous->x;
423         y += dialog_->input_search->h + 5;
424         fl_set_object_position(dialog_->button_previous, x, y);
425
426         x = dialog_->button_next->x;
427         y += dialog_->button_previous->h + 5;
428         fl_set_object_position(dialog_->button_next, x, y);
429
430         x = dialog_->button_search_type->x;
431         y = dialog_->button_previous->y;
432         fl_set_object_position(dialog_->button_search_type, x, y);
433
434         x = dialog_->button_search_case->x;
435         y = dialog_->button_next->y;
436         fl_set_object_position(dialog_->button_search_case, x, y);
437
438         y = dialog_->frame_search->y + hframe + dh1;
439         
440         if (natbib) {
441                 x = dialog_->choice_style->x;
442                 fl_set_object_position(dialog_->choice_style, x, y);
443                 fl_show_object(dialog_->choice_style);
444                 x = dialog_->input_before->x;
445                 y += hstyle + dh1;
446                 fl_set_object_position(dialog_->input_before, x, y);
447                 fl_show_object(dialog_->input_before);
448                 y += htext + dh2;
449         } else {
450                 fl_hide_object(dialog_->choice_style);
451                 fl_hide_object(dialog_->input_before);
452         }
453
454         x = dialog_->input_after->x;
455         fl_set_object_position(dialog_->input_after, x, y);
456
457         y += htext + dh1;
458         x = dialog_->button_restore->x;
459         fl_set_object_position(dialog_->button_restore, x, y);
460         x = dialog_->button_ok->x;
461         fl_set_object_position(dialog_->button_ok, x, y);
462         x = dialog_->button_apply->x;
463         fl_set_object_position(dialog_->button_apply, x, y);
464         x = dialog_->button_cancel->x;
465         fl_set_object_position(dialog_->button_cancel, x, y);
466 }