2 * \file xforms/FormCitation.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming, a.leeming@ic.ac.uk
13 #pragma implementation
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "form_citation.h"
21 #include "helper_funcs.h"
22 #include "xforms_helpers.h"
24 #include "support/LAssert.h"
25 #include "support/lstrings.h"
36 // shamelessly stolen from Menubar_pimpl.C
37 int string_width(string const & str)
39 return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
41 static_cast<int>(str.length()));
45 void fillChoice(FD_form_citation * dialog, vector<string> vec)
47 // Check whether the current contents of the browser will be
48 // changed by loading the contents of the vec...
49 vector<string> const choice_style =
50 getVectorFromChoice(dialog->choice_style);
52 if (vec == choice_style)
55 // They will be changed. Proceed
56 string const str = " " + getStringFromVector(vec, " | ") + " ";
58 fl_clear_choice(dialog->choice_style);
59 fl_addto_choice(dialog->choice_style, str.c_str());
61 // The width of the choice varies with the contents.
62 // Ensure that it is centred in the frame.
65 for (vector<string>::const_iterator it = vec.begin();
66 it != vec.end(); ++it) {
67 width = max(width, string_width(*it));
71 max(5, int(0.5 * (dialog->frame_style->w - width)));
73 fl_set_object_geometry(dialog->choice_style,
74 dialog->frame_style->x + dx,
75 dialog->choice_style->y,
77 dialog->choice_style->h);
81 void updateStyle(FD_form_citation * dialog, string command)
83 // Find the style of the citekeys
84 vector<biblio::CiteStyle> const & styles =
85 ControlCitation::getCiteStyles();
86 biblio::CitationStyle cs = biblio::getCitationStyle(command);
88 vector<biblio::CiteStyle>::const_iterator cit =
89 find(styles.begin(), styles.end(), cs.style);
91 // Use this to initialise the GUI
92 if (cit == styles.end()) {
93 fl_set_choice(dialog->choice_style, 1);
94 fl_set_button(dialog->check_full_author_list, 0);
95 fl_set_button(dialog->check_force_uppercase, 0);
97 int const i = int(cit - styles.begin());
98 fl_set_choice(dialog->choice_style, i+1);
99 fl_set_button(dialog->check_full_author_list, cs.full);
100 fl_set_button(dialog->check_force_uppercase, cs.forceUCase);
107 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
110 FormCitation::FormCitation(ControlCitation & c)
111 : base_class(c, _("Citation"), false)
115 void FormCitation::apply()
117 vector<biblio::CiteStyle> const & styles =
118 ControlCitation::getCiteStyles();
120 int const choice = fl_get_choice(dialog_->choice_style) - 1;
121 bool const full = fl_get_button(dialog_->check_full_author_list);
122 bool const force = fl_get_button(dialog_->check_force_uppercase);
124 string const command =
125 biblio::getCiteCommand(styles[choice], full, force);
127 controller().params().setCmdName(command);
128 controller().params().setContents(getStringFromVector(citekeys));
130 string const after = fl_get_input(dialog_->input_after);
131 controller().params().setOptions(after);
135 void FormCitation::hide()
144 void FormCitation::build()
146 dialog_.reset(build_citation());
148 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
149 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
150 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
152 fl_set_button(dialog_->check_search_case, 0);
153 fl_set_button(dialog_->check_search_type, 0);
155 setPrehandler(dialog_->input_search);
156 setPrehandler(dialog_->input_before);
157 setPrehandler(dialog_->input_after);
159 // Manage the ok, apply, restore and cancel/close buttons
160 bc().setOK(dialog_->button_ok);
161 bc().setApply(dialog_->button_apply);
162 bc().setCancel(dialog_->button_close);
163 bc().setRestore(dialog_->button_restore);
165 bc().addReadOnly(dialog_->button_add);
166 bc().addReadOnly(dialog_->button_del);
167 bc().addReadOnly(dialog_->button_up);
168 bc().addReadOnly(dialog_->button_down);
169 bc().addReadOnly(dialog_->choice_style);
170 bc().addReadOnly(dialog_->input_before);
171 bc().addReadOnly(dialog_->input_after);
172 bc().addReadOnly(dialog_->check_full_author_list);
173 bc().addReadOnly(dialog_->check_force_uppercase);
175 //set up the tooltip mechanism
176 string str = _("Add the selected entry to the current citation reference.");
177 tooltips().init(dialog_->button_add, str);
179 str = _("Delete the selected entry from the current citation reference.");
180 tooltips().init(dialog_->button_del, str);
182 str = _("Move the selected entry upwards (in the current list).");
183 tooltips().init(dialog_->button_up, str);
185 str = _("Move the selected entry downwards (in the current list).");
186 tooltips().init(dialog_->button_down, str);
188 str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
189 tooltips().init(dialog_->browser_cite, str);
191 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.");
192 tooltips().init(dialog_->browser_bib, str);
194 str = _("Information about the selected entry");
195 tooltips().init(dialog_->browser_info, str);
197 str = _("Here you may select how the citation label should look inside the text (Natbib).");
198 tooltips().init(dialog_->choice_style, str);
200 str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et.al.\" (Natbib).");
201 tooltips().init(dialog_->check_full_author_list, str);
203 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).");
204 tooltips().init(dialog_->check_force_uppercase, str);
206 str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
207 tooltips().init(dialog_->input_before, str);
209 str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
210 tooltips().init(dialog_->input_after, str);
212 str = _("Search your database (all fields will be searched).");
213 tooltips().init(dialog_->input_search, str);
215 str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
216 tooltips().init(dialog_->check_search_case, str);
218 str = _("Activate if you want to enter Regular Expressions.");
219 tooltips().init(dialog_->check_search_type, str);
223 void FormCitation::findBiblio(biblio::Direction const dir)
225 string const str = fl_get_input(dialog_->input_search);
226 biblio::InfoMap const & theMap = controller().bibkeysInfo();
227 bool const caseSensitive =
228 fl_get_button(dialog_->check_search_case);
229 biblio::Search const type =
230 fl_get_button(dialog_->check_search_type) ?
231 biblio::REGEX : biblio::SIMPLE;
233 vector<string>::const_iterator start = bibkeys.begin();
234 int const sel = fl_get_browser(dialog_->browser_bib);
235 if (sel >= 1 && sel <= int(bibkeys.size()))
238 // Find the NEXT instance...
239 (dir == biblio::FORWARD) ? ++start : --start;
242 vector<string>::const_iterator const cit =
243 biblio::searchKeys(theMap, bibkeys, str,
244 start, type, dir, caseSensitive);
246 if (cit == bibkeys.end())
249 int const found = int(cit - bibkeys.begin()) + 1;
253 // Update the display
254 int const top = max(found - 5, 1);
255 fl_set_browser_topline(dialog_->browser_bib, top);
256 fl_select_browser_line(dialog_->browser_bib, found);
257 input(dialog_->browser_bib, 0);
261 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
263 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
265 biblio::InfoMap const & theMap = controller().bibkeysInfo();
268 if (!citekeys.empty()) topCitekey = citekeys[0];
270 if (ob == dialog_->browser_bib) {
271 fl_deselect_browser(dialog_->browser_cite);
273 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
274 if (sel < 1 || sel > bibkeys.size())
275 return ButtonPolicy::SMI_NOOP;
277 // Put into browser_info the additional info associated with
278 // the selected browser_bib key
279 fl_clear_browser(dialog_->browser_info);
281 string const tmp = formatted(biblio::getInfo(theMap,
283 dialog_->browser_info->w-10);
284 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
286 // Highlight the selected browser_bib key in browser_cite if
288 vector<string>::const_iterator cit =
289 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
291 if (cit != citekeys.end()) {
292 int const n = int(cit - citekeys.begin());
293 fl_select_browser_line(dialog_->browser_cite, n+1);
294 fl_set_browser_topline(dialog_->browser_cite, n+1);
297 if (!controller().isReadonly()) {
298 if (cit != citekeys.end()) {
307 } else if (ob == dialog_->browser_cite) {
308 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
309 if (sel < 1 || sel > citekeys.size())
310 return ButtonPolicy::SMI_NOOP;
312 if (!controller().isReadonly()) {
317 // Highlight the selected browser_cite key in browser_bib
318 vector<string>::const_iterator cit =
319 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
321 if (cit != bibkeys.end()) {
322 int const n = int(cit - bibkeys.begin());
323 fl_select_browser_line(dialog_->browser_bib, n+1);
324 fl_set_browser_topline(dialog_->browser_bib, n+1);
326 // Put into browser_info the additional info associated
327 // with the selected browser_cite key
328 fl_clear_browser(dialog_->browser_info);
330 formatted(biblio::getInfo(theMap,
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 = 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()) key = citekeys[0];
445 fillChoice(dialog_.get(), controller().getCiteStrings(key));
447 // Use the citation command to update the GUI
448 updateStyle(dialog_.get(), controller().params().getCmdName());
450 bool const natbib = controller().usingNatbib();
451 setEnabled(dialog_->check_full_author_list, natbib);
452 setEnabled(dialog_->check_force_uppercase, natbib);
453 setEnabled(dialog_->choice_style, natbib);
455 // No keys have been selected yet, so...
456 fl_clear_browser(dialog_->browser_info);
460 // Natbib can have comments before and after the citation.
461 // This is not yet supported. After only.
462 fl_set_input(dialog_->input_after,
463 controller().params().getOptions().c_str());
465 fl_set_input(dialog_->input_before, _("Not yet supported"));
466 setEnabled(dialog_->input_before, false);
470 void FormCitation::updateBrowser(FL_OBJECT * browser,
471 vector<string> const & keys) const
473 // Check whether the current contents of the browser will be
474 // changed by loading the contents of the vec...
475 vector<string> browser_keys = getVectorFromBrowser(browser);
477 if (browser_keys == keys)
480 // They will be changed. Proceed.
481 fl_clear_browser(browser);
483 for (vector<string>::const_iterator it = keys.begin();
484 it < keys.end(); ++it) {
485 string key = frontStrip(strip(*it));
487 fl_add_browser_line(browser, key.c_str());
492 void FormCitation::setBibButtons(State status) const
494 setEnabled(dialog_->button_add, (status == ON));
498 void FormCitation::setCiteButtons(State status) const
500 int const sel = fl_get_browser(dialog_->browser_cite);
501 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
502 bool const activate = (status == ON);
503 bool const activate_up = (activate && sel != 1);
504 bool const activate_down = (activate && sel != maxline);
506 setEnabled(dialog_->button_del, activate);
507 setEnabled(dialog_->button_up, activate_up);
508 setEnabled(dialog_->button_down, activate_down);