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 "support/lstrings.h"
24 #include "lyx_forms.h"
26 using lyx::support::getStringFromVector;
27 using lyx::support::getVectorFromString;
28 using lyx::support::trim;
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 before = getString(dialog_->input_before);
119 controller().params().setSecOptions(before);
121 string const after = getString(dialog_->input_after);
122 controller().params().setOptions(after);
126 void FormCitation::hide()
131 FormDialogView::hide();
135 void FormCitation::build()
137 dialog_.reset(build_citation(this));
139 // Manage the ok, apply, restore and cancel/close buttons
140 bcview().setOK(dialog_->button_ok);
141 bcview().setApply(dialog_->button_apply);
142 bcview().setCancel(dialog_->button_close);
143 bcview().setRestore(dialog_->button_restore);
145 // disable for read-only documents
146 bcview().addReadOnly(dialog_->button_add);
147 bcview().addReadOnly(dialog_->button_del);
148 bcview().addReadOnly(dialog_->button_up);
149 bcview().addReadOnly(dialog_->button_down);
150 bcview().addReadOnly(dialog_->choice_style);
151 bcview().addReadOnly(dialog_->input_before);
152 bcview().addReadOnly(dialog_->input_after);
153 bcview().addReadOnly(dialog_->check_full_author_list);
154 bcview().addReadOnly(dialog_->check_force_uppercase);
156 // trigger an input event for cut&paste with middle mouse button.
157 setPrehandler(dialog_->input_search);
158 setPrehandler(dialog_->input_before);
159 setPrehandler(dialog_->input_after);
161 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
162 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
163 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
165 //set up the tooltip mechanism
166 string str = _("Add the selected entry to the current citation reference.");
167 tooltips().init(dialog_->button_add, str);
169 str = _("Delete the selected entry from the current citation reference.");
170 tooltips().init(dialog_->button_del, str);
172 str = _("Move the selected entry upwards (in the current list).");
173 tooltips().init(dialog_->button_up, str);
175 str = _("Move the selected entry downwards (in the current list).");
176 tooltips().init(dialog_->button_down, str);
178 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
179 tooltips().init(dialog_->browser_cite, str);
180 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
181 // Work-around xforms' bug; enable tooltips for browser widgets.
182 setPrehandler(dialog_->browser_cite);
185 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.");
186 tooltips().init(dialog_->browser_bib, str);
187 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
188 // Work-around xforms' bug; enable tooltips for browser widgets.
189 setPrehandler(dialog_->browser_bib);
192 str = _("Information about the selected entry");
193 tooltips().init(dialog_->browser_info, str);
194 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
195 // Work-around xforms' bug; enable tooltips for browser widgets.
196 setPrehandler(dialog_->browser_info);
199 str = _("Here you may select how the citation label should look inside the text (Natbib).");
200 tooltips().init(dialog_->choice_style, str);
202 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
203 tooltips().init(dialog_->check_full_author_list, str);
205 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).");
206 tooltips().init(dialog_->check_force_uppercase, str);
208 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
209 tooltips().init(dialog_->input_before, str);
211 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
212 tooltips().init(dialog_->input_after, str);
214 str = _("Search your database (all fields will be searched).");
215 tooltips().init(dialog_->input_search, str);
217 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
218 tooltips().init(dialog_->check_search_case, str);
220 str = _("Activate if you want to enter Regular Expressions.");
221 tooltips().init(dialog_->check_search_type, str);
225 void FormCitation::findBiblio(biblio::Direction const dir)
227 string const str = getString(dialog_->input_search);
228 biblio::InfoMap const & theMap = controller().bibkeysInfo();
229 bool const caseSensitive =
230 fl_get_button(dialog_->check_search_case);
231 biblio::Search const type =
232 fl_get_button(dialog_->check_search_type) ?
233 biblio::REGEX : biblio::SIMPLE;
235 vector<string>::const_iterator start = bibkeys.begin();
236 int const sel = fl_get_browser(dialog_->browser_bib);
237 if (sel >= 1 && sel <= int(bibkeys.size()))
240 // Find the NEXT instance...
241 (dir == biblio::FORWARD) ? ++start : --start;
244 vector<string>::const_iterator const cit =
245 biblio::searchKeys(theMap, bibkeys, str,
246 start, type, dir, caseSensitive);
248 if (cit == bibkeys.end())
251 int const found = int(cit - bibkeys.begin()) + 1;
255 // Update the display
256 int const top = max(found - 5, 1);
257 fl_set_browser_topline(dialog_->browser_bib, top);
258 fl_select_browser_line(dialog_->browser_bib, found);
259 input(dialog_->browser_bib, 0);
263 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
265 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
267 biblio::InfoMap const & theMap = controller().bibkeysInfo();
270 if (!citekeys.empty()) topCitekey = citekeys[0];
272 if (ob == dialog_->browser_bib) {
273 fl_deselect_browser(dialog_->browser_cite);
275 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
276 if (sel < 1 || sel > bibkeys.size())
277 return ButtonPolicy::SMI_NOOP;
279 // Put into browser_info the additional info associated with
280 // the selected browser_bib key
281 fl_clear_browser(dialog_->browser_info);
283 string const tmp = formatted(biblio::getInfo(theMap,
285 dialog_->browser_info->w - 10);
286 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
288 // Highlight the selected browser_bib key in browser_cite if
290 vector<string>::const_iterator cit =
291 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
293 if (cit != citekeys.end()) {
294 int const n = int(cit - citekeys.begin());
295 fl_select_browser_line(dialog_->browser_cite, n + 1);
296 fl_set_browser_topline(dialog_->browser_cite, n + 1);
299 if (!kernel().isBufferReadonly()) {
300 if (cit != citekeys.end()) {
309 } else if (ob == dialog_->browser_cite) {
310 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
311 if (sel < 1 || sel > citekeys.size())
312 return ButtonPolicy::SMI_NOOP;
314 if (!kernel().isBufferReadonly()) {
319 // Highlight the selected browser_cite key in browser_bib
320 vector<string>::const_iterator cit =
321 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
323 if (cit != bibkeys.end()) {
324 int const n = int(cit - bibkeys.begin());
325 fl_select_browser_line(dialog_->browser_bib, n + 1);
326 fl_set_browser_topline(dialog_->browser_bib, n + 1);
328 // Put into browser_info the additional info associated
329 // with the selected browser_cite key
330 fl_clear_browser(dialog_->browser_info);
332 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
333 dialog_->browser_info->w - 10);
334 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
337 } else if (ob == dialog_->button_add) {
338 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
339 if (sel < 1 || sel > bibkeys.size())
340 return ButtonPolicy::SMI_NOOP;
342 // Add the selected browser_bib key to browser_cite
343 fl_addto_browser(dialog_->browser_cite,
344 bibkeys[sel - 1].c_str());
345 citekeys.push_back(bibkeys[sel - 1]);
347 int const n = int(citekeys.size());
348 fl_select_browser_line(dialog_->browser_cite, n);
352 activate = ButtonPolicy::SMI_VALID;
354 } else if (ob == dialog_->button_del) {
355 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
356 if (sel < 1 || sel > citekeys.size())
357 return ButtonPolicy::SMI_NOOP;
359 // Remove the selected key from browser_cite
360 fl_delete_browser_line(dialog_->browser_cite, sel) ;
361 citekeys.erase(citekeys.begin() + sel - 1);
365 activate = ButtonPolicy::SMI_VALID;
367 } else if (ob == dialog_->button_up) {
368 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
369 if (sel < 2 || sel > citekeys.size())
370 return ButtonPolicy::SMI_NOOP;
372 // Move the selected key up one line
373 vector<string>::iterator it = citekeys.begin() + sel - 1;
374 string const tmp = *it;
376 fl_delete_browser_line(dialog_->browser_cite, sel);
379 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
380 fl_select_browser_line(dialog_->browser_cite, sel - 1);
381 citekeys.insert(it - 1, tmp);
383 activate = ButtonPolicy::SMI_VALID;
385 } else if (ob == dialog_->button_down) {
386 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
387 if (sel < 1 || sel > citekeys.size() - 1)
388 return ButtonPolicy::SMI_NOOP;
390 // Move the selected key down one line
391 vector<string>::iterator it = citekeys.begin() + sel - 1;
392 string const tmp = *it;
394 fl_delete_browser_line(dialog_->browser_cite, sel);
397 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
398 fl_select_browser_line(dialog_->browser_cite, sel+1);
399 citekeys.insert(it+1, tmp);
401 activate = ButtonPolicy::SMI_VALID;
403 } else if (ob == dialog_->button_previous) {
404 findBiblio(biblio::BACKWARD);
405 } else if (ob == dialog_->button_next) {
406 findBiblio(biblio::FORWARD);
407 } else if (ob == dialog_->input_search) {
408 findBiblio(biblio::FORWARD);
409 } else if (ob == dialog_->choice_style ||
410 ob == dialog_->check_full_author_list ||
411 ob == dialog_->check_force_uppercase ||
412 ob == dialog_->input_before ||
413 ob == dialog_->input_after) {
414 activate = ButtonPolicy::SMI_VALID;
417 string currentCitekey;
418 if (!citekeys.empty())
419 currentCitekey = citekeys[0];
421 if (topCitekey != currentCitekey) {
422 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
423 fillChoice(dialog_.get(),
424 controller().getCiteStrings(currentCitekey));
425 fl_set_choice(dialog_->choice_style, choice);
432 void FormCitation::update()
434 // Make the list of all available bibliography keys
435 bibkeys = biblio::getKeys(controller().bibkeysInfo());
436 updateBrowser(dialog_->browser_bib, bibkeys);
438 // Ditto for the keys cited in this inset
439 citekeys = getVectorFromString(controller().params().getContents());
440 updateBrowser(dialog_->browser_cite, citekeys);
442 // Use the first citekey to fill choice_style
444 if (!citekeys.empty())
447 fillChoice(dialog_.get(), controller().getCiteStrings(key));
449 // Use the citation command to update the GUI
450 updateStyle(dialog_.get(), controller().params().getCmdName());
452 bool const natbib = controller().usingNatbib();
453 setEnabled(dialog_->check_full_author_list, natbib);
454 setEnabled(dialog_->check_force_uppercase, natbib);
455 setEnabled(dialog_->choice_style, natbib);
456 setEnabled(dialog_->input_before, natbib);
458 // No keys have been selected yet, so...
459 fl_clear_browser(dialog_->browser_info);
463 // Natbib can have comments before and after the citation.
464 fl_set_input(dialog_->input_after,
465 controller().params().getOptions().c_str());
467 fl_set_input(dialog_->input_before,
468 controller().params().getSecOptions().c_str());
472 void FormCitation::updateBrowser(FL_OBJECT * browser,
473 vector<string> const & keys) const
475 // Check whether the current contents of the browser will be
476 // changed by loading the contents of the vec...
477 vector<string> browser_keys = getVector(browser);
479 if (browser_keys == keys) {
480 fl_deselect_browser(browser);
481 fl_set_browser_topline(browser, 1);
485 // They will be changed. Proceed.
486 fl_clear_browser(browser);
488 for (vector<string>::const_iterator it = keys.begin();
489 it != keys.end(); ++it) {
490 string key = trim(*it);
492 fl_add_browser_line(browser, key.c_str());
497 void FormCitation::setBibButtons(State status) const
499 setEnabled(dialog_->button_add, (status == ON));
503 void FormCitation::setCiteButtons(State status) const
505 int const sel = fl_get_browser(dialog_->browser_cite);
506 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
507 bool const activate = (status == ON);
508 bool const activate_up = (activate && sel != 1);
509 bool const activate_down = (activate && sel != maxline);
511 setEnabled(dialog_->button_del, activate);
512 setEnabled(dialog_->button_up, activate_up);
513 setEnabled(dialog_->button_down, activate_down);