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)
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()
56 void FormCitation::clearStore()
64 void FormCitation::build()
66 dialog_ = build_citation();
70 FL_FORM * const FormCitation::form() const
72 if( dialog_ ) // no need to test for dialog_->citation
73 return dialog_->form_citation;
79 void FormCitation::update()
84 vector<pair<string,string> > blist =
85 lv_->buffer()->getBibkeyList();
87 for( unsigned int i = 0; i < blist.size(); ++i ) {
88 bibkeys.push_back(blist[i].first);
89 bibkeysInfo.push_back(blist[i].second);
94 string tmp, keys( params.getContents() );
95 keys = frontStrip( split(keys, tmp, ',') );
96 while( !tmp.empty() ) {
97 citekeys.push_back( tmp );
98 keys = frontStrip( split(keys, tmp, ',') );
101 updateBrowser( dialog_->bibBrsr, bibkeys );
102 updateBrowser( dialog_->citeBrsr, citekeys );
103 fl_clear_browser( dialog_->infoBrsr );
105 // No keys have been selected yet, so...
106 setBibButtons( OFF );
107 setCiteButtons( OFF );
109 int noKeys = max( bibkeys.size(), citekeys.size() );
111 // Place bounds, so that 4 <= noKeys <= 15
112 noKeys = max( 4, min(15, noKeys) );
114 // Re-size the form to accommodate the new browser size
115 int size = 20 * noKeys;
116 bool bibPresent = ( bibkeys.size() > 0 );
117 setSize( size, bibPresent );
119 fl_set_input( dialog_->textAftr, params.getOptions().c_str() );
123 void FormCitation::updateBrowser( FL_OBJECT * browser,
124 vector<string> const & keys ) const
126 fl_clear_browser( browser );
128 for( unsigned int i = 0; i < keys.size(); ++i )
129 fl_add_browser_line( browser, keys[i].c_str() );
133 void FormCitation::setBibButtons( State status ) const
137 fl_activate_object( dialog_->addBtn );
138 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
142 fl_deactivate_object( dialog_->addBtn );
143 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
152 void FormCitation::setCiteButtons( State status ) const
157 fl_activate_object( dialog_->delBtn );
158 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
160 int sel = fl_get_browser( dialog_->citeBrsr );
163 fl_activate_object( dialog_->upBtn );
164 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
166 fl_deactivate_object( dialog_->upBtn );
167 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
170 if( sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
171 fl_activate_object( dialog_->downBtn );
172 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
174 fl_deactivate_object( dialog_->downBtn );
175 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
182 fl_deactivate_object( dialog_->delBtn );
183 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
185 fl_deactivate_object( dialog_->upBtn );
186 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
188 fl_deactivate_object( dialog_->downBtn );
189 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
197 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
199 int const hinfo = dialog_->infoBrsr->h;
200 int const hother = 140;
201 hbrsr = max( hbrsr, 175 );
202 int wform = dialog_->form_citation->w;
203 int hform = hbrsr + hother;
205 if( bibPresent ) hform += hinfo + 30;
206 fl_set_form_size( dialog_->form_citation, wform, hform );
208 // No resizing is alowed in the y-direction
209 fl_set_form_minsize( dialog_->form_citation, wform, hform );
210 fl_set_form_maxsize( dialog_->form_citation, 3*wform, hform );
213 fl_set_object_geometry( dialog_->box, 0, y, wform, hform );
215 fl_set_object_geometry( dialog_->citeBrsr, 10, y, 180, hbrsr );
216 fl_set_object_geometry( dialog_->bibBrsr, 240, y, 180, hbrsr );
218 fl_set_object_position( dialog_->addBtn, 200, y );
219 y += 5 + dialog_->addBtn->h;
220 fl_set_object_position( dialog_->delBtn, 200, y );
221 y += 5 + dialog_->delBtn->h;
222 fl_set_object_position( dialog_->upBtn, 200, y );
223 y += 5 + dialog_->upBtn->h;
224 fl_set_object_position( dialog_->downBtn, 200, y );
226 y = dialog_->bibBrsr->y + dialog_->bibBrsr->h;
228 // awaiting natbib support
229 fl_hide_object( dialog_->style );
233 fl_set_object_position( dialog_->infoBrsr, 10, y );
234 fl_show_object( dialog_->infoBrsr );
238 fl_hide_object( dialog_->infoBrsr );
241 // awaiting natbib support
242 fl_hide_object( dialog_->textBefore );
244 fl_set_object_position( dialog_->textAftr, 100, y );
245 fl_set_object_position( dialog_->ok, 230, y+50 );
246 fl_set_object_position( dialog_->cancel, 330, y+50 );
250 void FormCitation::input( long data )
252 State cb = static_cast<FormCitation::State>( data );
257 fl_deselect_browser( dialog_->citeBrsr );
259 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
260 if( sel < 1 || sel > bibkeys.size() ) break;
262 // Put into infoBrsr the additional info associated with
263 // the selected bibBrsr key
264 fl_clear_browser( dialog_->infoBrsr );
265 fl_add_browser_line( dialog_->infoBrsr,
266 bibkeysInfo[sel-1].c_str() );
268 // Highlight the selected bibBrsr key in citeBrsr if present
269 vector<string>::iterator it =
270 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
272 if( it != citekeys.end() ) {
273 int n = it - citekeys.begin();
274 fl_select_browser_line( dialog_->citeBrsr, n+1 );
275 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
278 if( !lv_->buffer()->isReadonly() ) {
279 if( it != citekeys.end() ) {
280 setBibButtons( OFF );
281 setCiteButtons( ON );
284 setCiteButtons( OFF );
291 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
292 if( sel < 1 || sel > citekeys.size() ) break;
294 if( !lv_->buffer()->isReadonly() ) {
295 setBibButtons( OFF );
296 setCiteButtons( ON );
299 // Highlight the selected citeBrsr key in bibBrsr
300 vector<string>::iterator it =
301 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
303 if (it != bibkeys.end()) {
304 int n = it - bibkeys.begin();
305 fl_select_browser_line( dialog_->bibBrsr, n+1 );
306 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
308 // Put into infoBrsr the additional info associated with
309 // the selected citeBrsr key
310 fl_clear_browser( dialog_->infoBrsr );
311 fl_add_browser_line( dialog_->infoBrsr,
312 bibkeysInfo[n].c_str() );
318 if( lv_->buffer()->isReadonly() ) break;
320 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
321 if( sel < 1 || sel > bibkeys.size() ) break;
323 // Add the selected bibBrsr key to citeBrsr
324 fl_addto_browser( dialog_->citeBrsr,
325 bibkeys[sel-1].c_str() );
326 citekeys.push_back( bibkeys[sel-1] );
328 int n = citekeys.size();
329 fl_select_browser_line( dialog_->citeBrsr, n );
331 setBibButtons( OFF );
332 setCiteButtons( ON );
337 if( lv_->buffer()->isReadonly() ) break;
339 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
340 if( sel < 1 || sel > citekeys.size() ) break;
342 // Remove the selected key from citeBrsr
343 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
344 citekeys.erase( citekeys.begin() + sel-1 );
347 setCiteButtons( OFF );
352 if( lv_->buffer()->isReadonly() ) break;
354 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
355 if( sel < 2 || sel > citekeys.size() ) break;
357 // Move the selected key up one line
358 vector<string>::iterator it = citekeys.begin() + sel-1;
361 fl_delete_browser_line( dialog_->citeBrsr, sel );
362 citekeys.erase( it );
364 fl_insert_browser_line( dialog_->citeBrsr, sel-1, tmp.c_str() );
365 fl_select_browser_line( dialog_->citeBrsr, sel-1 );
366 citekeys.insert( it-1, tmp );
367 setCiteButtons( ON );
372 if( lv_->buffer()->isReadonly() ) break;
374 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
375 if( sel < 1 || sel > citekeys.size()-1 ) break;
377 // Move the selected key down one line
378 vector<string>::iterator it = citekeys.begin() + sel-1;
381 fl_delete_browser_line( dialog_->citeBrsr, sel );
382 citekeys.erase( it );
384 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
385 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
386 citekeys.insert( it+1, tmp );
387 setCiteButtons( ON );
396 void FormCitation::apply()
398 if( lv_->buffer()->isReadonly() ) return;
401 for( unsigned int i = 0; i < citekeys.size(); ++i ) {
402 if (i > 0) contents += ", ";
403 contents += citekeys[i];
406 params.setContents( contents );
407 params.setOptions( fl_get_input(dialog_->textAftr) );
411 // Only update if contents have changed
412 if( params != inset_->params() ) {
413 inset_->setParams( params );
414 lv_->view()->updateInset( inset_, true );
417 lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
418 params.getAsString().c_str() );