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"
40 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
41 : FormCommand(lv, d, _("Citation")), dialog_(0)
44 // let the dialog be shown
45 // These are permanent connections so we won't bother
46 // storing a copy because we won't be disconnecting.
47 d->showCitation.connect(slot(this, &FormCitation::showInset));
48 d->createCitation.connect(slot(this, &FormCitation::createInset));
52 FormCitation::~FormCitation()
58 void FormCitation::clearStore()
66 void FormCitation::build()
68 dialog_ = build_citation();
69 min_wform = dialog_->form->w;
73 FL_FORM * const FormCitation::form() const
75 if( dialog_ ) // no need to test for dialog_->form
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 updateBrowser( dialog_->bibBrsr, bibkeys );
105 updateBrowser( dialog_->citeBrsr, citekeys );
106 fl_clear_browser( dialog_->infoBrsr );
108 // No keys have been selected yet, so...
109 setBibButtons( OFF );
110 setCiteButtons( OFF );
112 int noKeys = max( bibkeys.size(), citekeys.size() );
114 // Place bounds, so that 4 <= noKeys <= 10
115 noKeys = max( 4, min(10, noKeys) );
117 // Re-size the form to accommodate the new browser size
118 int size = 20 * noKeys;
119 bool bibPresent = ( bibkeys.size() > 0 );
120 setSize( size, bibPresent );
122 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
126 void FormCitation::updateBrowser( FL_OBJECT * browser,
127 vector<string> const & keys ) const
129 fl_clear_browser( browser );
131 for( unsigned int i = 0; i < keys.size(); ++i )
132 fl_add_browser_line( browser, keys[i].c_str() );
136 void FormCitation::setBibButtons( State status ) const
140 fl_activate_object( dialog_->addBtn );
141 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
145 fl_deactivate_object( dialog_->addBtn );
146 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
155 void FormCitation::setCiteButtons( State status ) const
160 fl_activate_object( dialog_->delBtn );
161 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
163 int sel = fl_get_browser( dialog_->citeBrsr );
166 fl_activate_object( dialog_->upBtn );
167 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
169 fl_deactivate_object( dialog_->upBtn );
170 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
173 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
174 fl_activate_object( dialog_->downBtn );
175 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
177 fl_deactivate_object( dialog_->downBtn );
178 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
185 fl_deactivate_object( dialog_->delBtn );
186 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
188 fl_deactivate_object( dialog_->upBtn );
189 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
191 fl_deactivate_object( dialog_->downBtn );
192 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
200 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
202 bool const natbib = false; // will eventually be input
203 hbrsr = max( hbrsr, 175 ); // limit max size of cite/bib brsrs
205 // dh1, dh2, dh3 are the vertical separation between elements.
206 // These can be specified because the browser height is fixed
207 // so they are not changed by dynamic resizing
208 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
209 // bottom of cite/bib brsrs to top of info;
210 // bottom of info to top next element;
211 // bottom of style to top textBefore;
212 // bottom of text to top ok/cancel buttons.
213 static int const dh2 = 10; // bottom of textBefore to top textAftr;
214 // bottom of ok/cancel buttons to bottom form
215 static int const dh3 = 5; // spacing between add/delete/... buttons.
217 int const wbrsr = dialog_->citeBrsr->w;
218 static int const hinfo = dialog_->infoBrsr->h;
219 static int const hstyle = dialog_->style->h;
220 static int const htext = dialog_->textAftr->h;
221 static int const hok = dialog_->ok->h;
223 int const wform = dialog_->form->w;
224 int hform = dh1 + hbrsr + dh1;
225 if( bibPresent ) hform += hinfo + dh1;
226 if( natbib ) hform += hstyle + dh1 + htext + dh2;
227 hform += htext + dh1 + hok + dh2;
229 bool const sizeSet = ( hform != dialog_->form->h );
230 if( sizeSet ) fl_set_form_size( dialog_->form, wform, hform );
232 // No vertical resizing is allowed
233 // min_wform set in build()
234 fl_set_form_minsize( dialog_->form, min_wform, hform );
235 fl_set_form_maxsize( dialog_->form, 3*min_wform, hform );
237 if( !sizeSet ) return;
241 fl_set_object_geometry( dialog_->box, x, y, wform, hform );
243 x = dialog_->citeBrsr->x;
245 fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
246 x = dialog_->bibBrsr->x;
247 fl_set_object_geometry( dialog_->bibBrsr, x, y, wbrsr, hbrsr );
249 x = dialog_->addBtn->x;
250 fl_set_object_position( dialog_->addBtn, x, y );
251 y += dh3 + dialog_->addBtn->h;
252 fl_set_object_position( dialog_->delBtn, x, y );
253 y += dh3 + dialog_->delBtn->h;
254 fl_set_object_position( dialog_->upBtn, x, y );
255 y += dh3 + dialog_->upBtn->h;
256 fl_set_object_position( dialog_->downBtn, x, y );
258 y = dh1 + hbrsr + dh1; // in position for next element
261 x = dialog_->infoBrsr->x;
262 fl_set_object_position( dialog_->infoBrsr, x, y );
263 fl_show_object( dialog_->infoBrsr );
266 fl_hide_object( dialog_->infoBrsr );
269 x = dialog_->style->x;
270 fl_set_object_position( dialog_->style, x, y );
271 fl_show_object( dialog_->style );
272 x = dialog_->textBefore->x;
274 fl_set_object_position( dialog_->textBefore, x, y );
275 fl_show_object( dialog_->textBefore );
278 fl_hide_object( dialog_->style );
279 fl_hide_object( dialog_->textBefore );
282 x = dialog_->textAftr->x;
283 fl_set_object_position( dialog_->textAftr, x, y );
287 fl_set_object_position( dialog_->ok, x, y );
288 x = dialog_->cancel->x;
289 fl_set_object_position( dialog_->cancel, x, y );
293 void FormCitation::input(long data)
295 State cb = static_cast<FormCitation::State>( data );
300 fl_deselect_browser( dialog_->citeBrsr );
302 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
303 if( sel < 1 || sel > bibkeys.size() ) break;
305 // Put into infoBrsr the additional info associated with
306 // the selected bibBrsr key
307 fl_clear_browser( dialog_->infoBrsr );
308 fl_add_browser_line( dialog_->infoBrsr,
309 bibkeysInfo[sel-1].c_str() );
311 // Highlight the selected bibBrsr key in citeBrsr if present
312 vector<string>::iterator it =
313 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
315 if( it != citekeys.end() ) {
316 int n = it - citekeys.begin();
317 fl_select_browser_line( dialog_->citeBrsr, n+1 );
318 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
321 if( !lv_->buffer()->isReadonly() ) {
322 if( it != citekeys.end() ) {
323 setBibButtons( OFF );
324 setCiteButtons( ON );
327 setCiteButtons( OFF );
334 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
335 if( sel < 1 || sel > citekeys.size() ) break;
337 if( !lv_->buffer()->isReadonly() ) {
338 setBibButtons( OFF );
339 setCiteButtons( ON );
342 // Highlight the selected citeBrsr key in bibBrsr
343 vector<string>::iterator it =
344 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
346 if (it != bibkeys.end()) {
347 int n = it - bibkeys.begin();
348 fl_select_browser_line( dialog_->bibBrsr, n+1 );
349 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
351 // Put into infoBrsr the additional info associated with
352 // the selected citeBrsr key
353 fl_clear_browser( dialog_->infoBrsr );
354 fl_add_browser_line( dialog_->infoBrsr,
355 bibkeysInfo[n].c_str() );
361 if( lv_->buffer()->isReadonly() ) break;
363 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
364 if( sel < 1 || sel > bibkeys.size() ) break;
366 // Add the selected bibBrsr key to citeBrsr
367 fl_addto_browser( dialog_->citeBrsr,
368 bibkeys[sel-1].c_str() );
369 citekeys.push_back( bibkeys[sel-1] );
371 int n = citekeys.size();
372 fl_select_browser_line( dialog_->citeBrsr, n );
374 setBibButtons( OFF );
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() ) break;
385 // Remove the selected key from citeBrsr
386 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
387 citekeys.erase( citekeys.begin() + sel-1 );
390 setCiteButtons( OFF );
395 if( lv_->buffer()->isReadonly() ) break;
397 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
398 if( sel < 2 || sel > citekeys.size() ) break;
400 // Move the selected key up one line
401 vector<string>::iterator it = citekeys.begin() + sel-1;
404 fl_delete_browser_line( dialog_->citeBrsr, sel );
405 citekeys.erase( it );
407 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
408 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
409 citekeys.insert( it-1, tmp );
410 setCiteButtons( ON );
415 if( lv_->buffer()->isReadonly() ) break;
417 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
418 if( sel < 1 || sel > citekeys.size()-1 ) break;
420 // Move the selected key down one line
421 vector<string>::iterator it = citekeys.begin() + sel-1;
424 fl_delete_browser_line( dialog_->citeBrsr, sel );
425 citekeys.erase( it );
427 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
428 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
429 citekeys.insert( it+1, tmp );
430 setCiteButtons( ON );
439 void FormCitation::apply()
441 if(lv_->buffer()->isReadonly()) return;
444 for(unsigned int i = 0; i < citekeys.size(); ++i) {
445 if (i > 0) contents += ", ";
446 contents += citekeys[i];
449 params.setContents(contents);
450 params.setOptions(fl_get_input(dialog_->textAftr));
453 // Only update if contents have changed
454 if(params != inset_->params()) {
455 inset_->setParams(params);
456 lv_->view()->updateInset(inset_, true);
459 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
460 params.getAsString());