2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
16 #pragma implementation
21 #include "FormCitation.h"
24 #include "form_citation.h"
26 #include "support/filetools.h"
36 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
37 : FormCommand(lv, d, _("Citation"), new NoRepeatedApplyReadOnlyPolicy),
40 // let the dialog be shown
41 // These are permanent connections so we won't bother
42 // storing a copy because we won't be disconnecting.
43 d->showCitation.connect(slot(this, &FormCitation::showInset));
44 d->createCitation.connect(slot(this, &FormCitation::createInset));
48 FormCitation::~FormCitation()
54 FL_FORM * FormCitation::form() const
56 if (dialog_) return dialog_->form;
61 void FormCitation::connect()
63 //fl_set_form_maxsize(dialog_->form, 3*minw_, minh_);
64 FormCommand::connect();
68 void FormCitation::disconnect()
74 FormCommand::disconnect();
78 void FormCitation::build()
80 dialog_ = build_citation();
82 // Workaround dumb xforms sizing bug
86 // Manage the ok, apply, restore and cancel/close buttons
87 bc_.setOK(dialog_->button_ok);
88 bc_.setApply(dialog_->button_apply);
89 bc_.setCancel(dialog_->button_cancel);
90 bc_.setUndoAll(dialog_->button_restore);
93 bc_.addReadOnly(dialog_->button_add);
94 bc_.addReadOnly(dialog_->button_del);
95 bc_.addReadOnly(dialog_->button_up);
96 bc_.addReadOnly(dialog_->button_down);
97 bc_.addReadOnly(dialog_->input_before);
98 bc_.addReadOnly(dialog_->input_after);
100 bc_.addDontTriggerChange(dialog_->browser_cite);
101 bc_.addDontTriggerChange(dialog_->browser_bib);
105 void FormCitation::update()
107 bc_.readOnly(lv_->buffer()->isReadonly());
112 vector<pair<string,string> > blist =
113 lv_->buffer()->getBibkeyList();
114 sort(blist.begin(), blist.end());
116 for (unsigned int i = 0; i < blist.size(); ++i) {
117 bibkeys.push_back(blist[i].first);
118 bibkeysInfo.push_back(blist[i].second);
121 updateBrowser(dialog_->browser_bib, bibkeys);
123 // Ditto for the keys cited in this inset
125 string tmp, keys(params.getContents());
126 keys = frontStrip(split(keys, tmp, ','));
127 while (!tmp.empty()) {
128 citekeys.push_back(tmp);
129 keys = frontStrip(split(keys, tmp, ','));
131 updateBrowser(dialog_->browser_cite, citekeys);
133 // No keys have been selected yet, so...
134 fl_clear_browser(dialog_->browser_info);
138 int noKeys = static_cast<int>(max(bibkeys.size(), citekeys.size()));
140 // Place bounds, so that 4 <= noKeys <= 10
141 noKeys = max(4, min(10, noKeys));
143 // Re-size the form to accommodate the new browser size
144 int size = 20 * noKeys;
145 bool bibPresent = (bibkeys.size() > 0);
146 setSize(size, bibPresent);
148 fl_set_input(dialog_->input_after, params.getOptions().c_str());
152 void FormCitation::updateBrowser(FL_OBJECT * browser,
153 vector<string> const & keys) const
155 fl_clear_browser(browser);
157 for (unsigned int i = 0; i < keys.size(); ++i)
158 fl_add_browser_line(browser, keys[i].c_str());
162 void FormCitation::setBibButtons(State status) const
166 fl_activate_object(dialog_->button_add);
167 fl_set_object_lcol(dialog_->button_add, FL_BLACK);
171 fl_deactivate_object(dialog_->button_add);
172 fl_set_object_lcol(dialog_->button_add, FL_INACTIVE);
181 void FormCitation::setCiteButtons(State status) const
186 fl_activate_object(dialog_->button_del);
187 fl_set_object_lcol(dialog_->button_del, FL_BLACK);
189 int sel = fl_get_browser(dialog_->browser_cite);
192 fl_activate_object(dialog_->button_up);
193 fl_set_object_lcol(dialog_->button_up, FL_BLACK);
195 fl_deactivate_object(dialog_->button_up);
196 fl_set_object_lcol(dialog_->button_up, FL_INACTIVE);
199 if (sel != fl_get_browser_maxline(dialog_->browser_cite)) {
200 fl_activate_object(dialog_->button_down);
201 fl_set_object_lcol(dialog_->button_down, FL_BLACK);
203 fl_deactivate_object(dialog_->button_down);
204 fl_set_object_lcol(dialog_->button_down, FL_INACTIVE);
211 fl_deactivate_object(dialog_->button_del);
212 fl_set_object_lcol(dialog_->button_del, FL_INACTIVE);
214 fl_deactivate_object(dialog_->button_up);
215 fl_set_object_lcol(dialog_->button_up, FL_INACTIVE);
217 fl_deactivate_object(dialog_->button_down);
218 fl_set_object_lcol(dialog_->button_down, FL_INACTIVE);
226 void FormCitation::setSize(int hbrsr, bool bibPresent) const
228 bool const natbib = false; // will eventually be input
229 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
231 // dh1, dh2, dh3 are the vertical separation between elements.
232 // These can be specified because the browser height is fixed
233 // so they are not changed by dynamic resizing
234 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
235 // bottom of cite/bib brsrs to top of info;
236 // bottom of info to top next element;
237 // bottom of style to top input_before;
238 // bottom of text to top ok/cancel buttons.
239 static int const dh2 = 10; // bottom of input_before to top input_after;
240 // bottom of ok/cancel buttons to bottom form
241 static int const dh3 = 5; // spacing between add/delete/... buttons.
243 int const wbrsr = dialog_->browser_cite->w;
244 static int const hinfo = dialog_->browser_info->h;
245 static int const hstyle = dialog_->choice_style->h;
246 static int const htext = dialog_->input_after->h;
247 static int const hok = dialog_->button_ok->h;
249 int hform = dh1 + hbrsr + dh1;
250 if (bibPresent) hform += hinfo + dh1;
251 if (natbib) hform += hstyle + dh1 + htext + dh2;
252 hform += htext + dh1 + hok + dh2;
254 if (hform != minh_) {
256 fl_set_form_size(dialog_->form, minw_, minh_);
262 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
264 x = dialog_->browser_cite->x;
266 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
267 x = dialog_->browser_bib->x;
268 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
270 x = dialog_->button_add->x;
271 fl_set_object_position(dialog_->button_add, x, y);
272 y += dh3 + dialog_->button_add->h;
273 fl_set_object_position(dialog_->button_del, x, y);
274 y += dh3 + dialog_->button_del->h;
275 fl_set_object_position(dialog_->button_up, x, y);
276 y += dh3 + dialog_->button_up->h;
277 fl_set_object_position(dialog_->button_down, x, y);
279 y = dh1 + hbrsr + dh1; // in position for next element
282 x = dialog_->browser_info->x;
283 fl_set_object_position(dialog_->browser_info, x, y);
284 fl_show_object(dialog_->browser_info);
287 fl_hide_object(dialog_->browser_info);
290 x = dialog_->choice_style->x;
291 fl_set_object_position(dialog_->choice_style, x, y);
292 fl_show_object(dialog_->choice_style);
293 x = dialog_->input_before->x;
295 fl_set_object_position(dialog_->input_before, x, y);
296 fl_show_object(dialog_->input_before);
299 fl_hide_object(dialog_->choice_style);
300 fl_hide_object(dialog_->input_before);
303 x = dialog_->input_after->x;
304 fl_set_object_position(dialog_->input_after, x, y);
307 x = dialog_->button_restore->x;
308 fl_set_object_position(dialog_->button_restore, x, y);
309 x = dialog_->button_ok->x;
310 fl_set_object_position(dialog_->button_ok, x, y);
311 x = dialog_->button_apply->x;
312 fl_set_object_position(dialog_->button_apply, x, y);
313 x = dialog_->button_cancel->x;
314 fl_set_object_position(dialog_->button_cancel, x, y);
318 bool FormCitation::input(FL_OBJECT * ob, long)
320 bool activate = false;
322 if (ob == dialog_->browser_bib) {
323 fl_deselect_browser(dialog_->browser_cite);
325 unsigned int sel = fl_get_browser(dialog_->browser_bib);
326 if (sel < 1 || sel > bibkeys.size()) return false;
328 // Put into browser_info the additional info associated with
329 // the selected browser_bib key
330 fl_clear_browser(dialog_->browser_info);
331 fl_add_browser_line(dialog_->browser_info,
332 bibkeysInfo[sel - 1].c_str());
334 // Highlight the selected browser_bib key in browser_cite if present
335 vector<string>::iterator it =
336 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
338 if (it != citekeys.end()) {
339 int n = static_cast<int>(it - citekeys.begin());
340 fl_select_browser_line(dialog_->browser_cite, n+1);
341 fl_set_browser_topline(dialog_->browser_cite, n+1);
344 if (!lv_->buffer()->isReadonly()) {
345 if (it != citekeys.end()) {
354 } else if (ob == dialog_->browser_cite) {
355 unsigned int sel = fl_get_browser(dialog_->browser_cite);
356 if (sel < 1 || sel > citekeys.size()) return false;
358 if (!lv_->buffer()->isReadonly()) {
363 // Highlight the selected browser_cite key in browser_bib
364 vector<string>::iterator it =
365 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
367 if (it != bibkeys.end()) {
368 int n = static_cast<int>(it - bibkeys.begin());
369 fl_select_browser_line(dialog_->browser_bib, n+1);
370 fl_set_browser_topline(dialog_->browser_bib, n+1);
372 // Put into browser_info the additional info associated with
373 // the selected browser_cite key
374 fl_clear_browser(dialog_->browser_info);
375 fl_add_browser_line(dialog_->browser_info,
376 bibkeysInfo[n].c_str());
379 } else if (ob == dialog_->button_add) {
380 unsigned int sel = fl_get_browser(dialog_->browser_bib);
381 if (sel < 1 || sel > bibkeys.size()) return false;
383 // Add the selected browser_bib key to browser_cite
384 fl_addto_browser(dialog_->browser_cite,
385 bibkeys[sel-1].c_str());
386 citekeys.push_back(bibkeys[sel-1]);
388 int n = static_cast<int>(citekeys.size());
389 fl_select_browser_line(dialog_->browser_cite, n);
395 } else if (ob == dialog_->button_del) {
396 unsigned int sel = fl_get_browser(dialog_->browser_cite);
397 if (sel < 1 || sel > citekeys.size()) return false;
399 // Remove the selected key from browser_cite
400 fl_delete_browser_line(dialog_->browser_cite, sel) ;
401 citekeys.erase(citekeys.begin() + sel-1);
407 } else if (ob == dialog_->button_up) {
408 unsigned int sel = fl_get_browser(dialog_->browser_cite);
409 if (sel < 2 || sel > citekeys.size()) return false;
411 // Move the selected key up one line
412 vector<string>::iterator it = citekeys.begin() + sel-1;
415 fl_delete_browser_line(dialog_->browser_cite, sel);
418 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
419 fl_select_browser_line(dialog_->browser_cite, sel-1);
420 citekeys.insert(it-1, tmp);
424 } else if (ob == dialog_->button_down) {
425 unsigned int sel = fl_get_browser(dialog_->browser_cite);
426 if (sel < 1 || sel > citekeys.size()-1) return false;
428 // Move the selected key down one line
429 vector<string>::iterator it = citekeys.begin() + sel-1;
432 fl_delete_browser_line(dialog_->browser_cite, sel);
435 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
436 fl_select_browser_line(dialog_->browser_cite, sel+1);
437 citekeys.insert(it+1, tmp);
440 } else if (ob == dialog_->choice_style ||
441 ob == dialog_->input_before ||
442 ob == dialog_->input_after) {
449 void FormCitation::apply()
451 if (lv_->buffer()->isReadonly()) return;
454 for (unsigned int i = 0; i < citekeys.size(); ++i) {
455 if (i > 0) contents += ", ";
456 contents += citekeys[i];
459 params.setContents(contents);
460 params.setOptions(fl_get_input(dialog_->input_after));
463 // Only update if contents have changed
464 if (params != inset_->params()) {
465 inset_->setParams(params);
466 lv_->view()->updateInset(inset_, true);
469 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
470 params.getAsString());