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
8 * Full author contact details are available in file CREDITS
15 #pragma implementation
19 #include "ControlCitation.h"
20 #include "FormCitation.h"
21 #include "forms/form_citation.h"
23 #include "helper_funcs.h"
24 #include "xforms_helpers.h"
25 #include FORMS_H_LOCATION
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
40 // shamelessly stolen from Menubar_pimpl.C
41 int string_width(string const & str)
43 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
45 static_cast<int>(str.length()));
49 void fillChoice(FD_citation * dialog, vector<string> vec)
51 // Check whether the current contents of the browser will be
52 // changed by loading the contents of the vec...
53 vector<string> const choice_style =
54 getVector(dialog->choice_style);
56 if (vec == choice_style)
59 // They will be changed. Proceed
62 str += getStringFromVector(vec, " | ") + " ";
64 fl_clear_choice(dialog->choice_style);
65 fl_addto_choice(dialog->choice_style, str.c_str());
67 setEnabled(dialog->choice_style, !vec.empty());
73 void updateStyle(FD_citation * dialog, string command)
75 // Find the style of the citekeys
76 vector<biblio::CiteStyle> const & styles =
77 ControlCitation::getCiteStyles();
78 biblio::CitationStyle cs = biblio::getCitationStyle(command);
80 vector<biblio::CiteStyle>::const_iterator cit =
81 find(styles.begin(), styles.end(), cs.style);
83 // Use this to initialise the GUI
84 if (cit == styles.end()) {
85 fl_set_choice(dialog->choice_style, 1);
86 fl_set_button(dialog->check_full_author_list, 0);
87 fl_set_button(dialog->check_force_uppercase, 0);
89 int const i = int(cit - styles.begin());
90 fl_set_choice(dialog->choice_style, i+1);
91 fl_set_button(dialog->check_full_author_list, cs.full);
92 fl_set_button(dialog->check_force_uppercase, cs.forceUCase);
99 typedef FormCB<ControlCitation, FormDB<FD_citation> > base_class;
102 FormCitation::FormCitation()
103 : base_class(_("Citation"))
107 void FormCitation::apply()
109 string command = "cite";
110 if (dialog_->choice_style->active != 0) {
111 vector<biblio::CiteStyle> const & styles =
112 ControlCitation::getCiteStyles();
115 std::max(0, fl_get_choice(dialog_->choice_style) - 1);
117 fl_get_button(dialog_->check_full_author_list);
119 fl_get_button(dialog_->check_force_uppercase);
121 command = biblio::getCiteCommand(styles[choice], full, force);
124 controller().params().setCmdName(command);
125 controller().params().setContents(getStringFromVector(citekeys));
127 string const after = fl_get_input(dialog_->input_after);
128 controller().params().setOptions(after);
132 void FormCitation::hide()
141 void FormCitation::build()
143 dialog_.reset(build_citation(this));
145 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
146 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
147 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
149 fl_set_button(dialog_->check_search_case, 0);
150 fl_set_button(dialog_->check_search_type, 0);
152 setPrehandler(dialog_->input_search);
153 setPrehandler(dialog_->input_before);
154 setPrehandler(dialog_->input_after);
156 // Manage the ok, apply, restore and cancel/close buttons
157 bc().setOK(dialog_->button_ok);
158 bc().setApply(dialog_->button_apply);
159 bc().setCancel(dialog_->button_close);
160 bc().setRestore(dialog_->button_restore);
162 bc().addReadOnly(dialog_->button_add);
163 bc().addReadOnly(dialog_->button_del);
164 bc().addReadOnly(dialog_->button_up);
165 bc().addReadOnly(dialog_->button_down);
166 bc().addReadOnly(dialog_->choice_style);
167 bc().addReadOnly(dialog_->input_before);
168 bc().addReadOnly(dialog_->input_after);
169 bc().addReadOnly(dialog_->check_full_author_list);
170 bc().addReadOnly(dialog_->check_force_uppercase);
172 //set up the tooltip mechanism
173 string str = _("Add the selected entry to the current citation reference.");
174 tooltips().init(dialog_->button_add, str);
176 str = _("Delete the selected entry from the current citation reference.");
177 tooltips().init(dialog_->button_del, str);
179 str = _("Move the selected entry upwards (in the current list).");
180 tooltips().init(dialog_->button_up, str);
182 str = _("Move the selected entry downwards (in the current list).");
183 tooltips().init(dialog_->button_down, str);
185 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
186 tooltips().init(dialog_->browser_cite, str);
188 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.");
189 tooltips().init(dialog_->browser_bib, str);
191 str = _("Information about the selected entry");
192 tooltips().init(dialog_->browser_info, str);
194 str = _("Here you may select how the citation label should look inside the text (Natbib).");
195 tooltips().init(dialog_->choice_style, str);
197 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
198 tooltips().init(dialog_->check_full_author_list, str);
200 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).");
201 tooltips().init(dialog_->check_force_uppercase, str);
203 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
204 tooltips().init(dialog_->input_before, str);
206 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
207 tooltips().init(dialog_->input_after, str);
209 str = _("Search your database (all fields will be searched).");
210 tooltips().init(dialog_->input_search, str);
212 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
213 tooltips().init(dialog_->check_search_case, str);
215 str = _("Activate if you want to enter Regular Expressions.");
216 tooltips().init(dialog_->check_search_type, str);
220 void FormCitation::findBiblio(biblio::Direction const dir)
222 string const str = fl_get_input(dialog_->input_search);
223 biblio::InfoMap const & theMap = controller().bibkeysInfo();
224 bool const caseSensitive =
225 fl_get_button(dialog_->check_search_case);
226 biblio::Search const type =
227 fl_get_button(dialog_->check_search_type) ?
228 biblio::REGEX : biblio::SIMPLE;
230 vector<string>::const_iterator start = bibkeys.begin();
231 int const sel = fl_get_browser(dialog_->browser_bib);
232 if (sel >= 1 && sel <= int(bibkeys.size()))
235 // Find the NEXT instance...
236 (dir == biblio::FORWARD) ? ++start : --start;
239 vector<string>::const_iterator const cit =
240 biblio::searchKeys(theMap, bibkeys, str,
241 start, type, dir, caseSensitive);
243 if (cit == bibkeys.end())
246 int const found = int(cit - bibkeys.begin()) + 1;
250 // Update the display
251 int const top = max(found - 5, 1);
252 fl_set_browser_topline(dialog_->browser_bib, top);
253 fl_select_browser_line(dialog_->browser_bib, found);
254 input(dialog_->browser_bib, 0);
258 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
260 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
262 biblio::InfoMap const & theMap = controller().bibkeysInfo();
265 if (!citekeys.empty()) topCitekey = citekeys[0];
267 if (ob == dialog_->browser_bib) {
268 fl_deselect_browser(dialog_->browser_cite);
270 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
271 if (sel < 1 || sel > bibkeys.size())
272 return ButtonPolicy::SMI_NOOP;
274 // Put into browser_info the additional info associated with
275 // the selected browser_bib key
276 fl_clear_browser(dialog_->browser_info);
278 string const tmp = formatted(biblio::getInfo(theMap,
280 dialog_->browser_info->w - 10);
281 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
283 // Highlight the selected browser_bib key in browser_cite if
285 vector<string>::const_iterator cit =
286 find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
288 if (cit != citekeys.end()) {
289 int const n = int(cit - citekeys.begin());
290 fl_select_browser_line(dialog_->browser_cite, n + 1);
291 fl_set_browser_topline(dialog_->browser_cite, n + 1);
294 if (!controller().bufferIsReadonly()) {
295 if (cit != citekeys.end()) {
304 } else if (ob == dialog_->browser_cite) {
305 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
306 if (sel < 1 || sel > citekeys.size())
307 return ButtonPolicy::SMI_NOOP;
309 if (!controller().bufferIsReadonly()) {
314 // Highlight the selected browser_cite key in browser_bib
315 vector<string>::const_iterator cit =
316 find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
318 if (cit != bibkeys.end()) {
319 int const n = int(cit - bibkeys.begin());
320 fl_select_browser_line(dialog_->browser_bib, n + 1);
321 fl_set_browser_topline(dialog_->browser_bib, n + 1);
323 // Put into browser_info the additional info associated
324 // with the selected browser_cite key
325 fl_clear_browser(dialog_->browser_info);
327 formatted(biblio::getInfo(theMap,
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()) key = citekeys[0];
442 fillChoice(dialog_.get(), controller().getCiteStrings(key));
444 // Use the citation command to update the GUI
445 updateStyle(dialog_.get(), controller().params().getCmdName());
447 bool const natbib = controller().usingNatbib();
448 setEnabled(dialog_->check_full_author_list, natbib);
449 setEnabled(dialog_->check_force_uppercase, natbib);
450 setEnabled(dialog_->choice_style, natbib);
452 // No keys have been selected yet, so...
453 fl_clear_browser(dialog_->browser_info);
457 // Natbib can have comments before and after the citation.
458 // This is not yet supported. After only.
459 fl_set_input(dialog_->input_after,
460 controller().params().getOptions().c_str());
462 fl_set_input(dialog_->input_before, _("Not yet supported"));
463 setEnabled(dialog_->input_before, false);
467 void FormCitation::updateBrowser(FL_OBJECT * browser,
468 vector<string> const & keys) const
470 // Check whether the current contents of the browser will be
471 // changed by loading the contents of the vec...
472 vector<string> browser_keys = getVector(browser);
474 if (browser_keys == keys)
477 // They will be changed. Proceed.
478 fl_clear_browser(browser);
480 for (vector<string>::const_iterator it = keys.begin();
481 it < keys.end(); ++it) {
482 string key = trim(*it);
484 fl_add_browser_line(browser, key.c_str());
489 void FormCitation::setBibButtons(State status) const
491 setEnabled(dialog_->button_add, (status == ON));
495 void FormCitation::setCiteButtons(State status) const
497 int const sel = fl_get_browser(dialog_->browser_cite);
498 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
499 bool const activate = (status == ON);
500 bool const activate_up = (activate && sel != 1);
501 bool const activate_down = (activate && sel != maxline);
503 setEnabled(dialog_->button_del, activate);
504 setEnabled(dialog_->button_up, activate_up);
505 setEnabled(dialog_->button_down, activate_down);