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, restore 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);
92 bc_.addReadOnly(dialog_->addBtn);
93 bc_.addReadOnly(dialog_->delBtn);
94 bc_.addReadOnly(dialog_->upBtn);
95 bc_.addReadOnly(dialog_->downBtn);
96 bc_.addReadOnly(dialog_->textBefore);
97 bc_.addReadOnly(dialog_->textAftr);
101 void FormCitation::update()
106 vector<pair<string,string> > blist =
107 lv_->buffer()->getBibkeyList();
109 for (unsigned int i = 0; i < blist.size(); ++i) {
110 bibkeys.push_back(blist[i].first);
111 bibkeysInfo.push_back(blist[i].second);
116 string tmp, keys( params.getContents() );
117 keys = frontStrip( split(keys, tmp, ',') );
118 while (!tmp.empty()) {
119 citekeys.push_back( tmp );
120 keys = frontStrip( split(keys, tmp, ',') );
123 updateBrowser( dialog_->bibBrsr, bibkeys );
124 updateBrowser( dialog_->citeBrsr, citekeys );
125 fl_clear_browser( dialog_->infoBrsr );
127 // No keys have been selected yet, so...
128 setBibButtons( OFF );
129 setCiteButtons( OFF );
131 int noKeys = static_cast<int>( max( bibkeys.size(), citekeys.size() ) );
133 // Place bounds, so that 4 <= noKeys <= 10
134 noKeys = max(4, min(10, noKeys) );
136 // Re-size the form to accommodate the new browser size
137 int size = 20 * noKeys;
138 bool bibPresent = ( bibkeys.size() > 0 );
139 setSize( size, bibPresent );
141 fl_set_input( dialog_->textAftr, params.getOptions().c_str());
143 bc_.readOnly(lv_->buffer()->isReadonly());
147 void FormCitation::updateBrowser( FL_OBJECT * browser,
148 vector<string> const & keys ) const
150 fl_clear_browser( browser );
152 for (unsigned int i = 0; i < keys.size(); ++i )
153 fl_add_browser_line( browser, keys[i].c_str());
157 void FormCitation::setBibButtons( State status ) const
161 fl_activate_object( dialog_->addBtn );
162 fl_set_object_lcol( dialog_->addBtn, FL_BLACK );
166 fl_deactivate_object( dialog_->addBtn );
167 fl_set_object_lcol( dialog_->addBtn, FL_INACTIVE );
176 void FormCitation::setCiteButtons( State status ) const
181 fl_activate_object( dialog_->delBtn );
182 fl_set_object_lcol( dialog_->delBtn, FL_BLACK );
184 int sel = fl_get_browser( dialog_->citeBrsr );
187 fl_activate_object( dialog_->upBtn );
188 fl_set_object_lcol( dialog_->upBtn, FL_BLACK );
190 fl_deactivate_object( dialog_->upBtn );
191 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
194 if (sel != fl_get_browser_maxline(dialog_->citeBrsr)) {
195 fl_activate_object( dialog_->downBtn );
196 fl_set_object_lcol( dialog_->downBtn, FL_BLACK );
198 fl_deactivate_object( dialog_->downBtn );
199 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
206 fl_deactivate_object( dialog_->delBtn );
207 fl_set_object_lcol( dialog_->delBtn, FL_INACTIVE );
209 fl_deactivate_object( dialog_->upBtn );
210 fl_set_object_lcol( dialog_->upBtn, FL_INACTIVE );
212 fl_deactivate_object( dialog_->downBtn );
213 fl_set_object_lcol( dialog_->downBtn, FL_INACTIVE );
221 void FormCitation::setSize( int hbrsr, bool bibPresent ) const
223 bool const natbib = false; // will eventually be input
224 hbrsr = max( hbrsr, 175 ); // limit max size of cite/bib brsrs
226 // dh1, dh2, dh3 are the vertical separation between elements.
227 // These can be specified because the browser height is fixed
228 // so they are not changed by dynamic resizing
229 static int const dh1 = 30; // top of form to top of cite/bib brsrs;
230 // bottom of cite/bib brsrs to top of info;
231 // bottom of info to top next element;
232 // bottom of style to top textBefore;
233 // bottom of text to top ok/cancel buttons.
234 static int const dh2 = 10; // bottom of textBefore to top textAftr;
235 // bottom of ok/cancel buttons to bottom form
236 static int const dh3 = 5; // spacing between add/delete/... buttons.
238 int const wbrsr = dialog_->citeBrsr->w;
239 static int const hinfo = dialog_->infoBrsr->h;
240 static int const hstyle = dialog_->style->h;
241 static int const htext = dialog_->textAftr->h;
242 static int const hok = dialog_->button_ok->h;
244 int hform = dh1 + hbrsr + dh1;
245 if (bibPresent ) hform += hinfo + dh1;
246 if (natbib ) hform += hstyle + dh1 + htext + dh2;
247 hform += htext + dh1 + hok + dh2;
249 if (hform != minh_) {
251 fl_set_form_size( dialog_->form, minw_, minh_ );
257 fl_set_object_geometry( dialog_->box, x, y, minw_, minh_ );
259 x = dialog_->citeBrsr->x;
261 fl_set_object_geometry( dialog_->citeBrsr, x, y, wbrsr, hbrsr );
262 x = dialog_->bibBrsr->x;
263 fl_set_object_geometry( dialog_->bibBrsr, x, y, wbrsr, hbrsr );
265 x = dialog_->addBtn->x;
266 fl_set_object_position( dialog_->addBtn, x, y );
267 y += dh3 + dialog_->addBtn->h;
268 fl_set_object_position( dialog_->delBtn, x, y );
269 y += dh3 + dialog_->delBtn->h;
270 fl_set_object_position( dialog_->upBtn, x, y );
271 y += dh3 + dialog_->upBtn->h;
272 fl_set_object_position( dialog_->downBtn, x, y );
274 y = dh1 + hbrsr + dh1; // in position for next element
277 x = dialog_->infoBrsr->x;
278 fl_set_object_position( dialog_->infoBrsr, x, y );
279 fl_show_object( dialog_->infoBrsr );
282 fl_hide_object( dialog_->infoBrsr );
285 x = dialog_->style->x;
286 fl_set_object_position( dialog_->style, x, y );
287 fl_show_object( dialog_->style );
288 x = dialog_->textBefore->x;
290 fl_set_object_position( dialog_->textBefore, x, y );
291 fl_show_object( dialog_->textBefore );
294 fl_hide_object( dialog_->style );
295 fl_hide_object( dialog_->textBefore );
298 x = dialog_->textAftr->x;
299 fl_set_object_position( dialog_->textAftr, x, y );
302 x = dialog_->button_restore->x;
303 fl_set_object_position( dialog_->button_restore, x, y );
304 x = dialog_->button_ok->x;
305 fl_set_object_position( dialog_->button_ok, x, y );
306 x = dialog_->button_apply->x;
307 fl_set_object_position( dialog_->button_apply, x, y );
308 x = dialog_->button_cancel->x;
309 fl_set_object_position( dialog_->button_cancel, x, y );
314 #warning convert this to use the buttoncontroller
316 bool FormCitation::input( FL_OBJECT *, long data )
318 bool activate = false;
319 State cb = static_cast<State>( data );
324 fl_deselect_browser( dialog_->citeBrsr );
326 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
327 if (sel < 1 || sel > bibkeys.size() ) break;
329 // Put into infoBrsr the additional info associated with
330 // the selected bibBrsr key
331 fl_clear_browser( dialog_->infoBrsr );
332 fl_add_browser_line( dialog_->infoBrsr,
333 bibkeysInfo[sel - 1].c_str() );
335 // Highlight the selected bibBrsr key in citeBrsr if present
336 vector<string>::iterator it =
337 find( citekeys.begin(), citekeys.end(), bibkeys[sel-1] );
339 if (it != citekeys.end()) {
340 int n = static_cast<int>( it - citekeys.begin() );
341 fl_select_browser_line( dialog_->citeBrsr, n+1 );
342 fl_set_browser_topline( dialog_->citeBrsr, n+1 );
345 if (!lv_->buffer()->isReadonly()) {
346 if (it != citekeys.end()) {
347 setBibButtons( OFF );
348 setCiteButtons( ON );
351 setCiteButtons( OFF );
358 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
359 if (sel < 1 || sel > citekeys.size() ) break;
361 if (!lv_->buffer()->isReadonly()) {
362 setBibButtons( OFF );
363 setCiteButtons( ON );
366 // Highlight the selected citeBrsr key in bibBrsr
367 vector<string>::iterator it =
368 find( bibkeys.begin(), bibkeys.end(), citekeys[sel-1] );
370 if (it != bibkeys.end()) {
371 int n = static_cast<int>( it - bibkeys.begin() );
372 fl_select_browser_line( dialog_->bibBrsr, n+1 );
373 fl_set_browser_topline( dialog_->bibBrsr, n+1 );
375 // Put into infoBrsr the additional info associated with
376 // the selected citeBrsr key
377 fl_clear_browser( dialog_->infoBrsr );
378 fl_add_browser_line( dialog_->infoBrsr,
379 bibkeysInfo[n].c_str() );
385 if (lv_->buffer()->isReadonly() ) break;
387 unsigned int sel = fl_get_browser( dialog_->bibBrsr );
388 if (sel < 1 || sel > bibkeys.size() ) break;
390 // Add the selected bibBrsr key to citeBrsr
391 fl_addto_browser( dialog_->citeBrsr,
392 bibkeys[sel-1].c_str() );
393 citekeys.push_back( bibkeys[sel-1] );
395 int n = static_cast<int>( citekeys.size() );
396 fl_select_browser_line( dialog_->citeBrsr, n );
398 setBibButtons( OFF );
399 setCiteButtons( ON );
405 if (lv_->buffer()->isReadonly() ) break;
407 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
408 if (sel < 1 || sel > citekeys.size() ) break;
410 // Remove the selected key from citeBrsr
411 fl_delete_browser_line( dialog_->citeBrsr, sel ) ;
412 citekeys.erase( citekeys.begin() + sel-1 );
415 setCiteButtons( OFF );
421 if (lv_->buffer()->isReadonly() ) break;
423 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
424 if (sel < 2 || sel > citekeys.size() ) break;
426 // Move the selected key up 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 );
442 if (lv_->buffer()->isReadonly() ) break;
444 unsigned int sel = fl_get_browser( dialog_->citeBrsr );
445 if (sel < 1 || sel > citekeys.size()-1 ) break;
447 // Move the selected key down one line
448 vector<string>::iterator it = citekeys.begin() + sel-1;
451 fl_delete_browser_line( dialog_->citeBrsr, sel );
452 citekeys.erase( it );
454 fl_insert_browser_line( dialog_->citeBrsr, sel+1, tmp.c_str() );
455 fl_select_browser_line( dialog_->citeBrsr, sel+1 );
456 citekeys.insert( it+1, tmp );
457 setCiteButtons( ON );
468 void FormCitation::apply()
470 if (lv_->buffer()->isReadonly()) return;
473 for (unsigned int i = 0; i < citekeys.size(); ++i) {
474 if (i > 0) contents += ", ";
475 contents += citekeys[i];
478 params.setContents(contents);
479 params.setOptions(fl_get_input(dialog_->textAftr));
482 // Only update if contents have changed
483 if (params != inset_->params()) {
484 inset_->setParams(params);
485 lv_->view()->updateInset(inset_, true);
488 lv_->getLyXFunc()->Dispatch(LFUN_CITATION_INSERT,
489 params.getAsString());