2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000-2001 The LyX Team.
9 * ======================================================
11 * \file FormCitation.C
12 * \author Angus Leeming, a.leeming@ic.ac.uk
19 #pragma implementation
23 #include "ControlCitation.h"
24 #include "FormCitation.h"
25 #include "form_citation.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29 #include "helper_funcs.h"
30 #include "xforms_helpers.h"
41 // shamelessly stolen from Menubar_pimpl.C
42 int string_width(string const & str)
44 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
46 static_cast<int>(str.length()));
50 void fillChoice(FD_form_citation * dialog, vector<string> vec)
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);
57 if (vec == choice_style)
60 // They will be changed. Proceed
61 string const str = " " + getStringFromVector(vec, " | ") + " ";
63 fl_clear_choice(dialog->choice_style);
64 fl_addto_choice(dialog->choice_style, str.c_str());
66 // The width of the choice varies with the contents.
67 // Ensure that it is centred in the frame.
70 for (vector<string>::const_iterator it = vec.begin();
71 it != vec.end(); ++it) {
72 width = max(width, string_width(*it));
76 max(5, int(0.5 * (dialog->frame_style->w - width)));
78 fl_set_object_geometry(dialog->choice_style,
79 dialog->frame_style->x + dx,
80 dialog->choice_style->y,
82 dialog->choice_style->h);
86 void updateStyle(FD_form_citation * dialog, string command)
88 // Find the style of the citekeys
89 vector<biblio::CiteStyle> const & styles =
90 ControlCitation::getCiteStyles();
91 biblio::CitationStyle cs = biblio::getCitationStyle(command);
93 vector<biblio::CiteStyle>::const_iterator cit =
94 find(styles.begin(), styles.end(), cs.style);
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);
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);
111 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
113 FormCitation::FormCitation(ControlCitation & c)
114 : base_class(c, _("Citation"), false)
118 void FormCitation::apply()
120 vector<biblio::CiteStyle> const & styles =
121 ControlCitation::getCiteStyles();
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);
127 string const command =
128 biblio::getCiteCommand(styles[choice], full, force);
130 controller().params().setCmdName(command);
131 controller().params().setContents(getStringFromVector(citekeys));
133 string const after = fl_get_input(dialog_->input_after);
134 controller().params().setOptions(after);
138 void FormCitation::hide()
147 void FormCitation::build()
149 dialog_.reset(build_citation());
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);
155 fl_set_button(dialog_->button_search_case, 0);
156 fl_set_button(dialog_->button_search_type, 0);
158 fillTooltipChoice(dialog_->choice_help);
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);
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);
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);
194 void FormCitation::findBiblio(biblio::Direction const dir)
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;
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()))
209 // Find the NEXT instance...
210 (dir == biblio::FORWARD) ? ++start : --start;
213 vector<string>::const_iterator const cit =
214 biblio::searchKeys(theMap, bibkeys, str,
215 start, type, dir, caseSensitive);
217 if (cit == bibkeys.end())
220 int const found = int(cit - bibkeys.begin()) + 1;
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);
232 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
234 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
236 biblio::InfoMap const & theMap = controller().bibkeysInfo();
239 if (!citekeys.empty()) topCitekey = citekeys[0];
241 if (ob == dialog_->browser_bib) {
242 fl_deselect_browser(dialog_->browser_cite);
244 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
245 if (sel < 1 || sel > bibkeys.size())
246 return ButtonPolicy::SMI_NOOP;
248 // Put into browser_info the additional info associated with
249 // the selected browser_bib key
250 fl_clear_browser(dialog_->browser_info);
252 string const tmp = formatted(biblio::getInfo(theMap,
254 dialog_->browser_info->w-10);
255 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
257 // Highlight the selected browser_bib key in browser_cite if
259 vector<string>::const_iterator cit =
260 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
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);
268 if (!controller().isReadonly()) {
269 if (cit != citekeys.end()) {
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;
283 if (!controller().isReadonly()) {
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]);
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);
297 // Put into browser_info the additional info associated
298 // with the selected browser_cite key
299 fl_clear_browser(dialog_->browser_info);
301 formatted(biblio::getInfo(theMap,
303 dialog_->browser_info->w-10);
304 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
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;
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]);
317 int const n = int(citekeys.size());
318 fl_select_browser_line(dialog_->browser_cite, n);
322 activate = ButtonPolicy::SMI_VALID;
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;
329 // Remove the selected key from browser_cite
330 fl_delete_browser_line(dialog_->browser_cite, sel) ;
331 citekeys.erase(citekeys.begin() + sel-1);
335 activate = ButtonPolicy::SMI_VALID;
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;
342 // Move the selected key up one line
343 vector<string>::iterator it = citekeys.begin() + sel-1;
344 string const tmp = *it;
346 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
353 activate = ButtonPolicy::SMI_VALID;
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;
360 // Move the selected key down one line
361 vector<string>::iterator it = citekeys.begin() + sel-1;
362 string const tmp = *it;
364 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
371 activate = ButtonPolicy::SMI_VALID;
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;
387 if (ob == dialog_->choice_help) {
388 setTooltipLevel(dialog_->choice_help);
389 return ButtonPolicy::SMI_NOOP;
392 string currentCitekey;
393 if (!citekeys.empty())
394 currentCitekey = citekeys[0];
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);
407 void FormCitation::update()
409 // Make the list of all available bibliography keys
410 bibkeys = biblio::getKeys(controller().bibkeysInfo());
411 updateBrowser(dialog_->browser_bib, bibkeys);
413 // Ditto for the keys cited in this inset
414 citekeys = getVectorFromString(controller().params().getContents());
415 updateBrowser(dialog_->browser_cite, citekeys);
417 // Use the first citekey to fill choice_style
419 if (!citekeys.empty()) key = citekeys[0];
421 fillChoice(dialog_.get(), controller().getCiteStrings(key));
423 // Use the citation command to update the GUI
424 updateStyle(dialog_.get(), controller().params().getCmdName());
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);
431 // No keys have been selected yet, so...
432 fl_clear_browser(dialog_->browser_info);
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());
441 fl_set_input(dialog_->input_before, _("Not yet supported"));
442 setEnabled(dialog_->input_before, false);
446 void FormCitation::updateBrowser(FL_OBJECT * browser,
447 vector<string> const & keys) const
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);
453 if (browser_keys == keys)
456 // They will be changed. Proceed.
457 fl_clear_browser(browser);
459 for (vector<string>::const_iterator it = keys.begin();
460 it < keys.end(); ++it) {
461 string key = frontStrip(strip(*it));
463 fl_add_browser_line(browser, key.c_str());
468 void FormCitation::setBibButtons(State status) const
470 setEnabled(dialog_->button_add, (status == ON));
474 void FormCitation::setCiteButtons(State status) const
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);
482 setEnabled(dialog_->button_del, activate);
483 setEnabled(dialog_->button_up, activate_up);
484 setEnabled(dialog_->button_down, activate_down);
488 string const FormCitation::getMinimalTooltip(FL_OBJECT const * ob) const
492 if (ob == dialog_->button_add) {
493 str = N_("Add entry");
495 } else if (ob == dialog_->button_del) {
496 str = _("Delete entry");
498 } else if (ob == dialog_->button_up) {
499 str = _("Move entry up");
501 } else if (ob == dialog_->button_down) {
502 str = _("Move entry down");
504 } else if (ob == dialog_->choice_style) {
505 str = N_("Natbib Citation Style");
507 } else if (ob == dialog_->button_full_author_list) {
508 str = N_("List all authors");
510 } else if (ob == dialog_->button_force_uppercase) {
511 str = N_("Author Name begins with uppercase");
513 } else if (ob == dialog_->input_before) {
514 str = N_("Optional text before");
516 } else if (ob == dialog_->input_after) {
517 str = N_("Optional text after");
519 } else if (ob == dialog_->input_search) {
520 str = N_("Search the database");
522 } else if (ob == dialog_->button_search_case) {
523 str = N_("Case sensitive search");
525 } else if (ob == dialog_->button_search_type) {
526 str = N_("Use Regular Expressions.");
528 } else if (ob == dialog_->button_previous) {
529 str = N_("Search Backwards");
531 } else if (ob == dialog_->button_next) {
532 str = N_("Search Forwards");
539 string const FormCitation::getVerboseTooltip(FL_OBJECT const * ob) const
543 if (ob == dialog_->button_add) {
544 str = N_("Add the selected entry to the current citation reference.");
546 } else if (ob == dialog_->button_del) {
547 str = N_("Delete the selected entry from the current citation reference.");
549 } else if (ob == dialog_->button_up) {
550 str = N_("Move the selected entry upwards (in the current list).");
552 } else if (ob == dialog_->button_down) {
553 str = N_("Move the selected entry downwards (in the current list).");
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.");
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.");
561 } else if (ob == dialog_->browser_info) {
562 str = N_("Information about the selected entry");
564 } else if (ob == dialog_->choice_style) {
565 str = N_("Here you may select how the citation label should look inside the text (Natbib).");
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).");
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).");
573 } else if (ob == dialog_->input_before) {
574 str = N_("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
576 } else if (ob == dialog_->input_after) {
577 str = N_("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
579 } else if (ob == dialog_->input_search) {
580 str = N_("Search your database (all fields will be searched).");
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\".");
585 } else if (ob == dialog_->button_search_type) {
586 str = N_("Activate if you want to enter Regular Expressions.");