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
15 #include "ControlCitation.h"
16 #include "FormCitation.h"
17 #include "forms/form_citation.h"
20 #include "helper_funcs.h"
21 #include "xforms_helpers.h"
22 #include "lyx_forms.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
30 using namespace lyx::support;
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 cs = biblio::getCitationStyle(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);
114 command = biblio::getCiteCommand(styles[choice], full, force);
117 controller().params().setCmdName(command);
118 controller().params().setContents(getStringFromVector(citekeys));
120 string const after = getString(dialog_->input_after);
121 controller().params().setOptions(after);
125 void FormCitation::hide()
130 FormDialogView::hide();
134 void FormCitation::build()
136 dialog_.reset(build_citation(this));
138 // Manage the ok, apply, restore and cancel/close buttons
139 bcview().setOK(dialog_->button_ok);
140 bcview().setApply(dialog_->button_apply);
141 bcview().setCancel(dialog_->button_close);
142 bcview().setRestore(dialog_->button_restore);
144 // disable for read-only documents
145 bcview().addReadOnly(dialog_->button_add);
146 bcview().addReadOnly(dialog_->button_del);
147 bcview().addReadOnly(dialog_->button_up);
148 bcview().addReadOnly(dialog_->button_down);
149 bcview().addReadOnly(dialog_->choice_style);
150 bcview().addReadOnly(dialog_->input_before);
151 bcview().addReadOnly(dialog_->input_after);
152 bcview().addReadOnly(dialog_->check_full_author_list);
153 bcview().addReadOnly(dialog_->check_force_uppercase);
155 // trigger an input event for cut&paste with middle mouse button.
156 setPrehandler(dialog_->input_search);
157 setPrehandler(dialog_->input_before);
158 setPrehandler(dialog_->input_after);
160 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
161 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
162 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
164 //set up the tooltip mechanism
165 string str = _("Add the selected entry to the current citation reference.");
166 tooltips().init(dialog_->button_add, str);
168 str = _("Delete the selected entry from the current citation reference.");
169 tooltips().init(dialog_->button_del, str);
171 str = _("Move the selected entry upwards (in the current list).");
172 tooltips().init(dialog_->button_up, str);
174 str = _("Move the selected entry downwards (in the current list).");
175 tooltips().init(dialog_->button_down, str);
177 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
178 tooltips().init(dialog_->browser_cite, str);
179 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
180 // Work-around xforms' bug; enable tooltips for browser widgets.
181 setPrehandler(dialog_->browser_cite);
184 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.");
185 tooltips().init(dialog_->browser_bib, str);
186 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
187 // Work-around xforms' bug; enable tooltips for browser widgets.
188 setPrehandler(dialog_->browser_bib);
191 str = _("Information about the selected entry");
192 tooltips().init(dialog_->browser_info, str);
193 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
194 // Work-around xforms' bug; enable tooltips for browser widgets.
195 setPrehandler(dialog_->browser_info);
198 str = _("Here you may select how the citation label should look inside the text (Natbib).");
199 tooltips().init(dialog_->choice_style, str);
201 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
202 tooltips().init(dialog_->check_full_author_list, str);
204 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).");
205 tooltips().init(dialog_->check_force_uppercase, str);
207 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
208 tooltips().init(dialog_->input_before, str);
210 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
211 tooltips().init(dialog_->input_after, str);
213 str = _("Search your database (all fields will be searched).");
214 tooltips().init(dialog_->input_search, str);
216 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
217 tooltips().init(dialog_->check_search_case, str);
219 str = _("Activate if you want to enter Regular Expressions.");
220 tooltips().init(dialog_->check_search_type, str);
224 void FormCitation::findBiblio(biblio::Direction const dir)
226 string const str = getString(dialog_->input_search);
227 biblio::InfoMap const & theMap = controller().bibkeysInfo();
228 bool const caseSensitive =
229 fl_get_button(dialog_->check_search_case);
230 biblio::Search const type =
231 fl_get_button(dialog_->check_search_type) ?
232 biblio::REGEX : biblio::SIMPLE;
234 vector<string>::const_iterator start = bibkeys.begin();
235 int const sel = fl_get_browser(dialog_->browser_bib);
236 if (sel >= 1 && sel <= int(bibkeys.size()))
239 // Find the NEXT instance...
240 (dir == biblio::FORWARD) ? ++start : --start;
243 vector<string>::const_iterator const cit =
244 biblio::searchKeys(theMap, bibkeys, str,
245 start, type, dir, caseSensitive);
247 if (cit == bibkeys.end())
250 int const found = int(cit - bibkeys.begin()) + 1;
254 // Update the display
255 int const top = max(found - 5, 1);
256 fl_set_browser_topline(dialog_->browser_bib, top);
257 fl_select_browser_line(dialog_->browser_bib, found);
258 input(dialog_->browser_bib, 0);
262 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
264 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
266 biblio::InfoMap const & theMap = controller().bibkeysInfo();
269 if (!citekeys.empty()) topCitekey = citekeys[0];
271 if (ob == dialog_->browser_bib) {
272 fl_deselect_browser(dialog_->browser_cite);
274 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
275 if (sel < 1 || sel > bibkeys.size())
276 return ButtonPolicy::SMI_NOOP;
278 // Put into browser_info the additional info associated with
279 // the selected browser_bib key
280 fl_clear_browser(dialog_->browser_info);
282 string const tmp = formatted(biblio::getInfo(theMap,
284 dialog_->browser_info->w - 10);
285 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
287 // Highlight the selected browser_bib key in browser_cite if
289 vector<string>::const_iterator cit =
290 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
292 if (cit != citekeys.end()) {
293 int const n = int(cit - citekeys.begin());
294 fl_select_browser_line(dialog_->browser_cite, n + 1);
295 fl_set_browser_topline(dialog_->browser_cite, n + 1);
298 if (!kernel().isBufferReadonly()) {
299 if (cit != citekeys.end()) {
308 } else if (ob == dialog_->browser_cite) {
309 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
310 if (sel < 1 || sel > citekeys.size())
311 return ButtonPolicy::SMI_NOOP;
313 if (!kernel().isBufferReadonly()) {
318 // Highlight the selected browser_cite key in browser_bib
319 vector<string>::const_iterator cit =
320 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
322 if (cit != bibkeys.end()) {
323 int const n = int(cit - bibkeys.begin());
324 fl_select_browser_line(dialog_->browser_bib, n + 1);
325 fl_set_browser_topline(dialog_->browser_bib, n + 1);
327 // Put into browser_info the additional info associated
328 // with the selected browser_cite key
329 fl_clear_browser(dialog_->browser_info);
331 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
332 dialog_->browser_info->w - 10);
333 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
336 } else if (ob == dialog_->button_add) {
337 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
338 if (sel < 1 || sel > bibkeys.size())
339 return ButtonPolicy::SMI_NOOP;
341 // Add the selected browser_bib key to browser_cite
342 fl_addto_browser(dialog_->browser_cite,
343 bibkeys[sel - 1].c_str());
344 citekeys.push_back(bibkeys[sel - 1]);
346 int const n = int(citekeys.size());
347 fl_select_browser_line(dialog_->browser_cite, n);
351 activate = ButtonPolicy::SMI_VALID;
353 } else if (ob == dialog_->button_del) {
354 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
355 if (sel < 1 || sel > citekeys.size())
356 return ButtonPolicy::SMI_NOOP;
358 // Remove the selected key from browser_cite
359 fl_delete_browser_line(dialog_->browser_cite, sel) ;
360 citekeys.erase(citekeys.begin() + sel - 1);
364 activate = ButtonPolicy::SMI_VALID;
366 } else if (ob == dialog_->button_up) {
367 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
368 if (sel < 2 || sel > citekeys.size())
369 return ButtonPolicy::SMI_NOOP;
371 // Move the selected key up one line
372 vector<string>::iterator it = citekeys.begin() + sel - 1;
373 string const tmp = *it;
375 fl_delete_browser_line(dialog_->browser_cite, sel);
378 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
379 fl_select_browser_line(dialog_->browser_cite, sel - 1);
380 citekeys.insert(it - 1, tmp);
382 activate = ButtonPolicy::SMI_VALID;
384 } else if (ob == dialog_->button_down) {
385 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
386 if (sel < 1 || sel > citekeys.size() - 1)
387 return ButtonPolicy::SMI_NOOP;
389 // Move the selected key down one line
390 vector<string>::iterator it = citekeys.begin() + sel - 1;
391 string const tmp = *it;
393 fl_delete_browser_line(dialog_->browser_cite, sel);
396 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
397 fl_select_browser_line(dialog_->browser_cite, sel+1);
398 citekeys.insert(it+1, tmp);
400 activate = ButtonPolicy::SMI_VALID;
402 } else if (ob == dialog_->button_previous) {
403 findBiblio(biblio::BACKWARD);
404 } else if (ob == dialog_->button_next) {
405 findBiblio(biblio::FORWARD);
406 } else if (ob == dialog_->input_search) {
407 findBiblio(biblio::FORWARD);
408 } else if (ob == dialog_->choice_style ||
409 ob == dialog_->check_full_author_list ||
410 ob == dialog_->check_force_uppercase ||
411 ob == dialog_->input_before ||
412 ob == dialog_->input_after) {
413 activate = ButtonPolicy::SMI_VALID;
416 string currentCitekey;
417 if (!citekeys.empty())
418 currentCitekey = citekeys[0];
420 if (topCitekey != currentCitekey) {
421 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
422 fillChoice(dialog_.get(),
423 controller().getCiteStrings(currentCitekey));
424 fl_set_choice(dialog_->choice_style, choice);
431 void FormCitation::update()
433 // Make the list of all available bibliography keys
434 bibkeys = biblio::getKeys(controller().bibkeysInfo());
435 updateBrowser(dialog_->browser_bib, bibkeys);
437 // Ditto for the keys cited in this inset
438 citekeys = getVectorFromString(controller().params().getContents());
439 updateBrowser(dialog_->browser_cite, citekeys);
441 // Use the first citekey to fill choice_style
443 if (!citekeys.empty())
446 fillChoice(dialog_.get(), controller().getCiteStrings(key));
448 // Use the citation command to update the GUI
449 updateStyle(dialog_.get(), controller().params().getCmdName());
451 bool const natbib = controller().usingNatbib();
452 setEnabled(dialog_->check_full_author_list, natbib);
453 setEnabled(dialog_->check_force_uppercase, natbib);
454 setEnabled(dialog_->choice_style, natbib);
456 // No keys have been selected yet, so...
457 fl_clear_browser(dialog_->browser_info);
461 // Natbib can have comments before and after the citation.
462 // This is not yet supported. After only.
463 fl_set_input(dialog_->input_after,
464 controller().params().getOptions().c_str());
466 fl_set_input(dialog_->input_before, _("Not yet supported").c_str());
467 setEnabled(dialog_->input_before, false);
471 void FormCitation::updateBrowser(FL_OBJECT * browser,
472 vector<string> const & keys) const
474 // Check whether the current contents of the browser will be
475 // changed by loading the contents of the vec...
476 vector<string> browser_keys = getVector(browser);
478 if (browser_keys == keys) {
479 fl_deselect_browser(browser);
480 fl_set_browser_topline(browser, 1);
484 // They will be changed. Proceed.
485 fl_clear_browser(browser);
487 for (vector<string>::const_iterator it = keys.begin();
488 it != keys.end(); ++it) {
489 string key = trim(*it);
491 fl_add_browser_line(browser, key.c_str());
496 void FormCitation::setBibButtons(State status) const
498 setEnabled(dialog_->button_add, (status == ON));
502 void FormCitation::setCiteButtons(State status) const
504 int const sel = fl_get_browser(dialog_->browser_cite);
505 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
506 bool const activate = (status == ON);
507 bool const activate_up = (activate && sel != 1);
508 bool const activate_down = (activate && sel != maxline);
510 setEnabled(dialog_->button_del, activate);
511 setEnabled(dialog_->button_up, activate_up);
512 setEnabled(dialog_->button_down, activate_down);