2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
17 #pragma implementation
23 #include "FormCitation.h"
27 #include "support/filetools.h"
30 #include "diainsertcitation_interface.h"
34 #include <gtk--/scrolledwindow.h>
35 #include <gnome--/pixmap.h>
43 #ifdef SIGC_CXX_NAMESPACES
48 // gnome configuration file keys
49 #define LOCAL_CONFIGURE_PREFIX "FormCitation"
51 #define CONF_DIALOG_WIDTH "width"
52 #define CONF_DIALOG_WIDTH_DEFAULT "=550"
54 #define CONF_DIALOG_HEIGTH "heigth"
55 #define CONF_DIALOG_HEIGTH_DEFAULT "=550"
57 #define CONF_PANE_INFO "paneinfo"
58 #define CONF_PANE_INFO_DEFAULT "=300"
60 #define CONF_PANE_KEY "panekey"
61 #define CONF_PANE_KEY_DEFAULT "=225"
63 #define CONF_COLUMN "column"
64 #define CONF_COLUMN_DEFAULT "=50"
67 FormCitation::FormCitation(LyXView * lv, Dialogs * d)
68 : lv_(lv), d_(d), u_(0), h_(0), ih_(0), inset_(0), dialog_(NULL)
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(slot(this, &FormCitation::showInset));
74 d->createCitation.connect(slot(this, &FormCitation::createInset));
78 FormCitation::~FormCitation()
83 void FormCitation::showInset( InsetCommand * const inset )
85 if( dialog_!=NULL || inset == 0 ) return;
88 ih_ = inset_->hide.connect(slot(this, &FormCitation::hide));
90 params = inset->params();
95 void FormCitation::createInset( string const & arg )
97 if( dialog_!=NULL ) return;
99 params.setFromString( arg );
104 void parseBibTeX(string data,
105 string const & findkey,
112 for (i=0; i<data.length(); ++i)
113 if (data[i]=='\n' || data[i]=='\t')
116 data = frontStrip(data);
119 && (data.find(' ')!=string::npos ||
120 data.find('=')!=string::npos) )
122 unsigned int keypos = min(data.find(' '), data.find('='));
123 string key = lowercase( data.substr(0, keypos) );
127 data = data.substr(keypos, data.length()-1);
128 data = frontStrip(strip(data));
129 if (data.length() > 1 && data[0]=='=')
131 data = frontStrip(data.substr(1, data.length()-1));
135 if (data[0]=='{') enclosing = '}';
136 else if (data[0]=='"') enclosing = '"';
137 else { keypos=0; enclosing=' '; }
140 data.find(enclosing)!=string::npos &&
143 tmp = data.substr(keypos, data.length()-1);
144 while (tmp.find('{')!=string::npos &&
145 tmp.find('}')!=string::npos &&
146 tmp.find('{') < tmp.find('}') &&
147 tmp.find('{') < tmp.find(enclosing))
149 keypos += tmp.find('{')+1;
150 tmp = data.substr(keypos, data.length()-1);
151 keypos += tmp.find('}')+1;
152 tmp = data.substr(keypos, data.length()-1);
155 if (tmp.find(enclosing)==string::npos) return;
158 keypos += tmp.find(enclosing);
159 tmp = data.substr(keypos, data.length()-1);
162 value = data.substr(1, keypos-1);
164 if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
170 ) // numerical value ?
172 keypos = data.length()-1;
173 if (data.find(' ')!=string::npos) keypos = data.find(' ');
174 if (data.find(',')!=string::npos &&
175 keypos > data.find(','))
176 keypos = data.find(',');
178 value = data.substr(0, keypos);
180 if (keypos+1<data.length()-1) data = frontStrip(data.substr(keypos+1, data.length()-1));
185 if (findkey == key) { keyvalue = value; return; }
187 data = frontStrip(frontStrip(data,','));
194 void FormCitation::show()
198 GtkWidget * pd = create_DiaInsertCitation();
200 dialog_ = Gtk::wrap(pd);
201 clist_selected_ = Gtk::wrap( GTK_CLIST( lookup_widget(pd, "clist_selected") ) );
202 info_ = Gtk::wrap( GNOME_LESS( lookup_widget(pd, "info") ) );
203 text_after_ = Gtk::wrap( GNOME_ENTRY( lookup_widget(pd, "text_after") ) );
204 search_text_ = Gtk::wrap( GNOME_ENTRY( lookup_widget(pd, "search_text") ) );
206 button_select_ = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_select") ) );
207 button_unselect_ = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_unselect") ) );
208 button_up_ = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_up") ) );
209 button_down_ = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_down") ) );
210 button_search_ = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_search") ) );
212 paned_info_ = Gtk::wrap( GTK_PANED( lookup_widget(pd, "vpaned_info") ) );
213 paned_key_ = Gtk::wrap( GTK_PANED( lookup_widget(pd, "hpaned_key") ) );
214 box_keys_ = Gtk::wrap( GTK_BOX( lookup_widget(pd, "vbox_keys") ) );
216 b_ok = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_ok") ) );
217 b_cancel = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_cancel") ) );
219 // constructing and packing CList
220 vector<string> colnames;
221 colnames.push_back("INVISIBLE");
222 colnames.push_back(N_("Key"));
223 colnames.push_back(N_("Author(s)"));
224 colnames.push_back(N_("Title"));
225 colnames.push_back(N_("Year"));
226 colnames.push_back(N_("Journal"));
227 clist_bib_ = manage( new Gtk::CList(colnames) );
228 clist_bib_->column(0).set_visiblity(false);
230 Gtk::ScrolledWindow * sw_ = Gtk::wrap( GTK_SCROLLED_WINDOW( lookup_widget(pd, "scrolledwindow_bib") ) );
231 sw_->add(*clist_bib_);
233 // populating buttons with icons
235 p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_BACK) ) );
236 button_select_->add(*p);
237 p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_TRASH) ) );
238 button_unselect_->add(*p);
239 p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_UP) ) );
241 p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_DOWN) ) );
242 button_down_->add(*p);
245 // connecting signals
246 clist_bib_->click_column.connect(slot(this, &FormCitation::sortBibList));
248 clist_selected_->select_row.connect(bind(slot(this, &FormCitation::selection_toggled),
250 clist_bib_->select_row.connect(bind(slot(this, &FormCitation::selection_toggled),
252 clist_selected_->unselect_row.connect(bind(slot(this, &FormCitation::selection_toggled),
254 clist_bib_->unselect_row.connect(bind(slot(this, &FormCitation::selection_toggled),
257 button_select_->clicked.connect(slot(this, &FormCitation::newCitation));
258 button_unselect_->clicked.connect(slot(this, &FormCitation::removeCitation));
259 button_up_->clicked.connect(slot(this, &FormCitation::moveCitationUp));
260 button_down_->clicked.connect(slot(this, &FormCitation::moveCitationDown));
262 search_text_->get_entry()->activate.connect(slot(this, &FormCitation::search));
263 button_search_->clicked.connect(slot(this, &FormCitation::search));
265 b_ok->clicked.connect(slot(this, &FormCitation::apply));
266 b_ok->clicked.connect(dialog_->destroy.slot());
267 b_cancel->clicked.connect(dialog_->destroy.slot());
268 dialog_->destroy.connect(slot(this, &FormCitation::free));
270 u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update));
271 h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide));
273 // setting sizes of the widgets
276 path += PACKAGE "/" LOCAL_CONFIGURE_PREFIX;
277 w = path + "/" + CONF_DIALOG_WIDTH + CONF_DIALOG_WIDTH_DEFAULT;
278 h = path + "/" + CONF_DIALOG_HEIGTH + CONF_DIALOG_HEIGTH_DEFAULT;
279 dialog_->set_usize(gnome_config_get_int(w.c_str()),
280 gnome_config_get_int(h.c_str()));
282 w = path + "/" + CONF_PANE_INFO + CONF_PANE_INFO_DEFAULT;
283 paned_info_->set_position( gnome_config_get_int(w.c_str()) );
285 w = path + "/" + CONF_PANE_KEY + CONF_PANE_KEY_DEFAULT;
286 paned_key_->set_position( gnome_config_get_int(w.c_str()) );
289 for (i = 0, sz = clist_bib_->columns().size(); i < sz; ++i)
291 w = path + "/" + CONF_COLUMN + "_" + tostr(i) + CONF_COLUMN_DEFAULT;
292 clist_bib_->column(i).set_width( gnome_config_get_int(w.c_str()) );
296 if (!dialog_->is_visible()) dialog_->show_all();
298 update(); // make sure its up-to-date
302 Gdk_Window dialog_win(dialog_->get_window());
307 void FormCitation::addItemToBibList(int i)
314 info = bibkeysInfo[i];
316 // don't change the order of these first two items:
317 // callback functions depend on the data stored in the first column (its hided)
318 // and in the second column (shown to user)
319 r.push_back( tostr(i) );
322 // this can be changed (configured by user?)
323 parseBibTeX( info, "author", val); r.push_back(val);
324 parseBibTeX( info, "title", val); r.push_back(val);
325 parseBibTeX( info, "year", val); r.push_back(val);
326 parseBibTeX( info, "journal", val); r.push_back(val);
328 clist_bib_->rows().push_back(r);
331 void FormCitation::update()
336 clist_selected_->rows().clear();
337 clist_bib_->rows().clear();
339 // populating clist_bib_
340 clist_bib_->freeze();
342 vector<pair<string,string> > blist =
343 lv_->buffer()->getBibkeyList();
346 for ( i = 0, sz = blist.size(); i < sz; ++i )
348 bibkeys.push_back(blist[i].first);
349 bibkeysInfo.push_back(blist[i].second);
354 for ( i = 0, sz = bibkeys.size(); i < sz; ++i )
361 // populating clist_selected_
363 string tmp, keys( params.getContents() );
364 keys = frontStrip( split(keys, tmp, ',') );
365 while( !tmp.empty() )
369 clist_selected_->rows().push_back(r);
371 keys = frontStrip( split(keys, tmp, ',') );
373 // clist_selected_: done
375 text_after_->get_entry()->set_text(params.getOptions());
380 void FormCitation::updateButtons()
384 sens = (clist_selected_->selection().size()>0);
385 button_unselect_->set_sensitive(sens);
386 button_up_->set_sensitive(sens &&
387 clist_selected_->selection().operator[](0).get_row_num()>0);
388 button_down_->set_sensitive(sens &&
389 clist_selected_->selection().operator[](0).get_row_num() <
390 clist_selected_->rows().size()-1);
392 sens = (clist_bib_->selection().size()>0);
393 button_select_->set_sensitive( (clist_bib_->selection().size()>0) );
396 void FormCitation::selection_toggled(gint row,
404 bool keyfound = false;
408 // lookup the record with the same key in bibkeys and show additional Info
410 int sz = bibkeys.size();
411 string key = clist_selected_->cell(row,0).get_text();
412 for (i=0; !keyfound && i<sz; ++i)
413 if (bibkeys[i] == key)
415 info = bibkeysInfo[i];
421 // the first column in clist_bib_ contains the index
423 info = bibkeysInfo[ strToInt(clist_bib_->cell(row,0).get_text()) ];
427 info_->show_string(info);
429 info_->show_string(N_("--- No such key in the database ---"));
433 info_->show_string("");
438 void FormCitation::removeCitation()
440 clist_selected_->rows().remove(clist_selected_->selection().operator[](0));
444 void FormCitation::moveCitationUp()
446 int i = clist_selected_->selection().operator[](0).get_row_num();
447 clist_selected_->swap_rows( i-1, i );
448 clist_selected_->row(i-1).select();
452 void FormCitation::moveCitationDown()
454 int i = clist_selected_->selection().operator[](0).get_row_num();
455 clist_selected_->swap_rows( i+1, i );
456 clist_selected_->row(i+1).select();
460 void FormCitation::newCitation()
462 // citation key is in the first column of clist_bib_ list
464 r.push_back( clist_bib_->selection().operator[](0).operator[](1).get_text() );
465 clist_selected_->rows().push_back(r);
466 clist_selected_->row( clist_selected_->rows().size()-1 ).select();
470 void FormCitation::hide()
472 if (dialog_!=NULL) dialog_->destroy();
475 void FormCitation::free()
479 // storing configuration
482 path = PACKAGE "/" LOCAL_CONFIGURE_PREFIX;
483 w = path + "/" + CONF_DIALOG_WIDTH;
484 h = path + "/" + CONF_DIALOG_HEIGTH;
486 gnome_config_set_int(w.c_str(), dialog_->width());
487 gnome_config_set_int(h.c_str(), dialog_->height());
489 w = path + "/" + CONF_PANE_INFO;
490 gnome_config_set_int(w.c_str(), paned_key_->height());
492 w = path + "/" + CONF_PANE_KEY;
493 gnome_config_set_int(w.c_str(), box_keys_->width());
496 for (i = 0, sz = clist_bib_->columns().size(); i < sz; ++i)
498 w = path + "/" + CONF_COLUMN + "_" + tostr(i);
499 gnome_config_set_int(w.c_str(), clist_bib_->get_column_width(i));
513 void FormCitation::apply()
515 if( lv_->buffer()->isReadonly() ) return;
518 for( unsigned int i = 0; i < clist_selected_->rows().size(); ++i )
520 if (i > 0) contents += ", ";
521 contents += clist_selected_->cell(i, 0).get_text();
524 params.setContents( contents );
525 params.setOptions( text_after_->get_entry()->get_text() );
529 // Only update if contents have changed
530 if( params != inset_->params() )
532 inset_->setParams( params );
533 lv_->view()->updateInset( inset_, true );
538 lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT,
539 params.getAsString().c_str() );
543 void FormCitation::sortBibList(gint col)
545 clist_bib_->set_sort_column(col);
549 // looking for entries which contain all the words specified in search_text entry
550 void FormCitation::search()
552 vector<string> searchwords;
553 string tmp, stext( search_text_->get_entry()->get_text() );
554 stext = frontStrip( strip( stext ) );
555 stext = frontStrip( split(stext, tmp, ' ') );
556 while( !tmp.empty() )
558 searchwords.push_back(tmp);
559 stext = frontStrip( split(stext, tmp, ' ') );
562 // populating clist_bib_
563 clist_bib_->rows().clear();
565 clist_bib_->freeze();
569 for ( i = 0, sz = bibkeys.size(); i < sz; ++i )
571 string data = bibkeys[i] + bibkeysInfo[i];
576 for (j = 0, szs = searchwords.size();
577 additem && j < szs; ++j )
578 if ( data.find(searchwords[j]) == string::npos )
581 if ( additem ) addItemToBibList(i);