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>
17 #pragma implementation
21 #include "ControlCitation.h"
22 #include "FormCitation.h"
23 #include "form_citation.h"
25 #include "support/lstrings.h"
26 #include "xforms_helpers.h"
28 #include "helper_funcs.h"
37 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
39 FormCitation::FormCitation(ControlCitation & c)
40 : base_class(c, _("Citation"))
44 void FormCitation::apply()
46 controller().params().setCmdName("cite");
47 controller().params().setContents(getStringFromVector(citekeys));
49 string const after = fl_get_input(dialog_->input_after);
50 controller().params().setOptions(after);
54 void FormCitation::hide()
63 void FormCitation::build()
65 dialog_.reset(build_citation());
67 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
68 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
70 fl_set_button(dialog_->button_search_type, 0);
71 fl_set_object_label(dialog_->button_search_type, _("Simple"));
73 // Manage the ok, apply, restore and cancel/close buttons
74 bc().setOK(dialog_->button_ok);
75 bc().setApply(dialog_->button_apply);
76 bc().setCancel(dialog_->button_cancel);
77 bc().setUndoAll(dialog_->button_restore);
79 bc().addReadOnly(dialog_->button_add);
80 bc().addReadOnly(dialog_->button_del);
81 bc().addReadOnly(dialog_->button_up);
82 bc().addReadOnly(dialog_->button_down);
83 bc().addReadOnly(dialog_->choice_style);
84 bc().addReadOnly(dialog_->input_before);
85 bc().addReadOnly(dialog_->input_after);
91 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
93 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
95 if (ob == dialog_->browser_bib) {
96 fl_deselect_browser(dialog_->browser_cite);
98 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
99 if (sel < 1 || sel > bibkeys.size())
100 return ButtonPolicy::SMI_NOOP;
102 // Put into browser_info the additional info associated with
103 // the selected browser_bib key
104 fl_clear_browser(dialog_->browser_info);
105 string const tmp = formatted( controller().
106 getBibkeyInfo(bibkeys[sel-1]),
107 dialog_->browser_info->w-10 );
108 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
110 // Highlight the selected browser_bib key in browser_cite if
112 vector<string>::const_iterator cit =
113 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
115 if (cit != citekeys.end()) {
116 int const n = int(cit - citekeys.begin());
117 fl_select_browser_line(dialog_->browser_cite, n+1);
118 fl_set_browser_topline(dialog_->browser_cite, n+1);
121 if (!controller().isReadonly()) {
122 if (cit != citekeys.end()) {
131 } else if (ob == dialog_->browser_cite) {
132 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
133 if (sel < 1 || sel > citekeys.size())
134 return ButtonPolicy::SMI_NOOP;
136 if (!controller().isReadonly()) {
141 // Highlight the selected browser_cite key in browser_bib
142 vector<string>::const_iterator cit =
143 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
145 if (cit != bibkeys.end()) {
146 int const n = int(cit - bibkeys.begin());
147 fl_select_browser_line(dialog_->browser_bib, n+1);
148 fl_set_browser_topline(dialog_->browser_bib, n+1);
150 // Put into browser_info the additional info associated
151 // with the selected browser_cite key
152 fl_clear_browser(dialog_->browser_info);
154 formatted( controller().
155 getBibkeyInfo(bibkeys[n]),
156 dialog_->browser_info->w-10 );
157 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
160 } else if (ob == dialog_->button_add) {
161 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
162 if (sel < 1 || sel > bibkeys.size())
163 return ButtonPolicy::SMI_NOOP;
165 // Add the selected browser_bib key to browser_cite
166 fl_addto_browser(dialog_->browser_cite,
167 bibkeys[sel-1].c_str());
168 citekeys.push_back(bibkeys[sel-1]);
170 int const n = int(citekeys.size());
171 fl_select_browser_line(dialog_->browser_cite, n);
175 activate = ButtonPolicy::SMI_VALID;
177 } else if (ob == dialog_->button_del) {
178 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
179 if (sel < 1 || sel > citekeys.size())
180 return ButtonPolicy::SMI_NOOP;
182 // Remove the selected key from browser_cite
183 fl_delete_browser_line(dialog_->browser_cite, sel) ;
184 citekeys.erase(citekeys.begin() + sel-1);
188 activate = ButtonPolicy::SMI_VALID;
190 } else if (ob == dialog_->button_up) {
191 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
192 if (sel < 2 || sel > citekeys.size())
193 return ButtonPolicy::SMI_NOOP;
195 // Move the selected key up one line
196 vector<string>::iterator it = citekeys.begin() + sel-1;
197 string const tmp = *it;
199 fl_delete_browser_line(dialog_->browser_cite, sel);
202 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
203 fl_select_browser_line(dialog_->browser_cite, sel-1);
204 citekeys.insert(it-1, tmp);
206 activate = ButtonPolicy::SMI_VALID;
208 } else if (ob == dialog_->button_down) {
209 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
210 if (sel < 1 || sel > citekeys.size()-1)
211 return ButtonPolicy::SMI_NOOP;
213 // Move the selected key down one line
214 vector<string>::iterator it = citekeys.begin() + sel-1;
215 string const tmp = *it;
217 fl_delete_browser_line(dialog_->browser_cite, sel);
220 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
221 fl_select_browser_line(dialog_->browser_cite, sel+1);
222 citekeys.insert(it+1, tmp);
224 activate = ButtonPolicy::SMI_VALID;
226 } else if (ob == dialog_->button_search_type) {
227 if (fl_get_button(dialog_->button_search_type))
228 fl_set_object_label(dialog_->button_search_type,
231 fl_set_object_label(dialog_->button_search_type,
233 return ButtonPolicy::SMI_NOOP;
235 } else if (ob == dialog_->button_previous ||
236 ob == dialog_->button_next) {
238 string const str = fl_get_input(dialog_->input_search);
240 ControlCitation::Direction const dir =
241 (ob == dialog_->button_previous) ?
242 ControlCitation::BACKWARD : ControlCitation::FORWARD;
244 ControlCitation::Search const type =
245 fl_get_button(dialog_->button_search_type) ?
246 ControlCitation::REGEX : ControlCitation::SIMPLE;
248 vector<string>::const_iterator start = bibkeys.begin();
249 int const sel = fl_get_browser(dialog_->browser_bib);
250 if (sel >= 1 && sel <= int(bibkeys.size()))
253 // Find the NEXT instance...
254 if (dir == ControlCitation::FORWARD)
259 vector<string>::const_iterator const cit =
260 searchKeys(controller(), bibkeys, str, start,
263 if (cit == bibkeys.end())
264 return ButtonPolicy::SMI_NOOP;
266 int const found = int(cit - bibkeys.begin()) + 1;
268 return ButtonPolicy::SMI_NOOP;
270 // Update the display
271 int const top = max(found-5, 1);
272 fl_set_browser_topline(dialog_->browser_bib, top);
273 fl_select_browser_line(dialog_->browser_bib, found);
274 input(dialog_->browser_bib, 0);
276 } else if (ob == dialog_->choice_style ||
277 ob == dialog_->input_before ||
278 ob == dialog_->input_after) {
279 activate = ButtonPolicy::SMI_VALID;
286 void FormCitation::update()
288 // Make the list of all available bibliography keys
289 bibkeys = controller().getBibkeys();
290 updateBrowser(dialog_->browser_bib, bibkeys);
292 // Ditto for the keys cited in this inset
293 citekeys = getVectorFromString(controller().params().getContents());
294 updateBrowser(dialog_->browser_cite, citekeys);
296 // No keys have been selected yet, so...
297 fl_clear_browser(dialog_->browser_info);
301 int noKeys = int(max(bibkeys.size(), citekeys.size()));
303 // Place bounds, so that 4 <= noKeys <= 10
304 noKeys = max(4, min(10, noKeys));
306 // Re-size the form to accommodate the new browser size
307 int const size = 20 * noKeys;
308 bool const bibPresent = (bibkeys.size() > 0);
309 setSize(size, bibPresent);
311 fl_set_input(dialog_->input_after,
312 controller().params().getOptions().c_str());
316 void FormCitation::updateBrowser(FL_OBJECT * browser,
317 vector<string> const & keys) const
319 fl_clear_browser(browser);
321 for (vector<string>::const_iterator it = keys.begin();
322 it < keys.end(); ++it) {
323 string key = frontStrip(strip(*it));
324 fl_add_browser_line(browser, key.c_str());
329 void FormCitation::setBibButtons(State status) const
331 setEnabled(dialog_->button_add, (status == ON));
335 void FormCitation::setCiteButtons(State status) const
337 int const sel = fl_get_browser(dialog_->browser_cite);
338 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
339 bool const activate = (status == ON);
340 bool const activate_up = (activate && sel != 1);
341 bool const activate_down = (activate && sel != maxline);
343 setEnabled(dialog_->button_del, activate);
344 setEnabled(dialog_->button_up, activate_up);
345 setEnabled(dialog_->button_down, activate_down);
349 void FormCitation::setSize(int hbrsr, bool bibPresent) const
351 bool const natbib = false; // will eventually be input
352 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
354 // dh1, dh2, dh3 are the vertical separation between elements.
355 // These can be specified because the browser height is fixed
356 // so they are not changed by dynamic resizing
357 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
358 // bottom of cite/bib brsrs to top of info;
359 // bottom of info to top search frame;
360 // bottom of search frame to top next elemnt;
361 // bottom of style to top input_before;
362 // bottom of text to top ok/cancel buttons.
363 static int const dh2 = 10; // bottom of input_before to top input_after;
364 // bottom of ok/cancel buttons to bottom form
365 static int const dh3 = 5; // spacing between add/delete/... buttons.
367 int const wbrsr = dialog_->browser_cite->w;
368 static int const hinfo = dialog_->browser_info->h;
369 static int const hframe = dialog_->frame_search->h;
370 static int const hstyle = dialog_->choice_style->h;
371 static int const htext = dialog_->input_after->h;
372 static int const hok = dialog_->button_ok->h;
374 int hform = dh1 + hbrsr + dh1 + hframe + dh1;
375 if (bibPresent) hform += hinfo + dh1;
376 if (natbib) hform += hstyle + dh1 + htext + dh2;
377 hform += htext + dh1 + hok + dh2;
379 if (hform != minh_) {
381 fl_set_form_size(dialog_->form, minw_, minh_);
387 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
389 x = dialog_->browser_cite->x;
391 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
392 x = dialog_->browser_bib->x;
393 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
395 x = dialog_->button_add->x;
396 fl_set_object_position(dialog_->button_add, x, y);
397 y += dh3 + dialog_->button_add->h;
398 fl_set_object_position(dialog_->button_del, x, y);
399 y += dh3 + dialog_->button_del->h;
400 fl_set_object_position(dialog_->button_up, x, y);
401 y += dh3 + dialog_->button_up->h;
402 fl_set_object_position(dialog_->button_down, x, y);
404 y = dh1 + hbrsr + dh1; // in position for next element
407 x = dialog_->browser_info->x;
408 fl_set_object_position(dialog_->browser_info, x, y);
409 fl_show_object(dialog_->browser_info);
412 fl_hide_object(dialog_->browser_info);
414 x = dialog_->frame_search->x;
415 // ??? The frame height seems to be reduced. Use geometry to enforce it.
416 fl_set_object_geometry(dialog_->frame_search, x, y,
417 dialog_->frame_search->w, hframe);
418 //fl_set_object_position(dialog_->frame_search, x, y);
420 x = dialog_->input_search->x;
422 fl_set_object_position(dialog_->input_search, x, y);
424 x = dialog_->button_previous->x;
425 y += dialog_->input_search->h + 5;
426 fl_set_object_position(dialog_->button_previous, x, y);
428 x = dialog_->button_next->x;
429 y += dialog_->button_previous->h + 5;
430 fl_set_object_position(dialog_->button_next, x, y);
432 x = dialog_->button_search_type->x;
433 y = dialog_->button_previous->y;
434 fl_set_object_position(dialog_->button_search_type, x, y);
436 y = dialog_->frame_search->y + hframe + dh1;
439 x = dialog_->choice_style->x;
440 fl_set_object_position(dialog_->choice_style, x, y);
441 fl_show_object(dialog_->choice_style);
442 x = dialog_->input_before->x;
444 fl_set_object_position(dialog_->input_before, x, y);
445 fl_show_object(dialog_->input_before);
448 fl_hide_object(dialog_->choice_style);
449 fl_hide_object(dialog_->input_before);
452 x = dialog_->input_after->x;
453 fl_set_object_position(dialog_->input_after, x, y);
456 x = dialog_->button_restore->x;
457 fl_set_object_position(dialog_->button_restore, x, y);
458 x = dialog_->button_ok->x;
459 fl_set_object_position(dialog_->button_ok, x, y);
460 x = dialog_->button_apply->x;
461 fl_set_object_position(dialog_->button_apply, x, y);
462 x = dialog_->button_cancel->x;
463 fl_set_object_position(dialog_->button_cancel, x, y);