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