3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "forms/form_citation.h"
22 #include "helper_funcs.h"
23 #include "xforms_helpers.h"
24 #include "lyx_forms.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
39 // shamelessly stolen from Menubar_pimpl.C
40 int string_width(string const & str)
42 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
44 static_cast<int>(str.length()));
48 void fillChoice(FD_citation * dialog, vector<string> vec)
50 // Check whether the current contents of the browser will be
51 // changed by loading the contents of the vec...
52 vector<string> const choice_style = getVector(dialog->choice_style);
54 if (vec == choice_style)
57 // They will be changed. Proceed
58 bool const noVec = vec.empty();
59 string const str = noVec ? string() : getStringFromVector(vec, "|");
61 fl_clear_choice(dialog->choice_style);
62 fl_addto_choice(dialog->choice_style, str.c_str());
63 setEnabled(dialog->choice_style, !noVec);
67 void updateStyle(FD_citation * dialog, string command)
69 // Find the style of the citekeys
70 vector<biblio::CiteStyle> const & styles =
71 ControlCitation::getCiteStyles();
72 biblio::CitationStyle cs = biblio::getCitationStyle(command);
74 vector<biblio::CiteStyle>::const_iterator cit =
75 find(styles.begin(), styles.end(), cs.style);
77 // Use this to initialise the GUI
78 bool const noStyles = cit == styles.end();
79 int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
80 fl_set_choice(dialog->choice_style, index);
82 // Disable if there are no styles, otherwise use cs member settings.
83 fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
84 fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
90 typedef FormController<ControlCitation, FormView<FD_citation> > base_class;
93 FormCitation::FormCitation(Dialog & parent)
94 : base_class(parent, _("Citation"))
98 void FormCitation::apply()
100 string command = "cite";
101 if (isActive(dialog_->choice_style)) {
102 vector<biblio::CiteStyle> const & styles =
103 ControlCitation::getCiteStyles();
106 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
108 fl_get_button(dialog_->check_full_author_list);
110 fl_get_button(dialog_->check_force_uppercase);
112 command = biblio::getCiteCommand(styles[choice], full, force);
115 controller().params().setCmdName(command);
116 controller().params().setContents(getStringFromVector(citekeys));
118 string const after = getString(dialog_->input_after);
119 controller().params().setOptions(after);
123 void FormCitation::hide()
128 FormDialogView::hide();
132 void FormCitation::build()
134 dialog_.reset(build_citation(this));
136 // Manage the ok, apply, restore and cancel/close buttons
137 bcview().setOK(dialog_->button_ok);
138 bcview().setApply(dialog_->button_apply);
139 bcview().setCancel(dialog_->button_close);
140 bcview().setRestore(dialog_->button_restore);
142 // disable for read-only documents
143 bcview().addReadOnly(dialog_->button_add);
144 bcview().addReadOnly(dialog_->button_del);
145 bcview().addReadOnly(dialog_->button_up);
146 bcview().addReadOnly(dialog_->button_down);
147 bcview().addReadOnly(dialog_->choice_style);
148 bcview().addReadOnly(dialog_->input_before);
149 bcview().addReadOnly(dialog_->input_after);
150 bcview().addReadOnly(dialog_->check_full_author_list);
151 bcview().addReadOnly(dialog_->check_force_uppercase);
153 // trigger an input event for cut&paste with middle mouse button.
154 setPrehandler(dialog_->input_search);
155 setPrehandler(dialog_->input_before);
156 setPrehandler(dialog_->input_after);
158 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
159 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
160 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
162 //set up the tooltip mechanism
163 string str = _("Add the selected entry to the current citation reference.");
164 tooltips().init(dialog_->button_add, str);
166 str = _("Delete the selected entry from the current citation reference.");
167 tooltips().init(dialog_->button_del, str);
169 str = _("Move the selected entry upwards (in the current list).");
170 tooltips().init(dialog_->button_up, str);
172 str = _("Move the selected entry downwards (in the current list).");
173 tooltips().init(dialog_->button_down, str);
175 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
176 tooltips().init(dialog_->browser_cite, str);
177 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
178 // Work-around xforms' bug; enable tooltips for browser widgets.
179 setPrehandler(dialog_->browser_cite);
182 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.");
183 tooltips().init(dialog_->browser_bib, str);
184 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
185 // Work-around xforms' bug; enable tooltips for browser widgets.
186 setPrehandler(dialog_->browser_bib);
189 str = _("Information about the selected entry");
190 tooltips().init(dialog_->browser_info, str);
191 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
192 // Work-around xforms' bug; enable tooltips for browser widgets.
193 setPrehandler(dialog_->browser_info);
196 str = _("Here you may select how the citation label should look inside the text (Natbib).");
197 tooltips().init(dialog_->choice_style, str);
199 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
200 tooltips().init(dialog_->check_full_author_list, str);
202 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).");
203 tooltips().init(dialog_->check_force_uppercase, str);
205 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
206 tooltips().init(dialog_->input_before, str);
208 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
209 tooltips().init(dialog_->input_after, str);
211 str = _("Search your database (all fields will be searched).");
212 tooltips().init(dialog_->input_search, str);
214 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
215 tooltips().init(dialog_->check_search_case, str);
217 str = _("Activate if you want to enter Regular Expressions.");
218 tooltips().init(dialog_->check_search_type, str);
222 void FormCitation::findBiblio(biblio::Direction const dir)
224 string const str = getString(dialog_->input_search);
225 biblio::InfoMap const & theMap = controller().bibkeysInfo();
226 bool const caseSensitive =
227 fl_get_button(dialog_->check_search_case);
228 biblio::Search const type =
229 fl_get_button(dialog_->check_search_type) ?
230 biblio::REGEX : biblio::SIMPLE;
232 vector<string>::const_iterator start = bibkeys.begin();
233 int const sel = fl_get_browser(dialog_->browser_bib);
234 if (sel >= 1 && sel <= int(bibkeys.size()))
237 // Find the NEXT instance...
238 (dir == biblio::FORWARD) ? ++start : --start;
241 vector<string>::const_iterator const cit =
242 biblio::searchKeys(theMap, bibkeys, str,
243 start, type, dir, caseSensitive);
245 if (cit == bibkeys.end())
248 int const found = int(cit - bibkeys.begin()) + 1;
252 // Update the display
253 int const top = max(found - 5, 1);
254 fl_set_browser_topline(dialog_->browser_bib, top);
255 fl_select_browser_line(dialog_->browser_bib, found);
256 input(dialog_->browser_bib, 0);
260 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
262 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
264 biblio::InfoMap const & theMap = controller().bibkeysInfo();
267 if (!citekeys.empty()) topCitekey = citekeys[0];
269 if (ob == dialog_->browser_bib) {
270 fl_deselect_browser(dialog_->browser_cite);
272 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
273 if (sel < 1 || sel > bibkeys.size())
274 return ButtonPolicy::SMI_NOOP;
276 // Put into browser_info the additional info associated with
277 // the selected browser_bib key
278 fl_clear_browser(dialog_->browser_info);
280 string const tmp = formatted(biblio::getInfo(theMap,
282 dialog_->browser_info->w - 10);
283 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
285 // Highlight the selected browser_bib key in browser_cite if
287 vector<string>::const_iterator cit =
288 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
290 if (cit != citekeys.end()) {
291 int const n = int(cit - citekeys.begin());
292 fl_select_browser_line(dialog_->browser_cite, n + 1);
293 fl_set_browser_topline(dialog_->browser_cite, n + 1);
296 if (!kernel().isBufferReadonly()) {
297 if (cit != citekeys.end()) {
306 } else if (ob == dialog_->browser_cite) {
307 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
308 if (sel < 1 || sel > citekeys.size())
309 return ButtonPolicy::SMI_NOOP;
311 if (!kernel().isBufferReadonly()) {
316 // Highlight the selected browser_cite key in browser_bib
317 vector<string>::const_iterator cit =
318 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
320 if (cit != bibkeys.end()) {
321 int const n = int(cit - bibkeys.begin());
322 fl_select_browser_line(dialog_->browser_bib, n + 1);
323 fl_set_browser_topline(dialog_->browser_bib, n + 1);
325 // Put into browser_info the additional info associated
326 // with the selected browser_cite key
327 fl_clear_browser(dialog_->browser_info);
329 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
330 dialog_->browser_info->w - 10);
331 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
334 } else if (ob == dialog_->button_add) {
335 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
336 if (sel < 1 || sel > bibkeys.size())
337 return ButtonPolicy::SMI_NOOP;
339 // Add the selected browser_bib key to browser_cite
340 fl_addto_browser(dialog_->browser_cite,
341 bibkeys[sel - 1].c_str());
342 citekeys.push_back(bibkeys[sel - 1]);
344 int const n = int(citekeys.size());
345 fl_select_browser_line(dialog_->browser_cite, n);
349 activate = ButtonPolicy::SMI_VALID;
351 } else if (ob == dialog_->button_del) {
352 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
353 if (sel < 1 || sel > citekeys.size())
354 return ButtonPolicy::SMI_NOOP;
356 // Remove the selected key from browser_cite
357 fl_delete_browser_line(dialog_->browser_cite, sel) ;
358 citekeys.erase(citekeys.begin() + sel - 1);
362 activate = ButtonPolicy::SMI_VALID;
364 } else if (ob == dialog_->button_up) {
365 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
366 if (sel < 2 || sel > citekeys.size())
367 return ButtonPolicy::SMI_NOOP;
369 // Move the selected key up one line
370 vector<string>::iterator it = citekeys.begin() + sel - 1;
371 string const tmp = *it;
373 fl_delete_browser_line(dialog_->browser_cite, sel);
376 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
377 fl_select_browser_line(dialog_->browser_cite, sel - 1);
378 citekeys.insert(it - 1, tmp);
380 activate = ButtonPolicy::SMI_VALID;
382 } else if (ob == dialog_->button_down) {
383 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
384 if (sel < 1 || sel > citekeys.size() - 1)
385 return ButtonPolicy::SMI_NOOP;
387 // Move the selected key down one line
388 vector<string>::iterator it = citekeys.begin() + sel - 1;
389 string const tmp = *it;
391 fl_delete_browser_line(dialog_->browser_cite, sel);
394 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
395 fl_select_browser_line(dialog_->browser_cite, sel+1);
396 citekeys.insert(it+1, tmp);
398 activate = ButtonPolicy::SMI_VALID;
400 } else if (ob == dialog_->button_previous) {
401 findBiblio(biblio::BACKWARD);
402 } else if (ob == dialog_->button_next) {
403 findBiblio(biblio::FORWARD);
404 } else if (ob == dialog_->input_search) {
405 findBiblio(biblio::FORWARD);
406 } else if (ob == dialog_->choice_style ||
407 ob == dialog_->check_full_author_list ||
408 ob == dialog_->check_force_uppercase ||
409 ob == dialog_->input_before ||
410 ob == dialog_->input_after) {
411 activate = ButtonPolicy::SMI_VALID;
414 string currentCitekey;
415 if (!citekeys.empty())
416 currentCitekey = citekeys[0];
418 if (topCitekey != currentCitekey) {
419 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
420 fillChoice(dialog_.get(),
421 controller().getCiteStrings(currentCitekey));
422 fl_set_choice(dialog_->choice_style, choice);
429 void FormCitation::update()
431 // Make the list of all available bibliography keys
432 bibkeys = biblio::getKeys(controller().bibkeysInfo());
433 updateBrowser(dialog_->browser_bib, bibkeys);
435 // Ditto for the keys cited in this inset
436 citekeys = getVectorFromString(controller().params().getContents());
437 updateBrowser(dialog_->browser_cite, citekeys);
439 // Use the first citekey to fill choice_style
441 if (!citekeys.empty())
444 fillChoice(dialog_.get(), controller().getCiteStrings(key));
446 // Use the citation command to update the GUI
447 updateStyle(dialog_.get(), controller().params().getCmdName());
449 bool const natbib = controller().usingNatbib();
450 setEnabled(dialog_->check_full_author_list, natbib);
451 setEnabled(dialog_->check_force_uppercase, natbib);
452 setEnabled(dialog_->choice_style, natbib);
454 // No keys have been selected yet, so...
455 fl_clear_browser(dialog_->browser_info);
459 // Natbib can have comments before and after the citation.
460 // This is not yet supported. After only.
461 fl_set_input(dialog_->input_after,
462 controller().params().getOptions().c_str());
464 fl_set_input(dialog_->input_before, _("Not yet supported"));
465 setEnabled(dialog_->input_before, false);
469 void FormCitation::updateBrowser(FL_OBJECT * browser,
470 vector<string> const & keys) const
472 // Check whether the current contents of the browser will be
473 // changed by loading the contents of the vec...
474 vector<string> browser_keys = getVector(browser);
476 if (browser_keys == keys) {
477 fl_deselect_browser(browser);
478 fl_set_browser_topline(browser, 1);
482 // They will be changed. Proceed.
483 fl_clear_browser(browser);
485 for (vector<string>::const_iterator it = keys.begin();
486 it != keys.end(); ++it) {
487 string key = trim(*it);
489 fl_add_browser_line(browser, key.c_str());
494 void FormCitation::setBibButtons(State status) const
496 setEnabled(dialog_->button_add, (status == ON));
500 void FormCitation::setCiteButtons(State status) const
502 int const sel = fl_get_browser(dialog_->browser_cite);
503 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
504 bool const activate = (status == ON);
505 bool const activate_up = (activate && sel != 1);
506 bool const activate_down = (activate && sel != maxline);
508 setEnabled(dialog_->button_del, activate);
509 setEnabled(dialog_->button_up, activate_up);
510 setEnabled(dialog_->button_down, activate_down);