2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
14 #include FORMS_H_LOCATION
19 #pragma implementation
25 #include "FormCitation.h"
28 #include "form_citation.h"
30 #include "support/filetools.h"
38 static vector<string> citekeys;
39 static vector<string> bibkeys;
40 static vector<string> bibkeysInfo;
42 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
43 : FormCommand(lv, d, _("Citation")), dialog_(0)
45 // let the dialog be shown
46 // These are permanent connections so we won't bother
47 // storing a copy because we won't be disconnecting.
48 d->showCitation.connect(slot(this, &FormCitation::showInset));
49 d->createCitation.connect(slot(this, &FormCitation::createInset));
53 FormCitation::~FormCitation()
60 void FormCitation::clearStore()
68 void FormCitation::build()
70 dialog_ = build_citation();
74 FL_FORM * const FormCitation::form() const
76 if( dialog_ && dialog_->form_citation )
77 return dialog_->form_citation;
83 void FormCitation::update()
88 vector<pair<string,string> > blist =
89 lv_->buffer()->getBibkeyList();
91 for( unsigned int i = 0; i < blist.size(); ++i ) {
92 bibkeys.push_back(blist[i].first);
93 bibkeysInfo.push_back(blist[i].second);
98 string tmp, keys( params.getContents() );
99 keys = frontStrip( split(keys, tmp, ',') );
100 while( !tmp.empty() ) {
101 citekeys.push_back( tmp );
102 keys = frontStrip( split(keys, tmp, ',') );
105 fl_freeze_form( dialog_->form_citation );
107 updateBrowser( dialog_->bibBrsr, bibkeys );
108 updateBrowser( dialog_->citeBrsr, citekeys );
109 fl_clear_browser( dialog_->infoBrsr );
111 // No keys have been selected yet, so...
112 setBibButtons( OFF );
113 setCiteButtons( OFF );
115 int noKeys = max( bibkeys.size(), citekeys.size() );
117 // Place bounds, so that 4 <= noKeys <= 15
118 noKeys = max( 4, min(15, noKeys) );
120 // Re-size the form to accommodate the new browser size
121 int size = 20 * noKeys;
122 bool bibPresent = ( bibkeys.size() > 0 );
123 setSize( size, bibPresent );
125 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
127 fl_unfreeze_form( dialog_->form_citation );
131 void FormCitation::updateBrowser( FL_OBJECT * browser,
132 vector<string> const & keys ) const
134 fl_clear_browser( browser );
136 for( unsigned int i = 0; i < keys.size(); ++i )
137 fl_add_browser_line( browser, keys[i].c_str() );
141 void FormCitation::setBibButtons( State status ) const
145 fl_activate_object( dialog_->addBtn );
146 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
150 fl_deactivate_object( dialog_->addBtn );
151 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
160 void FormCitation::setCiteButtons( State status ) const
165 fl_activate_object( dialog_->delBtn );
166 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
168 int sel = fl_get_browser( dialog_->citeBrsr );
171 fl_activate_object( dialog_->upBtn );
172 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
174 fl_deactivate_object( dialog_->upBtn );
175 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
178 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
179 fl_activate_object( dialog_->downBtn );
180 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
182 fl_deactivate_object( dialog_->downBtn );
183 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
190 fl_deactivate_object( dialog_->delBtn );
191 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
193 fl_deactivate_object( dialog_->upBtn );
194 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
196 fl_deactivate_object( dialog_->downBtn );
197 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
205 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
207 int const hinfo = dialog_->infoBrsr->h;
208 int const hother = 140;
209 hbrsr = max( hbrsr, 175 );
210 int wform = dialog_->form_citation->w;
211 int hform = hbrsr + hother;
213 if( bibPresent ) hform += hinfo + 30;
214 fl_set_form_size( dialog_->form_citation, wform, hform );
216 // No resizing is alowed in the y-direction
217 fl_set_form_minsize( dialog_->form_citation, wform, hform );
218 fl_set_form_maxsize( dialog_->form_citation, 3*wform, hform );
221 fl_set_object_geometry( dialog_->box, 0, y, wform, hform );
223 fl_set_object_geometry( dialog_->citeBrsr, 10, y, 180, hbrsr );
224 fl_set_object_geometry( dialog_->bibBrsr, 240, y, 180, hbrsr );
226 fl_set_object_position( dialog_->addBtn, 200, y );
227 y += 5 + dialog_->addBtn->h;
228 fl_set_object_position( dialog_->delBtn, 200, y );
229 y += 5 + dialog_->delBtn->h;
230 fl_set_object_position( dialog_->upBtn, 200, y );
231 y += 5 + dialog_->upBtn->h;
232 fl_set_object_position( dialog_->downBtn, 200, y );
234 y = dialog_->bibBrsr->y + dialog_->bibBrsr->h;
236 // awaiting natbib support
237 fl_hide_object( dialog_->style );
241 fl_set_object_position( dialog_->infoBrsr, 10, y );
242 fl_show_object( dialog_->infoBrsr );
246 fl_hide_object( dialog_->infoBrsr );
249 // awaiting natbib support
250 fl_hide_object( dialog_->textBefore );
252 fl_set_object_position( dialog_->textAftr, 100, y );
253 fl_set_object_position( dialog_->ok, 230, y+50 );
254 fl_set_object_position( dialog_->cancel, 330, y+50 );
258 void FormCitation::input( long data )
260 State cb = static_cast<FormCitation::State>( data );
265 fl_deselect_browser( dialog_->citeBrsr );
267 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
268 if( sel < 1 || sel > bibkeys.size() ) break;
270 // Put into infoBrsr the additional info associated with
271 // the selected bibBrsr key
272 fl_clear_browser( dialog_->infoBrsr );
273 fl_add_browser_line( dialog_->infoBrsr,
274 bibkeysInfo[sel-1].c_str() );
276 // Highlight the selected bibBrsr key in citeBrsr if present
277 vector<string>::iterator it =
278 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
280 if( it != citekeys.end() ) {
281 int n = it - citekeys.begin();
282 fl_select_browser_line( dialog_->citeBrsr, n+1 );
283 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
286 if( !lv_->buffer()->isReadonly() ) {
287 if( it != citekeys.end() ) {
288 setBibButtons( OFF );
289 setCiteButtons( ON );
292 setCiteButtons( OFF );
299 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
300 if( sel < 1 || sel > citekeys.size() ) break;
302 if( !lv_->buffer()->isReadonly() ) {
303 setBibButtons( OFF );
304 setCiteButtons( ON );
307 // Highlight the selected citeBrsr key in bibBrsr
308 vector<string>::iterator it =
309 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
311 if (it != bibkeys.end()) {
312 int n = it - bibkeys.begin();
313 fl_select_browser_line( dialog_->bibBrsr, n+1 );
314 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
316 // Put into infoBrsr the additional info associated with
317 // the selected citeBrsr key
318 fl_clear_browser( dialog_->infoBrsr );
319 fl_add_browser_line( dialog_->infoBrsr,
320 bibkeysInfo[n].c_str() );
326 if( lv_->buffer()->isReadonly() ) break;
328 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
329 if( sel < 1 || sel > bibkeys.size() ) break;
331 // Add the selected bibBrsr key to citeBrsr
332 fl_addto_browser( dialog_->citeBrsr,
333 bibkeys[sel-1].c_str() );
334 citekeys.push_back( bibkeys[sel-1] );
336 int n = citekeys.size();
337 fl_select_browser_line( dialog_->citeBrsr, n );
339 setBibButtons( OFF );
340 setCiteButtons( ON );
345 if( lv_->buffer()->isReadonly() ) break;
347 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
348 if( sel < 1 || sel > citekeys.size() ) break;
350 // Remove the selected key from citeBrsr
351 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
352 citekeys.erase( citekeys.begin() + sel-1 );
355 setCiteButtons( OFF );
360 if( lv_->buffer()->isReadonly() ) break;
362 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
363 if( sel < 2 || sel > citekeys.size() ) break;
365 // Move the selected key up one line
366 vector<string>::iterator it = citekeys.begin() + sel-1;
369 fl_delete_browser_line( dialog_->citeBrsr, sel );
370 citekeys.erase( it );
372 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
373 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
374 citekeys.insert( it-1, tmp );
375 setCiteButtons( ON );
380 if( lv_->buffer()->isReadonly() ) break;
382 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
383 if( sel < 1 || sel > citekeys.size()-1 ) break;
385 // Move the selected key down one line
386 vector<string>::iterator it = citekeys.begin() + sel-1;
389 fl_delete_browser_line( dialog_->citeBrsr, sel );
390 citekeys.erase( it );
392 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
393 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
394 citekeys.insert( it+1, tmp );
395 setCiteButtons( ON );
404 void FormCitation::apply()
406 if( lv_->buffer()->isReadonly() ) return;
409 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
410 if (i > 0) contents += ", ";
411 contents += citekeys[i];
414 params.setContents( contents );
415 params.setOptions( fl_get_input(dialog_->textAftr) );
419 // Only update if contents have changed
420 if( params.getCmdName() != inset_->getCmdName() ||
421 params.getContents() != inset_->getContents() ||
422 params.getOptions() != inset_->getOptions() ) {
423 inset_->setParams( params );
424 lv_->view()->updateInset( inset_, true );
427 lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
428 params.getAsString().c_str() );