]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormCitation.C
Angus's FormInset work; Dekel's languages patch; my reworking of Angus's stuff +...
[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 #ifdef SIGC_CXX_NAMESPACES
53 using SigC::slot;
54 using SigC::bind;
55 #endif
56
57
58 // configuration keys
59 static string const  LOCAL_CONFIGURE_PREFIX("FormCitation");
60
61 static string const  CONF_PANE_INFO("paneinfo");
62 static string const  CONF_PANE_INFO_DEFAULT("=300");
63
64 static string const  CONF_COLUMN("column");
65 static string const  CONF_COLUMN_DEFAULT("=50");
66
67 static string const  CONF_REGEXP("regexp");
68 static string const  CONF_REGEXP_DEFAULT("=0");
69
70 static string const CONF_SEARCH("FormCitation_search");
71 static string const CONF_TEXTAFTER("FormCitation_textafter");
72
73 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
74   : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0)
75 {
76   // let the dialog be shown
77   // These are permanent connections so we won't bother
78   // storing a copy because we won't be disconnecting.
79   d->showCitation.connect(slot(this, &FormCitation::showInset));
80   d->createCitation.connect(slot(this, &FormCitation::createInset));
81
82   cleanupWidgets();
83 }
84
85 FormCitation::~FormCitation()
86 {
87   hide();
88 }
89
90 void FormCitation::showInset( InsetCommand * const inset )
91 {
92   if( dialog_!=NULL || inset == 0 ) return;
93   
94   inset_ = inset;
95   ih_ = inset_->hide.connect(slot(this, &FormCitation::hide));
96
97 #ifdef WITH_WARNINGS
98 #warning This connection to update will do nothing!
99 #endif
100   u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
101   h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
102   
103   params = inset->params();
104
105   if ( params.getContents().empty() ) showStageSearch();
106   else showStageAction();
107 }
108
109
110 void FormCitation::createInset( string const & arg )
111 {
112   if( dialog_!=NULL ) return;
113   
114   u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
115   h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
116   
117   params.setFromString( arg );
118   showStageSearch();
119 }
120
121
122 static
123 void parseBibTeX(string data,
124                  string const & findkey,
125                  string & keyvalue)
126 {
127   unsigned int i;
128
129   keyvalue = "";
130   
131   for (i=0; i<data.length(); ++i)
132     if (data[i]=='\n' || data[i]=='\t')
133       data[i] = ' ';
134   
135   data = frontStrip(data);
136   while (!data.empty()
137          && data[0]!='='
138          && (data.find(' ')!=string::npos ||
139              data.find('=')!=string::npos) )
140     {
141       unsigned int keypos = min(data.find(' '), data.find('='));
142       string key = lowercase( data.substr(0, keypos) );
143       string value, tmp;
144       char enclosing;
145       
146       data = data.substr(keypos, data.length()-1);
147       data = frontStrip(strip(data));
148       if (data.length() > 1 && data[0]=='=')
149         {
150           data = frontStrip(data.substr(1, data.length()-1));
151           if (!data.empty())
152             {
153               keypos = 1;
154               if (data[0]=='{') enclosing = '}';
155               else if (data[0]=='"') enclosing = '"';
156               else { keypos=0; enclosing=' '; }
157
158               if (keypos &&
159                   data.find(enclosing)!=string::npos &&
160                   data.length()>1)
161                 {
162                   tmp = data.substr(keypos, data.length()-1);
163                   while (tmp.find('{')!=string::npos &&
164                          tmp.find('}')!=string::npos &&
165                          tmp.find('{') < tmp.find('}') &&
166                          tmp.find('{') < tmp.find(enclosing))
167                     {
168                       keypos += tmp.find('{')+1;
169                       tmp = data.substr(keypos, data.length()-1);
170                       keypos += tmp.find('}')+1;
171                       tmp = data.substr(keypos, data.length()-1);
172                     }
173
174                   if (tmp.find(enclosing)==string::npos) return;
175                   else
176                     {
177                       keypos += tmp.find(enclosing);
178                       tmp = data.substr(keypos, data.length()-1);
179                     }
180
181                   value = data.substr(1, keypos-1);
182
183                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
184                   else data = "";
185                 }
186               else if (!keypos &&
187                        (data.find(' ') ||
188                         data.find(','))
189                        ) // numerical value ?
190                 {
191                   keypos = data.length()-1;
192                   if (data.find(' ')!=string::npos) keypos = data.find(' ');
193                   if (data.find(',')!=string::npos &&
194                       keypos > data.find(','))
195                     keypos = data.find(',');
196
197                   value = data.substr(0, keypos);
198                   
199                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
200                   else data = "";
201                 }
202               else return;
203
204               if (findkey == key) { keyvalue = value; return; } 
205
206               data = frontStrip(frontStrip(data,','));
207             }
208         }
209       else return;
210     }
211 }
212
213
214 void FormCitation::cleanupWidgets()
215 {
216   dialog_ = NULL;
217   b_ok = NULL;
218   b_cancel = NULL;
219   search_text_ = NULL;
220   info_ = NULL;
221   text_after_ = NULL;
222   button_unselect_ = NULL;
223   button_up_ = NULL;
224   button_down_ = NULL;
225   button_regexp_ = NULL;
226   clist_selected_ = NULL;
227   clist_bib_ = NULL;
228   paned_info_ = NULL;
229 }
230
231
232 void FormCitation::initWidgets()
233 {
234   string const path = PACKAGE "/" + LOCAL_CONFIGURE_PREFIX;
235
236   if (search_text_ != NULL)
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_ != NULL )
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_ != NULL)
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_ != NULL)
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_ != NULL)
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, NULL, true, false);
299         }
300     }
301
302   if (clist_selected_ != NULL)
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, NULL, 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_ != NULL) search_text_->save_history();
335
336   if (text_after_ != NULL) text_after_->save_history();
337
338   if (button_regexp_ != NULL)
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_ != NULL) 
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_ != NULL)
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 = N_("_Add new citation");
375       string const editlabel = N_("_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_, N_(" 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(N_("Use Regular Expression")) );
455
456       b_ok = manage( new Gtk::Button(N_("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_, N_(" 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(slot(this, &FormCitation::moveFromSearchToSelect));
485       search_text_->get_entry()->activate.connect(slot(this, &FormCitation::moveFromSearchToSelect));
486
487       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
488       dialog_->destroy.connect(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(N_("Key"));
531       colnames.push_back(N_("Author(s)"));
532       colnames.push_back(N_("Title"));
533       colnames.push_back(N_("Year"));
534       colnames.push_back(N_("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(N_("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_, N_(" 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(slot(this, &FormCitation::applySelect));
576       text_after_->get_entry()->activate.connect(slot(this, &FormCitation::applySelect));
577
578       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
579
580       dialog_->destroy.connect(slot(this, &FormCitation::free));
581
582       clist_bib_->click_column.connect(slot(this, &FormCitation::sortBibList));
583       clist_bib_->select_row.connect(bind(slot(this, &FormCitation::selectionToggled),
584                                           true, false));
585       clist_bib_->unselect_row.connect(bind(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( N_("_Remove"), GNOME_STOCK_PIXMAP_TRASH ) );
611       button_up_ = manage( new Gnome::PixButton( N_("_Up"), GNOME_STOCK_PIXMAP_UP ) );
612       button_down_ = manage( new Gnome::PixButton( N_("_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(N_("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_, N_(" 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(slot(this, &FormCitation::applyEdit));
673       text_after_->get_entry()->activate.connect(slot(this, &FormCitation::applyEdit));
674
675       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
676
677       dialog_->destroy.connect(slot(this, &FormCitation::free));
678
679       button_unselect_->clicked.connect(slot(this, &FormCitation::removeCitation));
680       button_up_->clicked.connect(slot(this, &FormCitation::moveCitationUp));
681       button_down_->clicked.connect(slot(this, &FormCitation::moveCitationDown));      
682
683       clist_selected_->select_row.connect(bind(slot(this, &FormCitation::selectionToggled),
684                                           true, true));
685       clist_selected_->unselect_row.connect(bind(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_ != NULL) // => button_up_ and button_down_ are != NULL
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 void FormCitation::selectionToggled(gint            row,
733                                     gint            ,//column,
734                                     GdkEvent        * ,//event,
735                                     bool selected,
736                                     bool citeselected)
737 {
738   if (!citeselected)
739     {
740       if (selected)
741         {
742           bool keyfound = false;
743           string info;
744           
745           // the first column in clist_bib_ contains the index
746           keyfound = true;
747           info = bibkeysInfo[ strToInt(clist_bib_->cell(row,0).get_text()) ];
748
749           if (keyfound)
750             info_->show_string(info);
751           else
752             info_->show_string(N_("--- No such key in the database ---"));
753         }
754       else
755         {
756           info_->show_string("");
757         }
758     }
759
760   updateButtons();
761 }
762
763 void FormCitation::removeCitation()
764 {
765   clist_selected_->rows().remove(clist_selected_->selection()[0]);
766   updateButtons();
767 }
768
769 void FormCitation::moveCitationUp()
770 {
771   int i = clist_selected_->selection()[0].get_row_num();
772   clist_selected_->swap_rows( i-1, i );
773   clist_selected_->row(i-1).select();
774   updateButtons();
775 }
776
777 void FormCitation::moveCitationDown()
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::hide()
786 {
787   if (dialog_!=NULL) mainAppWin->remove_action();
788 }
789
790 void FormCitation::free()
791 {
792   if (dialog_!=NULL)
793     {
794       // cleaning up
795       cleanupWidgets();
796       u_.disconnect();
797       h_.disconnect();
798       inset_ = 0;
799       ih_.disconnect();
800     }
801 }
802
803 void FormCitation::applySelect()
804 {
805   if( lv_->buffer()->isReadonly() ) return;
806
807   string contents;
808   int sz;
809
810   contents = frontStrip( strip(params.getContents()) );
811   if (!contents.empty()) contents += ", ";
812   
813   sz = clist_bib_->selection().size();
814   for (int i=0; i < sz; ++i)
815     {
816       if (i > 0) contents += ", ";
817       contents += clist_bib_->selection()[i][1].get_text();
818     }
819   
820   params.setContents( contents );
821   params.setOptions( text_after_->get_entry()->get_text() );
822
823   if( inset_ != 0 )
824     {
825       // Only update if contents have changed
826       if( params != inset_->params() )
827         {
828           inset_->setParams( params );
829           lv_->view()->updateInset( inset_, true );
830         }
831     }
832   else
833     {
834       lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
835                                    params.getAsString() );
836     }
837
838   // close dialog
839   storeWidgets();
840   hide();
841 }
842
843 void FormCitation::applyEdit()
844 {
845   if( lv_->buffer()->isReadonly() ) return;
846
847   string contents;
848   int const sz = clist_selected_->rows().size();
849   for( int i = 0; i < sz; ++i )
850     {
851       if (i > 0) contents += ", ";
852       contents += clist_selected_->cell(i, 0).get_text();
853     }
854
855   params.setContents( contents );
856   params.setOptions( text_after_->get_entry()->get_text() );
857
858   if( inset_ != 0 )
859     {
860       // Only update if contents have changed
861       if( params != inset_->params() )
862         {
863           inset_->setParams( params );
864           lv_->view()->updateInset( inset_, true );
865         }
866     }
867   else
868     {
869       lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
870                                    params.getAsString() );
871     }
872   
873   // close dialog
874   storeWidgets();
875   hide();
876 }
877
878 void FormCitation::sortBibList(gint col)
879 {
880   clist_bib_->set_sort_column(col);
881   clist_bib_->sort();
882 }
883
884 void FormCitation::search()
885 {
886   if (use_regexp_) searchReg();
887   else searchSimple();
888 }
889
890 // looking for entries which match regexp
891 void FormCitation::searchReg()
892 {
893   string tmp, rexptxt( search_string_ );
894   rexptxt = frontStrip( strip( rexptxt ) );
895   
896   LRegex reg(rexptxt);
897
898   // populating clist_bib_
899   clist_bib_->rows().clear();
900
901   clist_bib_->freeze();
902
903   int const sz = bibkeys.size();
904   bool additem;
905   for ( int i = 0; i < sz; ++i )
906     {
907       string data = bibkeys[i] + bibkeysInfo[i];
908
909       if (rexptxt.empty()) additem = true;
910       else additem = (reg.exec(data).size() > 0);
911              
912       if ( additem ) addItemToBibList(i);
913     }
914
915   clist_bib_->sort();
916   clist_bib_->thaw();
917 }
918
919 // looking for entries which contain all the words specified in search_text entry
920 void FormCitation::searchSimple()
921 {
922   vector<string> searchwords;
923   string tmp, stext( search_string_ );
924   stext = frontStrip( strip( stext ) );
925   stext = frontStrip( split(stext, tmp, ' ') );
926   while( !tmp.empty() )
927     {
928       searchwords.push_back(tmp);
929       stext = frontStrip( split(stext, tmp, ' ') );
930     }
931   
932   // populating clist_bib_
933   clist_bib_->rows().clear();
934
935   clist_bib_->freeze();
936
937   int const sz = bibkeys.size();
938   bool additem;
939   for ( int i = 0; i < sz; ++i )
940     {
941       string data = bibkeys[i] + bibkeysInfo[i];
942
943       additem = true;
944
945       int j, szs;
946       for (j = 0, szs = searchwords.size();
947            additem && j < szs; ++j )
948         if ( data.find(searchwords[j]) == string::npos )
949           additem = false;
950              
951       if ( additem ) addItemToBibList(i);
952     }
953
954   clist_bib_->sort();
955   clist_bib_->thaw();
956 }