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