2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
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_type, 0);
72 fl_set_object_label(dialog_->button_search_type, _("Simple"));
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().setUndoAll(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);
92 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
94 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
96 biblio::InfoMap const & theMap = controller().bibkeysInfo();
98 if (ob == dialog_->browser_bib) {
99 fl_deselect_browser(dialog_->browser_cite);
101 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
102 if (sel < 1 || sel > bibkeys.size())
103 return ButtonPolicy::SMI_NOOP;
105 // Put into browser_info the additional info associated with
106 // the selected browser_bib key
107 fl_clear_browser(dialog_->browser_info);
109 string const tmp = formatted(biblio::getInfo(theMap,
111 dialog_->browser_info->w-10 );
112 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
114 // Highlight the selected browser_bib key in browser_cite if
116 vector<string>::const_iterator cit =
117 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
119 if (cit != citekeys.end()) {
120 int const n = int(cit - citekeys.begin());
121 fl_select_browser_line(dialog_->browser_cite, n+1);
122 fl_set_browser_topline(dialog_->browser_cite, n+1);
125 if (!controller().isReadonly()) {
126 if (cit != citekeys.end()) {
135 } else if (ob == dialog_->browser_cite) {
136 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
137 if (sel < 1 || sel > citekeys.size())
138 return ButtonPolicy::SMI_NOOP;
140 if (!controller().isReadonly()) {
145 // Highlight the selected browser_cite key in browser_bib
146 vector<string>::const_iterator cit =
147 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
149 if (cit != bibkeys.end()) {
150 int const n = int(cit - bibkeys.begin());
151 fl_select_browser_line(dialog_->browser_bib, n+1);
152 fl_set_browser_topline(dialog_->browser_bib, n+1);
154 // Put into browser_info the additional info associated
155 // with the selected browser_cite key
156 fl_clear_browser(dialog_->browser_info);
158 formatted(biblio::getInfo(theMap,
160 dialog_->browser_info->w-10);
161 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
164 } else if (ob == dialog_->button_add) {
165 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
166 if (sel < 1 || sel > bibkeys.size())
167 return ButtonPolicy::SMI_NOOP;
169 // Add the selected browser_bib key to browser_cite
170 fl_addto_browser(dialog_->browser_cite,
171 bibkeys[sel-1].c_str());
172 citekeys.push_back(bibkeys[sel-1]);
174 int const n = int(citekeys.size());
175 fl_select_browser_line(dialog_->browser_cite, n);
179 activate = ButtonPolicy::SMI_VALID;
181 } else if (ob == dialog_->button_del) {
182 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
183 if (sel < 1 || sel > citekeys.size())
184 return ButtonPolicy::SMI_NOOP;
186 // Remove the selected key from browser_cite
187 fl_delete_browser_line(dialog_->browser_cite, sel) ;
188 citekeys.erase(citekeys.begin() + sel-1);
192 activate = ButtonPolicy::SMI_VALID;
194 } else if (ob == dialog_->button_up) {
195 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
196 if (sel < 2 || sel > citekeys.size())
197 return ButtonPolicy::SMI_NOOP;
199 // Move the selected key up one line
200 vector<string>::iterator it = citekeys.begin() + sel-1;
201 string const tmp = *it;
203 fl_delete_browser_line(dialog_->browser_cite, sel);
206 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
207 fl_select_browser_line(dialog_->browser_cite, sel-1);
208 citekeys.insert(it-1, tmp);
210 activate = ButtonPolicy::SMI_VALID;
212 } else if (ob == dialog_->button_down) {
213 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
214 if (sel < 1 || sel > citekeys.size()-1)
215 return ButtonPolicy::SMI_NOOP;
217 // Move the selected key down one line
218 vector<string>::iterator it = citekeys.begin() + sel-1;
219 string const tmp = *it;
221 fl_delete_browser_line(dialog_->browser_cite, sel);
224 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
225 fl_select_browser_line(dialog_->browser_cite, sel+1);
226 citekeys.insert(it+1, tmp);
228 activate = ButtonPolicy::SMI_VALID;
230 } else if (ob == dialog_->button_search_type) {
231 if (fl_get_button(dialog_->button_search_type))
232 fl_set_object_label(dialog_->button_search_type,
235 fl_set_object_label(dialog_->button_search_type,
237 return ButtonPolicy::SMI_NOOP;
239 } else if (ob == dialog_->button_previous ||
240 ob == dialog_->button_next) {
242 string const str = fl_get_input(dialog_->input_search);
244 biblio::Direction const dir =
245 (ob == dialog_->button_previous) ?
246 biblio::BACKWARD : biblio::FORWARD;
248 biblio::Search const type =
249 fl_get_button(dialog_->button_search_type) ?
250 biblio::REGEX : biblio::SIMPLE;
252 vector<string>::const_iterator start = bibkeys.begin();
253 int const sel = fl_get_browser(dialog_->browser_bib);
254 if (sel >= 1 && sel <= int(bibkeys.size()))
257 // Find the NEXT instance...
258 if (dir == biblio::FORWARD)
263 vector<string>::const_iterator const cit =
264 biblio::searchKeys(theMap, bibkeys, str,
267 if (cit == bibkeys.end())
268 return ButtonPolicy::SMI_NOOP;
270 int const found = int(cit - bibkeys.begin()) + 1;
272 return ButtonPolicy::SMI_NOOP;
274 // Update the display
275 int const top = max(found-5, 1);
276 fl_set_browser_topline(dialog_->browser_bib, top);
277 fl_select_browser_line(dialog_->browser_bib, found);
278 input(dialog_->browser_bib, 0);
280 } else if (ob == dialog_->choice_style ||
281 ob == dialog_->input_before ||
282 ob == dialog_->input_after) {
283 activate = ButtonPolicy::SMI_VALID;
290 void FormCitation::update()
292 // Make the list of all available bibliography keys
293 bibkeys = biblio::getKeys(controller().bibkeysInfo());
294 updateBrowser(dialog_->browser_bib, bibkeys);
296 // Ditto for the keys cited in this inset
297 citekeys = getVectorFromString(controller().params().getContents());
298 updateBrowser(dialog_->browser_cite, citekeys);
300 // No keys have been selected yet, so...
301 fl_clear_browser(dialog_->browser_info);
305 int noKeys = int(max(bibkeys.size(), citekeys.size()));
307 // Place bounds, so that 4 <= noKeys <= 10
308 noKeys = max(4, min(10, noKeys));
310 // Re-size the form to accommodate the new browser size
311 int const size = 20 * noKeys;
312 bool const bibPresent = (bibkeys.size() > 0);
313 setSize(size, bibPresent);
315 fl_set_input(dialog_->input_after,
316 controller().params().getOptions().c_str());
320 void FormCitation::updateBrowser(FL_OBJECT * browser,
321 vector<string> const & keys) const
323 fl_clear_browser(browser);
325 for (vector<string>::const_iterator it = keys.begin();
326 it < keys.end(); ++it) {
327 string key = frontStrip(strip(*it));
328 fl_add_browser_line(browser, key.c_str());
333 void FormCitation::setBibButtons(State status) const
335 setEnabled(dialog_->button_add, (status == ON));
339 void FormCitation::setCiteButtons(State status) const
341 int const sel = fl_get_browser(dialog_->browser_cite);
342 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
343 bool const activate = (status == ON);
344 bool const activate_up = (activate && sel != 1);
345 bool const activate_down = (activate && sel != maxline);
347 setEnabled(dialog_->button_del, activate);
348 setEnabled(dialog_->button_up, activate_up);
349 setEnabled(dialog_->button_down, activate_down);
353 void FormCitation::setSize(int hbrsr, bool bibPresent) const
355 bool const natbib = false; // will eventually be input
356 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
358 // dh1, dh2, dh3 are the vertical separation between elements.
359 // These can be specified because the browser height is fixed
360 // so they are not changed by dynamic resizing
361 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
362 // bottom of cite/bib brsrs to top of info;
363 // bottom of info to top search frame;
364 // bottom of search frame to top next elemnt;
365 // bottom of style to top input_before;
366 // bottom of text to top ok/cancel buttons.
367 static int const dh2 = 10; // bottom of input_before to top input_after;
368 // bottom of ok/cancel buttons to bottom form
369 static int const dh3 = 5; // spacing between add/delete/... buttons.
371 int const wbrsr = dialog_->browser_cite->w;
372 static int const hinfo = dialog_->browser_info->h;
373 static int const hframe = dialog_->frame_search->h;
374 static int const hstyle = dialog_->choice_style->h;
375 static int const htext = dialog_->input_after->h;
376 static int const hok = dialog_->button_ok->h;
378 int hform = dh1 + hbrsr + dh1 + hframe + dh1;
379 if (bibPresent) hform += hinfo + dh1;
380 if (natbib) hform += hstyle + dh1 + htext + dh2;
381 hform += htext + dh1 + hok + dh2;
383 if (hform != minh_) {
385 fl_set_form_size(dialog_->form, minw_, minh_);
391 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
393 x = dialog_->browser_cite->x;
395 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
396 x = dialog_->browser_bib->x;
397 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
399 x = dialog_->button_add->x;
400 fl_set_object_position(dialog_->button_add, x, y);
401 y += dh3 + dialog_->button_add->h;
402 fl_set_object_position(dialog_->button_del, x, y);
403 y += dh3 + dialog_->button_del->h;
404 fl_set_object_position(dialog_->button_up, x, y);
405 y += dh3 + dialog_->button_up->h;
406 fl_set_object_position(dialog_->button_down, x, y);
408 y = dh1 + hbrsr + dh1; // in position for next element
411 x = dialog_->browser_info->x;
412 fl_set_object_position(dialog_->browser_info, x, y);
413 fl_show_object(dialog_->browser_info);
416 fl_hide_object(dialog_->browser_info);
418 x = dialog_->frame_search->x;
419 // ??? The frame height seems to be reduced. Use geometry to enforce it.
420 fl_set_object_geometry(dialog_->frame_search, x, y,
421 dialog_->frame_search->w, hframe);
422 //fl_set_object_position(dialog_->frame_search, x, y);
424 x = dialog_->input_search->x;
426 fl_set_object_position(dialog_->input_search, x, y);
428 x = dialog_->button_previous->x;
429 y += dialog_->input_search->h + 5;
430 fl_set_object_position(dialog_->button_previous, x, y);
432 x = dialog_->button_next->x;
433 y += dialog_->button_previous->h + 5;
434 fl_set_object_position(dialog_->button_next, x, y);
436 x = dialog_->button_search_type->x;
437 y = dialog_->button_previous->y;
438 fl_set_object_position(dialog_->button_search_type, x, y);
440 y = dialog_->frame_search->y + hframe + dh1;
443 x = dialog_->choice_style->x;
444 fl_set_object_position(dialog_->choice_style, x, y);
445 fl_show_object(dialog_->choice_style);
446 x = dialog_->input_before->x;
448 fl_set_object_position(dialog_->input_before, x, y);
449 fl_show_object(dialog_->input_before);
452 fl_hide_object(dialog_->choice_style);
453 fl_hide_object(dialog_->input_before);
456 x = dialog_->input_after->x;
457 fl_set_object_position(dialog_->input_after, x, y);
460 x = dialog_->button_restore->x;
461 fl_set_object_position(dialog_->button_restore, x, y);
462 x = dialog_->button_ok->x;
463 fl_set_object_position(dialog_->button_ok, x, y);
464 x = dialog_->button_apply->x;
465 fl_set_object_position(dialog_->button_apply, x, y);
466 x = dialog_->button_cancel->x;
467 fl_set_object_position(dialog_->button_cancel, x, y);