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 fdui->form = fl_bgn_form(FL_NO_BOX, 440, 790);
52 fdui->box = obj = fl_add_box(FL_UP_BOX,0,0,440,790,"");
53 fl_set_object_resize(obj, FL_RESIZE_X);
55 fdui->citeBrsr = obj =
56 fl_add_browser(FL_HOLD_BROWSER,20,40,170,370,_("Inset keys"));
57 fl_set_object_lalign(obj,FL_ALIGN_TOP_LEFT);
58 fl_set_object_resize(obj, FL_RESIZE_X);
59 fl_set_object_callback(obj,citation_cb,InsetCitation::CITEBRSR);
62 fl_add_browser(FL_HOLD_BROWSER,250,40,170,370,_("Bibliography keys"));
63 fl_set_object_lalign(obj,FL_ALIGN_TOP_LEFT);
64 fl_set_object_resize(obj, FL_RESIZE_X);
65 fl_set_object_callback(obj,citation_cb,InsetCitation::BIBBRSR);
68 fl_add_button(FL_NORMAL_BUTTON,200,40,40,40,"@4->");
69 fl_set_object_resize(obj, FL_RESIZE_NONE);
70 fl_set_object_callback(obj,citation_cb,InsetCitation::ADD);
73 fl_add_button(FL_NORMAL_BUTTON,200,90,40,40,"@9+");
74 fl_set_object_resize(obj, FL_RESIZE_NONE);
75 fl_set_object_callback(obj,citation_cb,InsetCitation::DELETE);
78 fl_add_button(FL_NORMAL_BUTTON,200,140,40,40,"@8->");
79 fl_set_object_resize(obj, FL_RESIZE_NONE);
80 fl_set_object_callback(obj,citation_cb,InsetCitation::UP);
83 fl_add_button(FL_NORMAL_BUTTON,200,190,40,40,"@2->");
84 fl_set_object_resize(obj, FL_RESIZE_NONE);
85 fl_set_object_callback(obj,citation_cb,InsetCitation::DOWN);
87 fdui->infoBrsr = obj =
88 fl_add_browser(FL_NORMAL_BROWSER,20,440,400,110,_("Info"));
89 fl_set_object_lalign(obj,FL_ALIGN_TOP_LEFT);
90 fl_set_object_resize(obj, FL_RESIZE_X);
94 fl_add_choice(FL_NORMAL_CHOICE,160,570,130,30,_("Citation style"));
95 fl_set_object_boxtype(obj,FL_DOWN_BOX);
96 fl_set_object_resize(obj, FL_RESIZE_X);
98 fdui->textBefore = obj =
99 fl_add_input(FL_NORMAL_INPUT,100,620,250,30,_("Text before"));
100 fl_set_object_resize(obj, FL_RESIZE_X);
103 fdui->textAftr = obj =
104 fl_add_input(FL_NORMAL_INPUT,100,660,250,30,_("Text after"));
105 fl_set_object_resize(obj, FL_RESIZE_X);
108 fl_add_button(FL_RETURN_BUTTON,190,730,110,40,_("OK"));
109 fl_set_object_gravity(obj, FL_SouthEast, FL_SouthEast);
110 fl_set_object_callback(obj,citation_cb,InsetCitation::OK);
113 fl_add_button(FL_NORMAL_BUTTON,310,730,110,40,idex(_("Cancel|^[")));
114 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
115 fl_set_object_gravity(obj, FL_SouthEast, FL_SouthEast);
116 fl_set_object_callback(obj,citation_cb,InsetCitation::CANCEL);
124 InsetCitation::InsetCitation(string const & key, string const & note)
125 : InsetCommand("cite", key, note)
130 InsetCitation::~InsetCitation()
132 if(citation_form && citation_form->form
133 && citation_form->form->visible
134 && citation_form->form->u_vdata == &holder)
135 fl_hide_form(citation_form->form);
139 void InsetCitation::Edit( BufferView * bv, int, int, unsigned int )
141 if ( !citation_form ) {
142 citation_form = create_form_citation_form();
143 fl_set_form_atclose( citation_form->form,
144 CancelCloseBoxCB, 0 );
150 citation_form->form->u_vdata = &holder;
152 // update the browsers, noting the number of keys.
153 bibkeys_info = bv->buffer()->getBibkeyList();
156 for( unsigned int i = 0; i < bibkeys_info.size(); ++i )
157 bibkeys.push_back(bibkeys_info[i].first);
160 string keys = getContents();
161 keys = frontStrip( split(keys, tmp, ',') );
162 while( !tmp.empty() ) {
163 insetkeys.push_back( tmp );
164 keys = frontStrip( split(keys, tmp, ',') );
167 updateBrowser( citation_form->bibBrsr, bibkeys );
168 updateBrowser( citation_form->citeBrsr, insetkeys );
169 fl_clear_browser( citation_form->infoBrsr );
171 // No keys have been selected yet, so...
172 setBibButtons( citation_form, OFF );
173 setCiteButtons( citation_form, OFF );
175 int noKeys = max( bibkeys.size(), insetkeys.size() );
177 // Place bounds, so that 4 <= noKeys <= 15
178 noKeys = max( 4, min(15, noKeys) );
180 // Re-size the form to accommodate the new browser size
181 int size = 20 * noKeys;
182 bool bibPresent = ( bibkeys.size() > 0 );
183 setSize(citation_form, size, bibPresent);
185 fl_set_input( citation_form->textAftr, getOptions().c_str() );
186 if( holder.view->buffer()->isReadonly() )
187 fl_deactivate_object( citation_form->textAftr );
189 if( citation_form->form->visible ) {
190 fl_raise_form( citation_form->form );
192 fl_show_form(citation_form->form,
193 FL_PLACE_MOUSE | FL_FREE_SIZE,
200 void InsetCitation::updateBrowser( FL_OBJECT * browser,
201 vector<string> const & inkeys ) const
203 fl_clear_browser( browser );
205 fl_freeze_form( browser->form );
206 for( unsigned int i = 0; i < inkeys.size(); ++i )
207 fl_add_browser_line( browser, inkeys[i].c_str() );
208 fl_unfreeze_form( browser->form );
211 void InsetCitation::callback( FD_citation_form * form, State cb )
215 fl_deselect_browser( form->citeBrsr );
217 unsigned int sel = fl_get_browser( form->bibBrsr );
218 if( sel < 1 || sel > bibkeys.size() ) break;
220 // Put into infoBrsr the additional info associated with
221 // the selected bibBrsr key
222 fl_clear_browser( form->infoBrsr );
223 fl_add_browser_line( form->infoBrsr,
224 bibkeys_info[sel-1].second.c_str() );
226 // Highlight the selected bibBrsr key in citeBrsr if present
227 vector<string>::iterator it =
228 find( insetkeys.begin(), insetkeys.end(), bibkeys[sel-1] );
230 if( it != insetkeys.end() ) {
231 int n = it - insetkeys.begin();
232 fl_select_browser_line( form->citeBrsr, n+1 );
233 fl_set_browser_topline( form->citeBrsr, n+1 );
236 if( !holder.view->buffer()->isReadonly() ) {
237 if( it != insetkeys.end() ) {
238 setBibButtons( form, OFF );
239 setCiteButtons( form, ON );
241 setBibButtons( form, ON );
242 setCiteButtons( form, OFF );
248 unsigned int sel = fl_get_browser( form->citeBrsr );
249 if( sel < 1 || sel > insetkeys.size() ) break;
251 if( !holder.view->buffer()->isReadonly() ) {
252 setBibButtons( form, OFF );
253 setCiteButtons( form, ON );
256 // Highlight the selected citeBrsr key in bibBrsr
257 vector<string>::iterator it =
258 find( bibkeys.begin(), bibkeys.end(), insetkeys[sel-1] );
260 if (it != bibkeys.end()) {
261 int n = it - bibkeys.begin();
262 fl_select_browser_line( form->bibBrsr, n+1 );
263 fl_set_browser_topline( form->bibBrsr, n+1 );
265 // Put into infoBrsr the additional info associated with
266 // the selected citeBrsr key
267 fl_clear_browser( form->infoBrsr );
268 fl_add_browser_line( form->infoBrsr,
269 bibkeys_info[n].second.c_str() );
274 if( holder.view->buffer()->isReadonly() ) break;
276 unsigned int sel = fl_get_browser( form->bibBrsr );
277 if( sel < 1 || sel > bibkeys.size() ) break;
279 // Add the selected bibBrsr key to citeBrsr
280 fl_addto_browser( form->citeBrsr,
281 bibkeys[sel-1].c_str() );
282 insetkeys.push_back( bibkeys[sel-1] );
284 int n = insetkeys.size();
285 fl_select_browser_line( form->citeBrsr, n );
287 setBibButtons( form, OFF );
288 setCiteButtons( form, ON );
293 if( holder.view->buffer()->isReadonly() ) break;
295 unsigned int sel = fl_get_browser( form->citeBrsr );
296 if( sel < 1 || sel > insetkeys.size() ) break;
298 // Remove the selected key from citeBrsr
299 fl_delete_browser_line( form->citeBrsr, sel ) ;
300 insetkeys.erase( insetkeys.begin() + sel-1 );
302 setBibButtons( form, ON );
303 setCiteButtons( form, OFF );
307 if( holder.view->buffer()->isReadonly() ) break;
309 unsigned int sel = fl_get_browser( form->citeBrsr );
310 if( sel < 2 || sel > insetkeys.size() ) break;
312 // Move the selected key up one line
313 vector<string>::iterator it = insetkeys.begin() + sel-1;
316 fl_delete_browser_line( form->citeBrsr, sel );
317 insetkeys.erase( it );
319 fl_insert_browser_line( form->citeBrsr, sel-1, tmp.c_str() );
320 fl_select_browser_line( form->citeBrsr, sel-1 );
321 insetkeys.insert( it-1, tmp );
322 setCiteButtons( form, ON );
327 if( holder.view->buffer()->isReadonly() ) break;
329 unsigned int sel = fl_get_browser( form->citeBrsr );
330 if( sel < 1 || sel > insetkeys.size()-1 ) break;
332 // Move the selected key down one line
333 vector<string>::iterator it = insetkeys.begin() + sel-1;
336 fl_delete_browser_line( form->citeBrsr, sel );
337 insetkeys.erase( it );
339 fl_insert_browser_line( form->citeBrsr, sel+1, tmp.c_str() );
340 fl_select_browser_line( form->citeBrsr, sel+1 );
341 insetkeys.insert( it+1, tmp );
342 setCiteButtons( form, ON );
347 // The inset contains a comma separated list of the keys
349 if( !holder.view->buffer()->isReadonly() )
352 for( unsigned int i = 0; i < insetkeys.size(); ++i ) {
358 setOptions( fl_get_input(form->textAftr) );
359 // shouldn't mark the buffer dirty unless something
360 // was actually altered
361 holder.view->updateInset( this, true );
363 // fall through to Cancel
366 fl_hide_form( form->form );
375 void InsetCitation::setSize( FD_citation_form * form,
376 int brsrHeight, bool bibPresent ) const
378 int const infoHeight = 110;
379 int const otherHeight = 140;
380 brsrHeight = max( brsrHeight, 175 );
381 int formHeight = brsrHeight + otherHeight;
383 if( bibPresent ) formHeight += infoHeight + 30;
384 fl_set_form_size( form->form, 430, formHeight );
386 // No resizing is alowed in the y-direction
387 fl_set_form_minsize( form->form, 300, formHeight );
388 fl_set_form_maxsize( form->form, 1000, formHeight );
391 fl_set_object_geometry( form->box, 0, ypos, 430, formHeight );
393 fl_set_object_geometry( form->citeBrsr, 10, ypos, 180, brsrHeight );
394 fl_set_object_geometry( form->bibBrsr, 240, ypos, 180, brsrHeight );
395 fl_set_object_geometry( form->addBtn, 200, ypos, 30, 30 );
397 fl_set_object_geometry( form->delBtn, 200, ypos, 30, 30 );
399 fl_set_object_geometry( form->upBtn, 200, ypos, 30, 30 );
401 fl_set_object_geometry( form->downBtn, 200, ypos, 30, 30 );
403 ypos = brsrHeight+30; // base of Citation/Bibliography browsers
407 fl_set_object_geometry( form->infoBrsr, 10, ypos, 410, infoHeight );
408 fl_show_object( form->infoBrsr );
412 fl_hide_object( form->infoBrsr );
415 fl_set_object_geometry( form->textAftr, 100, ypos, 250, 30 );
416 fl_set_object_geometry( form->ok, 230, ypos+50, 90, 30 );
417 fl_set_object_geometry( form->cancel, 330, ypos+50, 90, 30 );
421 void InsetCitation::setBibButtons( FD_citation_form * form, State status ) const
426 fl_activate_object( form->addBtn );
427 fl_set_object_lcol( form->addBtn, FL_BLACK );
433 fl_deactivate_object( form->addBtn );
434 fl_set_object_lcol( form->addBtn, FL_INACTIVE );
442 void InsetCitation::setCiteButtons( FD_citation_form * form, State status ) const
447 fl_activate_object( form->delBtn );
448 fl_set_object_lcol( form->delBtn, FL_BLACK );
450 int sel = fl_get_browser( form->citeBrsr );
453 fl_activate_object( form->upBtn );
454 fl_set_object_lcol( form->upBtn, FL_BLACK );
456 fl_deactivate_object( form->upBtn );
457 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
460 if( sel != fl_get_browser_maxline(form->citeBrsr)) {
461 fl_activate_object( form->downBtn );
462 fl_set_object_lcol( form->downBtn, FL_BLACK );
464 fl_deactivate_object( form->downBtn );
465 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
472 fl_deactivate_object( form->delBtn );
473 fl_set_object_lcol( form->delBtn, FL_INACTIVE );
475 fl_deactivate_object( form->upBtn );
476 fl_set_object_lcol( form->upBtn, FL_INACTIVE );
478 fl_deactivate_object( form->downBtn );
479 fl_set_object_lcol( form->downBtn, FL_INACTIVE );
487 string InsetCitation::getScreenLabel() const
491 temp += getContents();
493 if( !getOptions().empty() ) {
494 temp += ", " + getOptions();