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
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "forms/form_citation.h"
21 #include "helper_funcs.h"
22 #include "xforms_helpers.h"
23 #include FORMS_H_LOCATION
26 #include "support/LAssert.h"
27 #include "support/lstrings.h"
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 FormCB<ControlCitation, FormDB<FD_citation> > base_class;
92 FormCitation::FormCitation()
93 : base_class(_("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()
131 void FormCitation::build()
133 dialog_.reset(build_citation(this));
135 // Manage the ok, apply, restore and cancel/close buttons
136 bc().setOK(dialog_->button_ok);
137 bc().setApply(dialog_->button_apply);
138 bc().setCancel(dialog_->button_close);
139 bc().setRestore(dialog_->button_restore);
141 // disable for read-only documents
142 bc().addReadOnly(dialog_->button_add);
143 bc().addReadOnly(dialog_->button_del);
144 bc().addReadOnly(dialog_->button_up);
145 bc().addReadOnly(dialog_->button_down);
146 bc().addReadOnly(dialog_->choice_style);
147 bc().addReadOnly(dialog_->input_before);
148 bc().addReadOnly(dialog_->input_after);
149 bc().addReadOnly(dialog_->check_full_author_list);
150 bc().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 // Work-around xforms' bug; enable tooltips for browser widgets.
177 setPrehandler(dialog_->browser_cite);
179 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.");
180 tooltips().init(dialog_->browser_bib, str);
181 // Work-around xforms' bug; enable tooltips for browser widgets.
182 setPrehandler(dialog_->browser_bib);
184 str = _("Information about the selected entry");
185 tooltips().init(dialog_->browser_info, str);
186 // Work-around xforms' bug; enable tooltips for browser widgets.
187 setPrehandler(dialog_->browser_info);
189 str = _("Here you may select how the citation label should look inside the text (Natbib).");
190 tooltips().init(dialog_->choice_style, str);
192 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
193 tooltips().init(dialog_->check_full_author_list, str);
195 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).");
196 tooltips().init(dialog_->check_force_uppercase, str);
198 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
199 tooltips().init(dialog_->input_before, str);
201 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
202 tooltips().init(dialog_->input_after, str);
204 str = _("Search your database (all fields will be searched).");
205 tooltips().init(dialog_->input_search, str);
207 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
208 tooltips().init(dialog_->check_search_case, str);
210 str = _("Activate if you want to enter Regular Expressions.");
211 tooltips().init(dialog_->check_search_type, str);
215 void FormCitation::findBiblio(biblio::Direction const dir)
217 string const str = getString(dialog_->input_search);
218 biblio::InfoMap const & theMap = controller().bibkeysInfo();
219 bool const caseSensitive =
220 fl_get_button(dialog_->check_search_case);
221 biblio::Search const type =
222 fl_get_button(dialog_->check_search_type) ?
223 biblio::REGEX : biblio::SIMPLE;
225 vector<string>::const_iterator start = bibkeys.begin();
226 int const sel = fl_get_browser(dialog_->browser_bib);
227 if (sel >= 1 && sel <= int(bibkeys.size()))
230 // Find the NEXT instance...
231 (dir == biblio::FORWARD) ? ++start : --start;
234 vector<string>::const_iterator const cit =
235 biblio::searchKeys(theMap, bibkeys, str,
236 start, type, dir, caseSensitive);
238 if (cit == bibkeys.end())
241 int const found = int(cit - bibkeys.begin()) + 1;
245 // Update the display
246 int const top = max(found - 5, 1);
247 fl_set_browser_topline(dialog_->browser_bib, top);
248 fl_select_browser_line(dialog_->browser_bib, found);
249 input(dialog_->browser_bib, 0);
253 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
255 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
257 biblio::InfoMap const & theMap = controller().bibkeysInfo();
260 if (!citekeys.empty()) topCitekey = citekeys[0];
262 if (ob == dialog_->browser_bib) {
263 fl_deselect_browser(dialog_->browser_cite);
265 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
266 if (sel < 1 || sel > bibkeys.size())
267 return ButtonPolicy::SMI_NOOP;
269 // Put into browser_info the additional info associated with
270 // the selected browser_bib key
271 fl_clear_browser(dialog_->browser_info);
273 string const tmp = formatted(biblio::getInfo(theMap,
275 dialog_->browser_info->w - 10);
276 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
278 // Highlight the selected browser_bib key in browser_cite if
280 vector<string>::const_iterator cit =
281 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
283 if (cit != citekeys.end()) {
284 int const n = int(cit - citekeys.begin());
285 fl_select_browser_line(dialog_->browser_cite, n + 1);
286 fl_set_browser_topline(dialog_->browser_cite, n + 1);
289 if (!controller().bufferIsReadonly()) {
290 if (cit != citekeys.end()) {
299 } else if (ob == dialog_->browser_cite) {
300 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
301 if (sel < 1 || sel > citekeys.size())
302 return ButtonPolicy::SMI_NOOP;
304 if (!controller().bufferIsReadonly()) {
309 // Highlight the selected browser_cite key in browser_bib
310 vector<string>::const_iterator cit =
311 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
313 if (cit != bibkeys.end()) {
314 int const n = int(cit - bibkeys.begin());
315 fl_select_browser_line(dialog_->browser_bib, n + 1);
316 fl_set_browser_topline(dialog_->browser_bib, n + 1);
318 // Put into browser_info the additional info associated
319 // with the selected browser_cite key
320 fl_clear_browser(dialog_->browser_info);
322 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
323 dialog_->browser_info->w - 10);
324 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
327 } else if (ob == dialog_->button_add) {
328 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
329 if (sel < 1 || sel > bibkeys.size())
330 return ButtonPolicy::SMI_NOOP;
332 // Add the selected browser_bib key to browser_cite
333 fl_addto_browser(dialog_->browser_cite,
334 bibkeys[sel - 1].c_str());
335 citekeys.push_back(bibkeys[sel - 1]);
337 int const n = int(citekeys.size());
338 fl_select_browser_line(dialog_->browser_cite, n);
342 activate = ButtonPolicy::SMI_VALID;
344 } else if (ob == dialog_->button_del) {
345 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
346 if (sel < 1 || sel > citekeys.size())
347 return ButtonPolicy::SMI_NOOP;
349 // Remove the selected key from browser_cite
350 fl_delete_browser_line(dialog_->browser_cite, sel) ;
351 citekeys.erase(citekeys.begin() + sel - 1);
355 activate = ButtonPolicy::SMI_VALID;
357 } else if (ob == dialog_->button_up) {
358 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
359 if (sel < 2 || sel > citekeys.size())
360 return ButtonPolicy::SMI_NOOP;
362 // Move the selected key up one line
363 vector<string>::iterator it = citekeys.begin() + sel - 1;
364 string const tmp = *it;
366 fl_delete_browser_line(dialog_->browser_cite, sel);
369 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
370 fl_select_browser_line(dialog_->browser_cite, sel - 1);
371 citekeys.insert(it - 1, tmp);
373 activate = ButtonPolicy::SMI_VALID;
375 } else if (ob == dialog_->button_down) {
376 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
377 if (sel < 1 || sel > citekeys.size() - 1)
378 return ButtonPolicy::SMI_NOOP;
380 // Move the selected key down one line
381 vector<string>::iterator it = citekeys.begin() + sel - 1;
382 string const tmp = *it;
384 fl_delete_browser_line(dialog_->browser_cite, sel);
387 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
388 fl_select_browser_line(dialog_->browser_cite, sel+1);
389 citekeys.insert(it+1, tmp);
391 activate = ButtonPolicy::SMI_VALID;
393 } else if (ob == dialog_->button_previous) {
394 findBiblio(biblio::BACKWARD);
395 } else if (ob == dialog_->button_next) {
396 findBiblio(biblio::FORWARD);
397 } else if (ob == dialog_->input_search) {
398 findBiblio(biblio::FORWARD);
399 } else if (ob == dialog_->choice_style ||
400 ob == dialog_->check_full_author_list ||
401 ob == dialog_->check_force_uppercase ||
402 ob == dialog_->input_before ||
403 ob == dialog_->input_after) {
404 activate = ButtonPolicy::SMI_VALID;
407 string currentCitekey;
408 if (!citekeys.empty())
409 currentCitekey = citekeys[0];
411 if (topCitekey != currentCitekey) {
412 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
413 fillChoice(dialog_.get(),
414 controller().getCiteStrings(currentCitekey));
415 fl_set_choice(dialog_->choice_style, choice);
422 void FormCitation::update()
424 // Make the list of all available bibliography keys
425 bibkeys = biblio::getKeys(controller().bibkeysInfo());
426 updateBrowser(dialog_->browser_bib, bibkeys);
428 // Ditto for the keys cited in this inset
429 citekeys = getVectorFromString(controller().params().getContents());
430 updateBrowser(dialog_->browser_cite, citekeys);
432 // Use the first citekey to fill choice_style
434 if (!citekeys.empty())
437 fillChoice(dialog_.get(), controller().getCiteStrings(key));
439 // Use the citation command to update the GUI
440 updateStyle(dialog_.get(), controller().params().getCmdName());
442 bool const natbib = controller().usingNatbib();
443 setEnabled(dialog_->check_full_author_list, natbib);
444 setEnabled(dialog_->check_force_uppercase, natbib);
445 setEnabled(dialog_->choice_style, natbib);
447 // No keys have been selected yet, so...
448 fl_clear_browser(dialog_->browser_info);
452 // Natbib can have comments before and after the citation.
453 // This is not yet supported. After only.
454 fl_set_input(dialog_->input_after,
455 controller().params().getOptions().c_str());
457 fl_set_input(dialog_->input_before, _("Not yet supported"));
458 setEnabled(dialog_->input_before, false);
462 void FormCitation::updateBrowser(FL_OBJECT * browser,
463 vector<string> const & keys) const
465 // Check whether the current contents of the browser will be
466 // changed by loading the contents of the vec...
467 vector<string> browser_keys = getVector(browser);
469 if (browser_keys == keys) {
470 fl_deselect_browser(browser);
471 fl_set_browser_topline(browser, 1);
475 // They will be changed. Proceed.
476 fl_clear_browser(browser);
478 for (vector<string>::const_iterator it = keys.begin();
479 it != keys.end(); ++it) {
480 string key = trim(*it);
482 fl_add_browser_line(browser, key.c_str());
487 void FormCitation::setBibButtons(State status) const
489 setEnabled(dialog_->button_add, (status == ON));
493 void FormCitation::setCiteButtons(State status) const
495 int const sel = fl_get_browser(dialog_->browser_cite);
496 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
497 bool const activate = (status == ON);
498 bool const activate_up = (activate && sel != 1);
499 bool const activate_down = (activate && sel != maxline);
501 setEnabled(dialog_->button_del, activate);
502 setEnabled(dialog_->button_up, activate_up);
503 setEnabled(dialog_->button_down, activate_down);