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);
106 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
108 FormCitation::FormCitation(ControlCitation & c)
109 : base_class(c, _("Citation"), false)
113 void FormCitation::apply()
115 vector<biblio::CiteStyle> const & styles =
116 ControlCitation::getCiteStyles();
118 int const choice = fl_get_choice(dialog_->choice_style) - 1;
119 bool const full = fl_get_button(dialog_->check_full_author_list);
120 bool const force = fl_get_button(dialog_->check_force_uppercase);
122 string const command =
123 biblio::getCiteCommand(styles[choice], full, force);
125 controller().params().setCmdName(command);
126 controller().params().setContents(getStringFromVector(citekeys));
128 string const after = fl_get_input(dialog_->input_after);
129 controller().params().setOptions(after);
133 void FormCitation::hide()
142 void FormCitation::build()
144 dialog_.reset(build_citation());
146 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
147 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
148 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
150 fl_set_button(dialog_->check_search_case, 0);
151 fl_set_button(dialog_->check_search_type, 0);
153 setPrehandler(dialog_->input_search);
154 setPrehandler(dialog_->input_before);
155 setPrehandler(dialog_->input_after);
157 // Manage the ok, apply, restore and cancel/close buttons
158 bc().setOK(dialog_->button_ok);
159 bc().setApply(dialog_->button_apply);
160 bc().setCancel(dialog_->button_close);
161 bc().setRestore(dialog_->button_restore);
163 bc().addReadOnly(dialog_->button_add);
164 bc().addReadOnly(dialog_->button_del);
165 bc().addReadOnly(dialog_->button_up);
166 bc().addReadOnly(dialog_->button_down);
167 bc().addReadOnly(dialog_->choice_style);
168 bc().addReadOnly(dialog_->input_before);
169 bc().addReadOnly(dialog_->input_after);
170 bc().addReadOnly(dialog_->check_full_author_list);
171 bc().addReadOnly(dialog_->check_force_uppercase);
173 //set up the tooltip mechanism
174 string str = N_("Add the selected entry to the current citation reference.");
175 tooltips().initTooltip(dialog_->button_add, str);
177 str = N_("Delete the selected entry from the current citation reference.");
178 tooltips().initTooltip(dialog_->button_del, str);
180 str = N_("Move the selected entry upwards (in the current list).");
181 tooltips().initTooltip(dialog_->button_up, str);
183 str = N_("Move the selected entry downwards (in the current list).");
184 tooltips().initTooltip(dialog_->button_down, str);
186 str = N_("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
187 tooltips().initTooltip(dialog_->browser_cite, str);
189 str = N_("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.");
190 tooltips().initTooltip(dialog_->browser_bib, str);
192 str = N_("Information about the selected entry");
193 tooltips().initTooltip(dialog_->browser_info, str);
195 str = N_("Here you may select how the citation label should look inside the text (Natbib).");
196 tooltips().initTooltip(dialog_->choice_style, str);
198 str = N_("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et.al.\" (Natbib).");
199 tooltips().initTooltip(dialog_->check_full_author_list, str);
201 str = N_("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).");
202 tooltips().initTooltip(dialog_->check_force_uppercase, str);
204 str = N_("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
205 tooltips().initTooltip(dialog_->input_before, str);
207 str = N_("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
208 tooltips().initTooltip(dialog_->input_after, str);
210 str = N_("Search your database (all fields will be searched).");
211 tooltips().initTooltip(dialog_->input_search, str);
213 str = N_("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
214 tooltips().initTooltip(dialog_->check_search_case, str);
216 str = N_("Activate if you want to enter Regular Expressions.");
217 tooltips().initTooltip(dialog_->check_search_type, str);
221 void FormCitation::findBiblio(biblio::Direction const dir)
223 string const str = fl_get_input(dialog_->input_search);
224 biblio::InfoMap const & theMap = controller().bibkeysInfo();
225 bool const caseSensitive =
226 fl_get_button(dialog_->check_search_case);
227 biblio::Search const type =
228 fl_get_button(dialog_->check_search_type) ?
229 biblio::REGEX : biblio::SIMPLE;
231 vector<string>::const_iterator start = bibkeys.begin();
232 int const sel = fl_get_browser(dialog_->browser_bib);
233 if (sel >= 1 && sel <= int(bibkeys.size()))
236 // Find the NEXT instance...
237 (dir == biblio::FORWARD) ? ++start : --start;
240 vector<string>::const_iterator const cit =
241 biblio::searchKeys(theMap, bibkeys, str,
242 start, type, dir, caseSensitive);
244 if (cit == bibkeys.end())
247 int const found = int(cit - bibkeys.begin()) + 1;
251 // Update the display
252 int const top = max(found - 5, 1);
253 fl_set_browser_topline(dialog_->browser_bib, top);
254 fl_select_browser_line(dialog_->browser_bib, found);
255 input(dialog_->browser_bib, 0);
259 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
261 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
263 biblio::InfoMap const & theMap = controller().bibkeysInfo();
266 if (!citekeys.empty()) topCitekey = citekeys[0];
268 if (ob == dialog_->browser_bib) {
269 fl_deselect_browser(dialog_->browser_cite);
271 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
272 if (sel < 1 || sel > bibkeys.size())
273 return ButtonPolicy::SMI_NOOP;
275 // Put into browser_info the additional info associated with
276 // the selected browser_bib key
277 fl_clear_browser(dialog_->browser_info);
279 string const tmp = formatted(biblio::getInfo(theMap,
281 dialog_->browser_info->w-10);
282 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
284 // Highlight the selected browser_bib key in browser_cite if
286 vector<string>::const_iterator cit =
287 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
289 if (cit != citekeys.end()) {
290 int const n = int(cit - citekeys.begin());
291 fl_select_browser_line(dialog_->browser_cite, n+1);
292 fl_set_browser_topline(dialog_->browser_cite, n+1);
295 if (!controller().isReadonly()) {
296 if (cit != citekeys.end()) {
305 } else if (ob == dialog_->browser_cite) {
306 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
307 if (sel < 1 || sel > citekeys.size())
308 return ButtonPolicy::SMI_NOOP;
310 if (!controller().isReadonly()) {
315 // Highlight the selected browser_cite key in browser_bib
316 vector<string>::const_iterator cit =
317 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
319 if (cit != bibkeys.end()) {
320 int const n = int(cit - bibkeys.begin());
321 fl_select_browser_line(dialog_->browser_bib, n+1);
322 fl_set_browser_topline(dialog_->browser_bib, n+1);
324 // Put into browser_info the additional info associated
325 // with the selected browser_cite key
326 fl_clear_browser(dialog_->browser_info);
328 formatted(biblio::getInfo(theMap,
330 dialog_->browser_info->w-10);
331 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
334 } else if (ob == dialog_->button_add) {
335 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
336 if (sel < 1 || sel > bibkeys.size())
337 return ButtonPolicy::SMI_NOOP;
339 // Add the selected browser_bib key to browser_cite
340 fl_addto_browser(dialog_->browser_cite,
341 bibkeys[sel-1].c_str());
342 citekeys.push_back(bibkeys[sel-1]);
344 int const n = int(citekeys.size());
345 fl_select_browser_line(dialog_->browser_cite, n);
349 activate = ButtonPolicy::SMI_VALID;
351 } else if (ob == dialog_->button_del) {
352 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
353 if (sel < 1 || sel > citekeys.size())
354 return ButtonPolicy::SMI_NOOP;
356 // Remove the selected key from browser_cite
357 fl_delete_browser_line(dialog_->browser_cite, sel) ;
358 citekeys.erase(citekeys.begin() + sel-1);
362 activate = ButtonPolicy::SMI_VALID;
364 } else if (ob == dialog_->button_up) {
365 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
366 if (sel < 2 || sel > citekeys.size())
367 return ButtonPolicy::SMI_NOOP;
369 // Move the selected key up one line
370 vector<string>::iterator it = citekeys.begin() + sel-1;
371 string const tmp = *it;
373 fl_delete_browser_line(dialog_->browser_cite, sel);
376 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
377 fl_select_browser_line(dialog_->browser_cite, sel-1);
378 citekeys.insert(it-1, tmp);
380 activate = ButtonPolicy::SMI_VALID;
382 } else if (ob == dialog_->button_down) {
383 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
384 if (sel < 1 || sel > citekeys.size()-1)
385 return ButtonPolicy::SMI_NOOP;
387 // Move the selected key down one line
388 vector<string>::iterator it = citekeys.begin() + sel-1;
389 string const tmp = *it;
391 fl_delete_browser_line(dialog_->browser_cite, sel);
394 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
395 fl_select_browser_line(dialog_->browser_cite, sel+1);
396 citekeys.insert(it+1, tmp);
398 activate = ButtonPolicy::SMI_VALID;
400 } else if (ob == dialog_->button_previous) {
401 findBiblio(biblio::BACKWARD);
402 } else if (ob == dialog_->button_next) {
403 findBiblio(biblio::FORWARD);
404 } else if (ob == dialog_->input_search) {
405 findBiblio(biblio::FORWARD);
406 } else if (ob == dialog_->choice_style ||
407 ob == dialog_->check_full_author_list ||
408 ob == dialog_->check_force_uppercase ||
409 ob == dialog_->input_before ||
410 ob == dialog_->input_after) {
411 activate = ButtonPolicy::SMI_VALID;
414 string currentCitekey;
415 if (!citekeys.empty())
416 currentCitekey = citekeys[0];
418 if (topCitekey != currentCitekey) {
419 int choice = fl_get_choice(dialog_->choice_style);
420 fillChoice(dialog_.get(),
421 controller().getCiteStrings(currentCitekey));
422 fl_set_choice(dialog_->choice_style, choice);
429 void FormCitation::update()
431 // Make the list of all available bibliography keys
432 bibkeys = biblio::getKeys(controller().bibkeysInfo());
433 updateBrowser(dialog_->browser_bib, bibkeys);
435 // Ditto for the keys cited in this inset
436 citekeys = getVectorFromString(controller().params().getContents());
437 updateBrowser(dialog_->browser_cite, citekeys);
439 // Use the first citekey to fill choice_style
441 if (!citekeys.empty()) key = citekeys[0];
443 fillChoice(dialog_.get(), controller().getCiteStrings(key));
445 // Use the citation command to update the GUI
446 updateStyle(dialog_.get(), controller().params().getCmdName());
448 bool const natbib = controller().usingNatbib();
449 setEnabled(dialog_->check_full_author_list, natbib);
450 setEnabled(dialog_->check_force_uppercase, natbib);
451 setEnabled(dialog_->choice_style, natbib);
453 // No keys have been selected yet, so...
454 fl_clear_browser(dialog_->browser_info);
458 // Natbib can have comments before and after the citation.
459 // This is not yet supported. After only.
460 fl_set_input(dialog_->input_after,
461 controller().params().getOptions().c_str());
463 fl_set_input(dialog_->input_before, _("Not yet supported"));
464 setEnabled(dialog_->input_before, false);
468 void FormCitation::updateBrowser(FL_OBJECT * browser,
469 vector<string> const & keys) const
471 // Check whether the current contents of the browser will be
472 // changed by loading the contents of the vec...
473 vector<string> browser_keys = getVectorFromBrowser(browser);
475 if (browser_keys == keys)
478 // They will be changed. Proceed.
479 fl_clear_browser(browser);
481 for (vector<string>::const_iterator it = keys.begin();
482 it < keys.end(); ++it) {
483 string key = frontStrip(strip(*it));
485 fl_add_browser_line(browser, key.c_str());
490 void FormCitation::setBibButtons(State status) const
492 setEnabled(dialog_->button_add, (status == ON));
496 void FormCitation::setCiteButtons(State status) const
498 int const sel = fl_get_browser(dialog_->browser_cite);
499 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
500 bool const activate = (status == ON);
501 bool const activate_up = (activate && sel != 1);
502 bool const activate_down = (activate && sel != maxline);
504 setEnabled(dialog_->button_del, activate);
505 setEnabled(dialog_->button_up, activate_up);
506 setEnabled(dialog_->button_down, activate_down);