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::build()
62 dialog_ = build_citation();
66 FL_FORM * const FormCitation::form() const
68 if( dialog_ && dialog_->form_citation )
69 return dialog_->form_citation;
75 void FormCitation::update()
80 vector<pair<string,string> > blist =
81 lv_->buffer()->getBibkeyList();
83 for( unsigned int i = 0; i < blist.size(); ++i ) {
84 bibkeys.push_back(blist[i].first);
85 bibkeysInfo.push_back(blist[i].second);
90 string tmp, keys( params.getContents() );
91 keys = frontStrip( split(keys, tmp, ',') );
92 while( !tmp.empty() ) {
93 citekeys.push_back( tmp );
94 keys = frontStrip( split(keys, tmp, ',') );
97 fl_freeze_form( dialog_->form_citation );
99 updateBrowser( dialog_->bibBrsr, bibkeys );
100 updateBrowser( dialog_->citeBrsr, citekeys );
101 fl_clear_browser( dialog_->infoBrsr );
103 // No keys have been selected yet, so...
104 setBibButtons( OFF );
105 setCiteButtons( OFF );
107 int noKeys = max( bibkeys.size(), citekeys.size() );
109 // Place bounds, so that 4 <= noKeys <= 15
110 noKeys = max( 4, min(15, noKeys) );
112 // Re-size the form to accommodate the new browser size
113 int size = 20 * noKeys;
114 bool bibPresent = ( bibkeys.size() > 0 );
115 setSize( size, bibPresent );
117 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
119 fl_unfreeze_form( dialog_->form_citation );
123 void FormCitation::updateBrowser( FL_OBJECT * browser,
124 vector<string> const & keys ) const
126 fl_clear_browser( browser );
128 for( unsigned int i = 0; i < keys.size(); ++i )
129 fl_add_browser_line( browser, keys[i].c_str() );
133 void FormCitation::setBibButtons( State status ) const
137 fl_activate_object( dialog_->addBtn );
138 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
142 fl_deactivate_object( dialog_->addBtn );
143 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
152 void FormCitation::setCiteButtons( State status ) const
157 fl_activate_object( dialog_->delBtn );
158 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
160 int sel = fl_get_browser( dialog_->citeBrsr );
163 fl_activate_object( dialog_->upBtn );
164 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
166 fl_deactivate_object( dialog_->upBtn );
167 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
170 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
171 fl_activate_object( dialog_->downBtn );
172 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
174 fl_deactivate_object( dialog_->downBtn );
175 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
182 fl_deactivate_object( dialog_->delBtn );
183 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
185 fl_deactivate_object( dialog_->upBtn );
186 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
188 fl_deactivate_object( dialog_->downBtn );
189 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
197 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
199 int const hinfo = dialog_->infoBrsr->h;
200 int const hother = 140;
201 hbrsr = max( hbrsr, 175 );
202 int wform = dialog_->form_citation->w;
203 int hform = hbrsr + hother;
205 if( bibPresent ) hform += hinfo + 30;
206 fl_set_form_size( dialog_->form_citation, wform, hform );
208 // No resizing is alowed in the y-direction
209 fl_set_form_minsize( dialog_->form_citation, wform, hform );
210 fl_set_form_maxsize( dialog_->form_citation, 3*wform, hform );
213 fl_set_object_geometry( dialog_->box, 0, y, wform, hform );
215 fl_set_object_geometry( dialog_->citeBrsr, 10, y, 180, hbrsr );
216 fl_set_object_geometry( dialog_->bibBrsr, 240, y, 180, hbrsr );
218 fl_set_object_position( dialog_->addBtn, 200, y );
219 y += 5 + dialog_->addBtn->h;
220 fl_set_object_position( dialog_->delBtn, 200, y );
221 y += 5 + dialog_->delBtn->h;
222 fl_set_object_position( dialog_->upBtn, 200, y );
223 y += 5 + dialog_->upBtn->h;
224 fl_set_object_position( dialog_->downBtn, 200, y );
226 y = dialog_->bibBrsr->y + dialog_->bibBrsr->h;
228 // awaiting natbib support
229 fl_hide_object( dialog_->style );
233 fl_set_object_position( dialog_->infoBrsr, 10, y );
234 fl_show_object( dialog_->infoBrsr );
238 fl_hide_object( dialog_->infoBrsr );
241 // awaiting natbib support
242 fl_hide_object( dialog_->textBefore );
244 fl_set_object_position( dialog_->textAftr, 100, y );
245 fl_set_object_position( dialog_->ok, 230, y+50 );
246 fl_set_object_position( dialog_->cancel, 330, y+50 );
250 void FormCitation::input( long data )
252 State cb = static_cast<FormCitation::State>( data );
257 fl_deselect_browser( dialog_->citeBrsr );
259 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
260 if( sel < 1 || sel > bibkeys.size() ) break;
262 // Put into infoBrsr the additional info associated with
263 // the selected bibBrsr key
264 fl_clear_browser( dialog_->infoBrsr );
265 fl_add_browser_line( dialog_->infoBrsr,
266 bibkeysInfo[sel-1].c_str() );
268 // Highlight the selected bibBrsr key in citeBrsr if present
269 vector<string>::iterator it =
270 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
272 if( it != citekeys.end() ) {
273 int n = it - citekeys.begin();
274 fl_select_browser_line( dialog_->citeBrsr, n+1 );
275 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
278 if( !lv_->buffer()->isReadonly() ) {
279 if( it != citekeys.end() ) {
280 setBibButtons( OFF );
281 setCiteButtons( ON );
284 setCiteButtons( OFF );
291 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
292 if( sel < 1 || sel > citekeys.size() ) break;
294 if( !lv_->buffer()->isReadonly() ) {
295 setBibButtons( OFF );
296 setCiteButtons( ON );
299 // Highlight the selected citeBrsr key in bibBrsr
300 vector<string>::iterator it =
301 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
303 if (it != bibkeys.end()) {
304 int n = it - bibkeys.begin();
305 fl_select_browser_line( dialog_->bibBrsr, n+1 );
306 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
308 // Put into infoBrsr the additional info associated with
309 // the selected citeBrsr key
310 fl_clear_browser( dialog_->infoBrsr );
311 fl_add_browser_line( dialog_->infoBrsr,
312 bibkeysInfo[n].c_str() );
318 if( lv_->buffer()->isReadonly() ) break;
320 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
321 if( sel < 1 || sel > bibkeys.size() ) break;
323 // Add the selected bibBrsr key to citeBrsr
324 fl_addto_browser( dialog_->citeBrsr,
325 bibkeys[sel-1].c_str() );
326 citekeys.push_back( bibkeys[sel-1] );
328 int n = citekeys.size();
329 fl_select_browser_line( dialog_->citeBrsr, n );
331 setBibButtons( OFF );
332 setCiteButtons( ON );
337 if( lv_->buffer()->isReadonly() ) break;
339 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
340 if( sel < 1 || sel > citekeys.size() ) break;
342 // Remove the selected key from citeBrsr
343 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
344 citekeys.erase( citekeys.begin() + sel-1 );
347 setCiteButtons( OFF );
352 if( lv_->buffer()->isReadonly() ) break;
354 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
355 if( sel < 2 || sel > citekeys.size() ) break;
357 // Move the selected key up one line
358 vector<string>::iterator it = citekeys.begin() + sel-1;
361 fl_delete_browser_line( dialog_->citeBrsr, sel );
362 citekeys.erase( it );
364 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
365 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
366 citekeys.insert( it-1, tmp );
367 setCiteButtons( ON );
372 if( lv_->buffer()->isReadonly() ) break;
374 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
375 if( sel < 1 || sel > citekeys.size()-1 ) break;
377 // Move the selected key down one line
378 vector<string>::iterator it = citekeys.begin() + sel-1;
381 fl_delete_browser_line( dialog_->citeBrsr, sel );
382 citekeys.erase( it );
384 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
385 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
386 citekeys.insert( it+1, tmp );
387 setCiteButtons( ON );
396 void FormCitation::apply()
398 if( lv_->buffer()->isReadonly() ) return;
401 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
402 if (i > 0) contents += ", ";
403 contents += citekeys[i];
406 params.setContents( contents );
407 params.setOptions( fl_get_input(dialog_->textAftr) );
411 // Only update if contents have changed
412 if( params.getCmdName() != inset_->getCmdName() ||
413 params.getContents() != inset_->getContents() ||
414 params.getOptions() != inset_->getOptions() ) {
415 inset_->setParams( params );
416 lv_->view()->updateInset( inset_, true );
419 lv_->getLyXFunc()->Dispatch( LFUN_INSERT_CITATION,
420 params.getAsString().c_str() );