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
24 #include "FormCitation.h"
27 #include "form_citation.h"
29 #include "support/filetools.h"
39 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
40 : FormCommand(lv, d, _("Citation")), dialog_(0)
42 // let the dialog be shown
43 // These are permanent connections so we won't bother
44 // storing a copy because we won't be disconnecting.
45 d->showCitation.connect(slot(this, &FormCitation::showInset));
46 d->createCitation.connect(slot(this, &FormCitation::createInset));
50 FormCitation::~FormCitation()
56 FL_FORM * FormCitation::form() const
58 if ( dialog_ ) return dialog_->form;
63 void FormCitation::disconnect()
69 FormCommand::disconnect();
73 void FormCitation::build()
75 dialog_ = build_citation();
76 min_wform = dialog_->form->w;
80 void FormCitation::update(bool switched)
90 vector<pair<string,string> > blist =
91 lv_->buffer()->getBibkeyList();
93 for( unsigned int i = 0; i < blist.size(); ++i ) {
94 bibkeys.push_back(blist[i].first);
95 bibkeysInfo.push_back(blist[i].second);
100 string tmp, keys( params.getContents() );
101 keys = frontStrip( split(keys, tmp, ',') );
102 while( !tmp.empty() ) {
103 citekeys.push_back( tmp );
104 keys = frontStrip( split(keys, tmp, ',') );
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 = static_cast<int>( max( bibkeys.size(), citekeys.size() ) );
117 // Place bounds, so that 4 <= noKeys <= 10
118 noKeys = max(4, min(10, 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());
129 void FormCitation::updateBrowser( FL_OBJECT * browser,
130 vector<string> const & keys ) const
132 fl_clear_browser( browser );
134 for( unsigned int i = 0; i < keys.size(); ++i )
135 fl_add_browser_line( browser, keys[i].c_str());
139 void FormCitation::setBibButtons( State status ) const
143 fl_activate_object( dialog_->addBtn );
144 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
148 fl_deactivate_object( dialog_->addBtn );
149 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
158 void FormCitation::setCiteButtons( State status ) const
163 fl_activate_object( dialog_->delBtn );
164 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
166 int sel = fl_get_browser( dialog_->citeBrsr );
169 fl_activate_object( dialog_->upBtn );
170 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
172 fl_deactivate_object( dialog_->upBtn );
173 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
176 if ( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
177 fl_activate_object( dialog_->downBtn );
178 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
180 fl_deactivate_object( dialog_->downBtn );
181 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
188 fl_deactivate_object( dialog_->delBtn );
189 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
191 fl_deactivate_object( dialog_->upBtn );
192 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
194 fl_deactivate_object( dialog_->downBtn );
195 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
203 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
205 bool const natbib = false; // will eventually be input
206 hbrsr = max( hbrsr, 175 ); // limit max size of cite/bib brsrs
208 // dh1, dh2, dh3 are the vertical separation between elements.
209 // These can be specified because the browser height is fixed
210 // so they are not changed by dynamic resizing
211 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
212 // bottom of cite/bib brsrs to top of info;
213 // bottom of info to top next element;
214 // bottom of style to top textBefore;
215 // bottom of text to top ok/cancel buttons.
216 static int const dh2 = 10; // bottom of textBefore to top textAftr;
217 // bottom of ok/cancel buttons to bottom form
218 static int const dh3 = 5; // spacing between add/delete/... buttons.
220 int const wbrsr = dialog_->citeBrsr->w;
221 static int const hinfo = dialog_->infoBrsr->h;
222 static int const hstyle = dialog_->style->h;
223 static int const htext = dialog_->textAftr->h;
224 static int const hok = dialog_->button_ok->h;
226 int const wform = dialog_->form->w;
227 int hform = dh1 + hbrsr + dh1;
228 if ( bibPresent ) hform += hinfo + dh1;
229 if ( natbib ) hform += hstyle + dh1 + htext + dh2;
230 hform += htext + dh1 + hok + dh2;
232 bool const sizeSet = ( hform != dialog_->form->h );
233 if ( sizeSet ) fl_set_form_size( dialog_->form, wform, hform );
235 // No vertical resizing is allowed
236 // min_wform set in build()
237 fl_set_form_minsize( dialog_->form, min_wform, hform );
238 fl_set_form_maxsize( dialog_->form, 3*min_wform, hform );
240 if ( !sizeSet ) return;
244 fl_set_object_geometry( dialog_->box, x, y, wform, hform );
246 x = dialog_->citeBrsr->x;
248 fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
249 x = dialog_->bibBrsr->x;
250 fl_set_object_geometry( dialog_->bibBrsr, x, y, wbrsr, hbrsr );
252 x = dialog_->addBtn->x;
253 fl_set_object_position( dialog_->addBtn, x, y );
254 y += dh3 + dialog_->addBtn->h;
255 fl_set_object_position( dialog_->delBtn, x, y );
256 y += dh3 + dialog_->delBtn->h;
257 fl_set_object_position( dialog_->upBtn, x, y );
258 y += dh3 + dialog_->upBtn->h;
259 fl_set_object_position( dialog_->downBtn, x, y );
261 y = dh1 + hbrsr + dh1; // in position for next element
264 x = dialog_->infoBrsr->x;
265 fl_set_object_position( dialog_->infoBrsr, x, y );
266 fl_show_object( dialog_->infoBrsr );
269 fl_hide_object( dialog_->infoBrsr );
272 x = dialog_->style->x;
273 fl_set_object_position( dialog_->style, x, y );
274 fl_show_object( dialog_->style );
275 x = dialog_->textBefore->x;
277 fl_set_object_position( dialog_->textBefore, x, y );
278 fl_show_object( dialog_->textBefore );
281 fl_hide_object( dialog_->style );
282 fl_hide_object( dialog_->textBefore );
285 x = dialog_->textAftr->x;
286 fl_set_object_position( dialog_->textAftr, x, y );
288 x = dialog_->button_ok->x;
290 fl_set_object_position( dialog_->button_ok, x, y );
291 x = dialog_->button_cancel->x;
292 fl_set_object_position( dialog_->button_cancel, x, y );
297 #warning convert this to use the buttoncontroller
299 bool FormCitation::input( FL_OBJECT *, long data )
301 State cb = static_cast<State>( data );
306 fl_deselect_browser( dialog_->citeBrsr );
308 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
309 if ( sel < 1 || sel > bibkeys.size() ) break;
311 // Put into infoBrsr the additional info associated with
312 // the selected bibBrsr key
313 fl_clear_browser( dialog_->infoBrsr );
314 fl_add_browser_line( dialog_->infoBrsr,
315 bibkeysInfo[sel - 1].c_str() );
317 // Highlight the selected bibBrsr key in citeBrsr if present
318 vector<string>::iterator it =
319 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
321 if ( it != citekeys.end() ) {
322 int n = static_cast<int>( it - citekeys.begin() );
323 fl_select_browser_line( dialog_->citeBrsr, n+1 );
324 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
327 if ( !lv_->buffer()->isReadonly() ) {
328 if ( it != citekeys.end() ) {
329 setBibButtons( OFF );
330 setCiteButtons( ON );
333 setCiteButtons( OFF );
340 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
341 if ( sel < 1 || sel > citekeys.size() ) break;
343 if ( !lv_->buffer()->isReadonly() ) {
344 setBibButtons( OFF );
345 setCiteButtons( ON );
348 // Highlight the selected citeBrsr key in bibBrsr
349 vector<string>::iterator it =
350 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
352 if (it != bibkeys.end()) {
353 int n = static_cast<int>( it - bibkeys.begin() );
354 fl_select_browser_line( dialog_->bibBrsr, n+1 );
355 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
357 // Put into infoBrsr the additional info associated with
358 // the selected citeBrsr key
359 fl_clear_browser( dialog_->infoBrsr );
360 fl_add_browser_line( dialog_->infoBrsr,
361 bibkeysInfo[n].c_str() );
367 if ( lv_->buffer()->isReadonly() ) break;
369 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
370 if ( sel < 1 || sel > bibkeys.size() ) break;
372 // Add the selected bibBrsr key to citeBrsr
373 fl_addto_browser( dialog_->citeBrsr,
374 bibkeys[sel-1].c_str() );
375 citekeys.push_back( bibkeys[sel-1] );
377 int n = static_cast<int>( citekeys.size() );
378 fl_select_browser_line( dialog_->citeBrsr, n );
380 setBibButtons( OFF );
381 setCiteButtons( ON );
386 if ( lv_->buffer()->isReadonly() ) break;
388 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
389 if ( sel < 1 || sel > citekeys.size() ) break;
391 // Remove the selected key from citeBrsr
392 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
393 citekeys.erase( citekeys.begin() + sel-1 );
396 setCiteButtons( OFF );
401 if ( lv_->buffer()->isReadonly() ) break;
403 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
404 if ( sel < 2 || sel > citekeys.size() ) break;
406 // Move the selected key up one line
407 vector<string>::iterator it = citekeys.begin() + sel-1;
410 fl_delete_browser_line( dialog_->citeBrsr, sel );
411 citekeys.erase( it );
413 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
414 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
415 citekeys.insert( it-1, tmp );
416 setCiteButtons( ON );
421 if ( lv_->buffer()->isReadonly() ) break;
423 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
424 if ( sel < 1 || sel > citekeys.size()-1 ) break;
426 // Move the selected key down one line
427 vector<string>::iterator it = citekeys.begin() + sel-1;
430 fl_delete_browser_line( dialog_->citeBrsr, sel );
431 citekeys.erase( it );
433 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
434 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
435 citekeys.insert( it+1, tmp );
436 setCiteButtons( ON );
446 void FormCitation::apply()
448 if (lv_->buffer()->isReadonly()) return;
451 for(unsigned int i = 0; i < citekeys.size(); ++i) {
452 if (i > 0) contents += ", ";
453 contents += citekeys[i];
456 params.setContents(contents);
457 params.setOptions(fl_get_input(dialog_->textAftr));
460 // Only update if contents have changed
461 if (params != inset_->params()) {
462 inset_->setParams(params);
463 lv_->view()->updateInset(inset_, true);
466 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
467 params.getAsString());