2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
16 #pragma implementation
21 #include "FormCitation.h"
24 #include "form_citation.h"
26 #include "support/filetools.h"
35 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
36 : FormCommand(lv, d, _("Citation"), new OkApplyCancelReadOnlyPolicy),
39 // let the dialog be shown
40 // These are permanent connections so we won't bother
41 // storing a copy because we won't be disconnecting.
42 d->showCitation.connect(slot(this, &FormCitation::showInset));
43 d->createCitation.connect(slot(this, &FormCitation::createInset));
47 FormCitation::~FormCitation()
53 FL_FORM * FormCitation::form() const
55 if ( dialog_ ) return dialog_->form;
60 void FormCitation::connect()
62 fl_set_form_maxsize( dialog_->form, 3*minw_, minh_ );
63 FormCommand::connect();
67 void FormCitation::disconnect()
73 FormCommand::disconnect();
77 void FormCitation::build()
79 dialog_ = build_citation();
81 // Workaround dumb xforms sizing bug
85 // manage the ok, apply and cancel/close buttons
86 bc_.setOK(dialog_->button_ok);
87 bc_.setApply(dialog_->button_apply);
88 bc_.setCancel(dialog_->button_cancel);
89 bc_.setUndoAll(dialog_->button_restore);
94 void FormCitation::update()
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 );
293 x = dialog_->button_restore->x;
294 fl_set_object_position( dialog_->button_restore, x, y );
295 x = dialog_->button_ok->x;
296 fl_set_object_position( dialog_->button_ok, x, y );
297 x = dialog_->button_apply->x;
298 fl_set_object_position( dialog_->button_apply, x, y );
299 x = dialog_->button_cancel->x;
300 fl_set_object_position( dialog_->button_cancel, x, y );
305 #warning convert this to use the buttoncontroller
307 bool FormCitation::input( FL_OBJECT *, long data )
309 bool activate = false;
310 State cb = static_cast<State>( data );
315 fl_deselect_browser( dialog_->citeBrsr );
317 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
318 if ( sel < 1 || sel > bibkeys.size() ) break;
320 // Put into infoBrsr the additional info associated with
321 // the selected bibBrsr key
322 fl_clear_browser( dialog_->infoBrsr );
323 fl_add_browser_line( dialog_->infoBrsr,
324 bibkeysInfo[sel - 1].c_str() );
326 // Highlight the selected bibBrsr key in citeBrsr if present
327 vector<string>::iterator it =
328 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
330 if ( it != citekeys.end() ) {
331 int n = static_cast<int>( it - citekeys.begin() );
332 fl_select_browser_line( dialog_->citeBrsr, n+1 );
333 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
336 if ( !lv_->buffer()->isReadonly() ) {
337 if ( it != citekeys.end() ) {
338 setBibButtons( OFF );
339 setCiteButtons( ON );
342 setCiteButtons( OFF );
349 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
350 if ( sel < 1 || sel > citekeys.size() ) break;
352 if ( !lv_->buffer()->isReadonly() ) {
353 setBibButtons( OFF );
354 setCiteButtons( ON );
357 // Highlight the selected citeBrsr key in bibBrsr
358 vector<string>::iterator it =
359 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
361 if (it != bibkeys.end()) {
362 int n = static_cast<int>( it - bibkeys.begin() );
363 fl_select_browser_line( dialog_->bibBrsr, n+1 );
364 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
366 // Put into infoBrsr the additional info associated with
367 // the selected citeBrsr key
368 fl_clear_browser( dialog_->infoBrsr );
369 fl_add_browser_line( dialog_->infoBrsr,
370 bibkeysInfo[n].c_str() );
376 if ( lv_->buffer()->isReadonly() ) break;
378 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
379 if ( sel < 1 || sel > bibkeys.size() ) break;
381 // Add the selected bibBrsr key to citeBrsr
382 fl_addto_browser( dialog_->citeBrsr,
383 bibkeys[sel-1].c_str() );
384 citekeys.push_back( bibkeys[sel-1] );
386 int n = static_cast<int>( citekeys.size() );
387 fl_select_browser_line( dialog_->citeBrsr, n );
389 setBibButtons( OFF );
390 setCiteButtons( ON );
396 if ( lv_->buffer()->isReadonly() ) break;
398 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
399 if ( sel < 1 || sel > citekeys.size() ) break;
401 // Remove the selected key from citeBrsr
402 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
403 citekeys.erase( citekeys.begin() + sel-1 );
406 setCiteButtons( OFF );
412 if ( lv_->buffer()->isReadonly() ) break;
414 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
415 if ( sel < 2 || sel > citekeys.size() ) break;
417 // Move the selected key up one line
418 vector<string>::iterator it = citekeys.begin() + sel-1;
421 fl_delete_browser_line( dialog_->citeBrsr, sel );
422 citekeys.erase( it );
424 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
425 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
426 citekeys.insert( it-1, tmp );
427 setCiteButtons( ON );
433 if ( lv_->buffer()->isReadonly() ) break;
435 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
436 if ( sel < 1 || sel > citekeys.size()-1 ) break;
438 // Move the selected key down one line
439 vector<string>::iterator it = citekeys.begin() + sel-1;
442 fl_delete_browser_line( dialog_->citeBrsr, sel );
443 citekeys.erase( it );
445 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
446 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
447 citekeys.insert( it+1, tmp );
448 setCiteButtons( ON );
459 void FormCitation::apply()
461 if (lv_->buffer()->isReadonly()) return;
464 for(unsigned int i = 0; i < citekeys.size(); ++i) {
465 if (i > 0) contents += ", ";
466 contents += citekeys[i];
469 params.setContents(contents);
470 params.setOptions(fl_get_input(dialog_->textAftr));
473 // Only update if contents have changed
474 if (params != inset_->params()) {
475 inset_->setParams(params);
476 lv_->view()->updateInset(inset_, true);
479 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
480 params.getAsString());