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 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
87 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
89 // Manage the ok, apply, restore and cancel/close buttons
90 bc_.setOK(dialog_->button_ok);
91 bc_.setApply(dialog_->button_apply);
92 bc_.setCancel(dialog_->button_cancel);
93 bc_.setUndoAll(dialog_->button_restore);
96 bc_.addReadOnly(dialog_->button_add);
97 bc_.addReadOnly(dialog_->button_del);
98 bc_.addReadOnly(dialog_->button_up);
99 bc_.addReadOnly(dialog_->button_down);
100 bc_.addReadOnly(dialog_->input_before);
101 bc_.addReadOnly(dialog_->input_after);
103 bc_.addDontTriggerChange(dialog_->browser_cite);
104 bc_.addDontTriggerChange(dialog_->browser_bib);
108 void FormCitation::update()
110 bc_.readOnly(lv_->buffer()->isReadonly());
115 vector<pair<string,string> > blist =
116 lv_->buffer()->getBibkeyList();
117 sort(blist.begin(), blist.end());
119 for (unsigned int i = 0; i < blist.size(); ++i) {
120 bibkeys.push_back(blist[i].first);
121 bibkeysInfo.push_back(blist[i].second);
124 updateBrowser(dialog_->browser_bib, bibkeys);
126 // Ditto for the keys cited in this inset
128 string tmp, keys(params.getContents());
129 keys = frontStrip(split(keys, tmp, ','));
130 while (!tmp.empty()) {
131 citekeys.push_back(tmp);
132 keys = frontStrip(split(keys, tmp, ','));
134 updateBrowser(dialog_->browser_cite, citekeys);
136 // No keys have been selected yet, so...
137 fl_clear_browser(dialog_->browser_info);
141 int noKeys = static_cast<int>(max(bibkeys.size(), citekeys.size()));
143 // Place bounds, so that 4 <= noKeys <= 10
144 noKeys = max(4, min(10, noKeys));
146 // Re-size the form to accommodate the new browser size
147 int size = 20 * noKeys;
148 bool bibPresent = (bibkeys.size() > 0);
149 setSize(size, bibPresent);
151 fl_set_input(dialog_->input_after, params.getOptions().c_str());
155 void FormCitation::updateBrowser(FL_OBJECT * browser,
156 vector<string> const & keys) const
158 fl_clear_browser(browser);
160 for (unsigned int i = 0; i < keys.size(); ++i)
161 fl_add_browser_line(browser, keys[i].c_str());
165 void FormCitation::setBibButtons(State status) const
169 fl_activate_object(dialog_->button_add);
170 fl_set_object_lcol(dialog_->button_add, FL_BLACK);
174 fl_deactivate_object(dialog_->button_add);
175 fl_set_object_lcol(dialog_->button_add, FL_INACTIVE);
184 void FormCitation::setCiteButtons(State status) const
189 fl_activate_object(dialog_->button_del);
190 fl_set_object_lcol(dialog_->button_del, FL_BLACK);
192 int sel = fl_get_browser(dialog_->browser_cite);
195 fl_activate_object(dialog_->button_up);
196 fl_set_object_lcol(dialog_->button_up, FL_BLACK);
198 fl_deactivate_object(dialog_->button_up);
199 fl_set_object_lcol(dialog_->button_up, FL_INACTIVE);
202 if (sel != fl_get_browser_maxline(dialog_->browser_cite)) {
203 fl_activate_object(dialog_->button_down);
204 fl_set_object_lcol(dialog_->button_down, FL_BLACK);
206 fl_deactivate_object(dialog_->button_down);
207 fl_set_object_lcol(dialog_->button_down, FL_INACTIVE);
214 fl_deactivate_object(dialog_->button_del);
215 fl_set_object_lcol(dialog_->button_del, FL_INACTIVE);
217 fl_deactivate_object(dialog_->button_up);
218 fl_set_object_lcol(dialog_->button_up, FL_INACTIVE);
220 fl_deactivate_object(dialog_->button_down);
221 fl_set_object_lcol(dialog_->button_down, FL_INACTIVE);
229 void FormCitation::setSize(int hbrsr, bool bibPresent) const
231 bool const natbib = false; // will eventually be input
232 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
234 // dh1, dh2, dh3 are the vertical separation between elements.
235 // These can be specified because the browser height is fixed
236 // so they are not changed by dynamic resizing
237 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
238 // bottom of cite/bib brsrs to top of info;
239 // bottom of info to top next element;
240 // bottom of style to top input_before;
241 // bottom of text to top ok/cancel buttons.
242 static int const dh2 = 10; // bottom of input_before to top input_after;
243 // bottom of ok/cancel buttons to bottom form
244 static int const dh3 = 5; // spacing between add/delete/... buttons.
246 int const wbrsr = dialog_->browser_cite->w;
247 static int const hinfo = dialog_->browser_info->h;
248 static int const hstyle = dialog_->choice_style->h;
249 static int const htext = dialog_->input_after->h;
250 static int const hok = dialog_->button_ok->h;
252 int hform = dh1 + hbrsr + dh1;
253 if (bibPresent) hform += hinfo + dh1;
254 if (natbib) hform += hstyle + dh1 + htext + dh2;
255 hform += htext + dh1 + hok + dh2;
257 if (hform != minh_) {
259 fl_set_form_size(dialog_->form, minw_, minh_);
265 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
267 x = dialog_->browser_cite->x;
269 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
270 x = dialog_->browser_bib->x;
271 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
273 x = dialog_->button_add->x;
274 fl_set_object_position(dialog_->button_add, x, y);
275 y += dh3 + dialog_->button_add->h;
276 fl_set_object_position(dialog_->button_del, x, y);
277 y += dh3 + dialog_->button_del->h;
278 fl_set_object_position(dialog_->button_up, x, y);
279 y += dh3 + dialog_->button_up->h;
280 fl_set_object_position(dialog_->button_down, x, y);
282 y = dh1 + hbrsr + dh1; // in position for next element
285 x = dialog_->browser_info->x;
286 fl_set_object_position(dialog_->browser_info, x, y);
287 fl_show_object(dialog_->browser_info);
290 fl_hide_object(dialog_->browser_info);
293 x = dialog_->choice_style->x;
294 fl_set_object_position(dialog_->choice_style, x, y);
295 fl_show_object(dialog_->choice_style);
296 x = dialog_->input_before->x;
298 fl_set_object_position(dialog_->input_before, x, y);
299 fl_show_object(dialog_->input_before);
302 fl_hide_object(dialog_->choice_style);
303 fl_hide_object(dialog_->input_before);
306 x = dialog_->input_after->x;
307 fl_set_object_position(dialog_->input_after, x, y);
310 x = dialog_->button_restore->x;
311 fl_set_object_position(dialog_->button_restore, x, y);
312 x = dialog_->button_ok->x;
313 fl_set_object_position(dialog_->button_ok, x, y);
314 x = dialog_->button_apply->x;
315 fl_set_object_position(dialog_->button_apply, x, y);
316 x = dialog_->button_cancel->x;
317 fl_set_object_position(dialog_->button_cancel, x, y);
321 bool FormCitation::input(FL_OBJECT * ob, long)
323 bool activate = false;
325 if (ob == dialog_->browser_bib) {
326 fl_deselect_browser(dialog_->browser_cite);
328 unsigned int sel = fl_get_browser(dialog_->browser_bib);
329 if (sel < 1 || sel > bibkeys.size()) return false;
331 // Put into browser_info the additional info associated with
332 // the selected browser_bib key
333 fl_clear_browser(dialog_->browser_info);
334 fl_add_browser_line(dialog_->browser_info,
335 bibkeysInfo[sel - 1].c_str());
337 // Highlight the selected browser_bib key in browser_cite if present
338 vector<string>::iterator it =
339 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
341 if (it != citekeys.end()) {
342 int n = static_cast<int>(it - citekeys.begin());
343 fl_select_browser_line(dialog_->browser_cite, n+1);
344 fl_set_browser_topline(dialog_->browser_cite, n+1);
347 if (!lv_->buffer()->isReadonly()) {
348 if (it != citekeys.end()) {
357 } else if (ob == dialog_->browser_cite) {
358 unsigned int sel = fl_get_browser(dialog_->browser_cite);
359 if (sel < 1 || sel > citekeys.size()) return false;
361 if (!lv_->buffer()->isReadonly()) {
366 // Highlight the selected browser_cite key in browser_bib
367 vector<string>::iterator it =
368 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
370 if (it != bibkeys.end()) {
371 int n = static_cast<int>(it - bibkeys.begin());
372 fl_select_browser_line(dialog_->browser_bib, n+1);
373 fl_set_browser_topline(dialog_->browser_bib, n+1);
375 // Put into browser_info the additional info associated with
376 // the selected browser_cite key
377 fl_clear_browser(dialog_->browser_info);
378 fl_add_browser_line(dialog_->browser_info,
379 bibkeysInfo[n].c_str());
382 } else if (ob == dialog_->button_add) {
383 unsigned int sel = fl_get_browser(dialog_->browser_bib);
384 if (sel < 1 || sel > bibkeys.size()) return false;
386 // Add the selected browser_bib key to browser_cite
387 fl_addto_browser(dialog_->browser_cite,
388 bibkeys[sel-1].c_str());
389 citekeys.push_back(bibkeys[sel-1]);
391 int n = static_cast<int>(citekeys.size());
392 fl_select_browser_line(dialog_->browser_cite, n);
398 } else if (ob == dialog_->button_del) {
399 unsigned int sel = fl_get_browser(dialog_->browser_cite);
400 if (sel < 1 || sel > citekeys.size()) return false;
402 // Remove the selected key from browser_cite
403 fl_delete_browser_line(dialog_->browser_cite, sel) ;
404 citekeys.erase(citekeys.begin() + sel-1);
410 } else if (ob == dialog_->button_up) {
411 unsigned int sel = fl_get_browser(dialog_->browser_cite);
412 if (sel < 2 || sel > citekeys.size()) return false;
414 // Move the selected key up one line
415 vector<string>::iterator it = citekeys.begin() + sel-1;
418 fl_delete_browser_line(dialog_->browser_cite, sel);
421 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
422 fl_select_browser_line(dialog_->browser_cite, sel-1);
423 citekeys.insert(it-1, tmp);
427 } else if (ob == dialog_->button_down) {
428 unsigned int sel = fl_get_browser(dialog_->browser_cite);
429 if (sel < 1 || sel > citekeys.size()-1) return false;
431 // Move the selected key down one line
432 vector<string>::iterator it = citekeys.begin() + sel-1;
435 fl_delete_browser_line(dialog_->browser_cite, sel);
438 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
439 fl_select_browser_line(dialog_->browser_cite, sel+1);
440 citekeys.insert(it+1, tmp);
443 } else if (ob == dialog_->choice_style ||
444 ob == dialog_->input_before ||
445 ob == dialog_->input_after) {
452 void FormCitation::apply()
454 if (lv_->buffer()->isReadonly()) return;
457 for (unsigned int i = 0; i < citekeys.size(); ++i) {
458 if (i > 0) contents += ", ";
459 contents += citekeys[i];
462 params.setContents(contents);
463 params.setOptions(fl_get_input(dialog_->input_after));
466 // Only update if contents have changed
467 if (params != inset_->params()) {
468 inset_->setParams(params);
469 lv_->view()->updateInset(inset_, true);
472 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
473 params.getAsString());