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"
38 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
39 : FormCommand(lv, d, _("Citation")), dialog_(0)
41 // let the dialog be shown
42 // These are permanent connections so we won't bother
43 // storing a copy because we won't be disconnecting.
44 d->showCitation.connect(slot(this, &FormCitation::showInset));
45 d->createCitation.connect(slot(this, &FormCitation::createInset));
49 FormCitation::~FormCitation()
55 FL_FORM * FormCitation::form() const
57 if ( dialog_ ) return dialog_->form;
62 void FormCitation::connect()
64 fl_set_form_maxsize( dialog_->form, 3*minw_, minh_ );
65 FormCommand::connect();
69 void FormCitation::disconnect()
75 FormCommand::disconnect();
79 void FormCitation::build()
81 dialog_ = build_citation();
83 // Workaround dumb xforms sizing bug
89 void FormCitation::update(bool switched)
99 vector<pair<string,string> > blist =
100 lv_->buffer()->getBibkeyList();
102 for( unsigned int i = 0; i < blist.size(); ++i ) {
103 bibkeys.push_back(blist[i].first);
104 bibkeysInfo.push_back(blist[i].second);
109 string tmp, keys( params.getContents() );
110 keys = frontStrip( split(keys, tmp, ',') );
111 while( !tmp.empty() ) {
112 citekeys.push_back( tmp );
113 keys = frontStrip( split(keys, tmp, ',') );
116 updateBrowser( dialog_->bibBrsr, bibkeys );
117 updateBrowser( dialog_->citeBrsr, citekeys );
118 fl_clear_browser( dialog_->infoBrsr );
120 // No keys have been selected yet, so...
121 setBibButtons( OFF );
122 setCiteButtons( OFF );
124 int noKeys = static_cast<int>( max( bibkeys.size(), citekeys.size() ) );
126 // Place bounds, so that 4 <= noKeys <= 10
127 noKeys = max(4, min(10, noKeys) );
129 // Re-size the form to accommodate the new browser size
130 int size = 20 * noKeys;
131 bool bibPresent = ( bibkeys.size() > 0 );
132 setSize( size, bibPresent );
134 fl_set_input( dialog_->textAftr, params.getOptions().c_str());
138 void FormCitation::updateBrowser( FL_OBJECT * browser,
139 vector<string> const & keys ) const
141 fl_clear_browser( browser );
143 for( unsigned int i = 0; i < keys.size(); ++i )
144 fl_add_browser_line( browser, keys[i].c_str());
148 void FormCitation::setBibButtons( State status ) const
152 fl_activate_object( dialog_->addBtn );
153 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
157 fl_deactivate_object( dialog_->addBtn );
158 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
167 void FormCitation::setCiteButtons( State status ) const
172 fl_activate_object( dialog_->delBtn );
173 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
175 int sel = fl_get_browser( dialog_->citeBrsr );
178 fl_activate_object( dialog_->upBtn );
179 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
181 fl_deactivate_object( dialog_->upBtn );
182 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
185 if ( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
186 fl_activate_object( dialog_->downBtn );
187 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
189 fl_deactivate_object( dialog_->downBtn );
190 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
197 fl_deactivate_object( dialog_->delBtn );
198 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
200 fl_deactivate_object( dialog_->upBtn );
201 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
203 fl_deactivate_object( dialog_->downBtn );
204 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
212 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
214 bool const natbib = false; // will eventually be input
215 hbrsr = max( hbrsr, 175 ); // limit max size of cite/bib brsrs
217 // dh1, dh2, dh3 are the vertical separation between elements.
218 // These can be specified because the browser height is fixed
219 // so they are not changed by dynamic resizing
220 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
221 // bottom of cite/bib brsrs to top of info;
222 // bottom of info to top next element;
223 // bottom of style to top textBefore;
224 // bottom of text to top ok/cancel buttons.
225 static int const dh2 = 10; // bottom of textBefore to top textAftr;
226 // bottom of ok/cancel buttons to bottom form
227 static int const dh3 = 5; // spacing between add/delete/... buttons.
229 int const wbrsr = dialog_->citeBrsr->w;
230 static int const hinfo = dialog_->infoBrsr->h;
231 static int const hstyle = dialog_->style->h;
232 static int const htext = dialog_->textAftr->h;
233 static int const hok = dialog_->button_ok->h;
235 int hform = dh1 + hbrsr + dh1;
236 if ( bibPresent ) hform += hinfo + dh1;
237 if ( natbib ) hform += hstyle + dh1 + htext + dh2;
238 hform += htext + dh1 + hok + dh2;
240 if ( hform != minh_ ) {
242 fl_set_form_size( dialog_->form, minw_, minh_ );
248 fl_set_object_geometry( dialog_->box, x, y, minw_, minh_ );
250 x = dialog_->citeBrsr->x;
252 fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
253 x = dialog_->bibBrsr->x;
254 fl_set_object_geometry( dialog_->bibBrsr, x, y, wbrsr, hbrsr );
256 x = dialog_->addBtn->x;
257 fl_set_object_position( dialog_->addBtn, x, y );
258 y += dh3 + dialog_->addBtn->h;
259 fl_set_object_position( dialog_->delBtn, x, y );
260 y += dh3 + dialog_->delBtn->h;
261 fl_set_object_position( dialog_->upBtn, x, y );
262 y += dh3 + dialog_->upBtn->h;
263 fl_set_object_position( dialog_->downBtn, x, y );
265 y = dh1 + hbrsr + dh1; // in position for next element
268 x = dialog_->infoBrsr->x;
269 fl_set_object_position( dialog_->infoBrsr, x, y );
270 fl_show_object( dialog_->infoBrsr );
273 fl_hide_object( dialog_->infoBrsr );
276 x = dialog_->style->x;
277 fl_set_object_position( dialog_->style, x, y );
278 fl_show_object( dialog_->style );
279 x = dialog_->textBefore->x;
281 fl_set_object_position( dialog_->textBefore, x, y );
282 fl_show_object( dialog_->textBefore );
285 fl_hide_object( dialog_->style );
286 fl_hide_object( dialog_->textBefore );
289 x = dialog_->textAftr->x;
290 fl_set_object_position( dialog_->textAftr, x, y );
292 x = dialog_->button_ok->x;
294 fl_set_object_position( dialog_->button_ok, x, y );
295 x = dialog_->button_cancel->x;
296 fl_set_object_position( dialog_->button_cancel, x, y );
301 #warning convert this to use the buttoncontroller
303 bool FormCitation::input( FL_OBJECT *, long data )
305 State cb = static_cast<State>( data );
310 fl_deselect_browser( dialog_->citeBrsr );
312 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
313 if ( sel < 1 || sel > bibkeys.size() ) break;
315 // Put into infoBrsr the additional info associated with
316 // the selected bibBrsr key
317 fl_clear_browser( dialog_->infoBrsr );
318 fl_add_browser_line( dialog_->infoBrsr,
319 bibkeysInfo[sel - 1].c_str() );
321 // Highlight the selected bibBrsr key in citeBrsr if present
322 vector<string>::iterator it =
323 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
325 if ( it != citekeys.end() ) {
326 int n = static_cast<int>( it - citekeys.begin() );
327 fl_select_browser_line( dialog_->citeBrsr, n+1 );
328 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
331 if ( !lv_->buffer()->isReadonly() ) {
332 if ( it != citekeys.end() ) {
333 setBibButtons( OFF );
334 setCiteButtons( ON );
337 setCiteButtons( OFF );
344 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
345 if ( sel < 1 || sel > citekeys.size() ) break;
347 if ( !lv_->buffer()->isReadonly() ) {
348 setBibButtons( OFF );
349 setCiteButtons( ON );
352 // Highlight the selected citeBrsr key in bibBrsr
353 vector<string>::iterator it =
354 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
356 if (it != bibkeys.end()) {
357 int n = static_cast<int>( it - bibkeys.begin() );
358 fl_select_browser_line( dialog_->bibBrsr, n+1 );
359 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
361 // Put into infoBrsr the additional info associated with
362 // the selected citeBrsr key
363 fl_clear_browser( dialog_->infoBrsr );
364 fl_add_browser_line( dialog_->infoBrsr,
365 bibkeysInfo[n].c_str() );
371 if ( lv_->buffer()->isReadonly() ) break;
373 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
374 if ( sel < 1 || sel > bibkeys.size() ) break;
376 // Add the selected bibBrsr key to citeBrsr
377 fl_addto_browser( dialog_->citeBrsr,
378 bibkeys[sel-1].c_str() );
379 citekeys.push_back( bibkeys[sel-1] );
381 int n = static_cast<int>( citekeys.size() );
382 fl_select_browser_line( dialog_->citeBrsr, n );
384 setBibButtons( OFF );
385 setCiteButtons( ON );
390 if ( lv_->buffer()->isReadonly() ) break;
392 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
393 if ( sel < 1 || sel > citekeys.size() ) break;
395 // Remove the selected key from citeBrsr
396 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
397 citekeys.erase( citekeys.begin() + sel-1 );
400 setCiteButtons( OFF );
405 if ( lv_->buffer()->isReadonly() ) break;
407 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
408 if ( sel < 2 || sel > citekeys.size() ) break;
410 // Move the selected key up one line
411 vector<string>::iterator it = citekeys.begin() + sel-1;
414 fl_delete_browser_line( dialog_->citeBrsr, sel );
415 citekeys.erase( it );
417 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
418 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
419 citekeys.insert( it-1, tmp );
420 setCiteButtons( ON );
425 if ( lv_->buffer()->isReadonly() ) break;
427 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
428 if ( sel < 1 || sel > citekeys.size()-1 ) break;
430 // Move the selected key down one line
431 vector<string>::iterator it = citekeys.begin() + sel-1;
434 fl_delete_browser_line( dialog_->citeBrsr, sel );
435 citekeys.erase( it );
437 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
438 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
439 citekeys.insert( it+1, tmp );
440 setCiteButtons( ON );
450 void FormCitation::apply()
452 if (lv_->buffer()->isReadonly()) return;
455 for(unsigned int i = 0; i < citekeys.size(); ++i) {
456 if (i > 0) contents += ", ";
457 contents += citekeys[i];
460 params.setContents(contents);
461 params.setOptions(fl_get_input(dialog_->textAftr));
464 // Only update if contents have changed
465 if (params != inset_->params()) {
466 inset_->setParams(params);
467 lv_->view()->updateInset(inset_, true);
470 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
471 params.getAsString());