2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
16 #include FORMS_H_LOCATION
17 #include "xform_macros.h"
18 #include "FormCitation.h"
22 #include "insets/insetcite.h"
23 #include "form_citation.h"
25 #include "BufferView.h"
26 #include "support/filetools.h"
29 #pragma implementation
38 C_RETURNCB(FormCitation, WMHideCB)
39 C_GENERICCB(FormCitation, OKCB)
40 C_GENERICCB(FormCitation, CancelCB)
41 C_GENERICCB(FormCitation, InputCB)
43 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
44 : dialog_(0), lv_(lv), d_(d), h_(0), inset_(0), dialogIsOpen(false)
46 // let the dialog be shown
47 // These are permanent connections so we won't bother
48 // storing a copy because we won't be disconnecting.
49 d->showCitation.connect(slot(this, &FormCitation::showInset));
50 d->createCitation.connect(slot(this, &FormCitation::createInset));
54 FormCitation::~FormCitation()
60 void FormCitation::build()
62 dialog_ = build_citation();
66 void FormCitation::showInset( InsetCitation * inset )
68 if( dialogIsOpen || inset == 0 ) return;
71 ih_ = inset_->hide.connect(slot(this, &FormCitation::hide));
73 textAfter = inset->getOptions();
74 updateCitekeys(inset->getContents());
79 void FormCitation::createInset( string const & arg )
81 if( dialogIsOpen ) return;
84 if (contains(arg, "|")) {
85 keys = token(arg, '|', 0);
86 textAfter = token(arg, '|', 1);
97 void FormCitation::show()
101 fl_set_form_atclose(dialog_->form_citation,
102 C_FormCitationWMHideCB, 0);
105 update(); // make sure its up-to-date
108 if (dialog_->form_citation->visible) {
109 fl_raise_form(dialog_->form_citation);
111 fl_show_form(dialog_->form_citation,
112 FL_PLACE_MOUSE | FL_FREE_SIZE,
115 u_ = d_->updateBufferDependent.
116 connect(slot(this, &FormCitation::update));
117 h_ = d_->hideBufferDependent.
118 connect(slot(this, &FormCitation::hide));
123 void FormCitation::update()
128 vector<pair<string,string> > blist =
129 lv_->buffer()->getBibkeyList();
131 for( unsigned int i = 0; i < blist.size(); ++i ) {
132 bibkeys.push_back(blist[i].first);
133 bibkeysInfo.push_back(blist[i].second);
138 fl_freeze_form( dialog_->form_citation );
140 updateBrowser( dialog_->bibBrsr, bibkeys );
141 updateBrowser( dialog_->citeBrsr, citekeys );
142 fl_clear_browser( dialog_->infoBrsr );
144 // No keys have been selected yet, so...
145 setBibButtons( OFF );
146 setCiteButtons( OFF );
148 int noKeys = max( bibkeys.size(), citekeys.size() );
150 // Place bounds, so that 4 <= noKeys <= 15
151 noKeys = max( 4, min(15, noKeys) );
153 // Re-size the form to accommodate the new browser size
154 int size = 20 * noKeys;
155 bool bibPresent = ( bibkeys.size() > 0 );
156 setSize( size, bibPresent );
158 fl_set_input( dialog_->textAftr, textAfter.c_str() );
160 fl_unfreeze_form( dialog_->form_citation );
164 void FormCitation::updateCitekeys( string const & keysIn )
169 string keys = keysIn;
170 keys = frontStrip( split(keys, tmp, ',') );
171 while( !tmp.empty() ) {
172 citekeys.push_back( tmp );
173 keys = frontStrip( split(keys, tmp, ',') );
178 void FormCitation::updateBrowser( FL_OBJECT * browser,
179 vector<string> const & keys ) const
181 fl_clear_browser( browser );
183 for( unsigned int i = 0; i < keys.size(); ++i )
184 fl_add_browser_line( browser, keys[i].c_str() );
188 void FormCitation::setBibButtons( State status ) const
192 fl_activate_object( dialog_->addBtn );
193 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
197 fl_deactivate_object( dialog_->addBtn );
198 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
207 void FormCitation::setCiteButtons( State status ) const
212 fl_activate_object( dialog_->delBtn );
213 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
215 int sel = fl_get_browser( dialog_->citeBrsr );
218 fl_activate_object( dialog_->upBtn );
219 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
221 fl_deactivate_object( dialog_->upBtn );
222 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
225 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
226 fl_activate_object( dialog_->downBtn );
227 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
229 fl_deactivate_object( dialog_->downBtn );
230 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
237 fl_deactivate_object( dialog_->delBtn );
238 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
240 fl_deactivate_object( dialog_->upBtn );
241 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
243 fl_deactivate_object( dialog_->downBtn );
244 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
252 void FormCitation::setSize( int brsrHeight, bool bibPresent ) const
254 int const infoHeight = 110;
255 int const otherHeight = 140;
256 brsrHeight = max( brsrHeight, 175 );
257 int formHeight = brsrHeight + otherHeight;
259 if( bibPresent ) formHeight += infoHeight + 30;
260 fl_set_form_size( dialog_->form_citation, 430, formHeight );
262 // No resizing is alowed in the y-direction
263 fl_set_form_minsize( dialog_->form_citation, 430, formHeight );
264 fl_set_form_maxsize( dialog_->form_citation, 1000, formHeight );
267 fl_set_object_geometry( dialog_->box, 0, ypos, 430, formHeight );
269 fl_set_object_geometry( dialog_->citeBrsr, 10, ypos, 180, brsrHeight );
270 fl_set_object_geometry( dialog_->bibBrsr, 240, ypos, 180, brsrHeight );
272 fl_set_object_position( dialog_->addBtn, 200, ypos );
274 fl_set_object_position( dialog_->delBtn, 200, ypos );
276 fl_set_object_position( dialog_->upBtn, 200, ypos );
278 fl_set_object_position( dialog_->downBtn, 200, ypos );
280 ypos = brsrHeight+30; // base of Citation/Bibliography browsers
282 // awaiting natbib support
283 fl_hide_object( dialog_->style );
287 fl_set_object_position( dialog_->infoBrsr, 10, ypos );
288 fl_show_object( dialog_->infoBrsr );
292 fl_hide_object( dialog_->infoBrsr );
295 // awaiting natbib support
296 fl_hide_object( dialog_->textBefore );
298 fl_set_object_position( dialog_->textAftr, 100, ypos );
299 fl_set_object_position( dialog_->ok, 230, ypos+50 );
300 fl_set_object_position( dialog_->cancel, 330, ypos+50 );
304 void FormCitation::input( State cb )
309 fl_deselect_browser( dialog_->citeBrsr );
311 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
312 if( sel < 1 || sel > bibkeys.size() ) break;
314 // Put into infoBrsr the additional info associated with
315 // the selected bibBrsr key
316 fl_clear_browser( dialog_->infoBrsr );
317 fl_add_browser_line( dialog_->infoBrsr,
318 bibkeysInfo[sel-1].c_str() );
320 // Highlight the selected bibBrsr key in citeBrsr if present
321 vector<string>::iterator it =
322 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
324 if( it != citekeys.end() ) {
325 int n = it - citekeys.begin();
326 fl_select_browser_line( dialog_->citeBrsr, n+1 );
327 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
330 if( !lv_->buffer()->isReadonly() ) {
331 if( it != citekeys.end() ) {
332 setBibButtons( OFF );
333 setCiteButtons( ON );
336 setCiteButtons( OFF );
343 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
344 if( sel < 1 || sel > citekeys.size() ) break;
346 if( !lv_->buffer()->isReadonly() ) {
347 setBibButtons( OFF );
348 setCiteButtons( ON );
351 // Highlight the selected citeBrsr key in bibBrsr
352 vector<string>::iterator it =
353 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
355 if (it != bibkeys.end()) {
356 int n = it - bibkeys.begin();
357 fl_select_browser_line( dialog_->bibBrsr, n+1 );
358 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
360 // Put into infoBrsr the additional info associated with
361 // the selected citeBrsr key
362 fl_clear_browser( dialog_->infoBrsr );
363 fl_add_browser_line( dialog_->infoBrsr,
364 bibkeysInfo[n].c_str() );
370 if( lv_->buffer()->isReadonly() ) break;
372 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
373 if( sel < 1 || sel > bibkeys.size() ) break;
375 // Add the selected bibBrsr key to citeBrsr
376 fl_addto_browser( dialog_->citeBrsr,
377 bibkeys[sel-1].c_str() );
378 citekeys.push_back( bibkeys[sel-1] );
380 int n = citekeys.size();
381 fl_select_browser_line( dialog_->citeBrsr, n );
383 setBibButtons( OFF );
384 setCiteButtons( ON );
389 if( lv_->buffer()->isReadonly() ) break;
391 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
392 if( sel < 1 || sel > citekeys.size() ) break;
394 // Remove the selected key from citeBrsr
395 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
396 citekeys.erase( citekeys.begin() + sel-1 );
399 setCiteButtons( OFF );
404 if( lv_->buffer()->isReadonly() ) break;
406 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
407 if( sel < 2 || sel > citekeys.size() ) break;
409 // Move the selected key up one line
410 vector<string>::iterator it = citekeys.begin() + sel-1;
413 fl_delete_browser_line( dialog_->citeBrsr, sel );
414 citekeys.erase( it );
416 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
417 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
418 citekeys.insert( it-1, tmp );
419 setCiteButtons( ON );
424 if( lv_->buffer()->isReadonly() ) break;
426 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
427 if( sel < 1 || sel > citekeys.size()-1 ) break;
429 // Move the selected key down one line
430 vector<string>::iterator it = citekeys.begin() + sel-1;
433 fl_delete_browser_line( dialog_->citeBrsr, sel );
434 citekeys.erase( it );
436 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
437 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
438 citekeys.insert( it+1, tmp );
439 setCiteButtons( ON );
448 void FormCitation::apply()
450 if( lv_->buffer()->isReadonly() ) return;
453 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
454 if (i > 0) tmp += ", ";
458 textAfter = fl_get_input(dialog_->textAftr);
462 inset_->setContents( tmp );
463 inset_->setOptions( textAfter );
464 lv_->view()->updateInset( inset_, true );
466 string arg = tmp + '|' + textAfter;
467 lv_->getLyXFunc()->Dispatch( LFUN_INSERT_CITATION, arg.c_str() );
472 void FormCitation::hide()
475 && dialog_->form_citation
476 && dialog_->form_citation->visible) {
477 fl_hide_form(dialog_->form_citation);
482 // free up the dialog for another inset
485 dialogIsOpen = false;
489 void FormCitation::free()
491 // we don't need to delete u and h here because
492 // hide() does that after disconnecting.
494 if (dialog_->form_citation
495 && dialog_->form_citation->visible) {
498 fl_free_form(dialog_->form_citation);
505 int FormCitation::WMHideCB(FL_FORM * form, void *)
507 // Ensure that the signals (u and h) are disconnected even if the
508 // window manager is used to close the dialog.
509 FormCitation * pre = static_cast<FormCitation*>(form->u_vdata);
515 void FormCitation::OKCB(FL_OBJECT * ob, long)
517 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
523 void FormCitation::CancelCB(FL_OBJECT * ob, long)
525 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
530 void FormCitation::InputCB(FL_OBJECT * ob, long data)
532 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
533 pre->input( static_cast<FormCitation::State>(data) );