11 #include FORMS_H_LOCATION
12 #include "insetcite.h"
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
19 #include "support/filetools.h"
28 FD_citation_form * citation_form = 0;
29 FD_citation_form * create_form_citation_form(void);
30 void set_size_citation_form(FD_citation_form *, int, bool);
32 static vector<pair<string,string> > bibkeys_info;
33 static vector<string> bibkeys;
34 static vector<string> insetkeys;
36 extern "C" void citation_cb( FL_OBJECT *, long data )
38 InsetCitation::Holder * holder =
39 static_cast<InsetCitation::Holder*>(citation_form->form->u_vdata);
41 holder->inset->callback( citation_form,
42 static_cast<InsetCitation::State>(data) );
46 FD_citation_form * create_form_citation_form(void)
49 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(*fdui));
51 // NOTE: dialog geometry is set in setSize().
52 // Initial size is simply non-zero.
53 fdui->form = fl_bgn_form(FL_NO_BOX, 10, 10);
54 fdui->box = obj = fl_add_box(FL_UP_BOX, 0, 0, 10, 10, "");
56 fdui->citeBrsr = obj =
57 fl_add_browser(FL_HOLD_BROWSER, 0, 0, 10, 10, _("Citation keys"));
58 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
59 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60 fl_set_object_callback(obj, citation_cb, InsetCitation::CITEBRSR);
63 fl_add_browser(FL_HOLD_BROWSER, 0, 0, 10, 10, _("Bibliography keys"));
64 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
65 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66 fl_set_object_callback(obj, citation_cb, InsetCitation::BIBBRSR);
69 fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@4->");
70 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
71 fl_set_object_callback(obj, citation_cb, InsetCitation::ADD);
74 fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@9+");
75 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
76 fl_set_object_callback(obj, citation_cb, InsetCitation::DELETE);
79 fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@8->");
80 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
81 fl_set_object_callback(obj, citation_cb, InsetCitation::UP);
84 fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@2->");
85 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
86 fl_set_object_callback(obj, citation_cb, InsetCitation::DOWN);
88 fdui->infoBrsr = obj =
89 fl_add_browser(FL_NORMAL_BROWSER, 0, 0, 10, 10, _("Info"));
90 fl_set_object_lsize(obj, FL_NORMAL_SIZE) ;
91 fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
93 fdui->textAftr = obj =
94 fl_add_input(FL_NORMAL_INPUT, 0, 0, 10, 10, _("Text after"));
95 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
98 fl_add_button(FL_RETURN_BUTTON, 0, 0, 10, 10, _("OK"));
99 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
100 fl_set_object_callback(obj, citation_cb, InsetCitation::OK);
103 fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, idex(_("Cancel|^[")));
104 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
105 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
106 fl_set_object_callback(obj, citation_cb, InsetCitation::CANCEL);
114 InsetCitation::InsetCitation(string const & key, string const & note)
115 : InsetCommand("cite", key, note)
120 InsetCitation::~InsetCitation()
122 if(citation_form && citation_form->form
123 && citation_form->form->visible
124 && citation_form->form->u_vdata == &holder)
125 fl_hide_form(citation_form->form);
129 void InsetCitation::Edit( BufferView * bv, int, int, unsigned int )
131 if ( !citation_form ) {
132 citation_form = create_form_citation_form();
133 fl_set_form_atclose( citation_form->form,
134 CancelCloseBoxCB, 0 );
140 citation_form->form->u_vdata = &holder;
142 // update the browsers, noting the number of keys.
143 bibkeys_info = bv->buffer()->getBibkeyList();
146 for( unsigned int i = 0; i < bibkeys_info.size(); ++i )
147 bibkeys.push_back(bibkeys_info[i].first);
150 string keys = getContents();
151 keys = frontStrip( split(keys, tmp, ',') );
152 while( !tmp.empty() ) {
153 insetkeys.push_back( tmp );
154 keys = frontStrip( split(keys, tmp, ',') );
157 updateBrowser( citation_form->bibBrsr, bibkeys );
158 updateBrowser( citation_form->citeBrsr, insetkeys );
159 fl_clear_browser( citation_form->infoBrsr );
161 // No keys have been selected yet, so...
162 setBibButtons( citation_form, OFF );
163 setCiteButtons( citation_form, OFF );
165 int noKeys = max( bibkeys.size(), insetkeys.size() );
167 // Place bounds, so that 4 <= noKeys <= 15
168 noKeys = max( 4, min(15, noKeys) );
170 // Re-size the form to accommodate the new browser size
171 int size = 20 * noKeys;
172 bool bibPresent = ( bibkeys.size() > 0 );
173 setSize(citation_form, size, bibPresent);
175 fl_set_input( citation_form->textAftr, getOptions().c_str() );
176 if( holder.view->buffer()->isReadonly() )
177 fl_deactivate_object( citation_form->textAftr );
179 if( citation_form->form->visible ) {
180 fl_raise_form( citation_form->form );
182 fl_show_form(citation_form->form,
183 FL_PLACE_MOUSE | FL_FREE_SIZE,
190 void InsetCitation::updateBrowser( FL_OBJECT * browser,
191 vector<string> const & inkeys ) const
193 fl_clear_browser( browser );
195 fl_freeze_form( browser->form );
196 for( unsigned int i = 0; i < inkeys.size(); ++i )
197 fl_add_browser_line( browser, inkeys[i].c_str() );
198 fl_unfreeze_form( browser->form );
201 void InsetCitation::callback( FD_citation_form * form, State cb )
205 fl_deselect_browser( form->citeBrsr );
207 unsigned int sel = fl_get_browser( form->bibBrsr );
208 if( sel < 1 || sel > bibkeys.size() ) break;
210 // Put into infoBrsr the additional info associated with
211 // the selected bibBrsr key
212 fl_clear_browser( form->infoBrsr );
213 fl_add_browser_line( form->infoBrsr,
214 bibkeys_info[sel-1].second.c_str() );
216 // Highlight the selected bibBrsr key in citeBrsr if present
217 vector<string>::iterator it =
218 find( insetkeys.begin(), insetkeys.end(), bibkeys[sel-1] );
220 if( it != insetkeys.end() ) {
221 int n = it - insetkeys.begin();
222 fl_select_browser_line( form->citeBrsr, n+1 );
223 fl_set_browser_topline( form->citeBrsr, n+1 );
226 if( !holder.view->buffer()->isReadonly() ) {
227 if( it != insetkeys.end() ) {
228 setBibButtons( form, OFF );
229 setCiteButtons( form, ON );
231 setBibButtons( form, ON );
232 setCiteButtons( form, OFF );
238 unsigned int sel = fl_get_browser( form->citeBrsr );
239 if( sel < 1 || sel > insetkeys.size() ) break;
241 if( !holder.view->buffer()->isReadonly() ) {
242 setBibButtons( form, OFF );
243 setCiteButtons( form, ON );
246 // Highlight the selected citeBrsr key in bibBrsr
247 vector<string>::iterator it =
248 find( bibkeys.begin(), bibkeys.end(), insetkeys[sel-1] );
250 if (it != bibkeys.end()) {
251 int n = it - bibkeys.begin();
252 fl_select_browser_line( form->bibBrsr, n+1 );
253 fl_set_browser_topline( form->bibBrsr, n+1 );
255 // Put into infoBrsr the additional info associated with
256 // the selected citeBrsr key
257 fl_clear_browser( form->infoBrsr );
258 fl_add_browser_line( form->infoBrsr,
259 bibkeys_info[n].second.c_str() );
264 if( holder.view->buffer()->isReadonly() ) break;
266 unsigned int sel = fl_get_browser( form->bibBrsr );
267 if( sel < 1 || sel > bibkeys.size() ) break;
269 // Add the selected bibBrsr key to citeBrsr
270 fl_addto_browser( form->citeBrsr,
271 bibkeys[sel-1].c_str() );
272 insetkeys.push_back( bibkeys[sel-1] );
274 int n = insetkeys.size();
275 fl_select_browser_line( form->citeBrsr, n );
277 setBibButtons( form, OFF );
278 setCiteButtons( form, ON );
283 if( holder.view->buffer()->isReadonly() ) break;
285 unsigned int sel = fl_get_browser( form->citeBrsr );
286 if( sel < 1 || sel > insetkeys.size() ) break;
288 // Remove the selected key from citeBrsr
289 fl_delete_browser_line( form->citeBrsr, sel ) ;
290 insetkeys.erase( insetkeys.begin() + sel-1 );
292 setBibButtons( form, ON );
293 setCiteButtons( form, OFF );
297 if( holder.view->buffer()->isReadonly() ) break;
299 unsigned int sel = fl_get_browser( form->citeBrsr );
300 if( sel < 2 || sel > insetkeys.size() ) break;
302 // Move the selected key up one line
303 vector<string>::iterator it = insetkeys.begin() + sel-1;
306 fl_delete_browser_line( form->citeBrsr, sel );
307 insetkeys.erase( it );
309 fl_insert_browser_line( form->citeBrsr, sel-1, tmp.c_str() );
310 fl_select_browser_line( form->citeBrsr, sel-1 );
311 insetkeys.insert( it-1, tmp );
312 setCiteButtons( form, ON );
317 if( holder.view->buffer()->isReadonly() ) break;
319 unsigned int sel = fl_get_browser( form->citeBrsr );
320 if( sel < 1 || sel > insetkeys.size()-1 ) break;
322 // Move the selected key down one line
323 vector<string>::iterator it = insetkeys.begin() + sel-1;
326 fl_delete_browser_line( form->citeBrsr, sel );
327 insetkeys.erase( it );
329 fl_insert_browser_line( form->citeBrsr, sel+1, tmp.c_str() );
330 fl_select_browser_line( form->citeBrsr, sel+1 );
331 insetkeys.insert( it+1, tmp );
332 setCiteButtons( form, ON );
337 // The inset contains a comma separated list of the keys
339 if( !holder.view->buffer()->isReadonly() )
342 for( unsigned int i = 0; i < insetkeys.size(); ++i ) {
348 setOptions( fl_get_input(form->textAftr) );
349 // shouldn't mark the buffer dirty unless something
350 // was actually altered
351 holder.view->updateInset( this, true );
353 // fall through to Cancel
356 fl_hide_form( form->form );
365 void InsetCitation::setSize( FD_citation_form * form,
366 int brsrHeight, bool bibPresent ) const
368 int const infoHeight = 110;
369 int const otherHeight = 140;
370 brsrHeight = max( brsrHeight, 175 );
371 int formHeight = brsrHeight + otherHeight;
373 if( bibPresent ) formHeight += infoHeight + 30;
374 fl_set_form_size( form->form, 430, formHeight );
377 fl_set_object_geometry( form->box, 0, ypos, 430, formHeight );
379 fl_set_object_geometry( form->citeBrsr, 10, ypos, 180, brsrHeight );
380 fl_set_object_geometry( form->bibBrsr, 240, ypos, 180, brsrHeight );
381 fl_set_object_geometry( form->addBtn, 200, ypos, 30, 30 );
383 fl_set_object_geometry( form->delBtn, 200, ypos, 30, 30 );
385 fl_set_object_geometry( form->upBtn, 200, ypos, 30, 30 );
387 fl_set_object_geometry( form->downBtn, 200, ypos, 30, 30 );
389 ypos = brsrHeight+30; // base of Citation/Bibliography browsers
393 fl_set_object_geometry( form->infoBrsr, 10, ypos, 410, infoHeight );
394 fl_show_object( form->infoBrsr );
398 fl_hide_object( form->infoBrsr );
401 fl_set_object_geometry( form->textAftr, 100, ypos, 250, 30 );
402 fl_set_object_geometry( form->ok, 230, ypos+50, 90, 30 );
403 fl_set_object_geometry( form->cancel, 330, ypos+50, 90, 30 );
407 void InsetCitation::setBibButtons( FD_citation_form * form, State status ) const
412 fl_activate_object( form->addBtn );
413 fl_set_object_lcol( form->addBtn, FL_BLACK );
419 fl_deactivate_object( form->addBtn );
420 fl_set_object_lcol( form->addBtn, FL_INACTIVE );
428 void InsetCitation::setCiteButtons( FD_citation_form * form, State status ) const
433 fl_activate_object( form->delBtn );
434 fl_set_object_lcol( form->delBtn, FL_BLACK );
436 int sel = fl_get_browser( form->citeBrsr );
439 fl_activate_object( form->upBtn );
440 fl_set_object_lcol( form->upBtn, FL_BLACK );
442 fl_deactivate_object( form->upBtn );
443 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
446 if( sel != fl_get_browser_maxline(form->citeBrsr)) {
447 fl_activate_object( form->downBtn );
448 fl_set_object_lcol( form->downBtn, FL_BLACK );
450 fl_deactivate_object( form->downBtn );
451 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
458 fl_deactivate_object( form->delBtn );
459 fl_set_object_lcol( form->delBtn, FL_INACTIVE );
461 fl_deactivate_object( form->upBtn );
462 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
464 fl_deactivate_object( form->downBtn );
465 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
473 string InsetCitation::getScreenLabel() const
477 temp += getContents();
479 if( !getOptions().empty() ) {
480 temp += ", " + getOptions();