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.
14 #include "FormCitation.h"
15 #include "ControlCitation.h"
16 #include "forms/form_citation.h"
19 #include "xforms_helpers.h"
22 #include "bufferparams.h"
24 #include "support/lstrings.h"
26 #include "lyx_forms.h"
37 using support::getStringFromVector;
38 using support::getVectorFromString;
45 // shamelessly stolen from Menubar_pimpl.C
46 int string_width(string const & str)
48 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
50 static_cast<int>(str.length()));
54 void fillChoice(FD_citation * dialog, vector<string> vec)
56 // Check whether the current contents of the browser will be
57 // changed by loading the contents of the vec...
58 vector<string> const choice_style = getVector(dialog->choice_style);
60 if (vec == choice_style)
63 // They will be changed. Proceed
64 bool const noVec = vec.empty();
65 string const str = noVec ? string() : getStringFromVector(vec, "|");
67 fl_clear_choice(dialog->choice_style);
68 fl_addto_choice(dialog->choice_style, str.c_str());
69 setEnabled(dialog->choice_style, !noVec);
73 void updateStyle(FD_citation * dialog, string command)
75 // Find the style of the citekeys
76 vector<biblio::CiteStyle> const & styles =
77 ControlCitation::getCiteStyles();
78 biblio::CitationStyle const cs(command);
80 vector<biblio::CiteStyle>::const_iterator cit =
81 find(styles.begin(), styles.end(), cs.style);
83 // Use this to initialise the GUI
84 bool const noStyles = cit == styles.end();
85 int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
86 fl_set_choice(dialog->choice_style, index);
88 // Disable if there are no styles, otherwise use cs member settings.
89 fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
90 fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
96 typedef FormController<ControlCitation, FormView<FD_citation> > base_class;
99 FormCitation::FormCitation(Dialog & parent)
100 : base_class(parent, _("Citation"))
104 void FormCitation::apply()
106 string command = "cite";
107 if (isActive(dialog_->choice_style)) {
108 vector<biblio::CiteStyle> const & styles =
109 ControlCitation::getCiteStyles();
112 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
114 fl_get_button(dialog_->check_full_author_list);
116 fl_get_button(dialog_->check_force_uppercase);
119 biblio::CitationStyle(styles[choice], full, force)
124 controller().params().setCmdName(command);
125 controller().params().setContents(getStringFromVector(citekeys));
127 string const before = getString(dialog_->input_before);
128 controller().params().setSecOptions(before);
130 string const after = getString(dialog_->input_after);
131 controller().params().setOptions(after);
135 void FormCitation::hide()
140 FormDialogView::hide();
144 void FormCitation::build()
146 dialog_.reset(build_citation(this));
148 // Manage the ok, apply, restore and cancel/close buttons
149 bcview().setOK(dialog_->button_ok);
150 bcview().setApply(dialog_->button_apply);
151 bcview().setCancel(dialog_->button_close);
152 bcview().setRestore(dialog_->button_restore);
154 // disable for read-only documents
155 bcview().addReadOnly(dialog_->button_add);
156 bcview().addReadOnly(dialog_->button_del);
157 bcview().addReadOnly(dialog_->button_up);
158 bcview().addReadOnly(dialog_->button_down);
159 bcview().addReadOnly(dialog_->choice_style);
160 bcview().addReadOnly(dialog_->input_before);
161 bcview().addReadOnly(dialog_->input_after);
162 bcview().addReadOnly(dialog_->check_full_author_list);
163 bcview().addReadOnly(dialog_->check_force_uppercase);
165 // trigger an input event for cut&paste with middle mouse button.
166 setPrehandler(dialog_->input_search);
167 setPrehandler(dialog_->input_before);
168 setPrehandler(dialog_->input_after);
170 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
171 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
172 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
174 //set up the tooltip mechanism
175 string str = _("Add the selected entry to the current citation reference.");
176 tooltips().init(dialog_->button_add, str);
178 str = _("Delete the selected entry from the current citation reference.");
179 tooltips().init(dialog_->button_del, str);
181 str = _("Move the selected entry upwards (in the current list).");
182 tooltips().init(dialog_->button_up, str);
184 str = _("Move the selected entry downwards (in the current list).");
185 tooltips().init(dialog_->button_down, str);
187 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
188 tooltips().init(dialog_->browser_cite, str);
189 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
190 // Work-around xforms' bug; enable tooltips for browser widgets.
191 setPrehandler(dialog_->browser_cite);
194 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.");
195 tooltips().init(dialog_->browser_bib, str);
196 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
197 // Work-around xforms' bug; enable tooltips for browser widgets.
198 setPrehandler(dialog_->browser_bib);
201 str = _("Information about the selected entry");
202 tooltips().init(dialog_->browser_info, str);
203 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
204 // Work-around xforms' bug; enable tooltips for browser widgets.
205 setPrehandler(dialog_->browser_info);
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 = getString(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 (!kernel().isBufferReadonly()) {
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 (!kernel().isBufferReadonly()) {
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, citekeys[sel - 1]),
342 dialog_->browser_info->w - 10);
343 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
346 } else if (ob == dialog_->button_add) {
347 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
348 if (sel < 1 || sel > bibkeys.size())
349 return ButtonPolicy::SMI_NOOP;
351 // Add the selected browser_bib key to browser_cite
352 fl_addto_browser(dialog_->browser_cite,
353 bibkeys[sel - 1].c_str());
354 citekeys.push_back(bibkeys[sel - 1]);
356 int const n = int(citekeys.size());
357 fl_select_browser_line(dialog_->browser_cite, n);
361 activate = ButtonPolicy::SMI_VALID;
363 } else if (ob == dialog_->button_del) {
364 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
365 if (sel < 1 || sel > citekeys.size())
366 return ButtonPolicy::SMI_NOOP;
368 // Remove the selected key from browser_cite
369 fl_delete_browser_line(dialog_->browser_cite, sel) ;
370 citekeys.erase(citekeys.begin() + sel - 1);
374 activate = ButtonPolicy::SMI_VALID;
376 } else if (ob == dialog_->button_up) {
377 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
378 if (sel < 2 || sel > citekeys.size())
379 return ButtonPolicy::SMI_NOOP;
381 // Move the selected key up one line
382 vector<string>::iterator it = citekeys.begin() + sel - 1;
383 string const tmp = *it;
385 fl_delete_browser_line(dialog_->browser_cite, sel);
388 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
389 fl_select_browser_line(dialog_->browser_cite, sel - 1);
390 citekeys.insert(it - 1, tmp);
392 activate = ButtonPolicy::SMI_VALID;
394 } else if (ob == dialog_->button_down) {
395 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
396 if (sel < 1 || sel > citekeys.size() - 1)
397 return ButtonPolicy::SMI_NOOP;
399 // Move the selected key down one line
400 vector<string>::iterator it = citekeys.begin() + sel - 1;
401 string const tmp = *it;
403 fl_delete_browser_line(dialog_->browser_cite, sel);
406 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
407 fl_select_browser_line(dialog_->browser_cite, sel+1);
408 citekeys.insert(it+1, tmp);
410 activate = ButtonPolicy::SMI_VALID;
412 } else if (ob == dialog_->button_previous) {
413 findBiblio(biblio::BACKWARD);
414 } else if (ob == dialog_->button_next) {
415 findBiblio(biblio::FORWARD);
416 } else if (ob == dialog_->input_search) {
417 findBiblio(biblio::FORWARD);
418 } else if (ob == dialog_->choice_style ||
419 ob == dialog_->check_full_author_list ||
420 ob == dialog_->check_force_uppercase ||
421 ob == dialog_->input_before ||
422 ob == dialog_->input_after) {
423 activate = ButtonPolicy::SMI_VALID;
426 string currentCitekey;
427 if (!citekeys.empty())
428 currentCitekey = citekeys[0];
430 if (topCitekey != currentCitekey) {
431 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
432 fillChoice(dialog_.get(),
433 controller().getCiteStrings(currentCitekey));
434 fl_set_choice(dialog_->choice_style, choice);
437 // Invalid if no citation has been chosen
438 if (fl_get_browser_maxline(dialog_->browser_cite) < 1)
439 activate = ButtonPolicy::SMI_INVALID;
445 void FormCitation::update()
447 // Make the list of all available bibliography keys
448 bibkeys = biblio::getKeys(controller().bibkeysInfo());
449 updateBrowser(dialog_->browser_bib, bibkeys);
451 // Ditto for the keys cited in this inset
452 citekeys = getVectorFromString(controller().params().getContents());
453 updateBrowser(dialog_->browser_cite, citekeys);
455 // Use the first citekey to fill choice_style
457 if (!citekeys.empty())
460 fillChoice(dialog_.get(), controller().getCiteStrings(key));
462 // Use the citation command to update the GUI
463 updateStyle(dialog_.get(), controller().params().getCmdName());
465 biblio::CiteEngine const engine = controller().getEngine();
466 bool const natbib_engine =
467 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
468 engine == biblio::ENGINE_NATBIB_NUMERICAL;
469 bool const basic_engine = engine == biblio::ENGINE_BASIC;
471 setEnabled(dialog_->check_full_author_list, natbib_engine);
472 setEnabled(dialog_->check_force_uppercase, natbib_engine);
473 setEnabled(dialog_->choice_style, !basic_engine);
474 setEnabled(dialog_->input_before, !basic_engine);
476 // No keys have been selected yet, so...
477 fl_clear_browser(dialog_->browser_info);
481 // Natbib can have comments before and after the citation.
482 fl_set_input(dialog_->input_after,
483 controller().params().getOptions().c_str());
485 fl_set_input(dialog_->input_before,
486 controller().params().getSecOptions().c_str());
490 void FormCitation::updateBrowser(FL_OBJECT * browser,
491 vector<string> const & keys) const
493 // Check whether the current contents of the browser will be
494 // changed by loading the contents of the vec...
495 vector<string> browser_keys = getVector(browser);
497 if (browser_keys == keys) {
498 fl_deselect_browser(browser);
499 fl_set_browser_topline(browser, 1);
503 // They will be changed. Proceed.
504 fl_clear_browser(browser);
506 for (vector<string>::const_iterator it = keys.begin();
507 it != keys.end(); ++it) {
508 string key = trim(*it);
510 fl_add_browser_line(browser, key.c_str());
515 void FormCitation::setBibButtons(State status) const
517 setEnabled(dialog_->button_add, (status == ON));
521 void FormCitation::setCiteButtons(State status) const
523 int const sel = fl_get_browser(dialog_->browser_cite);
524 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
525 bool const activate = (status == ON);
526 bool const activate_up = (activate && sel != 1);
527 bool const activate_down = (activate && sel != maxline);
529 setEnabled(dialog_->button_del, activate);
530 setEnabled(dialog_->button_up, activate_up);
531 setEnabled(dialog_->button_down, activate_down);
534 } // namespace frontend