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_FULLBORDER,
189 void InsetCitation::updateBrowser( FL_OBJECT * browser,
190 vector<string> const & inkeys ) const
192 fl_clear_browser( browser );
194 fl_freeze_form( browser->form );
195 for( unsigned int i = 0; i < inkeys.size(); ++i )
196 fl_add_browser_line( browser, inkeys[i].c_str() );
197 fl_unfreeze_form( browser->form );
200 void InsetCitation::callback( FD_citation_form * form, State cb )
204 fl_deselect_browser( form->citeBrsr );
206 unsigned int sel = fl_get_browser( form->bibBrsr );
207 if( sel < 1 || sel > bibkeys.size() ) break;
209 // Put into infoBrsr the additional info associated with
210 // the selected bibBrsr key
211 fl_clear_browser( form->infoBrsr );
212 fl_add_browser_line( form->infoBrsr,
213 bibkeys_info[sel-1].second.c_str() );
215 // Highlight the selected bibBrsr key in citeBrsr if present
216 vector<string>::iterator it =
217 find( insetkeys.begin(), insetkeys.end(), bibkeys[sel-1] );
219 if( it != insetkeys.end() ) {
220 int n = it - insetkeys.begin();
221 fl_select_browser_line( form->citeBrsr, n+1 );
222 fl_set_browser_topline( form->citeBrsr, n+1 );
225 if( !holder.view->buffer()->isReadonly() ) {
226 if( it != insetkeys.end() ) {
227 setBibButtons( form, OFF );
228 setCiteButtons( form, ON );
230 setBibButtons( form, ON );
231 setCiteButtons( form, OFF );
237 unsigned int sel = fl_get_browser( form->citeBrsr );
238 if( sel < 1 || sel > insetkeys.size() ) break;
240 if( !holder.view->buffer()->isReadonly() ) {
241 setBibButtons( form, OFF );
242 setCiteButtons( form, ON );
245 // Highlight the selected citeBrsr key in bibBrsr
246 vector<string>::iterator it =
247 find( bibkeys.begin(), bibkeys.end(), insetkeys[sel-1] );
249 if (it != bibkeys.end()) {
250 int n = it - bibkeys.begin();
251 fl_select_browser_line( form->bibBrsr, n+1 );
252 fl_set_browser_topline( form->bibBrsr, n+1 );
254 // Put into infoBrsr the additional info associated with
255 // the selected citeBrsr key
256 fl_clear_browser( form->infoBrsr );
257 fl_add_browser_line( form->infoBrsr,
258 bibkeys_info[n].second.c_str() );
263 if( holder.view->buffer()->isReadonly() ) break;
265 unsigned int sel = fl_get_browser( form->bibBrsr );
266 if( sel < 1 || sel > bibkeys.size() ) break;
268 // Add the selected bibBrsr key to citeBrsr
269 fl_addto_browser( form->citeBrsr,
270 bibkeys[sel-1].c_str() );
271 insetkeys.push_back( bibkeys[sel-1] );
273 int n = insetkeys.size();
274 fl_select_browser_line( form->citeBrsr, n );
276 setBibButtons( form, OFF );
277 setCiteButtons( form, ON );
282 if( holder.view->buffer()->isReadonly() ) break;
284 unsigned int sel = fl_get_browser( form->citeBrsr );
285 if( sel < 1 || sel > insetkeys.size() ) break;
287 // Remove the selected key from citeBrsr
288 fl_delete_browser_line( form->citeBrsr, sel ) ;
289 insetkeys.erase( insetkeys.begin() + sel-1 );
291 setBibButtons( form, ON );
292 setCiteButtons( form, OFF );
296 if( holder.view->buffer()->isReadonly() ) break;
298 unsigned int sel = fl_get_browser( form->citeBrsr );
299 if( sel < 2 || sel > insetkeys.size() ) break;
301 // Move the selected key up one line
302 vector<string>::iterator it = insetkeys.begin() + sel-1;
305 fl_delete_browser_line( form->citeBrsr, sel );
306 insetkeys.erase( it );
308 fl_insert_browser_line( form->citeBrsr, sel-1, tmp.c_str() );
309 fl_select_browser_line( form->citeBrsr, sel-1 );
310 insetkeys.insert( it-1, tmp );
311 setCiteButtons( form, ON );
316 if( holder.view->buffer()->isReadonly() ) break;
318 unsigned int sel = fl_get_browser( form->citeBrsr );
319 if( sel < 1 || sel > insetkeys.size()-1 ) break;
321 // Move the selected key down one line
322 vector<string>::iterator it = insetkeys.begin() + sel-1;
325 fl_delete_browser_line( form->citeBrsr, sel );
326 insetkeys.erase( it );
328 fl_insert_browser_line( form->citeBrsr, sel+1, tmp.c_str() );
329 fl_select_browser_line( form->citeBrsr, sel+1 );
330 insetkeys.insert( it+1, tmp );
331 setCiteButtons( form, ON );
336 // The inset contains a comma separated list of the keys
338 if( !holder.view->buffer()->isReadonly() )
341 for( unsigned int i = 0; i < insetkeys.size(); ++i ) {
347 setOptions( fl_get_input(form->textAftr) );
348 // shouldn't mark the buffer dirty unless something
349 // was actually altered
350 holder.view->updateInset( this, true );
352 // fall through to Cancel
355 fl_hide_form( form->form );
364 void InsetCitation::setSize( FD_citation_form * form,
365 int brsrHeight, bool bibPresent ) const
367 int const infoHeight = 110;
368 int const otherHeight = 140;
369 brsrHeight = max( brsrHeight, 175 );
370 int formHeight = brsrHeight + otherHeight;
372 if( bibPresent ) formHeight += infoHeight + 30;
373 fl_set_form_size( form->form, 430, formHeight );
376 fl_set_object_geometry( form->box, 0, ypos, 430, formHeight );
378 fl_set_object_geometry( form->citeBrsr, 10, ypos, 180, brsrHeight );
379 fl_set_object_geometry( form->bibBrsr, 240, ypos, 180, brsrHeight );
380 fl_set_object_geometry( form->addBtn, 200, ypos, 30, 30 );
382 fl_set_object_geometry( form->delBtn, 200, ypos, 30, 30 );
384 fl_set_object_geometry( form->upBtn, 200, ypos, 30, 30 );
386 fl_set_object_geometry( form->downBtn, 200, ypos, 30, 30 );
388 ypos = brsrHeight+30; // base of Citation/Bibliography browsers
392 fl_set_object_geometry( form->infoBrsr, 10, ypos, 410, infoHeight );
393 fl_show_object( form->infoBrsr );
397 fl_hide_object( form->infoBrsr );
400 fl_set_object_geometry( form->textAftr, 100, ypos, 250, 30 );
401 fl_set_object_geometry( form->ok, 230, ypos+50, 90, 30 );
402 fl_set_object_geometry( form->cancel, 330, ypos+50, 90, 30 );
406 void InsetCitation::setBibButtons( FD_citation_form * form, State status ) const
411 fl_activate_object( form->addBtn );
412 fl_set_object_lcol( form->addBtn, FL_BLACK );
418 fl_deactivate_object( form->addBtn );
419 fl_set_object_lcol( form->addBtn, FL_INACTIVE );
427 void InsetCitation::setCiteButtons( FD_citation_form * form, State status ) const
432 fl_activate_object( form->delBtn );
433 fl_set_object_lcol( form->delBtn, FL_BLACK );
435 int sel = fl_get_browser( form->citeBrsr );
438 fl_activate_object( form->upBtn );
439 fl_set_object_lcol( form->upBtn, FL_BLACK );
441 fl_deactivate_object( form->upBtn );
442 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
445 if( sel != fl_get_browser_maxline(form->citeBrsr)) {
446 fl_activate_object( form->downBtn );
447 fl_set_object_lcol( form->downBtn, FL_BLACK );
449 fl_deactivate_object( form->downBtn );
450 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
457 fl_deactivate_object( form->delBtn );
458 fl_set_object_lcol( form->delBtn, FL_INACTIVE );
460 fl_deactivate_object( form->upBtn );
461 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
463 fl_deactivate_object( form->downBtn );
464 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
472 string InsetCitation::getScreenLabel() const
476 temp += getContents();
478 if( !getOptions().empty() ) {
479 temp += ", " + getOptions();