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