2 * \file xforms/FormCitation.C
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
16 #pragma implementation
20 #include "ControlCitation.h"
21 #include "FormCitation.h"
22 #include "forms/form_citation.h"
24 #include "helper_funcs.h"
25 #include "xforms_helpers.h"
26 #include FORMS_H_LOCATION
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
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 =
55 getVector(dialog->choice_style);
57 if (vec == choice_style)
60 // They will be changed. Proceed
61 bool const noVec = vec.empty();
62 string const str = noVec ? string() : getStringFromVector(vec, "|");
64 fl_clear_choice(dialog->choice_style);
65 fl_addto_choice(dialog->choice_style, str.c_str());
66 setEnabled(dialog->choice_style, !noVec);
70 void updateStyle(FD_citation * dialog, string command)
72 // Find the style of the citekeys
73 vector<biblio::CiteStyle> const & styles =
74 ControlCitation::getCiteStyles();
75 biblio::CitationStyle cs = biblio::getCitationStyle(command);
77 vector<biblio::CiteStyle>::const_iterator cit =
78 find(styles.begin(), styles.end(), cs.style);
80 // Use this to initialise the GUI
81 bool const noStyles = cit == styles.end();
82 int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
83 fl_set_choice(dialog->choice_style, index);
85 // Disable if there are no styles, otherwise use cs member settings.
86 fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
87 fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
93 typedef FormCB<ControlCitation, FormDB<FD_citation> > base_class;
96 FormCitation::FormCitation()
97 : base_class(_("Citation"))
101 void FormCitation::apply()
103 string command = "cite";
104 if (isActive(dialog_->choice_style)) {
105 vector<biblio::CiteStyle> const & styles =
106 ControlCitation::getCiteStyles();
109 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
111 fl_get_button(dialog_->check_full_author_list);
113 fl_get_button(dialog_->check_force_uppercase);
115 command = biblio::getCiteCommand(styles[choice], full, force);
118 controller().params().setCmdName(command);
119 controller().params().setContents(getStringFromVector(citekeys));
121 string const after = getString(dialog_->input_after);
122 controller().params().setOptions(after);
126 void FormCitation::hide()
135 void FormCitation::build()
137 dialog_.reset(build_citation(this));
139 // Manage the ok, apply, restore and cancel/close buttons
140 bc().setOK(dialog_->button_ok);
141 bc().setApply(dialog_->button_apply);
142 bc().setCancel(dialog_->button_close);
143 bc().setRestore(dialog_->button_restore);
145 // disable for read-only documents
146 bc().addReadOnly(dialog_->button_add);
147 bc().addReadOnly(dialog_->button_del);
148 bc().addReadOnly(dialog_->button_up);
149 bc().addReadOnly(dialog_->button_down);
150 bc().addReadOnly(dialog_->choice_style);
151 bc().addReadOnly(dialog_->input_before);
152 bc().addReadOnly(dialog_->input_after);
153 bc().addReadOnly(dialog_->check_full_author_list);
154 bc().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 // Work-around xforms' bug; enable tooltips for browser widgets.
181 setPrehandler(dialog_->browser_cite);
183 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.");
184 tooltips().init(dialog_->browser_bib, str);
185 // Work-around xforms' bug; enable tooltips for browser widgets.
186 setPrehandler(dialog_->browser_bib);
188 str = _("Information about the selected entry");
189 tooltips().init(dialog_->browser_info, str);
190 // Work-around xforms' bug; enable tooltips for browser widgets.
191 setPrehandler(dialog_->browser_info);
193 str = _("Here you may select how the citation label should look inside the text (Natbib).");
194 tooltips().init(dialog_->choice_style, str);
196 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
197 tooltips().init(dialog_->check_full_author_list, str);
199 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).");
200 tooltips().init(dialog_->check_force_uppercase, str);
202 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
203 tooltips().init(dialog_->input_before, str);
205 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
206 tooltips().init(dialog_->input_after, str);
208 str = _("Search your database (all fields will be searched).");
209 tooltips().init(dialog_->input_search, str);
211 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
212 tooltips().init(dialog_->check_search_case, str);
214 str = _("Activate if you want to enter Regular Expressions.");
215 tooltips().init(dialog_->check_search_type, str);
219 void FormCitation::findBiblio(biblio::Direction const dir)
221 string const str = getString(dialog_->input_search);
222 biblio::InfoMap const & theMap = controller().bibkeysInfo();
223 bool const caseSensitive =
224 fl_get_button(dialog_->check_search_case);
225 biblio::Search const type =
226 fl_get_button(dialog_->check_search_type) ?
227 biblio::REGEX : biblio::SIMPLE;
229 vector<string>::const_iterator start = bibkeys.begin();
230 int const sel = fl_get_browser(dialog_->browser_bib);
231 if (sel >= 1 && sel <= int(bibkeys.size()))
234 // Find the NEXT instance...
235 (dir == biblio::FORWARD) ? ++start : --start;
238 vector<string>::const_iterator const cit =
239 biblio::searchKeys(theMap, bibkeys, str,
240 start, type, dir, caseSensitive);
242 if (cit == bibkeys.end())
245 int const found = int(cit - bibkeys.begin()) + 1;
249 // Update the display
250 int const top = max(found - 5, 1);
251 fl_set_browser_topline(dialog_->browser_bib, top);
252 fl_select_browser_line(dialog_->browser_bib, found);
253 input(dialog_->browser_bib, 0);
257 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
259 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
261 biblio::InfoMap const & theMap = controller().bibkeysInfo();
264 if (!citekeys.empty()) topCitekey = citekeys[0];
266 if (ob == dialog_->browser_bib) {
267 fl_deselect_browser(dialog_->browser_cite);
269 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
270 if (sel < 1 || sel > bibkeys.size())
271 return ButtonPolicy::SMI_NOOP;
273 // Put into browser_info the additional info associated with
274 // the selected browser_bib key
275 fl_clear_browser(dialog_->browser_info);
277 string const tmp = formatted(biblio::getInfo(theMap,
279 dialog_->browser_info->w - 10);
280 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
282 // Highlight the selected browser_bib key in browser_cite if
284 vector<string>::const_iterator cit =
285 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
287 if (cit != citekeys.end()) {
288 int const n = int(cit - citekeys.begin());
289 fl_select_browser_line(dialog_->browser_cite, n + 1);
290 fl_set_browser_topline(dialog_->browser_cite, n + 1);
293 if (!controller().bufferIsReadonly()) {
294 if (cit != citekeys.end()) {
303 } else if (ob == dialog_->browser_cite) {
304 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
305 if (sel < 1 || sel > citekeys.size())
306 return ButtonPolicy::SMI_NOOP;
308 if (!controller().bufferIsReadonly()) {
313 // Highlight the selected browser_cite key in browser_bib
314 vector<string>::const_iterator cit =
315 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
317 if (cit != bibkeys.end()) {
318 int const n = int(cit - bibkeys.begin());
319 fl_select_browser_line(dialog_->browser_bib, n + 1);
320 fl_set_browser_topline(dialog_->browser_bib, n + 1);
322 // Put into browser_info the additional info associated
323 // with the selected browser_cite key
324 fl_clear_browser(dialog_->browser_info);
326 formatted(biblio::getInfo(theMap,
328 dialog_->browser_info->w - 10);
329 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
332 } else if (ob == dialog_->button_add) {
333 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
334 if (sel < 1 || sel > bibkeys.size())
335 return ButtonPolicy::SMI_NOOP;
337 // Add the selected browser_bib key to browser_cite
338 fl_addto_browser(dialog_->browser_cite,
339 bibkeys[sel - 1].c_str());
340 citekeys.push_back(bibkeys[sel - 1]);
342 int const n = int(citekeys.size());
343 fl_select_browser_line(dialog_->browser_cite, n);
347 activate = ButtonPolicy::SMI_VALID;
349 } else if (ob == dialog_->button_del) {
350 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
351 if (sel < 1 || sel > citekeys.size())
352 return ButtonPolicy::SMI_NOOP;
354 // Remove the selected key from browser_cite
355 fl_delete_browser_line(dialog_->browser_cite, sel) ;
356 citekeys.erase(citekeys.begin() + sel - 1);
360 activate = ButtonPolicy::SMI_VALID;
362 } else if (ob == dialog_->button_up) {
363 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
364 if (sel < 2 || sel > citekeys.size())
365 return ButtonPolicy::SMI_NOOP;
367 // Move the selected key up one line
368 vector<string>::iterator it = citekeys.begin() + sel - 1;
369 string const tmp = *it;
371 fl_delete_browser_line(dialog_->browser_cite, sel);
374 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
375 fl_select_browser_line(dialog_->browser_cite, sel - 1);
376 citekeys.insert(it - 1, tmp);
378 activate = ButtonPolicy::SMI_VALID;
380 } else if (ob == dialog_->button_down) {
381 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
382 if (sel < 1 || sel > citekeys.size() - 1)
383 return ButtonPolicy::SMI_NOOP;
385 // Move the selected key down one line
386 vector<string>::iterator it = citekeys.begin() + sel - 1;
387 string const tmp = *it;
389 fl_delete_browser_line(dialog_->browser_cite, sel);
392 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
393 fl_select_browser_line(dialog_->browser_cite, sel+1);
394 citekeys.insert(it+1, tmp);
396 activate = ButtonPolicy::SMI_VALID;
398 } else if (ob == dialog_->button_previous) {
399 findBiblio(biblio::BACKWARD);
400 } else if (ob == dialog_->button_next) {
401 findBiblio(biblio::FORWARD);
402 } else if (ob == dialog_->input_search) {
403 findBiblio(biblio::FORWARD);
404 } else if (ob == dialog_->choice_style ||
405 ob == dialog_->check_full_author_list ||
406 ob == dialog_->check_force_uppercase ||
407 ob == dialog_->input_before ||
408 ob == dialog_->input_after) {
409 activate = ButtonPolicy::SMI_VALID;
412 string currentCitekey;
413 if (!citekeys.empty())
414 currentCitekey = citekeys[0];
416 if (topCitekey != currentCitekey) {
417 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
418 fillChoice(dialog_.get(),
419 controller().getCiteStrings(currentCitekey));
420 fl_set_choice(dialog_->choice_style, choice);
427 void FormCitation::update()
429 // Make the list of all available bibliography keys
430 bibkeys = biblio::getKeys(controller().bibkeysInfo());
431 updateBrowser(dialog_->browser_bib, bibkeys);
433 // Ditto for the keys cited in this inset
434 citekeys = getVectorFromString(controller().params().getContents());
435 updateBrowser(dialog_->browser_cite, citekeys);
437 // Use the first citekey to fill choice_style
439 if (!citekeys.empty()) key = citekeys[0];
441 fillChoice(dialog_.get(), controller().getCiteStrings(key));
443 // Use the citation command to update the GUI
444 updateStyle(dialog_.get(), controller().params().getCmdName());
446 bool const natbib = controller().usingNatbib();
447 setEnabled(dialog_->check_full_author_list, natbib);
448 setEnabled(dialog_->check_force_uppercase, natbib);
449 setEnabled(dialog_->choice_style, natbib);
451 // No keys have been selected yet, so...
452 fl_clear_browser(dialog_->browser_info);
456 // Natbib can have comments before and after the citation.
457 // This is not yet supported. After only.
458 fl_set_input(dialog_->input_after,
459 controller().params().getOptions().c_str());
461 fl_set_input(dialog_->input_before, _("Not yet supported"));
462 setEnabled(dialog_->input_before, false);
466 void FormCitation::updateBrowser(FL_OBJECT * browser,
467 vector<string> const & keys) const
469 // Check whether the current contents of the browser will be
470 // changed by loading the contents of the vec...
471 vector<string> browser_keys = getVector(browser);
473 if (browser_keys == keys) {
474 fl_deselect_browser(browser);
475 fl_set_browser_topline(browser, 1);
479 // They will be changed. Proceed.
480 fl_clear_browser(browser);
482 for (vector<string>::const_iterator it = keys.begin();
483 it < keys.end(); ++it) {
484 string key = trim(*it);
486 fl_add_browser_line(browser, key.c_str());
491 void FormCitation::setBibButtons(State status) const
493 setEnabled(dialog_->button_add, (status == ON));
497 void FormCitation::setCiteButtons(State status) const
499 int const sel = fl_get_browser(dialog_->browser_cite);
500 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
501 bool const activate = (status == ON);
502 bool const activate_up = (activate && sel != 1);
503 bool const activate_down = (activate && sel != maxline);
505 setEnabled(dialog_->button_del, activate);
506 setEnabled(dialog_->button_up, activate_up);
507 setEnabled(dialog_->button_down, activate_down);