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
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "forms/form_citation.h"
22 #include "helper_funcs.h"
23 #include "xforms_helpers.h"
24 #include FORMS_H_LOCATION
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
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 after = getString(dialog_->input_after);
119 controller().params().setOptions(after);
123 void FormCitation::hide()
128 FormDialogView::hide();
132 void FormCitation::build()
134 dialog_.reset(build_citation(this));
136 // Manage the ok, apply, restore and cancel/close buttons
137 bcview().setOK(dialog_->button_ok);
138 bcview().setApply(dialog_->button_apply);
139 bcview().setCancel(dialog_->button_close);
140 bcview().setRestore(dialog_->button_restore);
142 // disable for read-only documents
143 bcview().addReadOnly(dialog_->button_add);
144 bcview().addReadOnly(dialog_->button_del);
145 bcview().addReadOnly(dialog_->button_up);
146 bcview().addReadOnly(dialog_->button_down);
147 bcview().addReadOnly(dialog_->choice_style);
148 bcview().addReadOnly(dialog_->input_before);
149 bcview().addReadOnly(dialog_->input_after);
150 bcview().addReadOnly(dialog_->check_full_author_list);
151 bcview().addReadOnly(dialog_->check_force_uppercase);
153 // trigger an input event for cut&paste with middle mouse button.
154 setPrehandler(dialog_->input_search);
155 setPrehandler(dialog_->input_before);
156 setPrehandler(dialog_->input_after);
158 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
159 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
160 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
162 //set up the tooltip mechanism
163 string str = _("Add the selected entry to the current citation reference.");
164 tooltips().init(dialog_->button_add, str);
166 str = _("Delete the selected entry from the current citation reference.");
167 tooltips().init(dialog_->button_del, str);
169 str = _("Move the selected entry upwards (in the current list).");
170 tooltips().init(dialog_->button_up, str);
172 str = _("Move the selected entry downwards (in the current list).");
173 tooltips().init(dialog_->button_down, str);
175 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
176 tooltips().init(dialog_->browser_cite, str);
177 // Work-around xforms' bug; enable tooltips for browser widgets.
178 setPrehandler(dialog_->browser_cite);
180 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.");
181 tooltips().init(dialog_->browser_bib, str);
182 // Work-around xforms' bug; enable tooltips for browser widgets.
183 setPrehandler(dialog_->browser_bib);
185 str = _("Information about the selected entry");
186 tooltips().init(dialog_->browser_info, str);
187 // Work-around xforms' bug; enable tooltips for browser widgets.
188 setPrehandler(dialog_->browser_info);
190 str = _("Here you may select how the citation label should look inside the text (Natbib).");
191 tooltips().init(dialog_->choice_style, str);
193 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
194 tooltips().init(dialog_->check_full_author_list, str);
196 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).");
197 tooltips().init(dialog_->check_force_uppercase, str);
199 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
200 tooltips().init(dialog_->input_before, str);
202 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
203 tooltips().init(dialog_->input_after, str);
205 str = _("Search your database (all fields will be searched).");
206 tooltips().init(dialog_->input_search, str);
208 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
209 tooltips().init(dialog_->check_search_case, str);
211 str = _("Activate if you want to enter Regular Expressions.");
212 tooltips().init(dialog_->check_search_type, str);
216 void FormCitation::findBiblio(biblio::Direction const dir)
218 string const str = getString(dialog_->input_search);
219 biblio::InfoMap const & theMap = controller().bibkeysInfo();
220 bool const caseSensitive =
221 fl_get_button(dialog_->check_search_case);
222 biblio::Search const type =
223 fl_get_button(dialog_->check_search_type) ?
224 biblio::REGEX : biblio::SIMPLE;
226 vector<string>::const_iterator start = bibkeys.begin();
227 int const sel = fl_get_browser(dialog_->browser_bib);
228 if (sel >= 1 && sel <= int(bibkeys.size()))
231 // Find the NEXT instance...
232 (dir == biblio::FORWARD) ? ++start : --start;
235 vector<string>::const_iterator const cit =
236 biblio::searchKeys(theMap, bibkeys, str,
237 start, type, dir, caseSensitive);
239 if (cit == bibkeys.end())
242 int const found = int(cit - bibkeys.begin()) + 1;
246 // Update the display
247 int const top = max(found - 5, 1);
248 fl_set_browser_topline(dialog_->browser_bib, top);
249 fl_select_browser_line(dialog_->browser_bib, found);
250 input(dialog_->browser_bib, 0);
254 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
256 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
258 biblio::InfoMap const & theMap = controller().bibkeysInfo();
261 if (!citekeys.empty()) topCitekey = citekeys[0];
263 if (ob == dialog_->browser_bib) {
264 fl_deselect_browser(dialog_->browser_cite);
266 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
267 if (sel < 1 || sel > bibkeys.size())
268 return ButtonPolicy::SMI_NOOP;
270 // Put into browser_info the additional info associated with
271 // the selected browser_bib key
272 fl_clear_browser(dialog_->browser_info);
274 string const tmp = formatted(biblio::getInfo(theMap,
276 dialog_->browser_info->w - 10);
277 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
279 // Highlight the selected browser_bib key in browser_cite if
281 vector<string>::const_iterator cit =
282 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
284 if (cit != citekeys.end()) {
285 int const n = int(cit - citekeys.begin());
286 fl_select_browser_line(dialog_->browser_cite, n + 1);
287 fl_set_browser_topline(dialog_->browser_cite, n + 1);
290 if (!kernel().isBufferReadonly()) {
291 if (cit != citekeys.end()) {
300 } else if (ob == dialog_->browser_cite) {
301 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
302 if (sel < 1 || sel > citekeys.size())
303 return ButtonPolicy::SMI_NOOP;
305 if (!kernel().isBufferReadonly()) {
310 // Highlight the selected browser_cite key in browser_bib
311 vector<string>::const_iterator cit =
312 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
314 if (cit != bibkeys.end()) {
315 int const n = int(cit - bibkeys.begin());
316 fl_select_browser_line(dialog_->browser_bib, n + 1);
317 fl_set_browser_topline(dialog_->browser_bib, n + 1);
319 // Put into browser_info the additional info associated
320 // with the selected browser_cite key
321 fl_clear_browser(dialog_->browser_info);
323 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
324 dialog_->browser_info->w - 10);
325 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
328 } else if (ob == dialog_->button_add) {
329 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
330 if (sel < 1 || sel > bibkeys.size())
331 return ButtonPolicy::SMI_NOOP;
333 // Add the selected browser_bib key to browser_cite
334 fl_addto_browser(dialog_->browser_cite,
335 bibkeys[sel - 1].c_str());
336 citekeys.push_back(bibkeys[sel - 1]);
338 int const n = int(citekeys.size());
339 fl_select_browser_line(dialog_->browser_cite, n);
343 activate = ButtonPolicy::SMI_VALID;
345 } else if (ob == dialog_->button_del) {
346 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
347 if (sel < 1 || sel > citekeys.size())
348 return ButtonPolicy::SMI_NOOP;
350 // Remove the selected key from browser_cite
351 fl_delete_browser_line(dialog_->browser_cite, sel) ;
352 citekeys.erase(citekeys.begin() + sel - 1);
356 activate = ButtonPolicy::SMI_VALID;
358 } else if (ob == dialog_->button_up) {
359 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
360 if (sel < 2 || sel > citekeys.size())
361 return ButtonPolicy::SMI_NOOP;
363 // Move the selected key up one line
364 vector<string>::iterator it = citekeys.begin() + sel - 1;
365 string const tmp = *it;
367 fl_delete_browser_line(dialog_->browser_cite, sel);
370 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
371 fl_select_browser_line(dialog_->browser_cite, sel - 1);
372 citekeys.insert(it - 1, tmp);
374 activate = ButtonPolicy::SMI_VALID;
376 } else if (ob == dialog_->button_down) {
377 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
378 if (sel < 1 || sel > citekeys.size() - 1)
379 return ButtonPolicy::SMI_NOOP;
381 // Move the selected key down 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_previous) {
395 findBiblio(biblio::BACKWARD);
396 } else if (ob == dialog_->button_next) {
397 findBiblio(biblio::FORWARD);
398 } else if (ob == dialog_->input_search) {
399 findBiblio(biblio::FORWARD);
400 } else if (ob == dialog_->choice_style ||
401 ob == dialog_->check_full_author_list ||
402 ob == dialog_->check_force_uppercase ||
403 ob == dialog_->input_before ||
404 ob == dialog_->input_after) {
405 activate = ButtonPolicy::SMI_VALID;
408 string currentCitekey;
409 if (!citekeys.empty())
410 currentCitekey = citekeys[0];
412 if (topCitekey != currentCitekey) {
413 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
414 fillChoice(dialog_.get(),
415 controller().getCiteStrings(currentCitekey));
416 fl_set_choice(dialog_->choice_style, choice);
423 void FormCitation::update()
425 // Make the list of all available bibliography keys
426 bibkeys = biblio::getKeys(controller().bibkeysInfo());
427 updateBrowser(dialog_->browser_bib, bibkeys);
429 // Ditto for the keys cited in this inset
430 citekeys = getVectorFromString(controller().params().getContents());
431 updateBrowser(dialog_->browser_cite, citekeys);
433 // Use the first citekey to fill choice_style
435 if (!citekeys.empty())
438 fillChoice(dialog_.get(), controller().getCiteStrings(key));
440 // Use the citation command to update the GUI
441 updateStyle(dialog_.get(), controller().params().getCmdName());
443 bool const natbib = controller().usingNatbib();
444 setEnabled(dialog_->check_full_author_list, natbib);
445 setEnabled(dialog_->check_force_uppercase, natbib);
446 setEnabled(dialog_->choice_style, natbib);
448 // No keys have been selected yet, so...
449 fl_clear_browser(dialog_->browser_info);
453 // Natbib can have comments before and after the citation.
454 // This is not yet supported. After only.
455 fl_set_input(dialog_->input_after,
456 controller().params().getOptions().c_str());
458 fl_set_input(dialog_->input_before, _("Not yet supported"));
459 setEnabled(dialog_->input_before, false);
463 void FormCitation::updateBrowser(FL_OBJECT * browser,
464 vector<string> const & keys) const
466 // Check whether the current contents of the browser will be
467 // changed by loading the contents of the vec...
468 vector<string> browser_keys = getVector(browser);
470 if (browser_keys == keys) {
471 fl_deselect_browser(browser);
472 fl_set_browser_topline(browser, 1);
476 // They will be changed. Proceed.
477 fl_clear_browser(browser);
479 for (vector<string>::const_iterator it = keys.begin();
480 it != keys.end(); ++it) {
481 string key = trim(*it);
483 fl_add_browser_line(browser, key.c_str());
488 void FormCitation::setBibButtons(State status) const
490 setEnabled(dialog_->button_add, (status == ON));
494 void FormCitation::setCiteButtons(State status) const
496 int const sel = fl_get_browser(dialog_->browser_cite);
497 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
498 bool const activate = (status == ON);
499 bool const activate_up = (activate && sel != 1);
500 bool const activate_down = (activate && sel != maxline);
502 setEnabled(dialog_->button_del, activate);
503 setEnabled(dialog_->button_up, activate_up);
504 setEnabled(dialog_->button_down, activate_down);