]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
Fix crash.
[lyx.git] / src / frontends / xforms / FormCitation.C
1 /**
2  * \file xforms/FormCitation.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Rob Lahaye
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13 #include <algorithm>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "xformsBC.h"
20 #include "ControlCitation.h"
21 #include "FormCitation.h"
22 #include "forms/form_citation.h"
23 #include "Tooltips.h"
24 #include "helper_funcs.h"
25 #include "xforms_helpers.h"
26 #include FORMS_H_LOCATION
27
28 #include "gettext.h"
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
31
32 using std::find;
33 using std::max;
34 using std::min;
35 using std::pair;
36 using std::sort;
37 using std::vector;
38
39 namespace {
40
41 // shamelessly stolen from Menubar_pimpl.C
42 int string_width(string const & str)
43 {
44         return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
45                                       str.c_str(),
46                                       static_cast<int>(str.length()));
47 }
48
49
50 void fillChoice(FD_citation * dialog, vector<string> vec)
51 {
52         // Check whether the current contents of the browser will be
53         // changed by loading the contents of the vec...
54         vector<string> const choice_style =
55                 getVector(dialog->choice_style);
56
57         if (vec == choice_style)
58                 return;
59
60         // They will be changed. Proceed
61         bool const noVec = vec.empty();
62         string const str = noVec ? string() : getStringFromVector(vec, "|");
63
64         fl_clear_choice(dialog->choice_style);
65         fl_addto_choice(dialog->choice_style, str.c_str());
66         setEnabled(dialog->choice_style, !noVec);
67 }
68
69
70 void updateStyle(FD_citation * dialog, string command)
71 {
72         // Find the style of the citekeys
73         vector<biblio::CiteStyle> const & styles =
74                 ControlCitation::getCiteStyles();
75         biblio::CitationStyle cs = biblio::getCitationStyle(command);
76
77         vector<biblio::CiteStyle>::const_iterator cit =
78                 find(styles.begin(), styles.end(), cs.style);
79
80         // Use this to initialise the GUI
81         bool const noStyles = cit == styles.end();
82         int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
83         fl_set_choice(dialog->choice_style, index);
84         
85         // Disable if there are no styles, otherwise use cs member settings.
86         fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
87         fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
88 }
89
90 } // namespace anon
91
92
93 typedef FormCB<ControlCitation, FormDB<FD_citation> > base_class;
94
95
96 FormCitation::FormCitation()
97         : base_class(_("Citation"))
98 {}
99
100
101 void FormCitation::apply()
102 {
103         string command = "cite";
104         if (isActive(dialog_->choice_style)) {
105                 vector<biblio::CiteStyle> const & styles =
106                         ControlCitation::getCiteStyles();
107
108                 int const choice =
109                         std::max(0, fl_get_choice(dialog_->choice_style) - 1);
110                 bool const full  =
111                         fl_get_button(dialog_->check_full_author_list);
112                 bool const force =
113                         fl_get_button(dialog_->check_force_uppercase);
114
115                 command = biblio::getCiteCommand(styles[choice], full, force);
116         }
117
118         controller().params().setCmdName(command);
119         controller().params().setContents(getStringFromVector(citekeys));
120
121         string const after  = getString(dialog_->input_after);
122         controller().params().setOptions(after);
123 }
124
125
126 void FormCitation::hide()
127 {
128         citekeys.clear();
129         bibkeys.clear();
130
131         FormBase::hide();
132 }
133
134
135 void FormCitation::build()
136 {
137         dialog_.reset(build_citation(this));
138
139         // Manage the ok, apply, restore and cancel/close buttons
140         bc().setOK(dialog_->button_ok);
141         bc().setApply(dialog_->button_apply);
142         bc().setCancel(dialog_->button_close);
143         bc().setRestore(dialog_->button_restore);
144
145         // disable for read-only documents
146         bc().addReadOnly(dialog_->button_add);
147         bc().addReadOnly(dialog_->button_del);
148         bc().addReadOnly(dialog_->button_up);
149         bc().addReadOnly(dialog_->button_down);
150         bc().addReadOnly(dialog_->choice_style);
151         bc().addReadOnly(dialog_->input_before);
152         bc().addReadOnly(dialog_->input_after);
153         bc().addReadOnly(dialog_->check_full_author_list);
154         bc().addReadOnly(dialog_->check_force_uppercase);
155
156         // trigger an input event for cut&paste with middle mouse button.
157         setPrehandler(dialog_->input_search);
158         setPrehandler(dialog_->input_before);
159         setPrehandler(dialog_->input_after);
160
161         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
162         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
163         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
164
165         //set up the tooltip mechanism
166         string str = _("Add the selected entry to the current citation reference.");
167         tooltips().init(dialog_->button_add, str);
168
169         str = _("Delete the selected entry from the current citation reference.");
170         tooltips().init(dialog_->button_del, str);
171
172         str = _("Move the selected entry upwards (in the current list).");
173         tooltips().init(dialog_->button_up, str);
174
175         str = _("Move the selected entry downwards (in the current list).");
176         tooltips().init(dialog_->button_down, str);
177
178         str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
179         tooltips().init(dialog_->browser_cite, str);
180
181         str = _("All entries in the database you have loaded (via \"Insert->Lists&TOC->BibTex Reference\"). Move the ones you want to cite with the arrow buttons into the left browser window.");
182         tooltips().init(dialog_->browser_bib, str);
183
184         str = _("Information about the selected entry");
185         tooltips().init(dialog_->browser_info, str);
186
187         str = _("Here you may select how the citation label should look inside the text (Natbib).");
188         tooltips().init(dialog_->choice_style, str);
189
190         str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
191         tooltips().init(dialog_->check_full_author_list, str);
192
193         str = _("Activate if you want to print the first character of the author name as uppercase (\"Van Gogh\", not \"van Gogh\"). Useful at the beginning of sentences (Natbib).");
194         tooltips().init(dialog_->check_force_uppercase, str);
195
196         str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
197         tooltips().init(dialog_->input_before, str);
198
199         str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
200         tooltips().init(dialog_->input_after, str);
201
202         str = _("Search your database (all fields will be searched).");
203         tooltips().init(dialog_->input_search, str);
204
205         str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
206         tooltips().init(dialog_->check_search_case, str);
207
208         str = _("Activate if you want to enter Regular Expressions.");
209         tooltips().init(dialog_->check_search_type, str);
210 }
211
212
213 void FormCitation::findBiblio(biblio::Direction const dir)
214 {
215         string const str = getString(dialog_->input_search);
216         biblio::InfoMap const & theMap = controller().bibkeysInfo();
217         bool const caseSensitive =
218                 fl_get_button(dialog_->check_search_case);
219         biblio::Search const type =
220                 fl_get_button(dialog_->check_search_type) ?
221                 biblio::REGEX : biblio::SIMPLE;
222
223         vector<string>::const_iterator start = bibkeys.begin();
224         int const sel = fl_get_browser(dialog_->browser_bib);
225         if (sel >= 1 && sel <= int(bibkeys.size()))
226                 start += sel - 1;
227
228         // Find the NEXT instance...
229         (dir == biblio::FORWARD) ? ++start : --start;
230
231
232         vector<string>::const_iterator const cit =
233                 biblio::searchKeys(theMap, bibkeys, str,
234                                    start, type, dir, caseSensitive);
235
236         if (cit == bibkeys.end())
237                 return;
238
239         int const found = int(cit - bibkeys.begin()) + 1;
240         if (found == sel)
241                 return;
242
243         // Update the display
244         int const top = max(found - 5, 1);
245         fl_set_browser_topline(dialog_->browser_bib, top);
246         fl_select_browser_line(dialog_->browser_bib, found);
247         input(dialog_->browser_bib, 0);
248 }
249
250
251 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
252 {
253         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
254
255         biblio::InfoMap const & theMap = controller().bibkeysInfo();
256
257         string topCitekey;
258         if (!citekeys.empty()) topCitekey = citekeys[0];
259
260         if (ob == dialog_->browser_bib) {
261                 fl_deselect_browser(dialog_->browser_cite);
262
263                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
264                 if (sel < 1 || sel > bibkeys.size())
265                         return ButtonPolicy::SMI_NOOP;
266
267                 // Put into browser_info the additional info associated with
268                 // the selected browser_bib key
269                 fl_clear_browser(dialog_->browser_info);
270
271                 string const tmp = formatted(biblio::getInfo(theMap,
272                                                              bibkeys[sel - 1]),
273                                               dialog_->browser_info->w - 10);
274                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
275
276                 // Highlight the selected browser_bib key in browser_cite if
277                 // present
278                 vector<string>::const_iterator cit =
279                         find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
280
281                 if (cit != citekeys.end()) {
282                         int const n = int(cit - citekeys.begin());
283                         fl_select_browser_line(dialog_->browser_cite, n + 1);
284                         fl_set_browser_topline(dialog_->browser_cite, n + 1);
285                 }
286
287                 if (!controller().bufferIsReadonly()) {
288                         if (cit != citekeys.end()) {
289                                 setBibButtons(OFF);
290                                 setCiteButtons(ON);
291                         } else {
292                                 setBibButtons(ON);
293                                 setCiteButtons(OFF);
294                         }
295                 }
296
297         } else if (ob == dialog_->browser_cite) {
298                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
299                 if (sel < 1 || sel > citekeys.size())
300                         return ButtonPolicy::SMI_NOOP;
301
302                 if (!controller().bufferIsReadonly()) {
303                         setBibButtons(OFF);
304                         setCiteButtons(ON);
305                 }
306
307                 // Highlight the selected browser_cite key in browser_bib
308                 vector<string>::const_iterator cit =
309                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
310
311                 if (cit != bibkeys.end()) {
312                         int const n = int(cit - bibkeys.begin());
313                         fl_select_browser_line(dialog_->browser_bib, n + 1);
314                         fl_set_browser_topline(dialog_->browser_bib, n + 1);
315
316                         // Put into browser_info the additional info associated
317                         // with the selected browser_cite key
318                         fl_clear_browser(dialog_->browser_info);
319                         string const tmp =
320                                 formatted(biblio::getInfo(theMap,
321                                                           citekeys[sel - 1]),
322                                           dialog_->browser_info->w - 10);
323                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
324                 }
325
326         } else if (ob == dialog_->button_add) {
327                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
328                 if (sel < 1 || sel > bibkeys.size())
329                         return ButtonPolicy::SMI_NOOP;
330
331                 // Add the selected browser_bib key to browser_cite
332                 fl_addto_browser(dialog_->browser_cite,
333                                   bibkeys[sel - 1].c_str());
334                 citekeys.push_back(bibkeys[sel - 1]);
335
336                 int const n = int(citekeys.size());
337                 fl_select_browser_line(dialog_->browser_cite, n);
338
339                 setBibButtons(OFF);
340                 setCiteButtons(ON);
341                 activate = ButtonPolicy::SMI_VALID;
342
343         } else if (ob == dialog_->button_del) {
344                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
345                 if (sel < 1 || sel > citekeys.size())
346                         return ButtonPolicy::SMI_NOOP;
347
348                 // Remove the selected key from browser_cite
349                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
350                 citekeys.erase(citekeys.begin() + sel - 1);
351
352                 setBibButtons(ON);
353                 setCiteButtons(OFF);
354                 activate = ButtonPolicy::SMI_VALID;
355
356         } else if (ob == dialog_->button_up) {
357                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
358                 if (sel < 2 || sel > citekeys.size())
359                         return ButtonPolicy::SMI_NOOP;
360
361                 // Move the selected key up one line
362                 vector<string>::iterator it = citekeys.begin() + sel - 1;
363                 string const tmp = *it;
364
365                 fl_delete_browser_line(dialog_->browser_cite, sel);
366                 citekeys.erase(it);
367
368                 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
369                 fl_select_browser_line(dialog_->browser_cite, sel - 1);
370                 citekeys.insert(it - 1, tmp);
371                 setCiteButtons(ON);
372                 activate = ButtonPolicy::SMI_VALID;
373
374         } else if (ob == dialog_->button_down) {
375                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
376                 if (sel < 1 || sel > citekeys.size() - 1)
377                         return ButtonPolicy::SMI_NOOP;
378
379                 // Move the selected key down one line
380                 vector<string>::iterator it = citekeys.begin() + sel - 1;
381                 string const tmp = *it;
382
383                 fl_delete_browser_line(dialog_->browser_cite, sel);
384                 citekeys.erase(it);
385
386                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
387                 fl_select_browser_line(dialog_->browser_cite, sel+1);
388                 citekeys.insert(it+1, tmp);
389                 setCiteButtons(ON);
390                 activate = ButtonPolicy::SMI_VALID;
391
392         } else if (ob == dialog_->button_previous) {
393                 findBiblio(biblio::BACKWARD);
394         } else if (ob == dialog_->button_next) {
395                 findBiblio(biblio::FORWARD);
396         } else if (ob == dialog_->input_search) {
397                 findBiblio(biblio::FORWARD);
398         } else if (ob == dialog_->choice_style ||
399                    ob == dialog_->check_full_author_list ||
400                    ob == dialog_->check_force_uppercase ||
401                    ob == dialog_->input_before ||
402                    ob == dialog_->input_after) {
403                 activate = ButtonPolicy::SMI_VALID;
404         }
405
406         string currentCitekey;
407         if (!citekeys.empty())
408                 currentCitekey = citekeys[0];
409
410         if (topCitekey != currentCitekey) {
411                 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
412                 fillChoice(dialog_.get(),
413                            controller().getCiteStrings(currentCitekey));
414                 fl_set_choice(dialog_->choice_style, choice);
415         }
416
417         return activate;
418 }
419
420
421 void FormCitation::update()
422 {
423         // Make the list of all available bibliography keys
424         bibkeys = biblio::getKeys(controller().bibkeysInfo());
425         updateBrowser(dialog_->browser_bib, bibkeys);
426
427         // Ditto for the keys cited in this inset
428         citekeys = getVectorFromString(controller().params().getContents());
429         updateBrowser(dialog_->browser_cite, citekeys);
430
431         // Use the first citekey to fill choice_style
432         string key;
433         if (!citekeys.empty()) key = citekeys[0];
434
435         fillChoice(dialog_.get(), controller().getCiteStrings(key));
436
437         // Use the citation command to update the GUI
438         updateStyle(dialog_.get(), controller().params().getCmdName());
439
440         bool const natbib = controller().usingNatbib();
441         setEnabled(dialog_->check_full_author_list, natbib);
442         setEnabled(dialog_->check_force_uppercase, natbib);
443         setEnabled(dialog_->choice_style, natbib);
444
445         // No keys have been selected yet, so...
446         fl_clear_browser(dialog_->browser_info);
447         setBibButtons(OFF);
448         setCiteButtons(OFF);
449
450         // Natbib can have comments before and after the citation.
451         // This is not yet supported. After only.
452         fl_set_input(dialog_->input_after,
453                      controller().params().getOptions().c_str());
454
455         fl_set_input(dialog_->input_before, _("Not yet supported"));
456         setEnabled(dialog_->input_before, false);
457 }
458
459
460 void FormCitation::updateBrowser(FL_OBJECT * browser,
461                                  vector<string> const & keys) const
462 {
463         // Check whether the current contents of the browser will be
464         // changed by loading the contents of the vec...
465         vector<string> browser_keys = getVector(browser);
466
467         if (browser_keys == keys) {
468                 fl_deselect_browser(browser);
469                 fl_set_browser_topline(browser, 1);
470                 return;
471         }
472
473         // They will be changed. Proceed.
474         fl_clear_browser(browser);
475
476         for (vector<string>::const_iterator it = keys.begin();
477              it < keys.end(); ++it) {
478                 string key = trim(*it);
479                 if (!key.empty())
480                         fl_add_browser_line(browser, key.c_str());
481         }
482 }
483
484
485 void FormCitation::setBibButtons(State status) const
486 {
487         setEnabled(dialog_->button_add, (status == ON));
488 }
489
490
491 void FormCitation::setCiteButtons(State status) const
492 {
493         int const sel     = fl_get_browser(dialog_->browser_cite);
494         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
495         bool const activate      = (status == ON);
496         bool const activate_up   = (activate && sel != 1);
497         bool const activate_down = (activate && sel != maxline);
498
499         setEnabled(dialog_->button_del,  activate);
500         setEnabled(dialog_->button_up,   activate_up);
501         setEnabled(dialog_->button_down, activate_down);
502 }