2 * \file xforms/FormCitation.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming, a.leeming@ic.ac.uk
13 #pragma implementation
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "form_citation.h"
21 #include "helper_funcs.h"
22 #include "xforms_helpers.h"
24 #include "support/LAssert.h"
25 #include "support/lstrings.h"
36 // shamelessly stolen from Menubar_pimpl.C
37 int string_width(string const & str)
39 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
41 static_cast<int>(str.length()));
45 void fillChoice(FD_form_citation * dialog, vector<string> vec)
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);
52 if (vec == choice_style)
55 // They will be changed. Proceed
58 str += getStringFromVector(vec, " | ") + " ";
60 fl_clear_choice(dialog->choice_style);
61 fl_addto_choice(dialog->choice_style, str.c_str());
63 setEnabled(dialog->choice_style, !vec.empty());
67 // The width of the choice varies with the contents.
68 // Ensure that it is centred in the frame.
71 for (vector<string>::const_iterator it = vec.begin();
72 it != vec.end(); ++it) {
73 width = max(width, string_width(*it));
77 max(5, int(0.5 * (dialog->frame_style->w - width)));
79 fl_set_object_geometry(dialog->choice_style,
80 dialog->frame_style->x + dx,
81 dialog->choice_style->y,
83 dialog->choice_style->h);
87 void updateStyle(FD_form_citation * dialog, string command)
89 // Find the style of the citekeys
90 vector<biblio::CiteStyle> const & styles =
91 ControlCitation::getCiteStyles();
92 biblio::CitationStyle cs = biblio::getCitationStyle(command);
94 vector<biblio::CiteStyle>::const_iterator cit =
95 find(styles.begin(), styles.end(), cs.style);
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);
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);
113 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
116 FormCitation::FormCitation(ControlCitation & c)
117 : base_class(c, _("Citation"), false)
121 void FormCitation::apply()
123 string command = "cite";
124 if (dialog_->choice_style->active != 0) {
125 vector<biblio::CiteStyle> const & styles =
126 ControlCitation::getCiteStyles();
129 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
131 fl_get_button(dialog_->check_full_author_list);
133 fl_get_button(dialog_->check_force_uppercase);
135 command = biblio::getCiteCommand(styles[choice], full, force);
138 controller().params().setCmdName(command);
139 controller().params().setContents(getStringFromVector(citekeys));
141 string const after = fl_get_input(dialog_->input_after);
142 controller().params().setOptions(after);
146 void FormCitation::hide()
155 void FormCitation::build()
157 dialog_.reset(build_citation());
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);
163 fl_set_button(dialog_->check_search_case, 0);
164 fl_set_button(dialog_->check_search_type, 0);
166 setPrehandler(dialog_->input_search);
167 setPrehandler(dialog_->input_before);
168 setPrehandler(dialog_->input_after);
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);
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);
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);
190 str = _("Delete the selected entry from the current citation reference.");
191 tooltips().init(dialog_->button_del, str);
193 str = _("Move the selected entry upwards (in the current list).");
194 tooltips().init(dialog_->button_up, str);
196 str = _("Move the selected entry downwards (in the current list).");
197 tooltips().init(dialog_->button_down, str);
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);
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);
205 str = _("Information about the selected entry");
206 tooltips().init(dialog_->browser_info, str);
208 str = _("Here you may select how the citation label should look inside the text (Natbib).");
209 tooltips().init(dialog_->choice_style, str);
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);
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);
217 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
218 tooltips().init(dialog_->input_before, str);
220 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
221 tooltips().init(dialog_->input_after, str);
223 str = _("Search your database (all fields will be searched).");
224 tooltips().init(dialog_->input_search, str);
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);
229 str = _("Activate if you want to enter Regular Expressions.");
230 tooltips().init(dialog_->check_search_type, str);
234 void FormCitation::findBiblio(biblio::Direction const dir)
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;
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()))
249 // Find the NEXT instance...
250 (dir == biblio::FORWARD) ? ++start : --start;
253 vector<string>::const_iterator const cit =
254 biblio::searchKeys(theMap, bibkeys, str,
255 start, type, dir, caseSensitive);
257 if (cit == bibkeys.end())
260 int const found = int(cit - bibkeys.begin()) + 1;
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);
272 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
274 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
276 biblio::InfoMap const & theMap = controller().bibkeysInfo();
279 if (!citekeys.empty()) topCitekey = citekeys[0];
281 if (ob == dialog_->browser_bib) {
282 fl_deselect_browser(dialog_->browser_cite);
284 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
285 if (sel < 1 || sel > bibkeys.size())
286 return ButtonPolicy::SMI_NOOP;
288 // Put into browser_info the additional info associated with
289 // the selected browser_bib key
290 fl_clear_browser(dialog_->browser_info);
292 string const tmp = formatted(biblio::getInfo(theMap,
294 dialog_->browser_info->w-10);
295 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
297 // Highlight the selected browser_bib key in browser_cite if
299 vector<string>::const_iterator cit =
300 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
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);
308 if (!controller().isReadonly()) {
309 if (cit != citekeys.end()) {
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;
323 if (!controller().isReadonly()) {
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]);
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);
337 // Put into browser_info the additional info associated
338 // with the selected browser_cite key
339 fl_clear_browser(dialog_->browser_info);
341 formatted(biblio::getInfo(theMap,
343 dialog_->browser_info->w-10);
344 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
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;
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]);
357 int const n = int(citekeys.size());
358 fl_select_browser_line(dialog_->browser_cite, n);
362 activate = ButtonPolicy::SMI_VALID;
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;
369 // Remove the selected key from browser_cite
370 fl_delete_browser_line(dialog_->browser_cite, sel) ;
371 citekeys.erase(citekeys.begin() + sel-1);
375 activate = ButtonPolicy::SMI_VALID;
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;
382 // Move the selected key up one line
383 vector<string>::iterator it = citekeys.begin() + sel-1;
384 string const tmp = *it;
386 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
393 activate = ButtonPolicy::SMI_VALID;
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;
400 // Move the selected key down one line
401 vector<string>::iterator it = citekeys.begin() + sel-1;
402 string const tmp = *it;
404 fl_delete_browser_line(dialog_->browser_cite, sel);
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);
411 activate = ButtonPolicy::SMI_VALID;
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;
427 string currentCitekey;
428 if (!citekeys.empty())
429 currentCitekey = citekeys[0];
431 if (topCitekey != currentCitekey) {
432 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
433 fillChoice(dialog_.get(),
434 controller().getCiteStrings(currentCitekey));
435 fl_set_choice(dialog_->choice_style, choice);
442 void FormCitation::update()
444 // Make the list of all available bibliography keys
445 bibkeys = biblio::getKeys(controller().bibkeysInfo());
446 updateBrowser(dialog_->browser_bib, bibkeys);
448 // Ditto for the keys cited in this inset
449 citekeys = getVectorFromString(controller().params().getContents());
450 updateBrowser(dialog_->browser_cite, citekeys);
452 // Use the first citekey to fill choice_style
454 if (!citekeys.empty()) key = citekeys[0];
456 fillChoice(dialog_.get(), controller().getCiteStrings(key));
458 // Use the citation command to update the GUI
459 updateStyle(dialog_.get(), controller().params().getCmdName());
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);
466 // No keys have been selected yet, so...
467 fl_clear_browser(dialog_->browser_info);
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());
476 fl_set_input(dialog_->input_before, _("Not yet supported"));
477 setEnabled(dialog_->input_before, false);
481 void FormCitation::updateBrowser(FL_OBJECT * browser,
482 vector<string> const & keys) const
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);
488 if (browser_keys == keys)
491 // They will be changed. Proceed.
492 fl_clear_browser(browser);
494 for (vector<string>::const_iterator it = keys.begin();
495 it < keys.end(); ++it) {
496 string key = frontStrip(strip(*it));
498 fl_add_browser_line(browser, key.c_str());
503 void FormCitation::setBibButtons(State status) const
505 setEnabled(dialog_->button_add, (status == ON));
509 void FormCitation::setCiteButtons(State status) const
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);
517 setEnabled(dialog_->button_del, activate);
518 setEnabled(dialog_->button_up, activate_up);
519 setEnabled(dialog_->button_down, activate_down);