1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
10 * \file FormCitation.C
11 * \author Angus Leeming, a.leeming@ic.ac.uk
18 #pragma implementation
22 #include "ControlCitation.h"
23 #include "FormCitation.h"
24 #include "form_citation.h"
26 #include "support/lstrings.h"
28 #include "helper_funcs.h"
29 #include "xforms_helpers.h"
38 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
40 FormCitation::FormCitation(ControlCitation & c)
41 : base_class(c, _("Citation"))
45 void FormCitation::apply()
47 controller().params().setCmdName("cite");
48 controller().params().setContents(getStringFromVector(citekeys));
50 string const after = fl_get_input(dialog_->input_after);
51 controller().params().setOptions(after);
55 void FormCitation::hide()
64 void FormCitation::build()
66 dialog_.reset(build_citation());
68 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
69 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
71 fl_set_button(dialog_->button_search_case, 0);
72 fl_set_button(dialog_->button_search_type, 0);
74 // Manage the ok, apply, restore and cancel/close buttons
75 bc().setOK(dialog_->button_ok);
76 bc().setApply(dialog_->button_apply);
77 bc().setCancel(dialog_->button_cancel);
78 bc().setRestore(dialog_->button_restore);
80 bc().addReadOnly(dialog_->button_add);
81 bc().addReadOnly(dialog_->button_del);
82 bc().addReadOnly(dialog_->button_up);
83 bc().addReadOnly(dialog_->button_down);
84 bc().addReadOnly(dialog_->choice_style);
85 bc().addReadOnly(dialog_->input_before);
86 bc().addReadOnly(dialog_->input_after);
91 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
93 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
95 biblio::InfoMap const & theMap = controller().bibkeysInfo();
97 if (ob == dialog_->browser_bib) {
98 fl_deselect_browser(dialog_->browser_cite);
100 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
101 if (sel < 1 || sel > bibkeys.size())
102 return ButtonPolicy::SMI_NOOP;
104 // Put into browser_info the additional info associated with
105 // the selected browser_bib key
106 fl_clear_browser(dialog_->browser_info);
108 string const tmp = formatted(biblio::getInfo(theMap,
110 dialog_->browser_info->w-10 );
111 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
113 // Highlight the selected browser_bib key in browser_cite if
115 vector<string>::const_iterator cit =
116 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
118 if (cit != citekeys.end()) {
119 int const n = int(cit - citekeys.begin());
120 fl_select_browser_line(dialog_->browser_cite, n+1);
121 fl_set_browser_topline(dialog_->browser_cite, n+1);
124 if (!controller().isReadonly()) {
125 if (cit != citekeys.end()) {
134 } else if (ob == dialog_->browser_cite) {
135 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
136 if (sel < 1 || sel > citekeys.size())
137 return ButtonPolicy::SMI_NOOP;
139 if (!controller().isReadonly()) {
144 // Highlight the selected browser_cite key in browser_bib
145 vector<string>::const_iterator cit =
146 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
148 if (cit != bibkeys.end()) {
149 int const n = int(cit - bibkeys.begin());
150 fl_select_browser_line(dialog_->browser_bib, n+1);
151 fl_set_browser_topline(dialog_->browser_bib, n+1);
153 // Put into browser_info the additional info associated
154 // with the selected browser_cite key
155 fl_clear_browser(dialog_->browser_info);
157 formatted(biblio::getInfo(theMap,
159 dialog_->browser_info->w-10);
160 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
163 } else if (ob == dialog_->button_add) {
164 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
165 if (sel < 1 || sel > bibkeys.size())
166 return ButtonPolicy::SMI_NOOP;
168 // Add the selected browser_bib key to browser_cite
169 fl_addto_browser(dialog_->browser_cite,
170 bibkeys[sel-1].c_str());
171 citekeys.push_back(bibkeys[sel-1]);
173 int const n = int(citekeys.size());
174 fl_select_browser_line(dialog_->browser_cite, n);
178 activate = ButtonPolicy::SMI_VALID;
180 } else if (ob == dialog_->button_del) {
181 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
182 if (sel < 1 || sel > citekeys.size())
183 return ButtonPolicy::SMI_NOOP;
185 // Remove the selected key from browser_cite
186 fl_delete_browser_line(dialog_->browser_cite, sel) ;
187 citekeys.erase(citekeys.begin() + sel-1);
191 activate = ButtonPolicy::SMI_VALID;
193 } else if (ob == dialog_->button_up) {
194 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
195 if (sel < 2 || sel > citekeys.size())
196 return ButtonPolicy::SMI_NOOP;
198 // Move the selected key up one line
199 vector<string>::iterator it = citekeys.begin() + sel-1;
200 string const tmp = *it;
202 fl_delete_browser_line(dialog_->browser_cite, sel);
205 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
206 fl_select_browser_line(dialog_->browser_cite, sel-1);
207 citekeys.insert(it-1, tmp);
209 activate = ButtonPolicy::SMI_VALID;
211 } else if (ob == dialog_->button_down) {
212 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
213 if (sel < 1 || sel > citekeys.size()-1)
214 return ButtonPolicy::SMI_NOOP;
216 // Move the selected key down one line
217 vector<string>::iterator it = citekeys.begin() + sel-1;
218 string const tmp = *it;
220 fl_delete_browser_line(dialog_->browser_cite, sel);
223 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
224 fl_select_browser_line(dialog_->browser_cite, sel+1);
225 citekeys.insert(it+1, tmp);
227 activate = ButtonPolicy::SMI_VALID;
229 } else if (ob == dialog_->button_previous ||
230 ob == dialog_->button_next) {
232 string const str = fl_get_input(dialog_->input_search);
234 biblio::Direction const dir =
235 (ob == dialog_->button_previous) ?
236 biblio::BACKWARD : biblio::FORWARD;
238 biblio::Search const type =
239 fl_get_button(dialog_->button_search_type) ?
240 biblio::REGEX : biblio::SIMPLE;
242 vector<string>::const_iterator start = bibkeys.begin();
243 int const sel = fl_get_browser(dialog_->browser_bib);
244 if (sel >= 1 && sel <= int(bibkeys.size()))
247 // Find the NEXT instance...
248 if (dir == biblio::FORWARD)
253 bool const caseSensitive =
254 fl_get_button(dialog_->button_search_case);
256 vector<string>::const_iterator const cit =
257 biblio::searchKeys(theMap, bibkeys, str,
258 start, type, dir, caseSensitive);
260 if (cit == bibkeys.end())
261 return ButtonPolicy::SMI_NOOP;
263 int const found = int(cit - bibkeys.begin()) + 1;
265 return ButtonPolicy::SMI_NOOP;
267 // Update the display
268 int const top = max(found-5, 1);
269 fl_set_browser_topline(dialog_->browser_bib, top);
270 fl_select_browser_line(dialog_->browser_bib, found);
271 input(dialog_->browser_bib, 0);
273 } else if (ob == dialog_->choice_style ||
274 ob == dialog_->input_before ||
275 ob == dialog_->input_after) {
276 activate = ButtonPolicy::SMI_VALID;
283 void FormCitation::update()
285 // Make the list of all available bibliography keys
286 bibkeys = biblio::getKeys(controller().bibkeysInfo());
287 updateBrowser(dialog_->browser_bib, bibkeys);
289 // Ditto for the keys cited in this inset
290 citekeys = getVectorFromString(controller().params().getContents());
291 updateBrowser(dialog_->browser_cite, citekeys);
293 // No keys have been selected yet, so...
294 fl_clear_browser(dialog_->browser_info);
298 int noKeys = int(max(bibkeys.size(), citekeys.size()));
300 // Place bounds, so that 4 <= noKeys <= 10
301 noKeys = max(4, min(10, noKeys));
303 // Re-size the form to accommodate the new browser size
304 int const size = 20 * noKeys;
305 bool const bibPresent = (bibkeys.size() > 0);
306 setSize(size, bibPresent);
308 fl_set_input(dialog_->input_after,
309 controller().params().getOptions().c_str());
313 void FormCitation::updateBrowser(FL_OBJECT * browser,
314 vector<string> const & keys) const
316 fl_clear_browser(browser);
318 for (vector<string>::const_iterator it = keys.begin();
319 it < keys.end(); ++it) {
320 string key = frontStrip(strip(*it));
322 fl_add_browser_line(browser, key.c_str());
327 void FormCitation::setBibButtons(State status) const
329 setEnabled(dialog_->button_add, (status == ON));
333 void FormCitation::setCiteButtons(State status) const
335 int const sel = fl_get_browser(dialog_->browser_cite);
336 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
337 bool const activate = (status == ON);
338 bool const activate_up = (activate && sel != 1);
339 bool const activate_down = (activate && sel != maxline);
341 setEnabled(dialog_->button_del, activate);
342 setEnabled(dialog_->button_up, activate_up);
343 setEnabled(dialog_->button_down, activate_down);
347 void FormCitation::setSize(int hbrsr, bool bibPresent) const
349 bool const natbib = false; // will eventually be input
350 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
352 // dh1, dh2, dh3 are the vertical separation between elements.
353 // These can be specified because the browser height is fixed
354 // so they are not changed by dynamic resizing
355 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
356 // bottom of cite/bib brsrs to top of info;
357 // bottom of info to top search frame;
358 // bottom of search frame to top next elemnt;
359 // bottom of style to top input_before;
360 // bottom of text to top ok/cancel buttons.
361 static int const dh2 = 10; // bottom of input_before to top input_after;
362 // bottom of ok/cancel buttons to bottom form
363 static int const dh3 = 5; // spacing between add/delete/... buttons.
365 int const wbrsr = dialog_->browser_cite->w;
366 static int const hinfo = dialog_->browser_info->h;
367 static int const hframe = dialog_->frame_search->h;
368 static int const hstyle = dialog_->choice_style->h;
369 static int const htext = dialog_->input_after->h;
370 static int const hok = dialog_->button_ok->h;
372 int hform = dh1 + hbrsr + dh1 + hframe + dh1;
373 if (bibPresent) hform += hinfo + dh1;
374 if (natbib) hform += hstyle + dh1 + htext + dh2;
375 hform += htext + dh1 + hok + dh2;
377 if (hform != minh_) {
379 fl_set_form_size(dialog_->form, minw_, minh_);
385 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
387 x = dialog_->browser_cite->x;
389 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
390 x = dialog_->browser_bib->x;
391 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
393 x = dialog_->button_add->x;
394 fl_set_object_position(dialog_->button_add, x, y);
395 y += dh3 + dialog_->button_add->h;
396 fl_set_object_position(dialog_->button_del, x, y);
397 y += dh3 + dialog_->button_del->h;
398 fl_set_object_position(dialog_->button_up, x, y);
399 y += dh3 + dialog_->button_up->h;
400 fl_set_object_position(dialog_->button_down, x, y);
402 y = dh1 + hbrsr + dh1; // in position for next element
405 x = dialog_->browser_info->x;
406 fl_set_object_position(dialog_->browser_info, x, y);
407 fl_show_object(dialog_->browser_info);
410 fl_hide_object(dialog_->browser_info);
412 x = dialog_->frame_search->x;
413 // ??? The frame height seems to be reduced. Use geometry to enforce it.
414 fl_set_object_geometry(dialog_->frame_search, x, y,
415 dialog_->frame_search->w, hframe);
416 //fl_set_object_position(dialog_->frame_search, x, y);
418 x = dialog_->input_search->x;
420 fl_set_object_position(dialog_->input_search, x, y);
422 x = dialog_->button_previous->x;
423 y += dialog_->input_search->h + 5;
424 fl_set_object_position(dialog_->button_previous, x, y);
426 x = dialog_->button_next->x;
427 y += dialog_->button_previous->h + 5;
428 fl_set_object_position(dialog_->button_next, x, y);
430 x = dialog_->button_search_type->x;
431 y = dialog_->button_previous->y;
432 fl_set_object_position(dialog_->button_search_type, x, y);
434 x = dialog_->button_search_case->x;
435 y = dialog_->button_next->y;
436 fl_set_object_position(dialog_->button_search_case, x, y);
438 y = dialog_->frame_search->y + hframe + dh1;
441 x = dialog_->choice_style->x;
442 fl_set_object_position(dialog_->choice_style, x, y);
443 fl_show_object(dialog_->choice_style);
444 x = dialog_->input_before->x;
446 fl_set_object_position(dialog_->input_before, x, y);
447 fl_show_object(dialog_->input_before);
450 fl_hide_object(dialog_->choice_style);
451 fl_hide_object(dialog_->input_before);
454 x = dialog_->input_after->x;
455 fl_set_object_position(dialog_->input_after, x, y);
458 x = dialog_->button_restore->x;
459 fl_set_object_position(dialog_->button_restore, x, y);
460 x = dialog_->button_ok->x;
461 fl_set_object_position(dialog_->button_ok, x, y);
462 x = dialog_->button_apply->x;
463 fl_set_object_position(dialog_->button_apply, x, y);
464 x = dialog_->button_cancel->x;
465 fl_set_object_position(dialog_->button_cancel, x, y);