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()
59 void FormCitation::clearStore()
67 void FormCitation::build()
69 dialog_ = build_citation();
73 FL_FORM * const FormCitation::form() const
75 if( dialog_ && dialog_->form_citation )
76 return dialog_->form_citation;
82 void FormCitation::update()
87 vector<pair<string,string> > blist =
88 lv_->buffer()->getBibkeyList();
90 for( unsigned int i = 0; i < blist.size(); ++i ) {
91 bibkeys.push_back(blist[i].first);
92 bibkeysInfo.push_back(blist[i].second);
97 string tmp, keys( params.getContents() );
98 keys = frontStrip( split(keys, tmp, ',') );
99 while( !tmp.empty() ) {
100 citekeys.push_back( tmp );
101 keys = frontStrip( split(keys, tmp, ',') );
104 fl_freeze_form( dialog_->form_citation );
106 updateBrowser( dialog_->bibBrsr, bibkeys );
107 updateBrowser( dialog_->citeBrsr, citekeys );
108 fl_clear_browser( dialog_->infoBrsr );
110 // No keys have been selected yet, so...
111 setBibButtons( OFF );
112 setCiteButtons( OFF );
114 int noKeys = max( bibkeys.size(), citekeys.size() );
116 // Place bounds, so that 4 <= noKeys <= 15
117 noKeys = max( 4, min(15, noKeys) );
119 // Re-size the form to accommodate the new browser size
120 int size = 20 * noKeys;
121 bool bibPresent = ( bibkeys.size() > 0 );
122 setSize( size, bibPresent );
124 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
126 fl_unfreeze_form( dialog_->form_citation );
130 void FormCitation::updateBrowser( FL_OBJECT * browser,
131 vector<string> const & keys ) const
133 fl_clear_browser( browser );
135 for( unsigned int i = 0; i < keys.size(); ++i )
136 fl_add_browser_line( browser, keys[i].c_str() );
140 void FormCitation::setBibButtons( State status ) const
144 fl_activate_object( dialog_->addBtn );
145 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
149 fl_deactivate_object( dialog_->addBtn );
150 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
159 void FormCitation::setCiteButtons( State status ) const
164 fl_activate_object( dialog_->delBtn );
165 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
167 int sel = fl_get_browser( dialog_->citeBrsr );
170 fl_activate_object( dialog_->upBtn );
171 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
173 fl_deactivate_object( dialog_->upBtn );
174 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
177 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
178 fl_activate_object( dialog_->downBtn );
179 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
181 fl_deactivate_object( dialog_->downBtn );
182 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
189 fl_deactivate_object( dialog_->delBtn );
190 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
192 fl_deactivate_object( dialog_->upBtn );
193 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
195 fl_deactivate_object( dialog_->downBtn );
196 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
204 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
206 int const hinfo = dialog_->infoBrsr->h;
207 int const hother = 140;
208 hbrsr = max( hbrsr, 175 );
209 int wform = dialog_->form_citation->w;
210 int hform = hbrsr + hother;
212 if( bibPresent ) hform += hinfo + 30;
213 fl_set_form_size( dialog_->form_citation, wform, hform );
215 // No resizing is alowed in the y-direction
216 fl_set_form_minsize( dialog_->form_citation, wform, hform );
217 fl_set_form_maxsize( dialog_->form_citation, 3*wform, hform );
220 fl_set_object_geometry( dialog_->box, 0, y, wform, hform );
222 fl_set_object_geometry( dialog_->citeBrsr, 10, y, 180, hbrsr );
223 fl_set_object_geometry( dialog_->bibBrsr, 240, y, 180, hbrsr );
225 fl_set_object_position( dialog_->addBtn, 200, y );
226 y += 5 + dialog_->addBtn->h;
227 fl_set_object_position( dialog_->delBtn, 200, y );
228 y += 5 + dialog_->delBtn->h;
229 fl_set_object_position( dialog_->upBtn, 200, y );
230 y += 5 + dialog_->upBtn->h;
231 fl_set_object_position( dialog_->downBtn, 200, y );
233 y = dialog_->bibBrsr->y + dialog_->bibBrsr->h;
235 // awaiting natbib support
236 fl_hide_object( dialog_->style );
240 fl_set_object_position( dialog_->infoBrsr, 10, y );
241 fl_show_object( dialog_->infoBrsr );
245 fl_hide_object( dialog_->infoBrsr );
248 // awaiting natbib support
249 fl_hide_object( dialog_->textBefore );
251 fl_set_object_position( dialog_->textAftr, 100, y );
252 fl_set_object_position( dialog_->ok, 230, y+50 );
253 fl_set_object_position( dialog_->cancel, 330, y+50 );
257 void FormCitation::input( long data )
259 State cb = static_cast<FormCitation::State>( data );
264 fl_deselect_browser( dialog_->citeBrsr );
266 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
267 if( sel < 1 || sel > bibkeys.size() ) break;
269 // Put into infoBrsr the additional info associated with
270 // the selected bibBrsr key
271 fl_clear_browser( dialog_->infoBrsr );
272 fl_add_browser_line( dialog_->infoBrsr,
273 bibkeysInfo[sel-1].c_str() );
275 // Highlight the selected bibBrsr key in citeBrsr if present
276 vector<string>::iterator it =
277 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
279 if( it != citekeys.end() ) {
280 int n = it - citekeys.begin();
281 fl_select_browser_line( dialog_->citeBrsr, n+1 );
282 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
285 if( !lv_->buffer()->isReadonly() ) {
286 if( it != citekeys.end() ) {
287 setBibButtons( OFF );
288 setCiteButtons( ON );
291 setCiteButtons( OFF );
298 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
299 if( sel < 1 || sel > citekeys.size() ) break;
301 if( !lv_->buffer()->isReadonly() ) {
302 setBibButtons( OFF );
303 setCiteButtons( ON );
306 // Highlight the selected citeBrsr key in bibBrsr
307 vector<string>::iterator it =
308 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
310 if (it != bibkeys.end()) {
311 int n = it - bibkeys.begin();
312 fl_select_browser_line( dialog_->bibBrsr, n+1 );
313 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
315 // Put into infoBrsr the additional info associated with
316 // the selected citeBrsr key
317 fl_clear_browser( dialog_->infoBrsr );
318 fl_add_browser_line( dialog_->infoBrsr,
319 bibkeysInfo[n].c_str() );
325 if( lv_->buffer()->isReadonly() ) break;
327 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
328 if( sel < 1 || sel > bibkeys.size() ) break;
330 // Add the selected bibBrsr key to citeBrsr
331 fl_addto_browser( dialog_->citeBrsr,
332 bibkeys[sel-1].c_str() );
333 citekeys.push_back( bibkeys[sel-1] );
335 int n = citekeys.size();
336 fl_select_browser_line( dialog_->citeBrsr, n );
338 setBibButtons( OFF );
339 setCiteButtons( ON );
344 if( lv_->buffer()->isReadonly() ) break;
346 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
347 if( sel < 1 || sel > citekeys.size() ) break;
349 // Remove the selected key from citeBrsr
350 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
351 citekeys.erase( citekeys.begin() + sel-1 );
354 setCiteButtons( OFF );
359 if( lv_->buffer()->isReadonly() ) break;
361 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
362 if( sel < 2 || sel > citekeys.size() ) break;
364 // Move the selected key up one line
365 vector<string>::iterator it = citekeys.begin() + sel-1;
368 fl_delete_browser_line( dialog_->citeBrsr, sel );
369 citekeys.erase( it );
371 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
372 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
373 citekeys.insert( it-1, tmp );
374 setCiteButtons( ON );
379 if( lv_->buffer()->isReadonly() ) break;
381 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
382 if( sel < 1 || sel > citekeys.size()-1 ) break;
384 // Move the selected key down one line
385 vector<string>::iterator it = citekeys.begin() + sel-1;
388 fl_delete_browser_line( dialog_->citeBrsr, sel );
389 citekeys.erase( it );
391 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
392 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
393 citekeys.insert( it+1, tmp );
394 setCiteButtons( ON );
403 void FormCitation::apply()
405 if( lv_->buffer()->isReadonly() ) return;
408 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
409 if (i > 0) contents += ", ";
410 contents += citekeys[i];
413 params.setContents( contents );
414 params.setOptions( fl_get_input(dialog_->textAftr) );
418 // Only update if contents have changed
419 if( params.getCmdName() != inset_->getCmdName() ||
420 params.getContents() != inset_->getContents() ||
421 params.getOptions() != inset_->getOptions() ) {
422 inset_->setParams( params );
423 lv_->view()->updateInset( inset_, true );
426 lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
427 params.getAsString().c_str() );