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