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"
27 #include "xforms_helpers.h"
37 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
38 : FormCommand(lv, d, _("Citation"), new NoRepeatedApplyReadOnlyPolicy),
41 // let the dialog be shown
42 // These are permanent connections so we won't bother
43 // storing a copy because we won't be disconnecting.
44 d->showCitation.connect(slot(this, &FormCitation::showInset));
45 d->createCitation.connect(slot(this, &FormCitation::createInset));
49 FormCitation::~FormCitation()
55 FL_FORM * FormCitation::form() const
57 if (dialog_) return dialog_->form;
62 void FormCitation::connect()
64 //fl_set_form_maxsize(dialog_->form, 3*minw_, minh_);
65 FormCommand::connect();
69 void FormCitation::disconnect()
75 FormCommand::disconnect();
79 void FormCitation::build()
81 dialog_ = build_citation();
83 // Workaround dumb xforms sizing bug
87 fl_set_input_return(dialog_->input_after, FL_RETURN_CHANGED);
88 fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
90 // Manage the ok, apply, restore and cancel/close buttons
91 bc_.setOK(dialog_->button_ok);
92 bc_.setApply(dialog_->button_apply);
93 bc_.setCancel(dialog_->button_cancel);
94 bc_.setUndoAll(dialog_->button_restore);
97 bc_.addReadOnly(dialog_->button_add);
98 bc_.addReadOnly(dialog_->button_del);
99 bc_.addReadOnly(dialog_->button_up);
100 bc_.addReadOnly(dialog_->button_down);
101 bc_.addReadOnly(dialog_->input_before);
102 bc_.addReadOnly(dialog_->input_after);
104 bc_.addDontTriggerChange(dialog_->browser_cite);
105 bc_.addDontTriggerChange(dialog_->browser_bib);
109 void FormCitation::update()
111 bc_.readOnly(lv_->buffer()->isReadonly());
116 vector<pair<string,string> > blist =
117 lv_->buffer()->getBibkeyList();
118 sort(blist.begin(), blist.end());
120 for (unsigned int i = 0; i < blist.size(); ++i) {
121 bibkeys.push_back(blist[i].first);
122 bibkeysInfo.push_back(blist[i].second);
125 updateBrowser(dialog_->browser_bib, bibkeys);
127 // Ditto for the keys cited in this inset
129 string tmp, keys(params.getContents());
130 keys = frontStrip(split(keys, tmp, ','));
131 while (!tmp.empty()) {
132 citekeys.push_back(tmp);
133 keys = frontStrip(split(keys, tmp, ','));
135 updateBrowser(dialog_->browser_cite, citekeys);
137 // No keys have been selected yet, so...
138 fl_clear_browser(dialog_->browser_info);
142 int noKeys = static_cast<int>(max(bibkeys.size(), citekeys.size()));
144 // Place bounds, so that 4 <= noKeys <= 10
145 noKeys = max(4, min(10, noKeys));
147 // Re-size the form to accommodate the new browser size
148 int size = 20 * noKeys;
149 bool bibPresent = (bibkeys.size() > 0);
150 setSize(size, bibPresent);
152 fl_set_input(dialog_->input_after, params.getOptions().c_str());
156 void FormCitation::updateBrowser(FL_OBJECT * browser,
157 vector<string> const & keys) const
159 fl_clear_browser(browser);
161 for (unsigned int i = 0; i < keys.size(); ++i)
162 fl_add_browser_line(browser, keys[i].c_str());
166 void FormCitation::setBibButtons(State status) const
168 setEnabled(dialog_->button_add, (status == ON));
172 void FormCitation::setCiteButtons(State status) const
174 int const sel = fl_get_browser(dialog_->browser_cite);
175 int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
177 bool const activate = (status == ON);
178 bool const activate_up = (activate && sel != 1);
179 bool const activate_down = (activate && sel != maxline);
181 setEnabled(dialog_->button_del, activate);
182 setEnabled(dialog_->button_up, activate_up);
183 setEnabled(dialog_->button_down, activate_down);
187 void FormCitation::setSize(int hbrsr, bool bibPresent) const
189 bool const natbib = false; // will eventually be input
190 hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
192 // dh1, dh2, dh3 are the vertical separation between elements.
193 // These can be specified because the browser height is fixed
194 // so they are not changed by dynamic resizing
195 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
196 // bottom of cite/bib brsrs to top of info;
197 // bottom of info to top next element;
198 // bottom of style to top input_before;
199 // bottom of text to top ok/cancel buttons.
200 static int const dh2 = 10; // bottom of input_before to top input_after;
201 // bottom of ok/cancel buttons to bottom form
202 static int const dh3 = 5; // spacing between add/delete/... buttons.
204 int const wbrsr = dialog_->browser_cite->w;
205 static int const hinfo = dialog_->browser_info->h;
206 static int const hstyle = dialog_->choice_style->h;
207 static int const htext = dialog_->input_after->h;
208 static int const hok = dialog_->button_ok->h;
210 int hform = dh1 + hbrsr + dh1;
211 if (bibPresent) hform += hinfo + dh1;
212 if (natbib) hform += hstyle + dh1 + htext + dh2;
213 hform += htext + dh1 + hok + dh2;
215 if (hform != minh_) {
217 fl_set_form_size(dialog_->form, minw_, minh_);
223 fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
225 x = dialog_->browser_cite->x;
227 fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
228 x = dialog_->browser_bib->x;
229 fl_set_object_geometry(dialog_->browser_bib, x, y, wbrsr, hbrsr);
231 x = dialog_->button_add->x;
232 fl_set_object_position(dialog_->button_add, x, y);
233 y += dh3 + dialog_->button_add->h;
234 fl_set_object_position(dialog_->button_del, x, y);
235 y += dh3 + dialog_->button_del->h;
236 fl_set_object_position(dialog_->button_up, x, y);
237 y += dh3 + dialog_->button_up->h;
238 fl_set_object_position(dialog_->button_down, x, y);
240 y = dh1 + hbrsr + dh1; // in position for next element
243 x = dialog_->browser_info->x;
244 fl_set_object_position(dialog_->browser_info, x, y);
245 fl_show_object(dialog_->browser_info);
248 fl_hide_object(dialog_->browser_info);
251 x = dialog_->choice_style->x;
252 fl_set_object_position(dialog_->choice_style, x, y);
253 fl_show_object(dialog_->choice_style);
254 x = dialog_->input_before->x;
256 fl_set_object_position(dialog_->input_before, x, y);
257 fl_show_object(dialog_->input_before);
260 fl_hide_object(dialog_->choice_style);
261 fl_hide_object(dialog_->input_before);
264 x = dialog_->input_after->x;
265 fl_set_object_position(dialog_->input_after, x, y);
268 x = dialog_->button_restore->x;
269 fl_set_object_position(dialog_->button_restore, x, y);
270 x = dialog_->button_ok->x;
271 fl_set_object_position(dialog_->button_ok, x, y);
272 x = dialog_->button_apply->x;
273 fl_set_object_position(dialog_->button_apply, x, y);
274 x = dialog_->button_cancel->x;
275 fl_set_object_position(dialog_->button_cancel, x, y);
279 bool FormCitation::input(FL_OBJECT * ob, long)
281 bool activate = false;
283 if (ob == dialog_->browser_bib) {
284 fl_deselect_browser(dialog_->browser_cite);
286 unsigned int sel = fl_get_browser(dialog_->browser_bib);
287 if (sel < 1 || sel > bibkeys.size()) return false;
289 // Put into browser_info the additional info associated with
290 // the selected browser_bib key
291 fl_clear_browser(dialog_->browser_info);
292 fl_add_browser_line(dialog_->browser_info,
293 bibkeysInfo[sel - 1].c_str());
295 // Highlight the selected browser_bib key in browser_cite if present
296 vector<string>::iterator it =
297 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
299 if (it != citekeys.end()) {
300 int n = static_cast<int>(it - citekeys.begin());
301 fl_select_browser_line(dialog_->browser_cite, n+1);
302 fl_set_browser_topline(dialog_->browser_cite, n+1);
305 if (!lv_->buffer()->isReadonly()) {
306 if (it != citekeys.end()) {
315 } else if (ob == dialog_->browser_cite) {
316 unsigned int sel = fl_get_browser(dialog_->browser_cite);
317 if (sel < 1 || sel > citekeys.size()) return false;
319 if (!lv_->buffer()->isReadonly()) {
324 // Highlight the selected browser_cite key in browser_bib
325 vector<string>::iterator it =
326 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
328 if (it != bibkeys.end()) {
329 int n = static_cast<int>(it - bibkeys.begin());
330 fl_select_browser_line(dialog_->browser_bib, n+1);
331 fl_set_browser_topline(dialog_->browser_bib, n+1);
333 // Put into browser_info the additional info associated with
334 // the selected browser_cite key
335 fl_clear_browser(dialog_->browser_info);
336 fl_add_browser_line(dialog_->browser_info,
337 bibkeysInfo[n].c_str());
340 } else if (ob == dialog_->button_add) {
341 unsigned int sel = fl_get_browser(dialog_->browser_bib);
342 if (sel < 1 || sel > bibkeys.size()) return false;
344 // Add the selected browser_bib key to browser_cite
345 fl_addto_browser(dialog_->browser_cite,
346 bibkeys[sel-1].c_str());
347 citekeys.push_back(bibkeys[sel-1]);
349 int n = static_cast<int>(citekeys.size());
350 fl_select_browser_line(dialog_->browser_cite, n);
356 } else if (ob == dialog_->button_del) {
357 unsigned int sel = fl_get_browser(dialog_->browser_cite);
358 if (sel < 1 || sel > citekeys.size()) return false;
360 // Remove the selected key from browser_cite
361 fl_delete_browser_line(dialog_->browser_cite, sel) ;
362 citekeys.erase(citekeys.begin() + sel-1);
368 } else if (ob == dialog_->button_up) {
369 unsigned int sel = fl_get_browser(dialog_->browser_cite);
370 if (sel < 2 || sel > citekeys.size()) return false;
372 // Move the selected key up one line
373 vector<string>::iterator it = citekeys.begin() + sel-1;
376 fl_delete_browser_line(dialog_->browser_cite, sel);
379 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
380 fl_select_browser_line(dialog_->browser_cite, sel-1);
381 citekeys.insert(it-1, tmp);
385 } else if (ob == dialog_->button_down) {
386 unsigned int sel = fl_get_browser(dialog_->browser_cite);
387 if (sel < 1 || sel > citekeys.size()-1) return false;
389 // Move the selected key down one line
390 vector<string>::iterator it = citekeys.begin() + sel-1;
393 fl_delete_browser_line(dialog_->browser_cite, sel);
396 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
397 fl_select_browser_line(dialog_->browser_cite, sel+1);
398 citekeys.insert(it+1, tmp);
401 } else if (ob == dialog_->choice_style ||
402 ob == dialog_->input_before ||
403 ob == dialog_->input_after) {
410 void FormCitation::apply()
412 if (lv_->buffer()->isReadonly()) return;
415 for (unsigned int i = 0; i < citekeys.size(); ++i) {
416 if (i > 0) contents += ", ";
417 contents += citekeys[i];
420 params.setContents(contents);
421 params.setOptions(fl_get_input(dialog_->input_after));
424 // Only update if contents have changed
425 if (params != inset_->params()) {
426 inset_->setParams(params);
427 lv_->view()->updateInset(inset_, true);
430 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
431 params.getAsString());