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"
28 using lyx::support::getStringFromVector;
29 using lyx::support::getVectorFromString;
30 using lyx::support::trim;
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_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 = getVector(dialog->choice_style);
56 if (vec == choice_style)
59 // They will be changed. Proceed
60 bool const noVec = vec.empty();
61 string const str = noVec ? string() : getStringFromVector(vec, "|");
63 fl_clear_choice(dialog->choice_style);
64 fl_addto_choice(dialog->choice_style, str.c_str());
65 setEnabled(dialog->choice_style, !noVec);
69 void updateStyle(FD_citation * dialog, string command)
71 // Find the style of the citekeys
72 vector<biblio::CiteStyle> const & styles =
73 ControlCitation::getCiteStyles();
74 biblio::CitationStyle const cs(command);
76 vector<biblio::CiteStyle>::const_iterator cit =
77 find(styles.begin(), styles.end(), cs.style);
79 // Use this to initialise the GUI
80 bool const noStyles = cit == styles.end();
81 int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
82 fl_set_choice(dialog->choice_style, index);
84 // Disable if there are no styles, otherwise use cs member settings.
85 fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
86 fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
92 typedef FormController<ControlCitation, FormView<FD_citation> > base_class;
95 FormCitation::FormCitation(Dialog & parent)
96 : base_class(parent, _("Citation"))
100 void FormCitation::apply()
102 string command = "cite";
103 if (isActive(dialog_->choice_style)) {
104 vector<biblio::CiteStyle> const & styles =
105 ControlCitation::getCiteStyles();
108 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
110 fl_get_button(dialog_->check_full_author_list);
112 fl_get_button(dialog_->check_force_uppercase);
115 biblio::CitationStyle(styles[choice], full, force)
120 controller().params().setCmdName(command);
121 controller().params().setContents(getStringFromVector(citekeys));
123 string const before = getString(dialog_->input_before);
124 controller().params().setSecOptions(before);
126 string const after = getString(dialog_->input_after);
127 controller().params().setOptions(after);
131 void FormCitation::hide()
136 FormDialogView::hide();
140 void FormCitation::build()
142 dialog_.reset(build_citation(this));
144 // Manage the ok, apply, restore and cancel/close buttons
145 bcview().setOK(dialog_->button_ok);
146 bcview().setApply(dialog_->button_apply);
147 bcview().setCancel(dialog_->button_close);
148 bcview().setRestore(dialog_->button_restore);
150 // disable for read-only documents
151 bcview().addReadOnly(dialog_->button_add);
152 bcview().addReadOnly(dialog_->button_del);
153 bcview().addReadOnly(dialog_->button_up);
154 bcview().addReadOnly(dialog_->button_down);
155 bcview().addReadOnly(dialog_->choice_style);
156 bcview().addReadOnly(dialog_->input_before);
157 bcview().addReadOnly(dialog_->input_after);
158 bcview().addReadOnly(dialog_->check_full_author_list);
159 bcview().addReadOnly(dialog_->check_force_uppercase);
161 // trigger an input event for cut&paste with middle mouse button.
162 setPrehandler(dialog_->input_search);
163 setPrehandler(dialog_->input_before);
164 setPrehandler(dialog_->input_after);
166 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
167 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
168 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
170 //set up the tooltip mechanism
171 string str = _("Add the selected entry to the current citation reference.");
172 tooltips().init(dialog_->button_add, str);
174 str = _("Delete the selected entry from the current citation reference.");
175 tooltips().init(dialog_->button_del, str);
177 str = _("Move the selected entry upwards (in the current list).");
178 tooltips().init(dialog_->button_up, str);
180 str = _("Move the selected entry downwards (in the current list).");
181 tooltips().init(dialog_->button_down, str);
183 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
184 tooltips().init(dialog_->browser_cite, str);
185 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
186 // Work-around xforms' bug; enable tooltips for browser widgets.
187 setPrehandler(dialog_->browser_cite);
190 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.");
191 tooltips().init(dialog_->browser_bib, str);
192 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
193 // Work-around xforms' bug; enable tooltips for browser widgets.
194 setPrehandler(dialog_->browser_bib);
197 str = _("Information about the selected entry");
198 tooltips().init(dialog_->browser_info, str);
199 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
200 // Work-around xforms' bug; enable tooltips for browser widgets.
201 setPrehandler(dialog_->browser_info);
204 str = _("Here you may select how the citation label should look inside the text (Natbib).");
205 tooltips().init(dialog_->choice_style, str);
207 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
208 tooltips().init(dialog_->check_full_author_list, str);
210 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).");
211 tooltips().init(dialog_->check_force_uppercase, str);
213 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
214 tooltips().init(dialog_->input_before, str);
216 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
217 tooltips().init(dialog_->input_after, str);
219 str = _("Search your database (all fields will be searched).");
220 tooltips().init(dialog_->input_search, str);
222 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
223 tooltips().init(dialog_->check_search_case, str);
225 str = _("Activate if you want to enter Regular Expressions.");
226 tooltips().init(dialog_->check_search_type, str);
230 void FormCitation::findBiblio(biblio::Direction const dir)
232 string const str = getString(dialog_->input_search);
233 biblio::InfoMap const & theMap = controller().bibkeysInfo();
234 bool const caseSensitive =
235 fl_get_button(dialog_->check_search_case);
236 biblio::Search const type =
237 fl_get_button(dialog_->check_search_type) ?
238 biblio::REGEX : biblio::SIMPLE;
240 vector<string>::const_iterator start = bibkeys.begin();
241 int const sel = fl_get_browser(dialog_->browser_bib);
242 if (sel >= 1 && sel <= int(bibkeys.size()))
245 // Find the NEXT instance...
246 (dir == biblio::FORWARD) ? ++start : --start;
249 vector<string>::const_iterator const cit =
250 biblio::searchKeys(theMap, bibkeys, str,
251 start, type, dir, caseSensitive);
253 if (cit == bibkeys.end())
256 int const found = int(cit - bibkeys.begin()) + 1;
260 // Update the display
261 int const top = max(found - 5, 1);
262 fl_set_browser_topline(dialog_->browser_bib, top);
263 fl_select_browser_line(dialog_->browser_bib, found);
264 input(dialog_->browser_bib, 0);
268 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
270 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
272 biblio::InfoMap const & theMap = controller().bibkeysInfo();
275 if (!citekeys.empty()) topCitekey = citekeys[0];
277 if (ob == dialog_->browser_bib) {
278 fl_deselect_browser(dialog_->browser_cite);
280 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
281 if (sel < 1 || sel > bibkeys.size())
282 return ButtonPolicy::SMI_NOOP;
284 // Put into browser_info the additional info associated with
285 // the selected browser_bib key
286 fl_clear_browser(dialog_->browser_info);
288 string const tmp = formatted(biblio::getInfo(theMap,
290 dialog_->browser_info->w - 10);
291 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
293 // Highlight the selected browser_bib key in browser_cite if
295 vector<string>::const_iterator cit =
296 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
298 if (cit != citekeys.end()) {
299 int const n = int(cit - citekeys.begin());
300 fl_select_browser_line(dialog_->browser_cite, n + 1);
301 fl_set_browser_topline(dialog_->browser_cite, n + 1);
304 if (!kernel().isBufferReadonly()) {
305 if (cit != citekeys.end()) {
314 } else if (ob == dialog_->browser_cite) {
315 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
316 if (sel < 1 || sel > citekeys.size())
317 return ButtonPolicy::SMI_NOOP;
319 if (!kernel().isBufferReadonly()) {
324 // Highlight the selected browser_cite key in browser_bib
325 vector<string>::const_iterator cit =
326 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
328 if (cit != bibkeys.end()) {
329 int const n = int(cit - bibkeys.begin());
330 fl_select_browser_line(dialog_->browser_bib, n + 1);
331 fl_set_browser_topline(dialog_->browser_bib, n + 1);
333 // Put into browser_info the additional info associated
334 // with the selected browser_cite key
335 fl_clear_browser(dialog_->browser_info);
337 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
338 dialog_->browser_info->w - 10);
339 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
342 } else if (ob == dialog_->button_add) {
343 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
344 if (sel < 1 || sel > bibkeys.size())
345 return ButtonPolicy::SMI_NOOP;
347 // Add the selected browser_bib key to browser_cite
348 fl_addto_browser(dialog_->browser_cite,
349 bibkeys[sel - 1].c_str());
350 citekeys.push_back(bibkeys[sel - 1]);
352 int const n = int(citekeys.size());
353 fl_select_browser_line(dialog_->browser_cite, n);
357 activate = ButtonPolicy::SMI_VALID;
359 } else if (ob == dialog_->button_del) {
360 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
361 if (sel < 1 || sel > citekeys.size())
362 return ButtonPolicy::SMI_NOOP;
364 // Remove the selected key from browser_cite
365 fl_delete_browser_line(dialog_->browser_cite, sel) ;
366 citekeys.erase(citekeys.begin() + sel - 1);
370 activate = ButtonPolicy::SMI_VALID;
372 } else if (ob == dialog_->button_up) {
373 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
374 if (sel < 2 || sel > citekeys.size())
375 return ButtonPolicy::SMI_NOOP;
377 // Move the selected key up one line
378 vector<string>::iterator it = citekeys.begin() + sel - 1;
379 string const tmp = *it;
381 fl_delete_browser_line(dialog_->browser_cite, sel);
384 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
385 fl_select_browser_line(dialog_->browser_cite, sel - 1);
386 citekeys.insert(it - 1, tmp);
388 activate = ButtonPolicy::SMI_VALID;
390 } else if (ob == dialog_->button_down) {
391 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
392 if (sel < 1 || sel > citekeys.size() - 1)
393 return ButtonPolicy::SMI_NOOP;
395 // Move the selected key down one line
396 vector<string>::iterator it = citekeys.begin() + sel - 1;
397 string const tmp = *it;
399 fl_delete_browser_line(dialog_->browser_cite, sel);
402 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
403 fl_select_browser_line(dialog_->browser_cite, sel+1);
404 citekeys.insert(it+1, tmp);
406 activate = ButtonPolicy::SMI_VALID;
408 } else if (ob == dialog_->button_previous) {
409 findBiblio(biblio::BACKWARD);
410 } else if (ob == dialog_->button_next) {
411 findBiblio(biblio::FORWARD);
412 } else if (ob == dialog_->input_search) {
413 findBiblio(biblio::FORWARD);
414 } else if (ob == dialog_->choice_style ||
415 ob == dialog_->check_full_author_list ||
416 ob == dialog_->check_force_uppercase ||
417 ob == dialog_->input_before ||
418 ob == dialog_->input_after) {
419 activate = ButtonPolicy::SMI_VALID;
422 string currentCitekey;
423 if (!citekeys.empty())
424 currentCitekey = citekeys[0];
426 if (topCitekey != currentCitekey) {
427 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
428 fillChoice(dialog_.get(),
429 controller().getCiteStrings(currentCitekey));
430 fl_set_choice(dialog_->choice_style, choice);
437 void FormCitation::update()
439 // Make the list of all available bibliography keys
440 bibkeys = biblio::getKeys(controller().bibkeysInfo());
441 updateBrowser(dialog_->browser_bib, bibkeys);
443 // Ditto for the keys cited in this inset
444 citekeys = getVectorFromString(controller().params().getContents());
445 updateBrowser(dialog_->browser_cite, citekeys);
447 // Use the first citekey to fill choice_style
449 if (!citekeys.empty())
452 fillChoice(dialog_.get(), controller().getCiteStrings(key));
454 // Use the citation command to update the GUI
455 updateStyle(dialog_.get(), controller().params().getCmdName());
457 biblio::CiteEngine const engine = controller().getEngine();
458 bool const natbib_engine =
459 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
460 engine == biblio::ENGINE_NATBIB_NUMERICAL;
461 bool const basic_engine = engine == biblio::ENGINE_BASIC;
463 setEnabled(dialog_->check_full_author_list, natbib_engine);
464 setEnabled(dialog_->check_force_uppercase, natbib_engine);
465 setEnabled(dialog_->choice_style, !basic_engine);
466 setEnabled(dialog_->input_before, !basic_engine);
468 // No keys have been selected yet, so...
469 fl_clear_browser(dialog_->browser_info);
473 // Natbib can have comments before and after the citation.
474 fl_set_input(dialog_->input_after,
475 controller().params().getOptions().c_str());
477 fl_set_input(dialog_->input_before,
478 controller().params().getSecOptions().c_str());
482 void FormCitation::updateBrowser(FL_OBJECT * browser,
483 vector<string> const & keys) const
485 // Check whether the current contents of the browser will be
486 // changed by loading the contents of the vec...
487 vector<string> browser_keys = getVector(browser);
489 if (browser_keys == keys) {
490 fl_deselect_browser(browser);
491 fl_set_browser_topline(browser, 1);
495 // They will be changed. Proceed.
496 fl_clear_browser(browser);
498 for (vector<string>::const_iterator it = keys.begin();
499 it != keys.end(); ++it) {
500 string key = trim(*it);
502 fl_add_browser_line(browser, key.c_str());
507 void FormCitation::setBibButtons(State status) const
509 setEnabled(dialog_->button_add, (status == ON));
513 void FormCitation::setCiteButtons(State status) const
515 int const sel = fl_get_browser(dialog_->browser_cite);
516 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
517 bool const activate = (status == ON);
518 bool const activate_up = (activate && sel != 1);
519 bool const activate_down = (activate && sel != maxline);
521 setEnabled(dialog_->button_del, activate);
522 setEnabled(dialog_->button_up, activate_up);
523 setEnabled(dialog_->button_down, activate_down);