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 // 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);
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);
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);
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);
209 void FormCitation::findBiblio(biblio::Direction const dir)
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;
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()))
224 // Find the NEXT instance...
225 (dir == biblio::FORWARD) ? ++start : --start;
228 vector<string>::const_iterator const cit =
229 biblio::searchKeys(theMap, bibkeys, str,
230 start, type, dir, caseSensitive);
232 if (cit == bibkeys.end())
235 int const found = int(cit - bibkeys.begin()) + 1;
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);
247 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
249 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
251 biblio::InfoMap const & theMap = controller().bibkeysInfo();
254 if (!citekeys.empty()) topCitekey = citekeys[0];
256 if (ob == dialog_->browser_bib) {
257 fl_deselect_browser(dialog_->browser_cite);
259 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
260 if (sel < 1 || sel > bibkeys.size())
261 return ButtonPolicy::SMI_NOOP;
263 // Put into browser_info the additional info associated with
264 // the selected browser_bib key
265 fl_clear_browser(dialog_->browser_info);
267 string const tmp = formatted(biblio::getInfo(theMap,
269 dialog_->browser_info->w-10 );
270 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
272 // Highlight the selected browser_bib key in browser_cite if
274 vector<string>::const_iterator cit =
275 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
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);
283 if (!controller().isReadonly()) {
284 if (cit != citekeys.end()) {
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;
298 if (!controller().isReadonly()) {
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]);
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);
312 // Put into browser_info the additional info associated
313 // with the selected browser_cite key
314 fl_clear_browser(dialog_->browser_info);
316 formatted(biblio::getInfo(theMap,
318 dialog_->browser_info->w-10);
319 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
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;
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]);
332 int const n = int(citekeys.size());
333 fl_select_browser_line(dialog_->browser_cite, n);
337 activate = ButtonPolicy::SMI_VALID;
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;
344 // Remove the selected key from browser_cite
345 fl_delete_browser_line(dialog_->browser_cite, sel) ;
346 citekeys.erase(citekeys.begin() + sel-1);
350 activate = ButtonPolicy::SMI_VALID;
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;
357 // Move the selected key up one line
358 vector<string>::iterator it = citekeys.begin() + sel-1;
359 string const tmp = *it;
361 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
368 activate = ButtonPolicy::SMI_VALID;
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;
375 // Move the selected key down one line
376 vector<string>::iterator it = citekeys.begin() + sel-1;
377 string const tmp = *it;
379 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
386 activate = ButtonPolicy::SMI_VALID;
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;
402 string currentCitekey;
403 if (!citekeys.empty())
404 currentCitekey = citekeys[0];
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);
417 void FormCitation::update()
419 // Make the list of all available bibliography keys
420 bibkeys = biblio::getKeys(controller().bibkeysInfo());
421 updateBrowser(dialog_->browser_bib, bibkeys);
423 // Ditto for the keys cited in this inset
424 citekeys = getVectorFromString(controller().params().getContents());
425 updateBrowser(dialog_->browser_cite, citekeys);
427 // Use the first citekey to fill choice_style
429 if (!citekeys.empty()) key = citekeys[0];
431 fillChoice(dialog_.get(), controller().getCiteStrings(key));
433 // Use the citation command to update the GUI
434 updateStyle(dialog_.get(), controller().params().getCmdName());
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);
441 // No keys have been selected yet, so...
442 fl_clear_browser(dialog_->browser_info);
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());
451 fl_set_input(dialog_->input_before, _("Not yet supported"));
452 setEnabled(dialog_->input_before, false);
456 void FormCitation::updateBrowser(FL_OBJECT * browser,
457 vector<string> const & keys) const
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);
463 if (browser_keys == keys)
466 // They will be changed. Proceed.
467 fl_clear_browser(browser);
469 for (vector<string>::const_iterator it = keys.begin();
470 it < keys.end(); ++it) {
471 string key = frontStrip(strip(*it));
473 fl_add_browser_line(browser, key.c_str());
478 void FormCitation::setBibButtons(State status) const
480 setEnabled(dialog_->button_add, (status == ON));
484 void FormCitation::setCiteButtons(State status) const
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);
492 setEnabled(dialog_->button_del, activate);
493 setEnabled(dialog_->button_up, activate_up);
494 setEnabled(dialog_->button_down, activate_down);
498 string const FormCitation::getMinimalTooltip(FL_OBJECT * ob) const
502 if (ob == dialog_->button_add) {
503 str = N_("Add entry");
505 } else if (ob == dialog_->button_del) {
506 str = _("Delete entry");
508 } else if (ob == dialog_->button_up) {
509 str = _("Move entry up");
511 } else if (ob == dialog_->button_down) {
512 str = _("Move entry down");
514 } else if (ob == dialog_->choice_style) {
515 str = N_("Natbib Citation Style");
517 } else if (ob == dialog_->button_full_author_list) {
518 str = N_("List all authors");
520 } else if (ob == dialog_->button_force_uppercase) {
521 str = N_("Author Name begins with uppercase");
523 } else if (ob == dialog_->input_before) {
524 str = N_("Optional text before");
526 } else if (ob == dialog_->input_after) {
527 str = N_("Optional text after");
529 } else if (ob == dialog_->input_search) {
530 str = N_("Search the database");
532 } else if (ob == dialog_->button_search_case) {
533 str = N_("Case sensitive search");
535 } else if (ob == dialog_->button_search_type) {
536 str = N_("Use Regular Expressions.");
538 } else if (ob == dialog_->button_previous) {
539 str = N_("Search Backwards");
541 } else if (ob == dialog_->button_next) {
542 str = N_("Search Forwards");
549 string const FormCitation::getVerboseTooltip(FL_OBJECT * ob) const
553 if (ob == dialog_->button_add) {
554 str = N_("Add the selected entry to the current citation reference.");
556 } else if (ob == dialog_->button_del) {
557 str = N_("Delete the selected entry from the current citation reference.");
559 } else if (ob == dialog_->button_up) {
560 str = N_("Move the selected entry upwards (in the current list).");
562 } else if (ob == dialog_->button_down) {
563 str = N_("Move the selected entry downwards (in the current list).");
565 } else if (ob == dialog_->browser_cite) {
566 str = N_("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
568 } else if (ob == dialog_->browser_bib) {
569 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.");
571 } else if (ob == dialog_->browser_info) {
572 str = N_("Information about the selected entry");
574 } else if (ob == dialog_->choice_style) {
575 str = N_("Here you may select how the citation label should look inside the text (Natbib).");
577 } else if (ob == dialog_->button_full_author_list) {
578 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).");
580 } else if (ob == dialog_->button_force_uppercase) {
581 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).");
583 } else if (ob == dialog_->input_before) {
584 str = N_("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
586 } else if (ob == dialog_->input_after) {
587 str = N_("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
589 } else if (ob == dialog_->input_search) {
590 str = N_("Search your database (all fields will be searched).");
592 } else if (ob == dialog_->button_search_case) {
593 str = N_("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
595 } else if (ob == dialog_->button_search_type) {
596 str = N_("Activate if you want to enter Regular Expressions.");