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_->addBtn);
94 bc_.addReadOnly(dialog_->delBtn);
95 bc_.addReadOnly(dialog_->upBtn);
96 bc_.addReadOnly(dialog_->downBtn);
97 bc_.addReadOnly(dialog_->textBefore);
98 bc_.addReadOnly(dialog_->textAftr);
100 bc_.addDontTriggerChange(dialog_->citeBrsr);
101 bc_.addDontTriggerChange(dialog_->bibBrsr);
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);
123 string tmp, keys( params.getContents() );
124 keys = frontStrip( split(keys, tmp, ',') );
125 while (!tmp.empty()) {
126 citekeys.push_back( tmp );
127 keys = frontStrip( split(keys, tmp, ',') );
130 updateBrowser( dialog_->bibBrsr, bibkeys );
131 updateBrowser( dialog_->citeBrsr, citekeys );
132 fl_clear_browser( dialog_->infoBrsr );
134 // No keys have been selected yet, so...
135 setBibButtons( OFF );
136 setCiteButtons( OFF );
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_->textAftr, 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_->addBtn );
167 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
171 fl_deactivate_object( dialog_->addBtn );
172 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
181 void FormCitation::setCiteButtons( State status ) const
186 fl_activate_object( dialog_->delBtn );
187 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
189 int sel = fl_get_browser( dialog_->citeBrsr );
192 fl_activate_object( dialog_->upBtn );
193 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
195 fl_deactivate_object( dialog_->upBtn );
196 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
199 if (sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
200 fl_activate_object( dialog_->downBtn );
201 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
203 fl_deactivate_object( dialog_->downBtn );
204 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
211 fl_deactivate_object( dialog_->delBtn );
212 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
214 fl_deactivate_object( dialog_->upBtn );
215 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
217 fl_deactivate_object( dialog_->downBtn );
218 fl_set_object_lcol( dialog_->downBtn, 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 textBefore;
238 // bottom of text to top ok/cancel buttons.
239 static int const dh2 = 10; // bottom of textBefore to top textAftr;
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_->citeBrsr->w;
244 static int const hinfo = dialog_->infoBrsr->h;
245 static int const hstyle = dialog_->style->h;
246 static int const htext = dialog_->textAftr->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_->citeBrsr->x;
266 fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
267 x = dialog_->bibBrsr->x;
268 fl_set_object_geometry( dialog_->bibBrsr, x, y, wbrsr, hbrsr );
270 x = dialog_->addBtn->x;
271 fl_set_object_position( dialog_->addBtn, x, y );
272 y += dh3 + dialog_->addBtn->h;
273 fl_set_object_position( dialog_->delBtn, x, y );
274 y += dh3 + dialog_->delBtn->h;
275 fl_set_object_position( dialog_->upBtn, x, y );
276 y += dh3 + dialog_->upBtn->h;
277 fl_set_object_position( dialog_->downBtn, x, y );
279 y = dh1 + hbrsr + dh1; // in position for next element
282 x = dialog_->infoBrsr->x;
283 fl_set_object_position( dialog_->infoBrsr, x, y );
284 fl_show_object( dialog_->infoBrsr );
287 fl_hide_object( dialog_->infoBrsr );
290 x = dialog_->style->x;
291 fl_set_object_position( dialog_->style, x, y );
292 fl_show_object( dialog_->style );
293 x = dialog_->textBefore->x;
295 fl_set_object_position( dialog_->textBefore, x, y );
296 fl_show_object( dialog_->textBefore );
299 fl_hide_object( dialog_->style );
300 fl_hide_object( dialog_->textBefore );
303 x = dialog_->textAftr->x;
304 fl_set_object_position( dialog_->textAftr, 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 );
319 #warning convert this to use the buttoncontroller
321 bool FormCitation::input( FL_OBJECT *, long data )
323 bool activate = false;
324 State cb = static_cast<State>( data );
329 fl_deselect_browser( dialog_->citeBrsr );
331 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
332 if (sel < 1 || sel > bibkeys.size() ) break;
334 // Put into infoBrsr the additional info associated with
335 // the selected bibBrsr key
336 fl_clear_browser( dialog_->infoBrsr );
337 fl_add_browser_line( dialog_->infoBrsr,
338 bibkeysInfo[sel - 1].c_str() );
340 // Highlight the selected bibBrsr key in citeBrsr if present
341 vector<string>::iterator it =
342 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
344 if (it != citekeys.end()) {
345 int n = static_cast<int>( it - citekeys.begin() );
346 fl_select_browser_line( dialog_->citeBrsr, n+1 );
347 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
350 if (!lv_->buffer()->isReadonly()) {
351 if (it != citekeys.end()) {
352 setBibButtons( OFF );
353 setCiteButtons( ON );
356 setCiteButtons( OFF );
363 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
364 if (sel < 1 || sel > citekeys.size() ) break;
366 if (!lv_->buffer()->isReadonly()) {
367 setBibButtons( OFF );
368 setCiteButtons( ON );
371 // Highlight the selected citeBrsr key in bibBrsr
372 vector<string>::iterator it =
373 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
375 if (it != bibkeys.end()) {
376 int n = static_cast<int>( it - bibkeys.begin() );
377 fl_select_browser_line( dialog_->bibBrsr, n+1 );
378 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
380 // Put into infoBrsr the additional info associated with
381 // the selected citeBrsr key
382 fl_clear_browser( dialog_->infoBrsr );
383 fl_add_browser_line( dialog_->infoBrsr,
384 bibkeysInfo[n].c_str() );
390 if (lv_->buffer()->isReadonly() ) break;
392 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
393 if (sel < 1 || sel > bibkeys.size() ) break;
395 // Add the selected bibBrsr key to citeBrsr
396 fl_addto_browser( dialog_->citeBrsr,
397 bibkeys[sel-1].c_str() );
398 citekeys.push_back( bibkeys[sel-1] );
400 int n = static_cast<int>( citekeys.size() );
401 fl_select_browser_line( dialog_->citeBrsr, n );
403 setBibButtons( OFF );
404 setCiteButtons( ON );
410 if (lv_->buffer()->isReadonly() ) break;
412 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
413 if (sel < 1 || sel > citekeys.size() ) break;
415 // Remove the selected key from citeBrsr
416 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
417 citekeys.erase( citekeys.begin() + sel-1 );
420 setCiteButtons( OFF );
426 if (lv_->buffer()->isReadonly() ) break;
428 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
429 if (sel < 2 || sel > citekeys.size() ) break;
431 // Move the selected key up one line
432 vector<string>::iterator it = citekeys.begin() + sel-1;
435 fl_delete_browser_line( dialog_->citeBrsr, sel );
436 citekeys.erase( it );
438 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
439 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
440 citekeys.insert( it-1, tmp );
441 setCiteButtons( ON );
447 if (lv_->buffer()->isReadonly() ) break;
449 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
450 if (sel < 1 || sel > citekeys.size()-1 ) break;
452 // Move the selected key down one line
453 vector<string>::iterator it = citekeys.begin() + sel-1;
456 fl_delete_browser_line( dialog_->citeBrsr, sel );
457 citekeys.erase( it );
459 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
460 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
461 citekeys.insert( it+1, tmp );
462 setCiteButtons( ON );
473 void FormCitation::apply()
475 if (lv_->buffer()->isReadonly()) return;
478 for (unsigned int i = 0; i < citekeys.size(); ++i) {
479 if (i > 0) contents += ", ";
480 contents += citekeys[i];
483 params.setContents(contents);
484 params.setOptions(fl_get_input(dialog_->textAftr));
487 // Only update if contents have changed
488 if (params != inset_->params()) {
489 inset_->setParams(params);
490 lv_->view()->updateInset(inset_, true);
493 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
494 params.getAsString());