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;
38 // shamelessly stolen from Menubar_pimpl.C
39 int string_width(string const & str)
41 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
43 static_cast<int>(str.length()));
47 void fillChoice(FD_citation * dialog, vector<string> vec)
49 // Check whether the current contents of the browser will be
50 // changed by loading the contents of the vec...
51 vector<string> const choice_style = getVector(dialog->choice_style);
53 if (vec == choice_style)
56 // They will be changed. Proceed
57 bool const noVec = vec.empty();
58 string const str = noVec ? string() : getStringFromVector(vec, "|");
60 fl_clear_choice(dialog->choice_style);
61 fl_addto_choice(dialog->choice_style, str.c_str());
62 setEnabled(dialog->choice_style, !noVec);
66 void updateStyle(FD_citation * dialog, string command)
68 // Find the style of the citekeys
69 vector<biblio::CiteStyle> const & styles =
70 ControlCitation::getCiteStyles();
71 biblio::CitationStyle cs = biblio::getCitationStyle(command);
73 vector<biblio::CiteStyle>::const_iterator cit =
74 find(styles.begin(), styles.end(), cs.style);
76 // Use this to initialise the GUI
77 bool const noStyles = cit == styles.end();
78 int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
79 fl_set_choice(dialog->choice_style, index);
81 // Disable if there are no styles, otherwise use cs member settings.
82 fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
83 fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
89 typedef FormController<ControlCitation, FormView<FD_citation> > base_class;
92 FormCitation::FormCitation(Dialog & parent)
93 : base_class(parent, _("Citation"))
97 void FormCitation::apply()
99 string command = "cite";
100 if (isActive(dialog_->choice_style)) {
101 vector<biblio::CiteStyle> const & styles =
102 ControlCitation::getCiteStyles();
105 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
107 fl_get_button(dialog_->check_full_author_list);
109 fl_get_button(dialog_->check_force_uppercase);
111 command = biblio::getCiteCommand(styles[choice], full, force);
114 controller().params().setCmdName(command);
115 controller().params().setContents(getStringFromVector(citekeys));
117 string const after = getString(dialog_->input_after);
118 controller().params().setOptions(after);
122 void FormCitation::hide()
127 FormDialogView::hide();
131 void FormCitation::build()
133 dialog_.reset(build_citation(this));
135 // Manage the ok, apply, restore and cancel/close buttons
136 bcview().setOK(dialog_->button_ok);
137 bcview().setApply(dialog_->button_apply);
138 bcview().setCancel(dialog_->button_close);
139 bcview().setRestore(dialog_->button_restore);
141 // disable for read-only documents
142 bcview().addReadOnly(dialog_->button_add);
143 bcview().addReadOnly(dialog_->button_del);
144 bcview().addReadOnly(dialog_->button_up);
145 bcview().addReadOnly(dialog_->button_down);
146 bcview().addReadOnly(dialog_->choice_style);
147 bcview().addReadOnly(dialog_->input_before);
148 bcview().addReadOnly(dialog_->input_after);
149 bcview().addReadOnly(dialog_->check_full_author_list);
150 bcview().addReadOnly(dialog_->check_force_uppercase);
152 // trigger an input event for cut&paste with middle mouse button.
153 setPrehandler(dialog_->input_search);
154 setPrehandler(dialog_->input_before);
155 setPrehandler(dialog_->input_after);
157 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
158 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
159 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
161 //set up the tooltip mechanism
162 string str = _("Add the selected entry to the current citation reference.");
163 tooltips().init(dialog_->button_add, str);
165 str = _("Delete the selected entry from the current citation reference.");
166 tooltips().init(dialog_->button_del, str);
168 str = _("Move the selected entry upwards (in the current list).");
169 tooltips().init(dialog_->button_up, str);
171 str = _("Move the selected entry downwards (in the current list).");
172 tooltips().init(dialog_->button_down, str);
174 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
175 tooltips().init(dialog_->browser_cite, str);
176 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
177 // Work-around xforms' bug; enable tooltips for browser widgets.
178 setPrehandler(dialog_->browser_cite);
181 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.");
182 tooltips().init(dialog_->browser_bib, str);
183 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
184 // Work-around xforms' bug; enable tooltips for browser widgets.
185 setPrehandler(dialog_->browser_bib);
188 str = _("Information about the selected entry");
189 tooltips().init(dialog_->browser_info, str);
190 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
191 // Work-around xforms' bug; enable tooltips for browser widgets.
192 setPrehandler(dialog_->browser_info);
195 str = _("Here you may select how the citation label should look inside the text (Natbib).");
196 tooltips().init(dialog_->choice_style, str);
198 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
199 tooltips().init(dialog_->check_full_author_list, str);
201 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).");
202 tooltips().init(dialog_->check_force_uppercase, str);
204 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
205 tooltips().init(dialog_->input_before, str);
207 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
208 tooltips().init(dialog_->input_after, str);
210 str = _("Search your database (all fields will be searched).");
211 tooltips().init(dialog_->input_search, str);
213 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
214 tooltips().init(dialog_->check_search_case, str);
216 str = _("Activate if you want to enter Regular Expressions.");
217 tooltips().init(dialog_->check_search_type, str);
221 void FormCitation::findBiblio(biblio::Direction const dir)
223 string const str = getString(dialog_->input_search);
224 biblio::InfoMap const & theMap = controller().bibkeysInfo();
225 bool const caseSensitive =
226 fl_get_button(dialog_->check_search_case);
227 biblio::Search const type =
228 fl_get_button(dialog_->check_search_type) ?
229 biblio::REGEX : biblio::SIMPLE;
231 vector<string>::const_iterator start = bibkeys.begin();
232 int const sel = fl_get_browser(dialog_->browser_bib);
233 if (sel >= 1 && sel <= int(bibkeys.size()))
236 // Find the NEXT instance...
237 (dir == biblio::FORWARD) ? ++start : --start;
240 vector<string>::const_iterator const cit =
241 biblio::searchKeys(theMap, bibkeys, str,
242 start, type, dir, caseSensitive);
244 if (cit == bibkeys.end())
247 int const found = int(cit - bibkeys.begin()) + 1;
251 // Update the display
252 int const top = max(found - 5, 1);
253 fl_set_browser_topline(dialog_->browser_bib, top);
254 fl_select_browser_line(dialog_->browser_bib, found);
255 input(dialog_->browser_bib, 0);
259 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
261 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
263 biblio::InfoMap const & theMap = controller().bibkeysInfo();
266 if (!citekeys.empty()) topCitekey = citekeys[0];
268 if (ob == dialog_->browser_bib) {
269 fl_deselect_browser(dialog_->browser_cite);
271 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
272 if (sel < 1 || sel > bibkeys.size())
273 return ButtonPolicy::SMI_NOOP;
275 // Put into browser_info the additional info associated with
276 // the selected browser_bib key
277 fl_clear_browser(dialog_->browser_info);
279 string const tmp = formatted(biblio::getInfo(theMap,
281 dialog_->browser_info->w - 10);
282 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
284 // Highlight the selected browser_bib key in browser_cite if
286 vector<string>::const_iterator cit =
287 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
289 if (cit != citekeys.end()) {
290 int const n = int(cit - citekeys.begin());
291 fl_select_browser_line(dialog_->browser_cite, n + 1);
292 fl_set_browser_topline(dialog_->browser_cite, n + 1);
295 if (!kernel().isBufferReadonly()) {
296 if (cit != citekeys.end()) {
305 } else if (ob == dialog_->browser_cite) {
306 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
307 if (sel < 1 || sel > citekeys.size())
308 return ButtonPolicy::SMI_NOOP;
310 if (!kernel().isBufferReadonly()) {
315 // Highlight the selected browser_cite key in browser_bib
316 vector<string>::const_iterator cit =
317 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
319 if (cit != bibkeys.end()) {
320 int const n = int(cit - bibkeys.begin());
321 fl_select_browser_line(dialog_->browser_bib, n + 1);
322 fl_set_browser_topline(dialog_->browser_bib, n + 1);
324 // Put into browser_info the additional info associated
325 // with the selected browser_cite key
326 fl_clear_browser(dialog_->browser_info);
328 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
329 dialog_->browser_info->w - 10);
330 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
333 } else if (ob == dialog_->button_add) {
334 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
335 if (sel < 1 || sel > bibkeys.size())
336 return ButtonPolicy::SMI_NOOP;
338 // Add the selected browser_bib key to browser_cite
339 fl_addto_browser(dialog_->browser_cite,
340 bibkeys[sel - 1].c_str());
341 citekeys.push_back(bibkeys[sel - 1]);
343 int const n = int(citekeys.size());
344 fl_select_browser_line(dialog_->browser_cite, n);
348 activate = ButtonPolicy::SMI_VALID;
350 } else if (ob == dialog_->button_del) {
351 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
352 if (sel < 1 || sel > citekeys.size())
353 return ButtonPolicy::SMI_NOOP;
355 // Remove the selected key from browser_cite
356 fl_delete_browser_line(dialog_->browser_cite, sel) ;
357 citekeys.erase(citekeys.begin() + sel - 1);
361 activate = ButtonPolicy::SMI_VALID;
363 } else if (ob == dialog_->button_up) {
364 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
365 if (sel < 2 || sel > citekeys.size())
366 return ButtonPolicy::SMI_NOOP;
368 // Move the selected key up one line
369 vector<string>::iterator it = citekeys.begin() + sel - 1;
370 string const tmp = *it;
372 fl_delete_browser_line(dialog_->browser_cite, sel);
375 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
376 fl_select_browser_line(dialog_->browser_cite, sel - 1);
377 citekeys.insert(it - 1, tmp);
379 activate = ButtonPolicy::SMI_VALID;
381 } else if (ob == dialog_->button_down) {
382 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
383 if (sel < 1 || sel > citekeys.size() - 1)
384 return ButtonPolicy::SMI_NOOP;
386 // Move the selected key down one line
387 vector<string>::iterator it = citekeys.begin() + sel - 1;
388 string const tmp = *it;
390 fl_delete_browser_line(dialog_->browser_cite, sel);
393 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
394 fl_select_browser_line(dialog_->browser_cite, sel+1);
395 citekeys.insert(it+1, tmp);
397 activate = ButtonPolicy::SMI_VALID;
399 } else if (ob == dialog_->button_previous) {
400 findBiblio(biblio::BACKWARD);
401 } else if (ob == dialog_->button_next) {
402 findBiblio(biblio::FORWARD);
403 } else if (ob == dialog_->input_search) {
404 findBiblio(biblio::FORWARD);
405 } else if (ob == dialog_->choice_style ||
406 ob == dialog_->check_full_author_list ||
407 ob == dialog_->check_force_uppercase ||
408 ob == dialog_->input_before ||
409 ob == dialog_->input_after) {
410 activate = ButtonPolicy::SMI_VALID;
413 string currentCitekey;
414 if (!citekeys.empty())
415 currentCitekey = citekeys[0];
417 if (topCitekey != currentCitekey) {
418 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
419 fillChoice(dialog_.get(),
420 controller().getCiteStrings(currentCitekey));
421 fl_set_choice(dialog_->choice_style, choice);
428 void FormCitation::update()
430 // Make the list of all available bibliography keys
431 bibkeys = biblio::getKeys(controller().bibkeysInfo());
432 updateBrowser(dialog_->browser_bib, bibkeys);
434 // Ditto for the keys cited in this inset
435 citekeys = getVectorFromString(controller().params().getContents());
436 updateBrowser(dialog_->browser_cite, citekeys);
438 // Use the first citekey to fill choice_style
440 if (!citekeys.empty())
443 fillChoice(dialog_.get(), controller().getCiteStrings(key));
445 // Use the citation command to update the GUI
446 updateStyle(dialog_.get(), controller().params().getCmdName());
448 bool const natbib = controller().usingNatbib();
449 setEnabled(dialog_->check_full_author_list, natbib);
450 setEnabled(dialog_->check_force_uppercase, natbib);
451 setEnabled(dialog_->choice_style, natbib);
453 // No keys have been selected yet, so...
454 fl_clear_browser(dialog_->browser_info);
458 // Natbib can have comments before and after the citation.
459 // This is not yet supported. After only.
460 fl_set_input(dialog_->input_after,
461 controller().params().getOptions().c_str());
463 fl_set_input(dialog_->input_before, _("Not yet supported").c_str());
464 setEnabled(dialog_->input_before, false);
468 void FormCitation::updateBrowser(FL_OBJECT * browser,
469 vector<string> const & keys) const
471 // Check whether the current contents of the browser will be
472 // changed by loading the contents of the vec...
473 vector<string> browser_keys = getVector(browser);
475 if (browser_keys == keys) {
476 fl_deselect_browser(browser);
477 fl_set_browser_topline(browser, 1);
481 // They will be changed. Proceed.
482 fl_clear_browser(browser);
484 for (vector<string>::const_iterator it = keys.begin();
485 it != keys.end(); ++it) {
486 string key = trim(*it);
488 fl_add_browser_line(browser, key.c_str());
493 void FormCitation::setBibButtons(State status) const
495 setEnabled(dialog_->button_add, (status == ON));
499 void FormCitation::setCiteButtons(State status) const
501 int const sel = fl_get_browser(dialog_->browser_cite);
502 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
503 bool const activate = (status == ON);
504 bool const activate_up = (activate && sel != 1);
505 bool const activate_down = (activate && sel != maxline);
507 setEnabled(dialog_->button_del, activate);
508 setEnabled(dialog_->button_up, activate_up);
509 setEnabled(dialog_->button_down, activate_down);