2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000-2001 The LyX Team.
9 * ======================================================
11 * \file FormCitation.C
12 * \author Angus Leeming, a.leeming@ic.ac.uk
19 #pragma implementation
23 #include "ControlCitation.h"
24 #include "FormCitation.h"
25 #include "form_citation.h"
27 #include "support/lstrings.h"
28 #include "helper_funcs.h"
29 #include "xforms_helpers.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(FL_OBJECT * choice, vector<string> const & vec)
51 string const str = " " + getStringFromVector(vec, " | ") + " ";
53 fl_clear_choice(choice);
54 fl_addto_choice(choice, str.c_str());
57 for (vector<string>::const_iterator it = vec.begin();
58 it != vec.end(); ++it) {
59 width = max(width, string_width(*it));
63 int const x = max(5, int(choice->x + 0.5 * (choice->w - width)));
64 if (x + width > choice->form->w)
65 width = choice->form->w - 10;
67 fl_set_object_geometry(choice, x, choice->y, width + 5, choice->h);
70 void updateStyle(FL_OBJECT * choice, FL_OBJECT * full, FL_OBJECT * force,
73 // Find the style of the citekeys
74 vector<biblio::CiteStyle> const & styles =
75 ControlCitation::getCiteStyles();
76 biblio::CitationStyle cs = biblio::getCitationStyle(command);
78 vector<biblio::CiteStyle>::const_iterator cit =
79 find(styles.begin(), styles.end(), cs.style);
81 // Use this to initialise the GUI
82 if (cit == styles.end()) {
83 fl_set_choice(choice, 1);
84 fl_set_button(full, 0);
85 fl_set_button(force, 0);
87 int const i = int(cit - styles.begin());
88 fl_set_choice(choice, i+1);
89 fl_set_button(full, cs.full);
90 fl_set_button(force, cs.forceUCase);
96 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
98 FormCitation::FormCitation(ControlCitation & c)
99 : base_class(c, _("Citation"))
103 void FormCitation::apply()
105 vector<biblio::CiteStyle> const & styles =
106 ControlCitation::getCiteStyles();
108 int const choice = fl_get_choice(dialog_->choice_style) - 1;
109 bool const full = fl_get_button(dialog_->button_full_author_list);
110 bool const force = fl_get_button(dialog_->button_force_uppercase);
112 string const command =
113 biblio::getCiteCommand(styles[choice], full, force);
115 controller().params().setCmdName(command);
116 controller().params().setContents(getStringFromVector(citekeys));
118 string const after = fl_get_input(dialog_->input_after);
119 controller().params().setOptions(after);
123 void FormCitation::hide()
132 void FormCitation::build()
134 dialog_.reset(build_citation());
136 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
137 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
138 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
140 fl_set_button(dialog_->button_search_case, 0);
141 fl_set_button(dialog_->button_search_type, 0);
143 // Manage the ok, apply, restore and cancel/close buttons
144 bc().setOK(dialog_->button_ok);
145 bc().setApply(dialog_->button_apply);
146 bc().setCancel(dialog_->button_cancel);
147 bc().setRestore(dialog_->button_restore);
149 bc().addReadOnly(dialog_->button_add);
150 bc().addReadOnly(dialog_->button_del);
151 bc().addReadOnly(dialog_->button_up);
152 bc().addReadOnly(dialog_->button_down);
153 bc().addReadOnly(dialog_->choice_style);
154 bc().addReadOnly(dialog_->input_before);
155 bc().addReadOnly(dialog_->input_after);
156 bc().addReadOnly(dialog_->button_full_author_list);
157 bc().addReadOnly(dialog_->button_force_uppercase);
161 void FormCitation::findBiblio(biblio::Direction const dir)
163 string const str = fl_get_input(dialog_->input_search);
164 biblio::InfoMap const & theMap = controller().bibkeysInfo();
165 bool const caseSensitive =
166 fl_get_button(dialog_->button_search_case);
167 biblio::Search const type =
168 fl_get_button(dialog_->button_search_type) ?
169 biblio::REGEX : biblio::SIMPLE;
171 vector<string>::const_iterator start = bibkeys.begin();
172 int const sel = fl_get_browser(dialog_->browser_bib);
173 if (sel >= 1 && sel <= int(bibkeys.size()))
176 // Find the NEXT instance...
177 (dir == biblio::FORWARD) ? ++start : --start;
180 vector<string>::const_iterator const cit =
181 biblio::searchKeys(theMap, bibkeys, str,
182 start, type, dir, caseSensitive);
184 if (cit == bibkeys.end())
187 int const found = int(cit - bibkeys.begin()) + 1;
191 // Update the display
192 int const top = max(found - 5, 1);
193 fl_set_browser_topline(dialog_->browser_bib, top);
194 fl_select_browser_line(dialog_->browser_bib, found);
195 input(dialog_->browser_bib, 0);
199 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
201 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
203 biblio::InfoMap const & theMap = controller().bibkeysInfo();
206 if (!citekeys.empty()) topCitekey = citekeys[0];
208 if (ob == dialog_->browser_bib) {
209 fl_deselect_browser(dialog_->browser_cite);
211 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
212 if (sel < 1 || sel > bibkeys.size())
213 return ButtonPolicy::SMI_NOOP;
215 // Put into browser_info the additional info associated with
216 // the selected browser_bib key
217 fl_clear_browser(dialog_->browser_info);
219 string const tmp = formatted(biblio::getInfo(theMap,
221 dialog_->browser_info->w-10 );
222 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
224 // Highlight the selected browser_bib key in browser_cite if
226 vector<string>::const_iterator cit =
227 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
229 if (cit != citekeys.end()) {
230 int const n = int(cit - citekeys.begin());
231 fl_select_browser_line(dialog_->browser_cite, n+1);
232 fl_set_browser_topline(dialog_->browser_cite, n+1);
235 if (!controller().isReadonly()) {
236 if (cit != citekeys.end()) {
245 } else if (ob == dialog_->browser_cite) {
246 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
247 if (sel < 1 || sel > citekeys.size())
248 return ButtonPolicy::SMI_NOOP;
250 if (!controller().isReadonly()) {
255 // Highlight the selected browser_cite key in browser_bib
256 vector<string>::const_iterator cit =
257 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
259 if (cit != bibkeys.end()) {
260 int const n = int(cit - bibkeys.begin());
261 fl_select_browser_line(dialog_->browser_bib, n+1);
262 fl_set_browser_topline(dialog_->browser_bib, n+1);
264 // Put into browser_info the additional info associated
265 // with the selected browser_cite key
266 fl_clear_browser(dialog_->browser_info);
268 formatted(biblio::getInfo(theMap,
270 dialog_->browser_info->w-10);
271 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
274 } else if (ob == dialog_->button_add) {
275 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
276 if (sel < 1 || sel > bibkeys.size())
277 return ButtonPolicy::SMI_NOOP;
279 // Add the selected browser_bib key to browser_cite
280 fl_addto_browser(dialog_->browser_cite,
281 bibkeys[sel-1].c_str());
282 citekeys.push_back(bibkeys[sel-1]);
284 int const n = int(citekeys.size());
285 fl_select_browser_line(dialog_->browser_cite, n);
289 activate = ButtonPolicy::SMI_VALID;
291 } else if (ob == dialog_->button_del) {
292 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
293 if (sel < 1 || sel > citekeys.size())
294 return ButtonPolicy::SMI_NOOP;
296 // Remove the selected key from browser_cite
297 fl_delete_browser_line(dialog_->browser_cite, sel) ;
298 citekeys.erase(citekeys.begin() + sel-1);
302 activate = ButtonPolicy::SMI_VALID;
304 } else if (ob == dialog_->button_up) {
305 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
306 if (sel < 2 || sel > citekeys.size())
307 return ButtonPolicy::SMI_NOOP;
309 // Move the selected key up one line
310 vector<string>::iterator it = citekeys.begin() + sel-1;
311 string const tmp = *it;
313 fl_delete_browser_line(dialog_->browser_cite, sel);
316 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
317 fl_select_browser_line(dialog_->browser_cite, sel-1);
318 citekeys.insert(it-1, tmp);
320 activate = ButtonPolicy::SMI_VALID;
322 } else if (ob == dialog_->button_down) {
323 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
324 if (sel < 1 || sel > citekeys.size()-1)
325 return ButtonPolicy::SMI_NOOP;
327 // Move the selected key down one line
328 vector<string>::iterator it = citekeys.begin() + sel-1;
329 string const tmp = *it;
331 fl_delete_browser_line(dialog_->browser_cite, sel);
334 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
335 fl_select_browser_line(dialog_->browser_cite, sel+1);
336 citekeys.insert(it+1, tmp);
338 activate = ButtonPolicy::SMI_VALID;
340 } else if (ob == dialog_->button_previous) {
341 findBiblio(biblio::BACKWARD);
342 } else if (ob == dialog_->button_next) {
343 findBiblio(biblio::FORWARD);
344 } else if (ob == dialog_->input_search) {
345 findBiblio(biblio::FORWARD);
346 } else if (ob == dialog_->choice_style ||
347 ob == dialog_->button_full_author_list ||
348 ob == dialog_->button_force_uppercase ||
349 ob == dialog_->input_before ||
350 ob == dialog_->input_after) {
351 activate = ButtonPolicy::SMI_VALID;
354 string currentCitekey;
355 if (!citekeys.empty())
356 currentCitekey = citekeys[0];
358 if (topCitekey != currentCitekey) {
359 int choice = fl_get_choice(dialog_->choice_style);
360 fillChoice(dialog_->choice_style,
361 controller().getCiteStrings(currentCitekey));
362 fl_set_choice(dialog_->choice_style, choice);
369 void FormCitation::update()
371 // Make the list of all available bibliography keys
372 bibkeys = biblio::getKeys(controller().bibkeysInfo());
373 updateBrowser(dialog_->browser_bib, bibkeys);
375 // Ditto for the keys cited in this inset
376 citekeys = getVectorFromString(controller().params().getContents());
377 updateBrowser(dialog_->browser_cite, citekeys);
379 // Use the first citekey to fill choice_style
381 if (!citekeys.empty()) key = citekeys[0];
383 fillChoice(dialog_->choice_style, controller().getCiteStrings(key));
385 // Use the citation command to update the GUI
386 updateStyle(dialog_->choice_style,
387 dialog_->button_full_author_list,
388 dialog_->button_force_uppercase,
389 controller().params().getCmdName());
391 bool const natbib = controller().usingNatbib();
392 setEnabled(dialog_->button_full_author_list, natbib);
393 setEnabled(dialog_->button_force_uppercase, natbib);
394 setEnabled(dialog_->choice_style, natbib);
396 // No keys have been selected yet, so...
397 fl_clear_browser(dialog_->browser_info);
401 // Natbib can have comments before and after the citation.
402 // This is not yet supported. After only.
403 fl_set_input(dialog_->input_after,
404 controller().params().getOptions().c_str());
406 fl_set_input(dialog_->input_before, _("Not yet supported"));
407 setEnabled(dialog_->input_before, false);
411 void FormCitation::updateBrowser(FL_OBJECT * browser,
412 vector<string> const & keys) const
414 fl_clear_browser(browser);
416 for (vector<string>::const_iterator it = keys.begin();
417 it < keys.end(); ++it) {
418 string key = frontStrip(strip(*it));
420 fl_add_browser_line(browser, key.c_str());
425 void FormCitation::setBibButtons(State status) const
427 setEnabled(dialog_->button_add, (status == ON));
431 void FormCitation::setCiteButtons(State status) const
433 int const sel = fl_get_browser(dialog_->browser_cite);
434 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
435 bool const activate = (status == ON);
436 bool const activate_up = (activate && sel != 1);
437 bool const activate_down = (activate && sel != maxline);
439 setEnabled(dialog_->button_del, activate);
440 setEnabled(dialog_->button_up, activate_up);
441 setEnabled(dialog_->button_down, activate_down);