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