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;
72 textAfter = inset->getOptions();
73 updateCitekeys(inset->getContents());
78 void FormCitation::createInset( string const & arg )
80 if( dialogIsOpen ) return;
83 if (contains(arg, "|")) {
84 keys = token(arg, '|', 0);
85 textAfter = token(arg, '|', 1);
96 void FormCitation::show()
100 fl_set_form_atclose(dialog_->form_citation,
101 C_FormCitationWMHideCB, 0);
104 update(); // make sure its up-to-date
107 if (dialog_->form_citation->visible) {
108 fl_raise_form(dialog_->form_citation);
110 fl_show_form(dialog_->form_citation,
111 FL_PLACE_MOUSE | FL_FREE_SIZE,
114 u_ = d_->updateBufferDependent.
115 connect(slot(this, &FormCitation::update));
116 h_ = d_->hideBufferDependent.
117 connect(slot(this, &FormCitation::hide));
122 void FormCitation::update()
127 vector<pair<string,string> > blist =
128 lv_->buffer()->getBibkeyList();
130 for( unsigned int i = 0; i < blist.size(); ++i ) {
131 bibkeys.push_back(blist[i].first);
132 bibkeysInfo.push_back(blist[i].second);
137 updateBrowser( dialog_->bibBrsr, bibkeys );
138 updateBrowser( dialog_->citeBrsr, citekeys );
139 fl_clear_browser( dialog_->infoBrsr );
141 // No keys have been selected yet, so...
142 setBibButtons( OFF );
143 setCiteButtons( OFF );
145 int noKeys = max( bibkeys.size(), citekeys.size() );
147 // Place bounds, so that 4 <= noKeys <= 15
148 noKeys = max( 4, min(15, noKeys) );
150 // Re-size the form to accommodate the new browser size
151 int size = 20 * noKeys;
152 bool bibPresent = ( bibkeys.size() > 0 );
153 setSize( size, bibPresent );
155 fl_set_input( dialog_->textAftr, textAfter.c_str() );
159 void FormCitation::updateCitekeys( string const & keysIn )
164 string keys = keysIn;
165 keys = frontStrip( split(keys, tmp, ',') );
166 while( !tmp.empty() ) {
167 citekeys.push_back( tmp );
168 keys = frontStrip( split(keys, tmp, ',') );
173 void FormCitation::updateBrowser( FL_OBJECT * browser,
174 vector<string> const & keys ) const
176 fl_clear_browser( browser );
178 fl_freeze_form( browser->form );
179 for( unsigned int i = 0; i < keys.size(); ++i )
180 fl_add_browser_line( browser, keys[i].c_str() );
181 fl_unfreeze_form( browser->form );
185 void FormCitation::setBibButtons( State status ) const
189 fl_activate_object( dialog_->addBtn );
190 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
194 fl_deactivate_object( dialog_->addBtn );
195 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
204 void FormCitation::setCiteButtons( State status ) const
209 fl_activate_object( dialog_->delBtn );
210 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
212 int sel = fl_get_browser( dialog_->citeBrsr );
215 fl_activate_object( dialog_->upBtn );
216 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
218 fl_deactivate_object( dialog_->upBtn );
219 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
222 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
223 fl_activate_object( dialog_->downBtn );
224 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
226 fl_deactivate_object( dialog_->downBtn );
227 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
234 fl_deactivate_object( dialog_->delBtn );
235 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
237 fl_deactivate_object( dialog_->upBtn );
238 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
240 fl_deactivate_object( dialog_->downBtn );
241 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
249 void FormCitation::setSize( int brsrHeight, bool bibPresent ) const
251 int const infoHeight = 110;
252 int const otherHeight = 140;
253 brsrHeight = max( brsrHeight, 175 );
254 int formHeight = brsrHeight + otherHeight;
256 if( bibPresent ) formHeight += infoHeight + 30;
257 fl_set_form_size( dialog_->form_citation, 430, formHeight );
259 // No resizing is alowed in the y-direction
260 fl_set_form_minsize( dialog_->form_citation, 430, formHeight );
261 fl_set_form_maxsize( dialog_->form_citation, 1000, formHeight );
264 fl_set_object_geometry( dialog_->box, 0, ypos, 430, formHeight );
266 fl_set_object_geometry( dialog_->citeBrsr, 10, ypos, 180, brsrHeight );
267 fl_set_object_geometry( dialog_->bibBrsr, 240, ypos, 180, brsrHeight );
268 fl_set_object_geometry( dialog_->addBtn, 200, ypos, 30, 30 );
270 fl_set_object_geometry( dialog_->delBtn, 200, ypos, 30, 30 );
272 fl_set_object_geometry( dialog_->upBtn, 200, ypos, 30, 30 );
274 fl_set_object_geometry( dialog_->downBtn, 200, ypos, 30, 30 );
276 ypos = brsrHeight+30; // base of Citation/Bibliography browsers
278 // awaiting natbib support
279 fl_hide_object( dialog_->style );
283 fl_set_object_geometry( dialog_->infoBrsr, 10, ypos, 410, infoHeight );
284 fl_show_object( dialog_->infoBrsr );
288 fl_hide_object( dialog_->infoBrsr );
291 // awaiting natbib support
292 fl_hide_object( dialog_->textBefore );
294 fl_set_object_geometry( dialog_->textAftr, 100, ypos, 250, 30 );
295 fl_set_object_geometry( dialog_->ok, 230, ypos+50, 90, 30 );
296 fl_set_object_geometry( dialog_->cancel, 330, ypos+50, 90, 30 );
300 void FormCitation::input( State cb )
305 fl_deselect_browser( dialog_->citeBrsr );
307 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
308 if( sel < 1 || sel > bibkeys.size() ) break;
310 // Put into infoBrsr the additional info associated with
311 // the selected bibBrsr key
312 fl_clear_browser( dialog_->infoBrsr );
313 fl_add_browser_line( dialog_->infoBrsr,
314 bibkeysInfo[sel-1].c_str() );
316 // Highlight the selected bibBrsr key in citeBrsr if present
317 vector<string>::iterator it =
318 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
320 if( it != citekeys.end() ) {
321 int n = it - citekeys.begin();
322 fl_select_browser_line( dialog_->citeBrsr, n+1 );
323 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
326 if( !lv_->buffer()->isReadonly() ) {
327 if( it != citekeys.end() ) {
328 setBibButtons( OFF );
329 setCiteButtons( ON );
332 setCiteButtons( OFF );
339 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
340 if( sel < 1 || sel > citekeys.size() ) break;
342 if( !lv_->buffer()->isReadonly() ) {
343 setBibButtons( OFF );
344 setCiteButtons( ON );
347 // Highlight the selected citeBrsr key in bibBrsr
348 vector<string>::iterator it =
349 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
351 if (it != bibkeys.end()) {
352 int n = it - bibkeys.begin();
353 fl_select_browser_line( dialog_->bibBrsr, n+1 );
354 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
356 // Put into infoBrsr the additional info associated with
357 // the selected citeBrsr key
358 fl_clear_browser( dialog_->infoBrsr );
359 fl_add_browser_line( dialog_->infoBrsr,
360 bibkeysInfo[n].c_str() );
366 if( lv_->buffer()->isReadonly() ) break;
368 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
369 if( sel < 1 || sel > bibkeys.size() ) break;
371 // Add the selected bibBrsr key to citeBrsr
372 fl_addto_browser( dialog_->citeBrsr,
373 bibkeys[sel-1].c_str() );
374 citekeys.push_back( bibkeys[sel-1] );
376 int n = citekeys.size();
377 fl_select_browser_line( dialog_->citeBrsr, n );
379 setBibButtons( OFF );
380 setCiteButtons( ON );
385 if( lv_->buffer()->isReadonly() ) break;
387 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
388 if( sel < 1 || sel > citekeys.size() ) break;
390 // Remove the selected key from citeBrsr
391 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
392 citekeys.erase( citekeys.begin() + sel-1 );
395 setCiteButtons( OFF );
400 if( lv_->buffer()->isReadonly() ) break;
402 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
403 if( sel < 2 || sel > citekeys.size() ) break;
405 // Move the selected key up one line
406 vector<string>::iterator it = citekeys.begin() + sel-1;
409 fl_delete_browser_line( dialog_->citeBrsr, sel );
410 citekeys.erase( it );
412 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
413 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
414 citekeys.insert( it-1, tmp );
415 setCiteButtons( ON );
420 if( lv_->buffer()->isReadonly() ) break;
422 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
423 if( sel < 1 || sel > citekeys.size()-1 ) break;
425 // Move the selected key down one line
426 vector<string>::iterator it = citekeys.begin() + sel-1;
429 fl_delete_browser_line( dialog_->citeBrsr, sel );
430 citekeys.erase( it );
432 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
433 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
434 citekeys.insert( it+1, tmp );
435 setCiteButtons( ON );
444 void FormCitation::apply()
446 if( lv_->buffer()->isReadonly() ) return;
449 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
450 if (i > 0) tmp += ", ";
454 textAfter = fl_get_input(dialog_->textAftr);
458 inset_->setContents( tmp );
459 inset_->setOptions( textAfter );
460 lv_->view()->updateInset( inset_, true );
462 string arg = tmp + '|' + textAfter;
463 lv_->getLyXFunc()->Dispatch( LFUN_INSERT_CITATION, arg.c_str() );
468 void FormCitation::hide()
471 && dialog_->form_citation
472 && dialog_->form_citation->visible) {
473 fl_hide_form(dialog_->form_citation);
478 // free up the dialog for another inset
480 dialogIsOpen = false;
484 void FormCitation::free()
486 // we don't need to delete u and h here because
487 // hide() does that after disconnecting.
489 if (dialog_->form_citation
490 && dialog_->form_citation->visible) {
493 fl_free_form(dialog_->form_citation);
500 int FormCitation::WMHideCB(FL_FORM * form, void *)
502 // Ensure that the signals (u and h) are disconnected even if the
503 // window manager is used to close the dialog.
504 FormCitation * pre = static_cast<FormCitation*>(form->u_vdata);
510 void FormCitation::OKCB(FL_OBJECT * ob, long)
512 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
518 void FormCitation::CancelCB(FL_OBJECT * ob, long)
520 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
525 void FormCitation::InputCB(FL_OBJECT * ob, long data)
527 FormCitation * pre = static_cast<FormCitation*>(ob->form->u_vdata);
528 pre->input( static_cast<FormCitation::State>(data) );