]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormCitation.C
remove CXX_WORKING_NAMESPACES
[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 // configuration keys
53 static string const  LOCAL_CONFIGURE_PREFIX("FormCitation");
54
55 static string const  CONF_PANE_INFO("paneinfo");
56 static string const  CONF_PANE_INFO_DEFAULT("=300");
57
58 static string const  CONF_COLUMN("column");
59 static string const  CONF_COLUMN_DEFAULT("=50");
60
61 static string const  CONF_REGEXP("regexp");
62 static string const  CONF_REGEXP_DEFAULT("=0");
63
64 static string const CONF_SEARCH("FormCitation_search");
65 static string const CONF_TEXTAFTER("FormCitation_textafter");
66
67 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
68   : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0)
69 {
70   // let the dialog be shown
71   // These are permanent connections so we won't bother
72   // storing a copy because we won't be disconnecting.
73   d->showCitation.connect(SigC::slot(this, &FormCitation::showInset));
74   d->createCitation.connect(SigC::slot(this, &FormCitation::createInset));
75
76   cleanupWidgets();
77 }
78
79 FormCitation::~FormCitation()
80 {
81   hide();
82 }
83
84 void FormCitation::showInset( InsetCommand * const inset )
85 {
86   if( dialog_!=0 || inset == 0 ) return;
87   
88   inset_ = inset;
89   ih_ = inset_->hideDialog.connect(SigC::slot(this, &FormCitation::hide));
90
91   u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormCitation::updateSlot));
92   h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormCitation::hide));
93   
94   params = inset->params();
95
96   if ( params.getContents().empty() ) showStageSearch();
97   else showStageAction();
98 }
99
100
101 void FormCitation::createInset( string const & arg )
102 {
103   if( dialog_!=0 ) return;
104   
105   u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormCitation::updateSlot)); 
106   h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormCitation::hide));
107   
108   params.setFromString( arg );
109   showStageSearch();
110 }
111
112
113 static
114 void parseBibTeX(string const & dat,
115                  string const & findkey,
116                  string & keyvalue)
117 {
118   unsigned int i;
119   string data(dat);
120   
121   keyvalue = "";
122   
123   for (i=0; i<data.length(); ++i)
124     if (data[i]=='\n' || data[i]=='\t')
125       data[i] = ' ';
126   
127   data = frontStrip(data);
128   while (!data.empty()
129          && data[0]!='='
130          && (data.find(' ')!=string::npos ||
131              data.find('=')!=string::npos) )
132     {
133       unsigned int keypos = min(data.find(' '), data.find('='));
134       string key = lowercase( data.substr(0, keypos) );
135       string value;
136       string tmp;
137       char enclosing;
138       
139       data = data.substr(keypos, data.length()-1);
140       data = frontStrip(strip(data));
141       if (data.length() > 1 && data[0]=='=')
142         {
143           data = frontStrip(data.substr(1, data.length()-1));
144           if (!data.empty())
145             {
146               keypos = 1;
147               if (data[0]=='{') enclosing = '}';
148               else if (data[0]=='"') enclosing = '"';
149               else { keypos=0; enclosing=' '; }
150
151               if (keypos &&
152                   data.find(enclosing)!=string::npos &&
153                   data.length()>1)
154                 {
155                   tmp = data.substr(keypos, data.length()-1);
156                   while (tmp.find('{')!=string::npos &&
157                          tmp.find('}')!=string::npos &&
158                          tmp.find('{') < tmp.find('}') &&
159                          tmp.find('{') < tmp.find(enclosing))
160                     {
161                       keypos += tmp.find('{')+1;
162                       tmp = data.substr(keypos, data.length()-1);
163                       keypos += tmp.find('}')+1;
164                       tmp = data.substr(keypos, data.length()-1);
165                     }
166
167                   if (tmp.find(enclosing)==string::npos) return;
168                   else
169                     {
170                       keypos += tmp.find(enclosing);
171                       tmp = data.substr(keypos, data.length()-1);
172                     }
173
174                   value = data.substr(1, keypos-1);
175
176                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
177                   else data = "";
178                 }
179               else if (!keypos &&
180                        (data.find(' ') ||
181                         data.find(','))
182                        ) // numerical value ?
183                 {
184                   keypos = data.length()-1;
185                   if (data.find(' ')!=string::npos) keypos = data.find(' ');
186                   if (data.find(',')!=string::npos &&
187                       keypos > data.find(','))
188                     keypos = data.find(',');
189
190                   value = data.substr(0, keypos);
191                   
192                   if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
193                   else data = "";
194                 }
195               else return;
196
197               if (findkey == key) { keyvalue = value; return; } 
198
199               data = frontStrip(frontStrip(data,','));
200             }
201         }
202       else return;
203     }
204 }
205
206
207 void FormCitation::cleanupWidgets()
208 {
209   dialog_ = 0;
210   b_ok = 0;
211   b_cancel = 0;
212   search_text_ = 0;
213   info_ = 0;
214   text_after_ = 0;
215   button_unselect_ = 0;
216   button_up_ = 0;
217   button_down_ = 0;
218   button_regexp_ = 0;
219   clist_selected_ = 0;
220   clist_bib_ = 0;
221   paned_info_ = 0;
222 }
223
224
225 void FormCitation::initWidgets()
226 {
227   string const path = PACKAGE "/" + LOCAL_CONFIGURE_PREFIX;
228
229   if (search_text_ != 0)
230     {
231       search_text_->set_history_id(CONF_SEARCH);
232       search_text_->set_max_saved(10);
233       search_text_->load_history();
234       search_text_->set_use_arrows_always(true);
235     }
236
237   if (text_after_ != 0 )
238     {
239       text_after_->set_history_id(CONF_TEXTAFTER);
240       text_after_->set_max_saved(10);
241       text_after_->load_history();
242       text_after_->set_use_arrows_always(true);
243       text_after_->get_entry()->set_text(params.getOptions());
244     }
245
246   if (button_regexp_ != 0)
247     {
248       string w = path + "/" + CONF_REGEXP + CONF_REGEXP_DEFAULT;
249       button_regexp_->set_active( (gnome_config_get_int(w.c_str()) > 0) );
250     }
251
252   if (paned_info_ != 0)
253     {
254       string w = path + "/" + CONF_PANE_INFO + CONF_PANE_INFO_DEFAULT;
255       paned_info_->set_position( gnome_config_get_int(w.c_str()) );
256     }
257
258   if (clist_bib_ != 0)
259     {
260       // preferences
261       clist_bib_->column(0).set_visiblity(false);
262       clist_bib_->set_selection_mode(GTK_SELECTION_BROWSE);
263
264       // setting up sizes of columns
265       string w;
266       int sz = clist_bib_->columns().size();
267       for (int i = 0; i < sz; ++i)
268         {
269           w = path + "/" + CONF_COLUMN + "_" + tostr(i) + CONF_COLUMN_DEFAULT;
270           clist_bib_->column(i).set_width( gnome_config_get_int(w.c_str()) );
271         }
272       
273       // retrieving data
274       vector<pair<string,string> > blist = lv_->buffer()->getBibkeyList();
275
276       sz = blist.size();
277       for (int i = 0; i < sz; ++i )
278         {
279           bibkeys.push_back(blist[i].first);
280           bibkeysInfo.push_back(blist[i].second);
281         }
282       
283       blist.clear();      
284       
285       // updating list
286       search();
287
288       if (clist_bib_->rows().size() > 0)
289         {
290           clist_bib_->rows()[0].select();
291           selectionToggled(0, 0, 0, true, false);
292         }
293     }
294
295   if (clist_selected_ != 0)
296     {
297       clist_selected_->set_selection_mode(GTK_SELECTION_BROWSE);
298
299       // populating clist_selected_
300       vector<string> r;
301       string tmp, keys( params.getContents() );
302       keys = frontStrip( split(keys, tmp, ',') );
303       while( !tmp.empty() )
304         {
305           r.clear();
306           r.push_back(tmp);
307           clist_selected_->rows().push_back(r);
308           
309           keys = frontStrip( split(keys, tmp, ',') );
310         }
311
312       if (clist_selected_->rows().size() > 0)
313         {
314           clist_selected_->rows()[0].select();
315           selectionToggled(0, 0, 0, true, true);
316         }
317     }
318
319   updateButtons();
320 }
321
322
323 void FormCitation::storeWidgets()
324 {
325   string const path = PACKAGE "/" + LOCAL_CONFIGURE_PREFIX;
326
327   if (search_text_ != 0) search_text_->save_history();
328
329   if (text_after_ != 0) text_after_->save_history();
330
331   if (button_regexp_ != 0)
332     {
333       string w = path + "/" + CONF_REGEXP;
334       gnome_config_set_int(w.c_str(), button_regexp_->get_active());
335     }
336
337   if (paned_info_ != 0) 
338     {
339       string w = path + "/" + CONF_PANE_INFO;
340       gnome_config_set_int(w.c_str(), paned_info_->width() - info_->width());
341     }
342
343   if (clist_bib_ != 0)
344     {
345       string w;
346       int const sz = clist_bib_->columns().size();
347       for (int i = 0; i < sz; ++i)
348         {
349           w = path + "/" + CONF_COLUMN + "_" + tostr(i);
350           gnome_config_set_int(w.c_str(), clist_bib_->get_column_width(i));
351         }
352     }
353
354   gnome_config_sync();
355 }
356
357
358 void FormCitation::showStageAction()
359 {
360   if (!dialog_)
361     {
362       using namespace Gtk::Box_Helpers;
363
364       Gtk::Alignment * mbox = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
365       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
366
367       string const addlabel = _("_Add new citation");
368       string const editlabel = _("_Edit/remove citation(s)");
369
370       Gnome::PixButton * b_add  = manage(new Gnome::PixButton(addlabel, GNOME_STOCK_PIXMAP_NEW));
371       Gnome::PixButton * b_edit = manage(new Gnome::PixButton(editlabel, GNOME_STOCK_PIXMAP_PROPERTIES));
372
373       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
374
375       // set up spacing
376       bbox->set_spacing(4);
377       bbox->set_layout(GTK_BUTTONBOX_SPREAD);
378       
379       bbox->children().push_back(Element(*b_add, false, false));
380       bbox->children().push_back(Element(*b_edit, false, false));
381       bbox->children().push_back(Element(*b_cancel, false, false));
382
383       mbox->add(*bbox);
384
385       // accelerators
386       Gtk::AccelGroup * accel = Gtk::AccelGroup::create();
387
388       b_add->add_accelerator("clicked", *accel, b_add->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
389       b_edit->add_accelerator("clicked", *accel, b_edit->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
390       
391       // packing dialog to main window
392       dialog_ = mbox;
393       mainAppWin->add_action(*dialog_, _(" Citation: Select action "), false, accel);
394
395       initWidgets();
396       
397       // setting focus
398       gtk_widget_grab_focus (GTK_WIDGET(b_add->gtkobj()));
399
400       // connecting signals
401       b_add->clicked.connect(slot(this, &FormCitation::moveFromActionToSearch));
402       b_edit->clicked.connect(slot(this, &FormCitation::moveFromActionToEdit));
403
404       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
405
406       dialog_->destroy.connect(slot(this, &FormCitation::free));
407     }
408 }
409
410
411 void FormCitation::moveFromActionToSearch()
412 {
413   // stores configuration and cleans all widgets
414   storeWidgets();
415   cleanupWidgets();
416   
417   // moves to stage "search"
418   mainAppWin->remove_action();
419   showStageSearch();
420 }
421
422
423 void FormCitation::moveFromActionToEdit()
424 {
425   // stores configuration and cleans all widgets
426   storeWidgets();
427   cleanupWidgets();
428   
429   // moves to stage "edit"
430   mainAppWin->remove_action();
431   showStageEdit();
432 }
433
434
435 void FormCitation::showStageSearch()
436 {
437   if (!dialog_)
438     {
439       using namespace Gtk::Box_Helpers;
440
441       Gtk::Box * mbox = manage( new Gtk::HBox() );
442       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
443       Gtk::Separator * sep = manage( new Gtk::VSeparator() );
444       
445       search_text_ = manage( new Gnome::Entry() );
446       
447       button_regexp_ = manage( new Gtk::CheckButton(_("Use Regular Expression")) );
448
449       b_ok = manage( new Gtk::Button(_("Search")) );
450       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
451       
452       // set up spacing
453       mbox->set_spacing(4);
454       bbox->set_spacing(4);
455
456       // packing
457       bbox->children().push_back(Element(*b_ok, false, false));
458       bbox->children().push_back(Element(*b_cancel, false, false));
459
460       mbox->children().push_back(Element(*search_text_, true, true));
461       mbox->children().push_back(Element(*button_regexp_, false, false));
462       mbox->children().push_back(Element(*sep, false, false));
463       mbox->children().push_back(Element(*bbox, false, false));
464
465       // packing dialog to main window
466       dialog_ = mbox;
467       mainAppWin->add_action(*dialog_, _(" Insert Citation: Enter keyword(s) or regular expression "));
468
469       initWidgets();
470       
471       // setting focus
472       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(search_text_->get_entry()->gtkobj()), GTK_CAN_DEFAULT);
473       gtk_widget_grab_focus (GTK_WIDGET(search_text_->get_entry()->gtkobj()));
474       gtk_widget_grab_default (GTK_WIDGET(search_text_->get_entry()->gtkobj()));
475
476       // connecting signals
477       b_ok->clicked.connect(SigC::slot(this, &FormCitation::moveFromSearchToSelect));
478       search_text_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::moveFromSearchToSelect));
479
480       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
481       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
482     }
483 }
484
485 void FormCitation::moveFromSearchToSelect()
486 {
487   search_string_ = search_text_->get_entry()->get_text();
488   use_regexp_ = button_regexp_->get_active();
489
490   // stores configuration and cleans all widgets
491   storeWidgets();
492   cleanupWidgets();
493   
494   // moves to stage "select"
495   mainAppWin->remove_action();
496   showStageSelect();
497 }
498
499 void FormCitation::showStageSelect()
500 {
501   if (!dialog_)
502     {
503       using namespace Gtk::Box_Helpers;
504
505       Gtk::Box * mbox = manage( new Gtk::VBox() );
506       Gtk::Box * tbox = manage( new Gtk::HBox() );
507       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
508       Gtk::Separator * sep = manage( new Gtk::HSeparator() );
509       Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
510
511       info_ = manage( new Gnome::Less() );
512       paned_info_ = manage( new Gtk::HPaned() );
513       text_after_ = manage( new Gnome::Entry() );
514       
515       b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
516       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
517
518       sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
519
520       // constructing CList
521       vector<string> colnames;
522       colnames.push_back("INVISIBLE");
523       colnames.push_back(_("Key"));
524       colnames.push_back(_("Author(s)"));
525       colnames.push_back(_("Title"));
526       colnames.push_back(_("Year"));
527       colnames.push_back(_("Journal"));
528       clist_bib_ = manage( new Gtk::CList(colnames) );
529
530       bbox->set_layout(GTK_BUTTONBOX_END);
531       
532       // set up spacing
533       mbox->set_spacing(4);
534       bbox->set_spacing(4);
535       tbox->set_spacing(4);
536
537       // packing widgets
538       sw->add(*clist_bib_);
539       paned_info_->add1(*sw);
540       paned_info_->add2(*info_);
541
542       bbox->children().push_back(Element(*b_ok, false, false));
543       bbox->children().push_back(Element(*b_cancel, false, false));
544
545       tbox->children().push_back(Element(*manage(new Gtk::Label(_("Text after"))), false, false));
546       tbox->children().push_back(Element(*text_after_, true, true));
547       tbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
548       tbox->children().push_back(Element(*bbox, false, false));
549
550       mbox->children().push_back(Element(*paned_info_,true,true));
551       mbox->children().push_back(Element(*sep, false, false));
552       mbox->children().push_back(Element(*tbox, false, false));
553       
554       // packing dialog to main window
555       dialog_ = mbox;
556       mainAppWin->add_action(*dialog_, _(" Insert Citation: Select citation "), true);
557
558       initWidgets();
559       
560       // setting focus
561       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
562       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
563       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(clist_bib_->gtkobj()), GTK_CAN_DEFAULT);
564       gtk_widget_grab_focus (GTK_WIDGET(clist_bib_->gtkobj()));
565       gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
566
567       // connecting signals
568       b_ok->clicked.connect(SigC::slot(this, &FormCitation::applySelect));
569       text_after_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::applySelect));
570
571       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
572
573       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
574
575       clist_bib_->click_column.connect(SigC::slot(this, &FormCitation::sortBibList));
576       clist_bib_->select_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
577                                           true, false));
578       clist_bib_->unselect_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
579                                             false, false));
580     }
581 }
582
583 void FormCitation::showStageEdit()
584 {
585   if (!dialog_)
586     {
587       using namespace Gtk::Box_Helpers;
588
589       Gtk::Box * mbox = manage( new Gtk::VBox() );
590       Gtk::Box * tbox = manage( new Gtk::HBox() );
591       Gtk::Box * t2box = manage( new Gtk::HBox() );
592       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
593       Gtk::ButtonBox * actbbox = manage( new Gtk::VButtonBox() );
594       Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
595
596       vector<string> colnames;
597       colnames.push_back(" ");
598       clist_selected_ = manage( new Gtk::CList(colnames) );
599       clist_selected_->column_titles_hide();
600       
601       text_after_ = manage( new Gnome::Entry() );
602       
603       button_unselect_ = manage( new Gnome::PixButton( _("_Remove"), GNOME_STOCK_PIXMAP_TRASH ) );
604       button_up_ = manage( new Gnome::PixButton( _("_Up"), GNOME_STOCK_PIXMAP_UP ) );
605       button_down_ = manage( new Gnome::PixButton( _("_Down"), GNOME_STOCK_PIXMAP_DOWN ) );
606
607       b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
608       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
609
610       sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
611
612       bbox->set_layout(GTK_BUTTONBOX_END);
613       actbbox->set_layout(GTK_BUTTONBOX_START);
614       
615       // set up spacing
616       mbox->set_spacing(4);
617       bbox->set_spacing(4);
618       actbbox->set_spacing(4);
619       tbox->set_spacing(4);
620       t2box->set_spacing(4);
621
622       // packing widgets
623       sw->add(*clist_selected_);
624
625       bbox->children().push_back(Element(*b_ok, false, false));
626       bbox->children().push_back(Element(*b_cancel, false, false));
627
628       actbbox->children().push_back(Element(*button_unselect_, false, false));
629       actbbox->children().push_back(Element(*button_up_, false, false));
630       actbbox->children().push_back(Element(*button_down_, false, false));
631
632       t2box->children().push_back(Element(*sw, true, true));
633       t2box->children().push_back(Element(*actbbox, false, false));
634
635       tbox->children().push_back(Element(*manage(new Gtk::Label(_("Text after"))), false, false));
636       tbox->children().push_back(Element(*text_after_, true, true));
637       tbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
638       tbox->children().push_back(Element(*bbox, false, false));
639
640       mbox->children().push_back(Element(*t2box,true,true));
641       mbox->children().push_back(Element(*manage(new Gtk::HSeparator()), false, false));
642       mbox->children().push_back(Element(*tbox, false, false));
643       
644       // accelerators
645       Gtk::AccelGroup * accel = Gtk::AccelGroup::create();
646
647       button_unselect_->add_accelerator("clicked", *accel, button_unselect_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
648       button_up_->add_accelerator("clicked", *accel, button_up_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
649       button_down_->add_accelerator("clicked", *accel, button_down_->get_accelkey(), 0, GTK_ACCEL_VISIBLE);
650
651       // packing dialog to main window
652       dialog_ = mbox;
653       mainAppWin->add_action(*dialog_, _(" Citation: Edit "), true, accel);
654
655       initWidgets();
656
657       // setting focus
658       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
659       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
660       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(clist_selected_->gtkobj()), GTK_CAN_DEFAULT);
661       gtk_widget_grab_focus (GTK_WIDGET(clist_selected_->gtkobj()));
662       gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
663
664       // connecting signals
665       b_ok->clicked.connect(SigC::slot(this, &FormCitation::applyEdit));
666       text_after_->get_entry()->activate.connect(SigC::slot(this, &FormCitation::applyEdit));
667
668       b_cancel->clicked.connect(SigC::slot(mainAppWin, &GLyxAppWin::remove_action));
669
670       dialog_->destroy.connect(SigC::slot(this, &FormCitation::free));
671
672       button_unselect_->clicked.connect(SigC::slot(this, &FormCitation::removeCitation));
673       button_up_->clicked.connect(SigC::slot(this, &FormCitation::moveCitationUp));
674       button_down_->clicked.connect(SigC::slot(this, &FormCitation::moveCitationDown));      
675
676       clist_selected_->select_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
677                                           true, true));
678       clist_selected_->unselect_row.connect(SigC::bind(SigC::slot(this, &FormCitation::selectionToggled),
679                                             false, true));
680     }
681 }
682
683
684 void FormCitation::addItemToBibList(int i)
685 {
686   vector<string> r;
687   string key, info;
688   string val;
689
690   key = bibkeys[i];
691   info = bibkeysInfo[i];
692
693   // don't change the order of these first two items:
694   // callback functions depend on the data stored in the first column (its hided)
695   // and in the second column (shown to user)
696   r.push_back( tostr(i) ); 
697   r.push_back( key );
698   
699   // this can be changed (configured by user?)
700   parseBibTeX( info, "author", val);  r.push_back(val);
701   parseBibTeX( info, "title", val);  r.push_back(val);
702   parseBibTeX( info, "year", val);  r.push_back(val);
703   parseBibTeX( info, "journal", val);  r.push_back(val);
704   
705   clist_bib_->rows().push_back(r);
706 }
707
708
709 void FormCitation::updateButtons()
710 {
711   if (button_unselect_ != 0) // => button_up_ and button_down_ are != 0
712     {
713       bool sens;
714
715       sens = (clist_selected_->selection().size()>0);
716       button_unselect_->set_sensitive(sens);
717       button_up_->set_sensitive(sens &&
718                                 clist_selected_->selection()[0].get_row_num()>0);
719       button_down_->set_sensitive(sens &&
720                                   clist_selected_->selection()[0].get_row_num() <
721                                   clist_selected_->rows().size()-1);
722     }
723 }
724
725
726 void FormCitation::updateSlot(bool buffchanged)
727 {
728   if (buffchanged) hide();
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(_("--- 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_!=0) mainAppWin->remove_action();
788 }
789
790 void FormCitation::free()
791 {
792   if (dialog_!=0)
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 = frontStrip( strip(params.getContents()) );
808   if (!contents.empty()) contents += ", ";
809   
810   int 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 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 const 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;
921   string stext(search_string_);
922   stext = frontStrip( strip( stext ) );
923   stext = frontStrip( split(stext, tmp, ' ') );
924   while ( !tmp.empty() )
925     {
926       searchwords.push_back(tmp);
927       stext = frontStrip( split(stext, tmp, ' ') );
928     }
929   
930   // populating clist_bib_
931   clist_bib_->rows().clear();
932
933   clist_bib_->freeze();
934
935   int const sz = bibkeys.size();
936   for (int i = 0; i < sz; ++i) {
937       string const data = bibkeys[i] + bibkeysInfo[i];
938
939       bool additem = true;
940
941       int const szs = searchwords.size();
942       for (int j = 0; additem && j < szs; ++j)
943               if (data.find(searchwords[j]) == string::npos)
944                       additem = false;
945       
946       if (additem) addItemToBibList(i);
947     }
948
949   clist_bib_->sort();
950   clist_bib_->thaw();
951 }