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