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 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
39 : 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()
57 void FormCitation::clearStore()
65 void FormCitation::build()
67 dialog_ = build_citation();
71 FL_FORM * const FormCitation::form() const
73 if( dialog_ ) // no need to test for dialog_->citation
74 return dialog_->form_citation;
80 void FormCitation::update()
85 vector<pair<string,string> > blist =
86 lv_->buffer()->getBibkeyList();
88 for( unsigned int i = 0; i < blist.size(); ++i ) {
89 bibkeys.push_back(blist[i].first);
90 bibkeysInfo.push_back(blist[i].second);
95 string tmp, keys( params.getContents() );
96 keys = frontStrip( split(keys, tmp, ',') );
97 while( !tmp.empty() ) {
98 citekeys.push_back( tmp );
99 keys = frontStrip( split(keys, tmp, ',') );
102 updateBrowser( dialog_->bibBrsr, bibkeys );
103 updateBrowser( dialog_->citeBrsr, citekeys );
104 fl_clear_browser( dialog_->infoBrsr );
106 // No keys have been selected yet, so...
107 setBibButtons( OFF );
108 setCiteButtons( OFF );
110 int noKeys = max( bibkeys.size(), citekeys.size() );
112 // Place bounds, so that 4 <= noKeys <= 15
113 noKeys = max( 4, min(15, noKeys) );
115 // Re-size the form to accommodate the new browser size
116 int size = 20 * noKeys;
117 bool bibPresent = ( bibkeys.size() > 0 );
118 setSize( size, bibPresent );
120 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
124 void FormCitation::updateBrowser( FL_OBJECT * browser,
125 vector<string> const & keys ) const
127 fl_clear_browser( browser );
129 for( unsigned int i = 0; i < keys.size(); ++i )
130 fl_add_browser_line( browser, keys[i].c_str() );
134 void FormCitation::setBibButtons( State status ) const
138 fl_activate_object( dialog_->addBtn );
139 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
143 fl_deactivate_object( dialog_->addBtn );
144 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
153 void FormCitation::setCiteButtons( State status ) const
158 fl_activate_object( dialog_->delBtn );
159 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
161 int sel = fl_get_browser( dialog_->citeBrsr );
164 fl_activate_object( dialog_->upBtn );
165 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
167 fl_deactivate_object( dialog_->upBtn );
168 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
171 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
172 fl_activate_object( dialog_->downBtn );
173 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
175 fl_deactivate_object( dialog_->downBtn );
176 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
183 fl_deactivate_object( dialog_->delBtn );
184 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
186 fl_deactivate_object( dialog_->upBtn );
187 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
189 fl_deactivate_object( dialog_->downBtn );
190 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
198 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
200 int const hinfo = dialog_->infoBrsr->h;
201 int const hother = 140;
202 hbrsr = max( hbrsr, 175 );
203 int wform = dialog_->form_citation->w;
204 int hform = hbrsr + hother;
206 if( bibPresent ) hform += hinfo + 30;
207 fl_set_form_size( dialog_->form_citation, wform, hform );
209 // No resizing is alowed in the y-direction
210 fl_set_form_minsize( dialog_->form_citation, wform, hform );
211 fl_set_form_maxsize( dialog_->form_citation, 3*wform, hform );
214 fl_set_object_geometry( dialog_->box, 0, y, wform, hform );
216 fl_set_object_geometry( dialog_->citeBrsr, 10, y, 180, hbrsr );
217 fl_set_object_geometry( dialog_->bibBrsr, 240, y, 180, hbrsr );
219 fl_set_object_position( dialog_->addBtn, 200, y );
220 y += 5 + dialog_->addBtn->h;
221 fl_set_object_position( dialog_->delBtn, 200, y );
222 y += 5 + dialog_->delBtn->h;
223 fl_set_object_position( dialog_->upBtn, 200, y );
224 y += 5 + dialog_->upBtn->h;
225 fl_set_object_position( dialog_->downBtn, 200, y );
227 y = dialog_->bibBrsr->y + dialog_->bibBrsr->h;
229 // awaiting natbib support
230 fl_hide_object( dialog_->style );
234 fl_set_object_position( dialog_->infoBrsr, 10, y );
235 fl_show_object( dialog_->infoBrsr );
239 fl_hide_object( dialog_->infoBrsr );
242 // awaiting natbib support
243 fl_hide_object( dialog_->textBefore );
245 fl_set_object_position( dialog_->textAftr, 100, y );
246 fl_set_object_position( dialog_->ok, 230, y+50 );
247 fl_set_object_position( dialog_->cancel, 330, y+50 );
251 void FormCitation::input( long data )
253 State cb = static_cast<FormCitation::State>( data );
258 fl_deselect_browser( dialog_->citeBrsr );
260 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
261 if( sel < 1 || sel > bibkeys.size() ) break;
263 // Put into infoBrsr the additional info associated with
264 // the selected bibBrsr key
265 fl_clear_browser( dialog_->infoBrsr );
266 fl_add_browser_line( dialog_->infoBrsr,
267 bibkeysInfo[sel-1].c_str() );
269 // Highlight the selected bibBrsr key in citeBrsr if present
270 vector<string>::iterator it =
271 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
273 if( it != citekeys.end() ) {
274 int n = it - citekeys.begin();
275 fl_select_browser_line( dialog_->citeBrsr, n+1 );
276 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
279 if( !lv_->buffer()->isReadonly() ) {
280 if( it != citekeys.end() ) {
281 setBibButtons( OFF );
282 setCiteButtons( ON );
285 setCiteButtons( OFF );
292 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
293 if( sel < 1 || sel > citekeys.size() ) break;
295 if( !lv_->buffer()->isReadonly() ) {
296 setBibButtons( OFF );
297 setCiteButtons( ON );
300 // Highlight the selected citeBrsr key in bibBrsr
301 vector<string>::iterator it =
302 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
304 if (it != bibkeys.end()) {
305 int n = it - bibkeys.begin();
306 fl_select_browser_line( dialog_->bibBrsr, n+1 );
307 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
309 // Put into infoBrsr the additional info associated with
310 // the selected citeBrsr key
311 fl_clear_browser( dialog_->infoBrsr );
312 fl_add_browser_line( dialog_->infoBrsr,
313 bibkeysInfo[n].c_str() );
319 if( lv_->buffer()->isReadonly() ) break;
321 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
322 if( sel < 1 || sel > bibkeys.size() ) break;
324 // Add the selected bibBrsr key to citeBrsr
325 fl_addto_browser( dialog_->citeBrsr,
326 bibkeys[sel-1].c_str() );
327 citekeys.push_back( bibkeys[sel-1] );
329 int n = citekeys.size();
330 fl_select_browser_line( dialog_->citeBrsr, n );
332 setBibButtons( OFF );
333 setCiteButtons( ON );
338 if( lv_->buffer()->isReadonly() ) break;
340 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
341 if( sel < 1 || sel > citekeys.size() ) break;
343 // Remove the selected key from citeBrsr
344 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
345 citekeys.erase( citekeys.begin() + sel-1 );
348 setCiteButtons( OFF );
353 if( lv_->buffer()->isReadonly() ) break;
355 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
356 if( sel < 2 || sel > citekeys.size() ) break;
358 // Move the selected key up one line
359 vector<string>::iterator it = citekeys.begin() + sel-1;
362 fl_delete_browser_line( dialog_->citeBrsr, sel );
363 citekeys.erase( it );
365 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
366 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
367 citekeys.insert( it-1, tmp );
368 setCiteButtons( ON );
373 if( lv_->buffer()->isReadonly() ) break;
375 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
376 if( sel < 1 || sel > citekeys.size()-1 ) break;
378 // Move the selected key down one line
379 vector<string>::iterator it = citekeys.begin() + sel-1;
382 fl_delete_browser_line( dialog_->citeBrsr, sel );
383 citekeys.erase( it );
385 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
386 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
387 citekeys.insert( it+1, tmp );
388 setCiteButtons( ON );
397 void FormCitation::apply()
399 if( lv_->buffer()->isReadonly() ) return;
402 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
403 if (i > 0) contents += ", ";
404 contents += citekeys[i];
407 params.setContents( contents );
408 params.setOptions( fl_get_input(dialog_->textAftr) );
412 // Only update if contents have changed
413 if( params != inset_->params() ) {
414 inset_->setParams( params );
415 lv_->view()->updateInset( inset_, true );
418 lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
419 params.getAsString().c_str() );