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