]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormCitation.C
We don't currently use fork anywhere (or if we do it's by mistake!), so
[lyx.git] / src / frontends / gnome / FormCitation.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #include <algorithm>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19
20 #include "gettext.h"
21 #include "Dialogs.h"
22 #include "FormCitation.h"
23 #include "LyXView.h"
24 #include "buffer.h"
25 #include "lyxfunc.h"
26 #include "support/filetools.h"
27 #include "support/LRegex.h"
28
29 #include <gtk--/scrolledwindow.h>
30 #include <gnome--/pixmap.h>
31 #include <gtk--/label.h>
32 #include <gtk--/box.h>
33 #include <gtk--/buttonbox.h>
34 #include <gnome--/entry.h>
35 #include <gnome--/stock.h>
36 #include <gtk--/separator.h>
37 #include <libgnome/gnome-config.h>
38 #include <gtk--/alignment.h>
39 #include "pixbutton.h"
40
41 // temporary solution for LyXView
42 #include "mainapp.h"
43 extern GLyxAppWin * mainAppWin;
44
45 using std::vector;
46 using std::pair;
47 using std::max;
48 using std::min;
49 using std::find;
50
51 namespace {
52
53 // configuration keys
54 string const  LOCAL_CONFIGURE_PREFIX("FormCitation");
55
56 string const  CONF_PANE_INFO("paneinfo");
57 string const  CONF_PANE_INFO_DEFAULT("=300");
58
59 string const  CONF_COLUMN("column");
60 string const  CONF_COLUMN_DEFAULT("=50");
61
62 string const  CONF_REGEXP("regexp");
63 string const  CONF_REGEXP_DEFAULT("=0");
64
65 string const CONF_SEARCH("FormCitation_search");
66 string const CONF_TEXTAFTER("FormCitation_textafter");
67
68 } // namespace anon
69
70
71 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
72   : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0)
73 {
74   // let the dialog be shown
75   // These are permanent connections so we won't bother
76   // storing a copy because we won't be disconnecting.
77   d->showCitation.connect(SigC::slot(this, &FormCitation::showInset));
78   d->createCitation.connect(SigC::slot(this, &FormCitation::createInset));
79
80   cleanupWidgets();
81 }
82
83 FormCitation::~FormCitation()
84 {
85   hide();
86 }
87
88 void FormCitation::showInset( InsetCommand * const inset )
89 {
90   if( dialog_!=0 || inset == 0 ) return;
91   
92   inset_ = inset;
93   ih_ = inset_->hideDialog.connect(SigC::slot(this, &FormCitation::hide));
94
95   u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormCitation::updateSlot));
96   h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormCitation::hide));
97   
98   params = inset->params();
99
100   if ( params.getContents().empty() ) showStageSearch();
101   else showStageAction();
102 }
103
104
105 void FormCitation::createInset( string const & arg )
106 {
107   if( dialog_!=0 ) return;
108   
109   u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormCitation::updateSlot)); 
110   h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormCitation::hide));
111   
112   params.setFromString( arg );
113   showStageSearch();
114 }
115
116
117 namespace {
118
119 void parseBibTeX(string const & dat,
120                  string const & findkey,
121                  string & keyvalue)
122 {
123   unsigned int i;
124   string data(dat);
125   
126   keyvalue = "";
127   
128   for (i=0; i<data.length(); ++i)
129     if (data[i]=='\n' || data[i]=='\t')
130       data[i] = ' ';
131   
132   data = frontStrip(data);
133   while (!data.empty()
134          && data[0]!='='
135          && (data.find(' ')!=string::npos ||
136              data.find('=')!=string::npos) )
137     {
138       unsigned int keypos = min(data.find(' '), data.find('='));
139       string key = lowercase( data.substr(0, keypos) );
140       string value;
141       string tmp;
142       char enclosing;
143       
144       data = data.substr(keypos, data.length()-1);
145       data = frontStrip(strip(data));
146       if (data.length() > 1 && data[0]=='=')
147         {
148           data = frontStrip(data.substr(1, data.length()-1));
149           if (!data.empty())
150             {
151               keypos = 1;
152               if (data[0]=='{') enclosing = '}';
153               else if (data[0]=='"') enclosing = '"';
154               else { keypos=0; enclosing=' '; }
155
156               if (keypos &&
157                   data.find(enclosing)!=string::npos &&
158                   data.length()>1)
159                 {
160                   tmp = data.substr(keypos, data.length()-1);
161                   while (tmp.find('{')!=string::npos &&
162                          tmp.find('}')!=string::npos &&
163                          tmp.find('{') < tmp.find('}') &&
164                          tmp.find('{') < tmp.find(enclosing))
165                     {
166                       keypos += tmp.find('{')+1;
167                       tmp = data.substr(keypos, data.length()-1);
168                       keypos += tmp.find('}')+1;
169                       tmp = data.substr(keypos, data.length()-1);
170                     }
171
172                   if (tmp.find(enclosing)==string::npos) return;
173                   else
174                     {
175                       keypos += tmp.find(enclosing);
176                       tmp = data.substr(keypos, data.length()-1);
177                     }
178
179                   value = data.substr(1, keypos-1);
180
181                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
182                   else data = "";
183                 }
184               else if (!keypos &&
185                        (data.find(' ') ||
186                         data.find(','))
187                        ) // numerical value ?
188                 {
189                   keypos = data.length()-1;
190                   if (data.find(' ')!=string::npos) keypos = data.find(' ');
191                   if (data.find(',')!=string::npos &&
192                       keypos > data.find(','))
193                     keypos = data.find(',');
194
195                   value = data.substr(0, keypos);
196                   
197                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
198                   else data = "";
199                 }
200               else return;
201
202               if (findkey == key) { keyvalue = value; return; } 
203
204               data = frontStrip(frontStrip(data,','));
205             }
206         }
207       else return;
208     }
209 }
210
211 } // namespace anon
212
213
214 void FormCitation::cleanupWidgets()
215 {
216   dialog_ = 0;
217   b_ok = 0;
218   b_cancel = 0;
219   search_text_ = 0;
220   info_ = 0;
221   text_after_ = 0;
222   button_unselect_ = 0;
223   button_up_ = 0;
224   button_down_ = 0;
225   button_regexp_ = 0;
226   clist_selected_ = 0;
227   clist_bib_ = 0;
228   paned_info_ = 0;
229 }
230
231
232 void FormCitation::initWidgets()
233 {
234   string const path = PACKAGE "/" + LOCAL_CONFIGURE_PREFIX;
235
236   if (search_text_ != 0)
237     {
238       search_text_->set_history_id(CONF_SEARCH);
239       search_text_->set_max_saved(10);
240       search_text_->load_history();
241       search_text_->set_use_arrows_always(true);
242     }
243
244   if (text_after_ != 0 )
245     {
246       text_after_->set_history_id(CONF_TEXTAFTER);
247       text_after_->set_max_saved(10);
248       text_after_->load_history();
249       text_after_->set_use_arrows_always(true);
250       text_after_->get_entry()->set_text(params.getOptions());
251     }
252
253   if (button_regexp_ != 0)
254     {
255       string w = path + "/" + CONF_REGEXP + CONF_REGEXP_DEFAULT;
256       button_regexp_->set_active( (gnome_config_get_int(w.c_str()) > 0) );
257     }
258
259   if (paned_info_ != 0)
260     {
261       string w = path + "/" + CONF_PANE_INFO + CONF_PANE_INFO_DEFAULT;
262       paned_info_->set_position( gnome_config_get_int(w.c_str()) );
263     }
264
265   if (clist_bib_ != 0)
266     {
267       // preferences
268       clist_bib_->column(0).set_visiblity(false);
269       clist_bib_->set_selection_mode(GTK_SELECTION_BROWSE);
270
271       // setting up sizes of columns
272       string w;
273       int sz = clist_bib_->columns().size();
274       for (int i = 0; i < sz; ++i)
275         {
276           w = path + "/" + CONF_COLUMN + "_" + tostr(i) + CONF_COLUMN_DEFAULT;
277           clist_bib_->column(i).set_width( gnome_config_get_int(w.c_str()) );
278         }
279       
280       // retrieving data
281       vector<pair<string,string> > blist = lv_->buffer()->getBibkeyList();
282
283       sz = blist.size();
284       for (int i = 0; i < sz; ++i )
285         {
286           bibkeys.push_back(blist[i].first);
287           bibkeysInfo.push_back(blist[i].second);
288         }
289       
290       blist.clear();      
291       
292       // updating list
293       search();
294
295       if (clist_bib_->rows().size() > 0)
296         {
297           clist_bib_->rows()[0].select();
298           selectionToggled(0, 0, 0, true, false);
299         }
300     }
301
302   if (clist_selected_ != 0)
303     {
304       clist_selected_->set_selection_mode(GTK_SELECTION_BROWSE);
305
306       // populating clist_selected_
307       vector<string> r;
308       string tmp, keys( params.getContents() );
309       keys = frontStrip( split(keys, tmp, ',') );
310       while ( !tmp.empty() )
311         {
312           r.clear();
313           r.push_back(tmp);
314           clist_selected_->rows().push_back(r);
315           
316           keys = frontStrip( split(keys, tmp, ',') );
317         }
318
319       if (clist_selected_->rows().size() > 0)
320         {
321           clist_selected_->rows()[0].select();
322           selectionToggled(0, 0, 0, true, true);
323         }
324     }
325
326   updateButtons();
327 }
328
329
330 void FormCitation::storeWidgets()
331 {
332   string const path = PACKAGE "/" + LOCAL_CONFIGURE_PREFIX;
333
334   if (search_text_ != 0) search_text_->save_history();
335
336   if (text_after_ != 0) text_after_->save_history();
337
338   if (button_regexp_ != 0)
339     {
340       string w = path + "/" + CONF_REGEXP;
341       gnome_config_set_int(w.c_str(), button_regexp_->get_active());
342     }
343
344   if (paned_info_ != 0) 
345     {
346       string w = path + "/" + CONF_PANE_INFO;
347       gnome_config_set_int(w.c_str(), paned_info_->width() - info_->width());
348     }
349
350   if (clist_bib_ != 0)
351     {
352       string w;
353       int const sz = clist_bib_->columns().size();
354       for (int i = 0; i < sz; ++i)
355         {
356           w = path + "/" + CONF_COLUMN + "_" + tostr(i);
357           gnome_config_set_int(w.c_str(), clist_bib_->get_column_width(i));
358         }
359     }
360
361   gnome_config_sync();
362 }
363
364
365 void FormCitation::showStageAction()
366 {
367   if (!dialog_)
368     {
369       using namespace Gtk::Box_Helpers;
370
371       Gtk::Alignment * mbox = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
372       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
373
374       string const addlabel = _("_Add new citation");
375       string const editlabel = _("_Edit/remove citation(s)");
376
377       Gnome::PixButton * b_add  = manage(new Gnome::PixButton(addlabel, GNOME_STOCK_PIXMAP_NEW));
378       Gnome::PixButton * b_edit = manage(new Gnome::PixButton(editlabel, GNOME_STOCK_PIXMAP_PROPERTIES));
379
380       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
381
382       // set up spacing
383       bbox->set_spacing(4);
384       bbox->set_layout(GTK_BUTTONBOX_SPREAD);
385       
386       bbox->children().push_back(Element(*b_add, false, false));
387       bbox->children().push_back(Element(*b_edit, false, false));
388       bbox->children().push_back(Element(*b_cancel, false, false));
389
390       mbox->add(*bbox);
391
392       // accelerators
393       Gtk::AccelGroup * accel = Gtk::AccelGroup::create();
394
395       b_add->add_accelerator("clicked", *accel, b_add->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
396       b_edit->add_accelerator("clicked", *accel, b_edit->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
397       
398       // packing dialog to main window
399       dialog_ = mbox;
400       mainAppWin->add_action(*dialog_, _(" Citation: Select action "), false, accel);
401
402       initWidgets();
403       
404       // setting focus
405       gtk_widget_grab_focus (GTK_WIDGET(b_add->gtkobj()));
406
407       // connecting signals
408       b_add->clicked.connect(slot(this, &FormCitation::moveFromActionToSearch));
409       b_edit->clicked.connect(slot(this, &FormCitation::moveFromActionToEdit));
410
411       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
412
413       dialog_->destroy.connect(slot(this, &FormCitation::free));
414     }
415 }
416
417
418 void FormCitation::moveFromActionToSearch()
419 {
420   // stores configuration and cleans all widgets
421   storeWidgets();
422   cleanupWidgets();
423   
424   // moves to stage "search"
425   mainAppWin->remove_action();
426   showStageSearch();
427 }
428
429
430 void FormCitation::moveFromActionToEdit()
431 {
432   // stores configuration and cleans all widgets
433   storeWidgets();
434   cleanupWidgets();
435   
436   // moves to stage "edit"
437   mainAppWin->remove_action();
438   showStageEdit();
439 }
440
441
442 void FormCitation::showStageSearch()
443 {
444   if (!dialog_)
445     {
446       using namespace Gtk::Box_Helpers;
447
448       Gtk::Box * mbox = manage( new Gtk::HBox() );
449       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
450       Gtk::Separator * sep = manage( new Gtk::VSeparator() );
451       
452       search_text_ = manage( new Gnome::Entry() );
453       
454       button_regexp_ = manage( new Gtk::CheckButton(_("Use Regular Expression")) );
455
456       b_ok = manage( new Gtk::Button(_("Search")) );
457       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
458       
459       // set up spacing
460       mbox->set_spacing(4);
461       bbox->set_spacing(4);
462
463       // packing
464       bbox->children().push_back(Element(*b_ok, false, false));
465       bbox->children().push_back(Element(*b_cancel, false, false));
466
467       mbox->children().push_back(Element(*search_text_, true, true));
468       mbox->children().push_back(Element(*button_regexp_, false, false));
469       mbox->children().push_back(Element(*sep, false, false));
470       mbox->children().push_back(Element(*bbox, false, false));
471
472       // packing dialog to main window
473       dialog_ = mbox;
474       mainAppWin->add_action(*dialog_, _(" Insert Citation: Enter keyword(s) or regular expression "));
475
476       initWidgets();
477       
478       // setting focus
479       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(search_text_->get_entry()->gtkobj()), GTK_CAN_DEFAULT);
480       gtk_widget_grab_focus (GTK_WIDGET(search_text_->get_entry()->gtkobj()));
481       gtk_widget_grab_default (GTK_WIDGET(search_text_->get_entry()->gtkobj()));
482
483       // connecting signals
484       b_ok->clicked.connect(SigC::slot(this, &FormCitation::moveFromSearchToSelect));
485       search_text_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::moveFromSearchToSelect));
486
487       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
488       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
489     }
490 }
491
492 void FormCitation::moveFromSearchToSelect()
493 {
494   search_string_ = search_text_->get_entry()->get_text();
495   use_regexp_ = button_regexp_->get_active();
496
497   // stores configuration and cleans all widgets
498   storeWidgets();
499   cleanupWidgets();
500   
501   // moves to stage "select"
502   mainAppWin->remove_action();
503   showStageSelect();
504 }
505
506 void FormCitation::showStageSelect()
507 {
508   if (!dialog_)
509     {
510       using namespace Gtk::Box_Helpers;
511
512       Gtk::Box * mbox = manage( new Gtk::VBox() );
513       Gtk::Box * tbox = manage( new Gtk::HBox() );
514       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
515       Gtk::Separator * sep = manage( new Gtk::HSeparator() );
516       Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
517
518       info_ = manage( new Gnome::Less() );
519       paned_info_ = manage( new Gtk::HPaned() );
520       text_after_ = manage( new Gnome::Entry() );
521       
522       b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
523       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
524
525       sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
526
527       // constructing CList
528       vector<string> colnames;
529       colnames.push_back("INVISIBLE");
530       colnames.push_back(_("Key"));
531       colnames.push_back(_("Author(s)"));
532       colnames.push_back(_("Title"));
533       colnames.push_back(_("Year"));
534       colnames.push_back(_("Journal"));
535       clist_bib_ = manage( new Gtk::CList(colnames) );
536
537       bbox->set_layout(GTK_BUTTONBOX_END);
538       
539       // set up spacing
540       mbox->set_spacing(4);
541       bbox->set_spacing(4);
542       tbox->set_spacing(4);
543
544       // packing widgets
545       sw->add(*clist_bib_);
546       paned_info_->add1(*sw);
547       paned_info_->add2(*info_);
548
549       bbox->children().push_back(Element(*b_ok, false, false));
550       bbox->children().push_back(Element(*b_cancel, false, false));
551
552       tbox->children().push_back(Element(*manage(new Gtk::Label(_("Text after"))), false, false));
553       tbox->children().push_back(Element(*text_after_, true, true));
554       tbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
555       tbox->children().push_back(Element(*bbox, false, false));
556
557       mbox->children().push_back(Element(*paned_info_,true,true));
558       mbox->children().push_back(Element(*sep, false, false));
559       mbox->children().push_back(Element(*tbox, false, false));
560       
561       // packing dialog to main window
562       dialog_ = mbox;
563       mainAppWin->add_action(*dialog_, _(" Insert Citation: Select citation "), true);
564
565       initWidgets();
566       
567       // setting focus
568       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
569       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
570       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(clist_bib_->gtkobj()), GTK_CAN_DEFAULT);
571       gtk_widget_grab_focus (GTK_WIDGET(clist_bib_->gtkobj()));
572       gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
573
574       // connecting signals
575       b_ok->clicked.connect(SigC::slot(this, &FormCitation::applySelect));
576       text_after_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::applySelect));
577
578       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
579
580       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
581
582       clist_bib_->click_column.connect(SigC::slot(this, &FormCitation::sortBibList));
583       clist_bib_->select_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
584                                           true, false));
585       clist_bib_->unselect_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
586                                             false, false));
587     }
588 }
589
590 void FormCitation::showStageEdit()
591 {
592   if (!dialog_)
593     {
594       using namespace Gtk::Box_Helpers;
595
596       Gtk::Box * mbox = manage( new Gtk::VBox() );
597       Gtk::Box * tbox = manage( new Gtk::HBox() );
598       Gtk::Box * t2box = manage( new Gtk::HBox() );
599       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
600       Gtk::ButtonBox * actbbox = manage( new Gtk::VButtonBox() );
601       Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
602
603       vector<string> colnames;
604       colnames.push_back(" ");
605       clist_selected_ = manage( new Gtk::CList(colnames) );
606       clist_selected_->column_titles_hide();
607       
608       text_after_ = manage( new Gnome::Entry() );
609       
610       button_unselect_ = manage( new Gnome::PixButton( _("_Remove"), GNOME_STOCK_PIXMAP_TRASH ) );
611       button_up_ = manage( new Gnome::PixButton( _("_Up"), GNOME_STOCK_PIXMAP_UP ) );
612       button_down_ = manage( new Gnome::PixButton( _("_Down"), GNOME_STOCK_PIXMAP_DOWN ) );
613
614       b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
615       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
616
617       sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
618
619       bbox->set_layout(GTK_BUTTONBOX_END);
620       actbbox->set_layout(GTK_BUTTONBOX_START);
621       
622       // set up spacing
623       mbox->set_spacing(4);
624       bbox->set_spacing(4);
625       actbbox->set_spacing(4);
626       tbox->set_spacing(4);
627       t2box->set_spacing(4);
628
629       // packing widgets
630       sw->add(*clist_selected_);
631
632       bbox->children().push_back(Element(*b_ok, false, false));
633       bbox->children().push_back(Element(*b_cancel, false, false));
634
635       actbbox->children().push_back(Element(*button_unselect_, false, false));
636       actbbox->children().push_back(Element(*button_up_, false, false));
637       actbbox->children().push_back(Element(*button_down_, false, false));
638
639       t2box->children().push_back(Element(*sw, true, true));
640       t2box->children().push_back(Element(*actbbox, false, false));
641
642       tbox->children().push_back(Element(*manage(new Gtk::Label(_("Text after"))), false, false));
643       tbox->children().push_back(Element(*text_after_, true, true));
644       tbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
645       tbox->children().push_back(Element(*bbox, false, false));
646
647       mbox->children().push_back(Element(*t2box,true,true));
648       mbox->children().push_back(Element(*manage(new Gtk::HSeparator()), false, false));
649       mbox->children().push_back(Element(*tbox, false, false));
650       
651       // accelerators
652       Gtk::AccelGroup * accel = Gtk::AccelGroup::create();
653
654       button_unselect_->add_accelerator("clicked", *accel, button_unselect_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
655       button_up_->add_accelerator("clicked", *accel, button_up_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
656       button_down_->add_accelerator("clicked", *accel, button_down_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
657
658       // packing dialog to main window
659       dialog_ = mbox;
660       mainAppWin->add_action(*dialog_, _(" Citation: Edit "), true, accel);
661
662       initWidgets();
663
664       // setting focus
665       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
666       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
667       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(clist_selected_->gtkobj()), GTK_CAN_DEFAULT);
668       gtk_widget_grab_focus (GTK_WIDGET(clist_selected_->gtkobj()));
669       gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
670
671       // connecting signals
672       b_ok->clicked.connect(SigC::slot(this, &FormCitation::applyEdit));
673       text_after_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::applyEdit));
674
675       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
676
677       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
678
679       button_unselect_->clicked.connect(SigC::slot(this, &FormCitation::removeCitation));
680       button_up_->clicked.connect(SigC::slot(this, &FormCitation::moveCitationUp));
681       button_down_->clicked.connect(SigC::slot(this, &FormCitation::moveCitationDown));      
682
683       clist_selected_->select_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
684                                           true, true));
685       clist_selected_->unselect_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
686                                             false, true));
687     }
688 }
689
690
691 void FormCitation::addItemToBibList(int i)
692 {
693   vector<string> r;
694   string key, info;
695   string val;
696
697   key = bibkeys[i];
698   info = bibkeysInfo[i];
699
700   // don't change the order of these first two items:
701   // callback functions depend on the data stored in the first column (its hided)
702   // and in the second column (shown to user)
703   r.push_back( tostr(i) ); 
704   r.push_back( key );
705   
706   // this can be changed (configured by user?)
707   parseBibTeX( info, "author", val);  r.push_back(val);
708   parseBibTeX( info, "title", val);  r.push_back(val);
709   parseBibTeX( info, "year", val);  r.push_back(val);
710   parseBibTeX( info, "journal", val);  r.push_back(val);
711   
712   clist_bib_->rows().push_back(r);
713 }
714
715
716 void FormCitation::updateButtons()
717 {
718   if (button_unselect_ != 0) // => button_up_ and button_down_ are != 0
719     {
720       bool sens;
721
722       sens = (clist_selected_->selection().size()>0);
723       button_unselect_->set_sensitive(sens);
724       button_up_->set_sensitive(sens &&
725                                 clist_selected_->selection()[0].get_row_num()>0);
726       button_down_->set_sensitive(sens &&
727                                   clist_selected_->selection()[0].get_row_num() <
728                                   clist_selected_->rows().size()-1);
729     }
730 }
731
732
733 void FormCitation::updateSlot(bool buffchanged)
734 {
735   if (buffchanged) hide();
736 }
737
738
739 void FormCitation::selectionToggled(gint            row,
740                                     gint            ,//column,
741                                     GdkEvent        * ,//event,
742                                     bool selected,
743                                     bool citeselected)
744 {
745   if (!citeselected)
746     {
747       if (selected)
748         {
749           bool keyfound = false;
750           string info;
751           
752           // the first column in clist_bib_ contains the index
753           keyfound = true;
754           info = bibkeysInfo[ strToInt(clist_bib_->cell(row,0).get_text()) ];
755
756           if (keyfound)
757             info_->show_string(info);
758           else
759             info_->show_string(_("--- No such key in the database ---"));
760         }
761       else
762         {
763           info_->show_string("");
764         }
765     }
766
767   updateButtons();
768 }
769
770 void FormCitation::removeCitation()
771 {
772   clist_selected_->rows().remove(clist_selected_->selection()[0]);
773   updateButtons();
774 }
775
776 void FormCitation::moveCitationUp()
777 {
778   int i = clist_selected_->selection()[0].get_row_num();
779   clist_selected_->swap_rows( i-1, i );
780   clist_selected_->row(i-1).select();
781   updateButtons();
782 }
783
784 void FormCitation::moveCitationDown()
785 {
786   int i = clist_selected_->selection()[0].get_row_num();
787   clist_selected_->swap_rows( i+1, i );
788   clist_selected_->row(i+1).select();
789   updateButtons();
790 }
791
792 void FormCitation::hide()
793 {
794   if (dialog_!=0) mainAppWin->remove_action();
795 }
796
797 void FormCitation::free()
798 {
799   if (dialog_!=0)
800     {
801       // cleaning up
802       cleanupWidgets();
803       u_.disconnect();
804       h_.disconnect();
805       inset_ = 0;
806       ih_.disconnect();
807     }
808 }
809
810 void FormCitation::applySelect()
811 {
812   if( lv_->buffer()->isReadonly() ) return;
813
814   string contents = frontStrip( strip(params.getContents()) );
815   if (!contents.empty()) contents += ", ";
816   
817   int sz = clist_bib_->selection().size();
818   for (int i=0; i < sz; ++i)
819     {
820       if (i > 0) contents += ", ";
821       contents += clist_bib_->selection()[i][1].get_text();
822     }
823   
824   params.setContents( contents );
825   params.setOptions( text_after_->get_entry()->get_text() );
826
827   if( inset_ != 0 )
828     {
829       // Only update if contents have changed
830       if( params != inset_->params() )
831         {
832           inset_->setParams( params );
833           lv_->view()->updateInset( inset_, true );
834         }
835     }
836   else
837     {
838       lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
839                                    params.getAsString() );
840     }
841
842   // close dialog
843   storeWidgets();
844   hide();
845 }
846
847 void FormCitation::applyEdit()
848 {
849   if( lv_->buffer()->isReadonly() ) return;
850
851   string contents;
852   int const sz = clist_selected_->rows().size();
853   for( int i = 0; i < sz; ++i )
854     {
855       if (i > 0) contents += ", ";
856       contents += clist_selected_->cell(i, 0).get_text();
857     }
858
859   params.setContents( contents );
860   params.setOptions( text_after_->get_entry()->get_text() );
861
862   if( inset_ != 0 )
863     {
864       // Only update if contents have changed
865       if( params != inset_->params() )
866         {
867           inset_->setParams( params );
868           lv_->view()->updateInset( inset_, true );
869         }
870     }
871   else
872     {
873       lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
874                                    params.getAsString() );
875     }
876   
877   // close dialog
878   storeWidgets();
879   hide();
880 }
881
882 void FormCitation::sortBibList(gint col)
883 {
884   clist_bib_->set_sort_column(col);
885   clist_bib_->sort();
886 }
887
888 void FormCitation::search()
889 {
890   if (use_regexp_) searchReg();
891   else searchSimple();
892 }
893
894 // looking for entries which match regexp
895 void FormCitation::searchReg()
896 {
897   string rexptxt(search_string_);
898   rexptxt = frontStrip( strip( rexptxt ) );
899   
900   LRegex reg(rexptxt);
901
902   // populating clist_bib_
903   clist_bib_->rows().clear();
904
905   clist_bib_->freeze();
906
907   int const sz = bibkeys.size();
908   bool additem;
909   for ( int i = 0; i < sz; ++i )
910     {
911       string const data = bibkeys[i] + bibkeysInfo[i];
912
913       if (rexptxt.empty()) additem = true;
914       else additem = (reg.exec(data).size() > 0);
915              
916       if ( additem ) addItemToBibList(i);
917     }
918
919   clist_bib_->sort();
920   clist_bib_->thaw();
921 }
922
923 // looking for entries which contain all the words specified in search_text entry
924 void FormCitation::searchSimple()
925 {
926   vector<string> searchwords;
927   string tmp;
928   string stext(search_string_);
929   stext = frontStrip( strip( stext ) );
930   stext = frontStrip( split(stext, tmp, ' ') );
931   while ( !tmp.empty() )
932     {
933       searchwords.push_back(tmp);
934       stext = frontStrip( split(stext, tmp, ' ') );
935     }
936   
937   // populating clist_bib_
938   clist_bib_->rows().clear();
939
940   clist_bib_->freeze();
941
942   int const sz = bibkeys.size();
943   for (int i = 0; i < sz; ++i) {
944       string const data = bibkeys[i] + bibkeysInfo[i];
945
946       bool additem = true;
947
948       int const szs = searchwords.size();
949       for (int j = 0; additem && j < szs; ++j)
950               if (data.find(searchwords[j]) == string::npos)
951                       additem = false;
952       
953       if (additem) addItemToBibList(i);
954     }
955
956   clist_bib_->sort();
957   clist_bib_->thaw();
958 }