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(FD_form_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 getVectorFromChoice(dialog->choice_style);
56 if (vec == choice_style)
59 // They will be changed. Proceed
60 string const str = " " + getStringFromVector(vec, " | ") + " ";
62 fl_clear_choice(dialog->choice_style);
63 fl_addto_choice(dialog->choice_style, str.c_str());
65 // The width of the choice varies with the contents.
66 // Ensure that it is centred in the frame.
69 for (vector<string>::const_iterator it = vec.begin();
70 it != vec.end(); ++it) {
71 width = max(width, string_width(*it));
75 max(5, int(0.5 * (dialog->frame_style->w - width)));
77 fl_set_object_geometry(dialog->choice_style,
78 dialog->frame_style->x + dx,
79 dialog->choice_style->y,
81 dialog->choice_style->h);
85 void updateStyle(FD_form_citation * dialog, string command)
87 // Find the style of the citekeys
88 vector<biblio::CiteStyle> const & styles =
89 ControlCitation::getCiteStyles();
90 biblio::CitationStyle cs = biblio::getCitationStyle(command);
92 vector<biblio::CiteStyle>::const_iterator cit =
93 find(styles.begin(), styles.end(), cs.style);
95 // Use this to initialise the GUI
96 if (cit == styles.end()) {
97 fl_set_choice(dialog->choice_style, 1);
98 fl_set_button(dialog->button_full_author_list, 0);
99 fl_set_button(dialog->button_force_uppercase, 0);
101 int const i = int(cit - styles.begin());
102 fl_set_choice(dialog->choice_style, i+1);
103 fl_set_button(dialog->button_full_author_list, cs.full);
104 fl_set_button(dialog->button_force_uppercase, cs.forceUCase);
110 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
112 FormCitation::FormCitation(ControlCitation & c)
113 : base_class(c, _("Citation"), false)
117 void FormCitation::apply()
119 vector<biblio::CiteStyle> const & styles =
120 ControlCitation::getCiteStyles();
122 int const choice = fl_get_choice(dialog_->choice_style) - 1;
123 bool const full = fl_get_button(dialog_->button_full_author_list);
124 bool const force = fl_get_button(dialog_->button_force_uppercase);
126 string const command =
127 biblio::getCiteCommand(styles[choice], full, force);
129 controller().params().setCmdName(command);
130 controller().params().setContents(getStringFromVector(citekeys));
132 string const after = fl_get_input(dialog_->input_after);
133 controller().params().setOptions(after);
137 void FormCitation::hide()
146 void FormCitation::build()
148 dialog_.reset(build_citation());
150 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
151 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
152 fl_set_input_return(dialog_->input_search, FL_RETURN_END);
154 fl_set_button(dialog_->button_search_case, 0);
155 fl_set_button(dialog_->button_search_type, 0);
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_cancel);
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_->button_full_author_list);
171 bc().addReadOnly(dialog_->button_force_uppercase);
175 void FormCitation::findBiblio(biblio::Direction const dir)
177 string const str = fl_get_input(dialog_->input_search);
178 biblio::InfoMap const & theMap = controller().bibkeysInfo();
179 bool const caseSensitive =
180 fl_get_button(dialog_->button_search_case);
181 biblio::Search const type =
182 fl_get_button(dialog_->button_search_type) ?
183 biblio::REGEX : biblio::SIMPLE;
185 vector<string>::const_iterator start = bibkeys.begin();
186 int const sel = fl_get_browser(dialog_->browser_bib);
187 if (sel >= 1 && sel <= int(bibkeys.size()))
190 // Find the NEXT instance...
191 (dir == biblio::FORWARD) ? ++start : --start;
194 vector<string>::const_iterator const cit =
195 biblio::searchKeys(theMap, bibkeys, str,
196 start, type, dir, caseSensitive);
198 if (cit == bibkeys.end())
201 int const found = int(cit - bibkeys.begin()) + 1;
205 // Update the display
206 int const top = max(found - 5, 1);
207 fl_set_browser_topline(dialog_->browser_bib, top);
208 fl_select_browser_line(dialog_->browser_bib, found);
209 input(dialog_->browser_bib, 0);
213 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
215 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
217 biblio::InfoMap const & theMap = controller().bibkeysInfo();
220 if (!citekeys.empty()) topCitekey = citekeys[0];
222 if (ob == dialog_->browser_bib) {
223 fl_deselect_browser(dialog_->browser_cite);
225 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
226 if (sel < 1 || sel > bibkeys.size())
227 return ButtonPolicy::SMI_NOOP;
229 // Put into browser_info the additional info associated with
230 // the selected browser_bib key
231 fl_clear_browser(dialog_->browser_info);
233 string const tmp = formatted(biblio::getInfo(theMap,
235 dialog_->browser_info->w-10 );
236 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
238 // Highlight the selected browser_bib key in browser_cite if
240 vector<string>::const_iterator cit =
241 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
243 if (cit != citekeys.end()) {
244 int const n = int(cit - citekeys.begin());
245 fl_select_browser_line(dialog_->browser_cite, n+1);
246 fl_set_browser_topline(dialog_->browser_cite, n+1);
249 if (!controller().isReadonly()) {
250 if (cit != citekeys.end()) {
259 } else if (ob == dialog_->browser_cite) {
260 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
261 if (sel < 1 || sel > citekeys.size())
262 return ButtonPolicy::SMI_NOOP;
264 if (!controller().isReadonly()) {
269 // Highlight the selected browser_cite key in browser_bib
270 vector<string>::const_iterator cit =
271 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
273 if (cit != bibkeys.end()) {
274 int const n = int(cit - bibkeys.begin());
275 fl_select_browser_line(dialog_->browser_bib, n+1);
276 fl_set_browser_topline(dialog_->browser_bib, n+1);
278 // Put into browser_info the additional info associated
279 // with the selected browser_cite key
280 fl_clear_browser(dialog_->browser_info);
282 formatted(biblio::getInfo(theMap,
284 dialog_->browser_info->w-10);
285 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
288 } else if (ob == dialog_->button_add) {
289 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
290 if (sel < 1 || sel > bibkeys.size())
291 return ButtonPolicy::SMI_NOOP;
293 // Add the selected browser_bib key to browser_cite
294 fl_addto_browser(dialog_->browser_cite,
295 bibkeys[sel-1].c_str());
296 citekeys.push_back(bibkeys[sel-1]);
298 int const n = int(citekeys.size());
299 fl_select_browser_line(dialog_->browser_cite, n);
303 activate = ButtonPolicy::SMI_VALID;
305 } else if (ob == dialog_->button_del) {
306 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
307 if (sel < 1 || sel > citekeys.size())
308 return ButtonPolicy::SMI_NOOP;
310 // Remove the selected key from browser_cite
311 fl_delete_browser_line(dialog_->browser_cite, sel) ;
312 citekeys.erase(citekeys.begin() + sel-1);
316 activate = ButtonPolicy::SMI_VALID;
318 } else if (ob == dialog_->button_up) {
319 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
320 if (sel < 2 || sel > citekeys.size())
321 return ButtonPolicy::SMI_NOOP;
323 // Move the selected key up one line
324 vector<string>::iterator it = citekeys.begin() + sel-1;
325 string const tmp = *it;
327 fl_delete_browser_line(dialog_->browser_cite, sel);
330 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
331 fl_select_browser_line(dialog_->browser_cite, sel-1);
332 citekeys.insert(it-1, tmp);
334 activate = ButtonPolicy::SMI_VALID;
336 } else if (ob == dialog_->button_down) {
337 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
338 if (sel < 1 || sel > citekeys.size()-1)
339 return ButtonPolicy::SMI_NOOP;
341 // Move the selected key down one line
342 vector<string>::iterator it = citekeys.begin() + sel-1;
343 string const tmp = *it;
345 fl_delete_browser_line(dialog_->browser_cite, sel);
348 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
349 fl_select_browser_line(dialog_->browser_cite, sel+1);
350 citekeys.insert(it+1, tmp);
352 activate = ButtonPolicy::SMI_VALID;
354 } else if (ob == dialog_->button_previous) {
355 findBiblio(biblio::BACKWARD);
356 } else if (ob == dialog_->button_next) {
357 findBiblio(biblio::FORWARD);
358 } else if (ob == dialog_->input_search) {
359 findBiblio(biblio::FORWARD);
360 } else if (ob == dialog_->choice_style ||
361 ob == dialog_->button_full_author_list ||
362 ob == dialog_->button_force_uppercase ||
363 ob == dialog_->input_before ||
364 ob == dialog_->input_after) {
365 activate = ButtonPolicy::SMI_VALID;
368 string currentCitekey;
369 if (!citekeys.empty())
370 currentCitekey = citekeys[0];
372 if (topCitekey != currentCitekey) {
373 int choice = fl_get_choice(dialog_->choice_style);
374 fillChoice(dialog_.get(),
375 controller().getCiteStrings(currentCitekey));
376 fl_set_choice(dialog_->choice_style, choice);
383 void FormCitation::update()
385 // Make the list of all available bibliography keys
386 bibkeys = biblio::getKeys(controller().bibkeysInfo());
387 updateBrowser(dialog_->browser_bib, bibkeys);
389 // Ditto for the keys cited in this inset
390 citekeys = getVectorFromString(controller().params().getContents());
391 updateBrowser(dialog_->browser_cite, citekeys);
393 // Use the first citekey to fill choice_style
395 if (!citekeys.empty()) key = citekeys[0];
397 fillChoice(dialog_.get(), controller().getCiteStrings(key));
399 // Use the citation command to update the GUI
400 updateStyle(dialog_.get(), controller().params().getCmdName());
402 bool const natbib = controller().usingNatbib();
403 setEnabled(dialog_->button_full_author_list, natbib);
404 setEnabled(dialog_->button_force_uppercase, natbib);
405 setEnabled(dialog_->choice_style, natbib);
407 // No keys have been selected yet, so...
408 fl_clear_browser(dialog_->browser_info);
412 // Natbib can have comments before and after the citation.
413 // This is not yet supported. After only.
414 fl_set_input(dialog_->input_after,
415 controller().params().getOptions().c_str());
417 fl_set_input(dialog_->input_before, _("Not yet supported"));
418 setEnabled(dialog_->input_before, false);
422 void FormCitation::updateBrowser(FL_OBJECT * browser,
423 vector<string> const & keys) const
425 // Check whether the current contents of the browser will be
426 // changed by loading the contents of the vec...
427 vector<string> browser_keys = getVectorFromBrowser(browser);
429 if (browser_keys == keys)
432 // They will be changed. Proceed.
433 fl_clear_browser(browser);
435 for (vector<string>::const_iterator it = keys.begin();
436 it < keys.end(); ++it) {
437 string key = frontStrip(strip(*it));
439 fl_add_browser_line(browser, key.c_str());
444 void FormCitation::setBibButtons(State status) const
446 setEnabled(dialog_->button_add, (status == ON));
450 void FormCitation::setCiteButtons(State status) const
452 int const sel = fl_get_browser(dialog_->browser_cite);
453 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
454 bool const activate = (status == ON);
455 bool const activate_up = (activate && sel != 1);
456 bool const activate_down = (activate && sel != maxline);
458 setEnabled(dialog_->button_del, activate);
459 setEnabled(dialog_->button_up, activate_up);
460 setEnabled(dialog_->button_down, activate_down);