]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
Stabilise the tooltips interface and use it successfully in the Bibtex,
[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         // the help choice
158         fillTooltipChoice(dialog_->choice_help);
159
160         // Manage the ok, apply, restore and cancel/close buttons
161         bc().setOK(dialog_->button_ok);
162         bc().setApply(dialog_->button_apply);
163         bc().setCancel(dialog_->button_cancel);
164         bc().setRestore(dialog_->button_restore);
165
166         bc().addReadOnly(dialog_->button_add);
167         bc().addReadOnly(dialog_->button_del);
168         bc().addReadOnly(dialog_->button_up);
169         bc().addReadOnly(dialog_->button_down);
170         bc().addReadOnly(dialog_->choice_style);
171         bc().addReadOnly(dialog_->input_before);
172         bc().addReadOnly(dialog_->input_after);
173         bc().addReadOnly(dialog_->button_full_author_list);
174         bc().addReadOnly(dialog_->button_force_uppercase);
175
176         //set up the tooltip mechanism
177         setTooltipHandler(dialog_->button_add);
178         setTooltipHandler(dialog_->button_del);
179         setTooltipHandler(dialog_->button_up);
180         setTooltipHandler(dialog_->button_down);
181         setTooltipHandler(dialog_->choice_style);
182         setTooltipHandler(dialog_->input_before);
183         setTooltipHandler(dialog_->input_after);
184         setTooltipHandler(dialog_->button_full_author_list);
185         setTooltipHandler(dialog_->button_force_uppercase);
186         setTooltipHandler(dialog_->input_search);
187         setTooltipHandler(dialog_->button_search_case);
188         setTooltipHandler(dialog_->button_search_type);
189         setTooltipHandler(dialog_->button_previous);
190         setTooltipHandler(dialog_->button_next);
191 }
192
193
194 void FormCitation::findBiblio(biblio::Direction const dir)
195 {
196         string const str = fl_get_input(dialog_->input_search);
197         biblio::InfoMap const & theMap = controller().bibkeysInfo();
198         bool const caseSensitive =
199                 fl_get_button(dialog_->button_search_case);
200         biblio::Search const type =
201                 fl_get_button(dialog_->button_search_type) ?
202                 biblio::REGEX : biblio::SIMPLE;
203
204         vector<string>::const_iterator start = bibkeys.begin();
205         int const sel = fl_get_browser(dialog_->browser_bib);
206         if (sel >= 1 && sel <= int(bibkeys.size()))
207                 start += sel - 1;
208
209         // Find the NEXT instance...
210         (dir == biblio::FORWARD) ? ++start : --start;
211
212
213         vector<string>::const_iterator const cit =
214         biblio::searchKeys(theMap, bibkeys, str,
215                            start, type, dir, caseSensitive);
216
217         if (cit == bibkeys.end())
218                 return;
219
220         int const found = int(cit - bibkeys.begin()) + 1;
221         if (found == sel)
222                 return;
223
224         // Update the display
225         int const top = max(found - 5, 1);
226         fl_set_browser_topline(dialog_->browser_bib, top);
227         fl_select_browser_line(dialog_->browser_bib, found);
228         input(dialog_->browser_bib, 0);
229 }
230  
231
232 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
233 {
234         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
235
236         biblio::InfoMap const & theMap = controller().bibkeysInfo();
237
238         string topCitekey;
239         if (!citekeys.empty()) topCitekey = citekeys[0];
240
241         if (ob == dialog_->browser_bib) {
242                 fl_deselect_browser(dialog_->browser_cite);
243
244                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
245                 if (sel < 1 || sel > bibkeys.size())
246                         return ButtonPolicy::SMI_NOOP;
247
248                 // Put into browser_info the additional info associated with
249                 // the selected browser_bib key
250                 fl_clear_browser(dialog_->browser_info);
251
252                 string const tmp = formatted(biblio::getInfo(theMap,
253                                                              bibkeys[sel-1]),
254                                               dialog_->browser_info->w-10 );
255                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
256
257                 // Highlight the selected browser_bib key in browser_cite if
258                 // present
259                 vector<string>::const_iterator cit =
260                         find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
261
262                 if (cit != citekeys.end()) {
263                         int const n = int(cit - citekeys.begin());
264                         fl_select_browser_line(dialog_->browser_cite, n+1);
265                         fl_set_browser_topline(dialog_->browser_cite, n+1);
266                 }
267
268                 if (!controller().isReadonly()) {
269                         if (cit != citekeys.end()) {
270                                 setBibButtons(OFF);
271                                 setCiteButtons(ON);
272                         } else {
273                                 setBibButtons(ON);
274                                 setCiteButtons(OFF);
275                         }
276                 }
277
278         } else if (ob == dialog_->browser_cite) {
279                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
280                 if (sel < 1 || sel > citekeys.size())
281                         return ButtonPolicy::SMI_NOOP;
282
283                 if (!controller().isReadonly()) {
284                         setBibButtons(OFF);
285                         setCiteButtons(ON);
286                 }
287
288                 // Highlight the selected browser_cite key in browser_bib
289                 vector<string>::const_iterator cit =
290                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
291
292                 if (cit != bibkeys.end()) {
293                         int const n = int(cit - bibkeys.begin());
294                         fl_select_browser_line(dialog_->browser_bib, n+1);
295                         fl_set_browser_topline(dialog_->browser_bib, n+1);
296
297                         // Put into browser_info the additional info associated
298                         // with the selected browser_cite key
299                         fl_clear_browser(dialog_->browser_info);
300                         string const tmp =
301                                 formatted(biblio::getInfo(theMap,
302                                                           citekeys[sel-1]),
303                                           dialog_->browser_info->w-10);
304                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
305                 }
306
307         } else if (ob == dialog_->button_add) {
308                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
309                 if (sel < 1 || sel > bibkeys.size())
310                         return ButtonPolicy::SMI_NOOP;
311
312                 // Add the selected browser_bib key to browser_cite
313                 fl_addto_browser(dialog_->browser_cite,
314                                   bibkeys[sel-1].c_str());
315                 citekeys.push_back(bibkeys[sel-1]);
316
317                 int const n = int(citekeys.size());
318                 fl_select_browser_line(dialog_->browser_cite, n);
319
320                 setBibButtons(OFF);
321                 setCiteButtons(ON);
322                 activate = ButtonPolicy::SMI_VALID;
323
324         } else if (ob == dialog_->button_del) {
325                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
326                 if (sel < 1 || sel > citekeys.size())
327                         return ButtonPolicy::SMI_NOOP;
328
329                 // Remove the selected key from browser_cite
330                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
331                 citekeys.erase(citekeys.begin() + sel-1);
332
333                 setBibButtons(ON);
334                 setCiteButtons(OFF);
335                 activate = ButtonPolicy::SMI_VALID;
336
337         } else if (ob == dialog_->button_up) {
338                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
339                 if (sel < 2 || sel > citekeys.size())
340                         return ButtonPolicy::SMI_NOOP;
341
342                 // Move the selected key up one line
343                 vector<string>::iterator it = citekeys.begin() + sel-1;
344                 string const tmp = *it;
345
346                 fl_delete_browser_line(dialog_->browser_cite, sel);
347                 citekeys.erase(it);
348
349                 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
350                 fl_select_browser_line(dialog_->browser_cite, sel-1);
351                 citekeys.insert(it-1, tmp);
352                 setCiteButtons(ON);
353                 activate = ButtonPolicy::SMI_VALID;
354
355         } else if (ob == dialog_->button_down) {
356                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
357                 if (sel < 1 || sel > citekeys.size()-1)
358                         return ButtonPolicy::SMI_NOOP;
359
360                 // Move the selected key down one line
361                 vector<string>::iterator it = citekeys.begin() + sel-1;
362                 string const tmp = *it;
363
364                 fl_delete_browser_line(dialog_->browser_cite, sel);
365                 citekeys.erase(it);
366
367                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
368                 fl_select_browser_line(dialog_->browser_cite, sel+1);
369                 citekeys.insert(it+1, tmp);
370                 setCiteButtons(ON);
371                 activate = ButtonPolicy::SMI_VALID;
372
373         } else if (ob == dialog_->button_previous) {
374                 findBiblio(biblio::BACKWARD);
375         } else if (ob == dialog_->button_next) {
376                 findBiblio(biblio::FORWARD);
377         } else if (ob == dialog_->input_search) {
378                 findBiblio(biblio::FORWARD);
379         } else if (ob == dialog_->choice_style ||
380                    ob == dialog_->button_full_author_list ||
381                    ob == dialog_->button_force_uppercase ||
382                    ob == dialog_->input_before ||
383                    ob == dialog_->input_after) {
384                 activate = ButtonPolicy::SMI_VALID;
385         }
386
387         if (ob == dialog_->choice_help) {
388                 setTooltipLevel(dialog_->choice_help);
389                 return ButtonPolicy::SMI_NOOP;
390         }
391
392         string currentCitekey;
393         if (!citekeys.empty())
394                 currentCitekey = citekeys[0];
395
396         if (topCitekey != currentCitekey) {
397                 int choice = fl_get_choice(dialog_->choice_style);
398                 fillChoice(dialog_.get(),
399                            controller().getCiteStrings(currentCitekey));
400                 fl_set_choice(dialog_->choice_style, choice);
401         }
402
403         return activate;
404 }
405
406
407 void FormCitation::update()
408 {
409         // Make the list of all available bibliography keys
410         bibkeys = biblio::getKeys(controller().bibkeysInfo());
411         updateBrowser(dialog_->browser_bib, bibkeys);
412
413         // Ditto for the keys cited in this inset
414         citekeys = getVectorFromString(controller().params().getContents());
415         updateBrowser(dialog_->browser_cite, citekeys);
416
417         // Use the first citekey to fill choice_style
418         string key;
419         if (!citekeys.empty()) key = citekeys[0];
420
421         fillChoice(dialog_.get(), controller().getCiteStrings(key));
422
423         // Use the citation command to update the GUI
424         updateStyle(dialog_.get(), controller().params().getCmdName());
425
426         bool const natbib = controller().usingNatbib();
427         setEnabled(dialog_->button_full_author_list, natbib);
428         setEnabled(dialog_->button_force_uppercase, natbib);
429         setEnabled(dialog_->choice_style, natbib);
430         
431         // No keys have been selected yet, so...
432         fl_clear_browser(dialog_->browser_info);
433         setBibButtons(OFF);
434         setCiteButtons(OFF);
435
436         // Natbib can have comments before and after the citation.
437         // This is not yet supported. After only.
438         fl_set_input(dialog_->input_after,
439                      controller().params().getOptions().c_str());
440
441         fl_set_input(dialog_->input_before, _("Not yet supported"));
442         setEnabled(dialog_->input_before, false);
443 }
444
445
446 void FormCitation::updateBrowser(FL_OBJECT * browser,
447                                  vector<string> const & keys) const
448 {
449         // Check whether the current contents of the browser will be
450         // changed by loading the contents of the vec...
451         vector<string> browser_keys = getVectorFromBrowser(browser);
452
453         if (browser_keys == keys)
454                 return;
455
456         // They will be changed. Proceed.
457         fl_clear_browser(browser);
458
459         for (vector<string>::const_iterator it = keys.begin();
460              it < keys.end(); ++it) {
461                 string key = frontStrip(strip(*it));
462                 if (!key.empty())
463                         fl_add_browser_line(browser, key.c_str());
464         }
465 }
466
467
468 void FormCitation::setBibButtons(State status) const
469 {
470         setEnabled(dialog_->button_add, (status == ON));
471 }
472
473
474 void FormCitation::setCiteButtons(State status) const
475 {
476         int const sel     = fl_get_browser(dialog_->browser_cite);
477         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
478         bool const activate      = (status == ON);
479         bool const activate_up   = (activate && sel != 1);
480         bool const activate_down = (activate && sel != maxline);
481
482         setEnabled(dialog_->button_del,  activate);
483         setEnabled(dialog_->button_up,   activate_up);
484         setEnabled(dialog_->button_down, activate_down);
485 }
486
487
488 string const FormCitation::getMinimalTooltip(FL_OBJECT const * ob) const
489 {
490         string str;
491
492         if (ob == dialog_->button_add) {
493                 str = N_("Add entry");
494
495         } else if (ob == dialog_->button_del) {
496                 str = _("Delete entry");
497
498         } else if (ob == dialog_->button_up) {
499                 str = _("Move entry up");
500
501         } else if (ob == dialog_->button_down) {
502                 str = _("Move entry down");
503
504         } else if (ob == dialog_->choice_style) {
505                 str = N_("Natbib Citation Style");
506
507         } else if (ob == dialog_->button_full_author_list) {
508                 str = N_("List all authors");
509
510         } else if (ob == dialog_->button_force_uppercase) {
511                 str = N_("Author Name begins with uppercase");
512
513         } else if (ob == dialog_->input_before) {
514                 str = N_("Optional text before");
515
516         } else if (ob == dialog_->input_after) {
517                 str = N_("Optional text after");
518
519         } else if (ob == dialog_->input_search) {
520                 str = N_("Search the database");
521
522         } else if (ob == dialog_->button_search_case) {
523                 str = N_("Case sensitive search");
524
525         } else if (ob == dialog_->button_search_type) {
526                 str = N_("Use Regular Expressions.");
527
528         } else if (ob == dialog_->button_previous) {
529                 str = N_("Search Backwards");
530
531         } else if (ob == dialog_->button_next) {
532                 str = N_("Search Forwards");
533         }
534
535         return str;
536 }
537
538
539 string const FormCitation::getVerboseTooltip(FL_OBJECT const * ob) const
540 {
541         string str;
542
543         if (ob == dialog_->button_add) {
544                 str = N_("Add the selected entry to the current citation reference.");
545
546         } else if (ob == dialog_->button_del) {
547                 str = N_("Delete the selected entry from the current citation reference.");
548
549         } else if (ob == dialog_->button_up) {
550                 str = N_("Move the selected entry upwards (in the current list).");
551
552         } else if (ob == dialog_->button_down) {
553                 str = N_("Move the selected entry downwards (in the current list).");
554
555         } else if (ob == dialog_->browser_cite) {
556                 str = N_("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
557
558         } else if (ob == dialog_->browser_bib) {
559                 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.");
560
561         } else if (ob == dialog_->browser_info) {
562                 str = N_("Information about the selected entry");
563
564         } else if (ob == dialog_->choice_style) {
565                 str = N_("Here you may select how the citation label should look inside the text (Natbib).");
566
567         } else if (ob == dialog_->button_full_author_list) {
568                 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).");
569
570         } else if (ob == dialog_->button_force_uppercase) {
571                 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).");
572
573         } else if (ob == dialog_->input_before) {
574                 str = N_("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
575
576         } else if (ob == dialog_->input_after) {
577                 str = N_("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
578
579         } else if (ob == dialog_->input_search) {
580                 str = N_("Search your database (all fields will be searched).");
581
582         } else if (ob == dialog_->button_search_case) {
583                 str = N_("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
584
585         } else if (ob == dialog_->button_search_type) {
586                 str = N_("Activate if you want to enter Regular Expressions.");
587         }
588
589         return str;
590 }