1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 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);
73 fl_set_object_label(dialog_->button_search_type, _("Simple"));
75 // Manage the ok, apply, restore and cancel/close buttons
76 bc().setOK(dialog_->button_ok);
77 bc().setApply(dialog_->button_apply);
78 bc().setCancel(dialog_->button_cancel);
79 bc().setUndoAll(dialog_->button_restore);
81 bc().addReadOnly(dialog_->button_add);
82 bc().addReadOnly(dialog_->button_del);
83 bc().addReadOnly(dialog_->button_up);
84 bc().addReadOnly(dialog_->button_down);
85 bc().addReadOnly(dialog_->choice_style);
86 bc().addReadOnly(dialog_->input_before);
87 bc().addReadOnly(dialog_->input_after);
93 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
95 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
97 biblio::InfoMap const & theMap = controller().bibkeysInfo();
99 if (ob == dialog_->browser_bib) {
100 fl_deselect_browser(dialog_->browser_cite);
102 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
103 if (sel < 1 || sel > bibkeys.size())
104 return ButtonPolicy::SMI_NOOP;
106 // Put into browser_info the additional info associated with
107 // the selected browser_bib key
108 fl_clear_browser(dialog_->browser_info);
110 string const tmp = formatted(biblio::getInfo(theMap,
112 dialog_->browser_info->w-10 );
113 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
115 // Highlight the selected browser_bib key in browser_cite if
117 vector<string>::const_iterator cit =
118 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
120 if (cit != citekeys.end()) {
121 int const n = int(cit - citekeys.begin());
122 fl_select_browser_line(dialog_->browser_cite, n+1);
123 fl_set_browser_topline(dialog_->browser_cite, n+1);
126 if (!controller().isReadonly()) {
127 if (cit != citekeys.end()) {
136 } else if (ob == dialog_->browser_cite) {
137 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
138 if (sel < 1 || sel > citekeys.size())
139 return ButtonPolicy::SMI_NOOP;
141 if (!controller().isReadonly()) {
146 // Highlight the selected browser_cite key in browser_bib
147 vector<string>::const_iterator cit =
148 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
150 if (cit != bibkeys.end()) {
151 int const n = int(cit - bibkeys.begin());
152 fl_select_browser_line(dialog_->browser_bib, n+1);
153 fl_set_browser_topline(dialog_->browser_bib, n+1);
155 // Put into browser_info the additional info associated
156 // with the selected browser_cite key
157 fl_clear_browser(dialog_->browser_info);
159 formatted(biblio::getInfo(theMap,
161 dialog_->browser_info->w-10);
162 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
165 } else if (ob == dialog_->button_add) {
166 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
167 if (sel < 1 || sel > bibkeys.size())
168 return ButtonPolicy::SMI_NOOP;
170 // Add the selected browser_bib key to browser_cite
171 fl_addto_browser(dialog_->browser_cite,
172 bibkeys[sel-1].c_str());
173 citekeys.push_back(bibkeys[sel-1]);
175 int const n = int(citekeys.size());
176 fl_select_browser_line(dialog_->browser_cite, n);
180 activate = ButtonPolicy::SMI_VALID;
182 } else if (ob == dialog_->button_del) {
183 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
184 if (sel < 1 || sel > citekeys.size())
185 return ButtonPolicy::SMI_NOOP;
187 // Remove the selected key from browser_cite
188 fl_delete_browser_line(dialog_->browser_cite, sel) ;
189 citekeys.erase(citekeys.begin() + sel-1);
193 activate = ButtonPolicy::SMI_VALID;
195 } else if (ob == dialog_->button_up) {
196 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
197 if (sel < 2 || sel > citekeys.size())
198 return ButtonPolicy::SMI_NOOP;
200 // Move the selected key up one line
201 vector<string>::iterator it = citekeys.begin() + sel-1;
202 string const tmp = *it;
204 fl_delete_browser_line(dialog_->browser_cite, sel);
207 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
208 fl_select_browser_line(dialog_->browser_cite, sel-1);
209 citekeys.insert(it-1, tmp);
211 activate = ButtonPolicy::SMI_VALID;
213 } else if (ob == dialog_->button_down) {
214 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
215 if (sel < 1 || sel > citekeys.size()-1)
216 return ButtonPolicy::SMI_NOOP;
218 // Move the selected key down one line
219 vector<string>::iterator it = citekeys.begin() + sel-1;
220 string const tmp = *it;
222 fl_delete_browser_line(dialog_->browser_cite, sel);
225 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
226 fl_select_browser_line(dialog_->browser_cite, sel+1);
227 citekeys.insert(it+1, tmp);
229 activate = ButtonPolicy::SMI_VALID;
231 } else if (ob == dialog_->button_search_type) {
232 fl_freeze_form(form());
233 // Fudge to overcome xforms drawing bug
234 fl_hide_object(dialog_->button_search_type);
236 if (fl_get_button(dialog_->button_search_type))
237 fl_set_object_label(dialog_->button_search_type,
240 fl_set_object_label(dialog_->button_search_type,
243 fl_show_object(dialog_->button_search_type);
244 fl_unfreeze_form(form());
246 return ButtonPolicy::SMI_NOOP;
248 } else if (ob == dialog_->button_previous ||
249 ob == dialog_->button_next) {
251 string const str = fl_get_input(dialog_->input_search);
253 biblio::Direction const dir =
254 (ob == dialog_->button_previous) ?
255 biblio::BACKWARD : biblio::FORWARD;
257 biblio::Search const type =
258 fl_get_button(dialog_->button_search_type) ?
259 biblio::REGEX : biblio::SIMPLE;
261 vector<string>::const_iterator start = bibkeys.begin();
262 int const sel = fl_get_browser(dialog_->browser_bib);
263 if (sel >= 1 && sel <= int(bibkeys.size()))
266 // Find the NEXT instance...
267 if (dir == biblio::FORWARD)
272 bool const caseSensitive =
273 fl_get_button(dialog_->button_search_case);
275 vector<string>::const_iterator const cit =
276 biblio::searchKeys(theMap, bibkeys, str,
277 start, type, dir, caseSensitive);
279 if (cit == bibkeys.end())
280 return ButtonPolicy::SMI_NOOP;
282 int const found = int(cit - bibkeys.begin()) + 1;
284 return ButtonPolicy::SMI_NOOP;
286 // Update the display
287 int const top = max(found-5, 1);
288 fl_set_browser_topline(dialog_->browser_bib, top);
289 fl_select_browser_line(dialog_->browser_bib, found);
290 input(dialog_->browser_bib, 0);
292 } else if (ob == dialog_->choice_style ||
293 ob == dialog_->input_before ||
294 ob == dialog_->input_after) {
295 activate = ButtonPolicy::SMI_VALID;
302 void FormCitation::update()
304 // Make the list of all available bibliography keys
305 bibkeys = biblio::getKeys(controller().bibkeysInfo());
306 updateBrowser(dialog_->browser_bib, bibkeys);
308 // Ditto for the keys cited in this inset
309 citekeys = getVectorFromString(controller().params().getContents());
310 updateBrowser(dialog_->browser_cite, citekeys);
312 // No keys have been selected yet, so...
313 fl_clear_browser(dialog_->browser_info);
317 int noKeys = int(max(bibkeys.size(), citekeys.size()));
319 // Place bounds, so that 4 <= noKeys <= 10
320 noKeys = max(4, min(10, noKeys));
322 // Re-size the form to accommodate the new browser size
323 int const size = 20 * noKeys;
324 bool const bibPresent = (bibkeys.size() > 0);
325 setSize(size, bibPresent);
327 fl_set_input(dialog_->input_after,
328 controller().params().getOptions().c_str());
332 void FormCitation::updateBrowser(FL_OBJECT * browser,
333 vector<string> const & keys) const
335 fl_clear_browser(browser);
337 for (vector<string>::const_iterator it = keys.begin();
338 it < keys.end(); ++it) {
339 string key = frontStrip(strip(*it));
340 fl_add_browser_line(browser, key.c_str());
345 void FormCitation::setBibButtons(State status) const
347 setEnabled(dialog_->button_add, (status == ON));
351 void FormCitation::setCiteButtons(State status) const
353 int const sel = fl_get_browser(dialog_->browser_cite);
354 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
355 bool const activate = (status == ON);
356 bool const activate_up = (activate && sel != 1);
357 bool const activate_down = (activate && sel != maxline);
359 setEnabled(dialog_->button_del, activate);
360 setEnabled(dialog_->button_up, activate_up);
361 setEnabled(dialog_->button_down, activate_down);
365 void FormCitation::setSize(int hbrsr, bool bibPresent) const
367 bool const natbib = false; // will eventually be input
368 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
370 // dh1, dh2, dh3 are the vertical separation between elements.
371 // These can be specified because the browser height is fixed
372 // so they are not changed by dynamic resizing
373 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
374 // bottom of cite/bib brsrs to top of info;
375 // bottom of info to top search frame;
376 // bottom of search frame to top next elemnt;
377 // bottom of style to top input_before;
378 // bottom of text to top ok/cancel buttons.
379 static int const dh2 = 10; // bottom of input_before to top input_after;
380 // bottom of ok/cancel buttons to bottom form
381 static int const dh3 = 5; // spacing between add/delete/... buttons.
383 int const wbrsr = dialog_->browser_cite->w;
384 static int const hinfo = dialog_->browser_info->h;
385 static int const hframe = dialog_->frame_search->h;
386 static int const hstyle = dialog_->choice_style->h;
387 static int const htext = dialog_->input_after->h;
388 static int const hok = dialog_->button_ok->h;
390 int hform = dh1 + hbrsr + dh1 + hframe + dh1;
391 if (bibPresent) hform += hinfo + dh1;
392 if (natbib) hform += hstyle + dh1 + htext + dh2;
393 hform += htext + dh1 + hok + dh2;
395 if (hform != minh_) {
397 fl_set_form_size(dialog_->form, minw_, minh_);
403 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
405 x = dialog_->browser_cite->x;
407 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
408 x = dialog_->browser_bib->x;
409 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
411 x = dialog_->button_add->x;
412 fl_set_object_position(dialog_->button_add, x, y);
413 y += dh3 + dialog_->button_add->h;
414 fl_set_object_position(dialog_->button_del, x, y);
415 y += dh3 + dialog_->button_del->h;
416 fl_set_object_position(dialog_->button_up, x, y);
417 y += dh3 + dialog_->button_up->h;
418 fl_set_object_position(dialog_->button_down, x, y);
420 y = dh1 + hbrsr + dh1; // in position for next element
423 x = dialog_->browser_info->x;
424 fl_set_object_position(dialog_->browser_info, x, y);
425 fl_show_object(dialog_->browser_info);
428 fl_hide_object(dialog_->browser_info);
430 x = dialog_->frame_search->x;
431 // ??? The frame height seems to be reduced. Use geometry to enforce it.
432 fl_set_object_geometry(dialog_->frame_search, x, y,
433 dialog_->frame_search->w, hframe);
434 //fl_set_object_position(dialog_->frame_search, x, y);
436 x = dialog_->input_search->x;
438 fl_set_object_position(dialog_->input_search, x, y);
440 x = dialog_->button_previous->x;
441 y += dialog_->input_search->h + 5;
442 fl_set_object_position(dialog_->button_previous, x, y);
444 x = dialog_->button_next->x;
445 y += dialog_->button_previous->h + 5;
446 fl_set_object_position(dialog_->button_next, x, y);
448 x = dialog_->button_search_type->x;
449 y = dialog_->button_previous->y;
450 fl_set_object_position(dialog_->button_search_type, x, y);
452 x = dialog_->button_search_case->x;
453 y = dialog_->button_next->y;
454 fl_set_object_position(dialog_->button_search_case, x, y);
456 y = dialog_->frame_search->y + hframe + dh1;
459 x = dialog_->choice_style->x;
460 fl_set_object_position(dialog_->choice_style, x, y);
461 fl_show_object(dialog_->choice_style);
462 x = dialog_->input_before->x;
464 fl_set_object_position(dialog_->input_before, x, y);
465 fl_show_object(dialog_->input_before);
468 fl_hide_object(dialog_->choice_style);
469 fl_hide_object(dialog_->input_before);
472 x = dialog_->input_after->x;
473 fl_set_object_position(dialog_->input_after, x, y);
476 x = dialog_->button_restore->x;
477 fl_set_object_position(dialog_->button_restore, x, y);
478 x = dialog_->button_ok->x;
479 fl_set_object_position(dialog_->button_ok, x, y);
480 x = dialog_->button_apply->x;
481 fl_set_object_position(dialog_->button_apply, x, y);
482 x = dialog_->button_cancel->x;
483 fl_set_object_position(dialog_->button_cancel, x, y);