// -*- C++ -*- /* This file is part of * ====================================================== * * LyX, The Document Processor * * Copyright 2000 The LyX Team. * * ====================================================== */ #include #include #ifdef __GNUG__ #pragma implementation #endif #include "gettext.h" #include "Dialogs.h" #include "FormCitation.h" #include "LyXView.h" #include "buffer.h" #include "lyxfunc.h" #include "support/filetools.h" extern "C" { #include "diainsertcitation_interface.h" #include "support.h" } #include #include using std::vector; using std::pair; using std::max; using std::min; using std::find; #ifdef SIGC_CXX_NAMESPACES using SigC::slot; using SigC::bind; #endif // gnome configuration file keys #define LOCAL_CONFIGURE_PREFIX "FormCitation" #define CONF_DIALOG_WIDTH "width" #define CONF_DIALOG_WIDTH_DEFAULT "=550" #define CONF_DIALOG_HEIGTH "heigth" #define CONF_DIALOG_HEIGTH_DEFAULT "=550" #define CONF_PANE_INFO "paneinfo" #define CONF_PANE_INFO_DEFAULT "=300" #define CONF_PANE_KEY "panekey" #define CONF_PANE_KEY_DEFAULT "=225" #define CONF_COLUMN "column" #define CONF_COLUMN_DEFAULT "=50" FormCitation::FormCitation(LyXView * lv, Dialogs * d) : lv_(lv), d_(d), u_(0), h_(0), ih_(0), inset_(0), dialog_(NULL) { // let the dialog be shown // These are permanent connections so we won't bother // storing a copy because we won't be disconnecting. d->showCitation.connect(slot(this, &FormCitation::showInset)); d->createCitation.connect(slot(this, &FormCitation::createInset)); } FormCitation::~FormCitation() { hide(); } void FormCitation::showInset( InsetCommand * const inset ) { if( dialog_!=NULL || inset == 0 ) return; inset_ = inset; ih_ = inset_->hide.connect(slot(this, &FormCitation::hide)); params = inset->params(); show(); } void FormCitation::createInset( string const & arg ) { if( dialog_!=NULL ) return; params.setFromString( arg ); show(); } static void parseBibTeX(string data, string const & findkey, string & keyvalue) { unsigned int i; keyvalue = ""; for (i=0; i1) { tmp = data.substr(keypos, data.length()-1); while (tmp.find('{')!=string::npos && tmp.find('}')!=string::npos && tmp.find('{') < tmp.find('}') && tmp.find('{') < tmp.find(enclosing)) { keypos += tmp.find('{')+1; tmp = data.substr(keypos, data.length()-1); keypos += tmp.find('}')+1; tmp = data.substr(keypos, data.length()-1); } if (tmp.find(enclosing)==string::npos) return; else { keypos += tmp.find(enclosing); tmp = data.substr(keypos, data.length()-1); } value = data.substr(1, keypos-1); if (keypos+1 data.find(',')) keypos = data.find(','); value = data.substr(0, keypos); if (keypos+1 colnames; colnames.push_back("INVISIBLE"); colnames.push_back(N_("Key")); colnames.push_back(N_("Author(s)")); colnames.push_back(N_("Title")); colnames.push_back(N_("Year")); colnames.push_back(N_("Journal")); clist_bib_ = manage( new Gtk::CList(colnames) ); clist_bib_->column(0).set_visiblity(false); Gtk::ScrolledWindow * sw_ = Gtk::wrap( GTK_SCROLLED_WINDOW( lookup_widget(pd, "scrolledwindow_bib") ) ); sw_->add(*clist_bib_); // populating buttons with icons Gnome::Pixmap * p; p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_BACK) ) ); button_select_->add(*p); p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_TRASH) ) ); button_unselect_->add(*p); p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_UP) ) ); button_up_->add(*p); p = Gtk::wrap( GNOME_PIXMAP( gnome_stock_pixmap_widget(NULL, GNOME_STOCK_PIXMAP_DOWN) ) ); button_down_->add(*p); // connecting signals clist_bib_->click_column.connect(slot(this, &FormCitation::sortBibList)); clist_selected_->select_row.connect(bind(slot(this, &FormCitation::selection_toggled), true, true)); clist_bib_->select_row.connect(bind(slot(this, &FormCitation::selection_toggled), true, false)); clist_selected_->unselect_row.connect(bind(slot(this, &FormCitation::selection_toggled), false, true)); clist_bib_->unselect_row.connect(bind(slot(this, &FormCitation::selection_toggled), false, false)); button_select_->clicked.connect(slot(this, &FormCitation::newCitation)); button_unselect_->clicked.connect(slot(this, &FormCitation::removeCitation)); button_up_->clicked.connect(slot(this, &FormCitation::moveCitationUp)); button_down_->clicked.connect(slot(this, &FormCitation::moveCitationDown)); search_text_->get_entry()->activate.connect(slot(this, &FormCitation::search)); button_search_->clicked.connect(slot(this, &FormCitation::search)); b_ok->clicked.connect(slot(this, &FormCitation::apply)); b_ok->clicked.connect(dialog_->destroy.slot()); b_cancel->clicked.connect(dialog_->destroy.slot()); dialog_->destroy.connect(slot(this, &FormCitation::free)); u_ = d_->updateBufferDependent.connect(slot(this, &FormCitation::update)); h_ = d_->hideBufferDependent.connect(slot(this, &FormCitation::hide)); // setting sizes of the widgets string path; string w, h; path += PACKAGE "/" LOCAL_CONFIGURE_PREFIX; w = path + "/" + CONF_DIALOG_WIDTH + CONF_DIALOG_WIDTH_DEFAULT; h = path + "/" + CONF_DIALOG_HEIGTH + CONF_DIALOG_HEIGTH_DEFAULT; dialog_->set_usize(gnome_config_get_int(w.c_str()), gnome_config_get_int(h.c_str())); w = path + "/" + CONF_PANE_INFO + CONF_PANE_INFO_DEFAULT; paned_info_->set_position( gnome_config_get_int(w.c_str()) ); w = path + "/" + CONF_PANE_KEY + CONF_PANE_KEY_DEFAULT; paned_key_->set_position( gnome_config_get_int(w.c_str()) ); int i, sz; for (i = 0, sz = clist_bib_->columns().size(); i < sz; ++i) { w = path + "/" + CONF_COLUMN + "_" + tostr(i) + CONF_COLUMN_DEFAULT; clist_bib_->column(i).set_width( gnome_config_get_int(w.c_str()) ); } // ready to go... if (!dialog_->is_visible()) dialog_->show_all(); update(); // make sure its up-to-date } else { Gdk_Window dialog_win(dialog_->get_window()); dialog_win.raise(); } } void FormCitation::addItemToBibList(int i) { vector r; string key, info; string val; key = bibkeys[i]; info = bibkeysInfo[i]; // don't change the order of these first two items: // callback functions depend on the data stored in the first column (its hided) // and in the second column (shown to user) r.push_back( tostr(i) ); r.push_back( key ); // this can be changed (configured by user?) parseBibTeX( info, "author", val); r.push_back(val); parseBibTeX( info, "title", val); r.push_back(val); parseBibTeX( info, "year", val); r.push_back(val); parseBibTeX( info, "journal", val); r.push_back(val); clist_bib_->rows().push_back(r); } void FormCitation::update() { bibkeys.clear(); bibkeysInfo.clear(); clist_selected_->rows().clear(); clist_bib_->rows().clear(); // populating clist_bib_ clist_bib_->freeze(); vector > blist = lv_->buffer()->getBibkeyList(); int i, sz; for ( i = 0, sz = blist.size(); i < sz; ++i ) { bibkeys.push_back(blist[i].first); bibkeysInfo.push_back(blist[i].second); } blist.clear(); for ( i = 0, sz = bibkeys.size(); i < sz; ++i ) addItemToBibList(i); clist_bib_->sort(); clist_bib_->thaw(); // clist_bib_: done // populating clist_selected_ vector r; string tmp, keys( params.getContents() ); keys = frontStrip( split(keys, tmp, ',') ); while( !tmp.empty() ) { r.clear(); r.push_back(tmp); clist_selected_->rows().push_back(r); keys = frontStrip( split(keys, tmp, ',') ); } // clist_selected_: done text_after_->get_entry()->set_text(params.getOptions()); updateButtons(); } void FormCitation::updateButtons() { bool sens; sens = (clist_selected_->selection().size()>0); button_unselect_->set_sensitive(sens); button_up_->set_sensitive(sens && clist_selected_->selection().operator[](0).get_row_num()>0); button_down_->set_sensitive(sens && clist_selected_->selection().operator[](0).get_row_num() < clist_selected_->rows().size()-1); sens = (clist_bib_->selection().size()>0); button_select_->set_sensitive( (clist_bib_->selection().size()>0) ); } void FormCitation::selection_toggled(gint row, gint ,//column, GdkEvent * ,//event, bool selected, bool citeselected) { if (selected) { bool keyfound = false; string info; if (citeselected) { // lookup the record with the same key in bibkeys and show additional Info int i; int sz = bibkeys.size(); string key = clist_selected_->cell(row,0).get_text(); for (i=0; !keyfound && icell(row,0).get_text()) ]; } if (keyfound) info_->show_string(info); else info_->show_string(N_("--- No such key in the database ---")); } else { info_->show_string(""); } updateButtons(); } void FormCitation::removeCitation() { clist_selected_->rows().remove(clist_selected_->selection().operator[](0)); updateButtons(); } void FormCitation::moveCitationUp() { int i = clist_selected_->selection().operator[](0).get_row_num(); clist_selected_->swap_rows( i-1, i ); clist_selected_->row(i-1).select(); updateButtons(); } void FormCitation::moveCitationDown() { int i = clist_selected_->selection().operator[](0).get_row_num(); clist_selected_->swap_rows( i+1, i ); clist_selected_->row(i+1).select(); updateButtons(); } void FormCitation::newCitation() { // citation key is in the first column of clist_bib_ list vector r; r.push_back( clist_bib_->selection().operator[](0).operator[](1).get_text() ); clist_selected_->rows().push_back(r); clist_selected_->row( clist_selected_->rows().size()-1 ).select(); updateButtons(); } void FormCitation::hide() { if (dialog_!=NULL) dialog_->destroy(); } void FormCitation::free() { if (dialog_!=NULL) { // storing configuration string path; string w, h; path = PACKAGE "/" LOCAL_CONFIGURE_PREFIX; w = path + "/" + CONF_DIALOG_WIDTH; h = path + "/" + CONF_DIALOG_HEIGTH; gnome_config_set_int(w.c_str(), dialog_->width()); gnome_config_set_int(h.c_str(), dialog_->height()); w = path + "/" + CONF_PANE_INFO; gnome_config_set_int(w.c_str(), paned_key_->height()); w = path + "/" + CONF_PANE_KEY; gnome_config_set_int(w.c_str(), box_keys_->width()); int i, sz; for (i = 0, sz = clist_bib_->columns().size(); i < sz; ++i) { w = path + "/" + CONF_COLUMN + "_" + tostr(i); gnome_config_set_int(w.c_str(), clist_bib_->get_column_width(i)); } gnome_config_sync(); // cleaning up dialog_ = NULL; u_.disconnect(); h_.disconnect(); inset_ = 0; ih_.disconnect(); } } void FormCitation::apply() { if( lv_->buffer()->isReadonly() ) return; string contents; for( unsigned int i = 0; i < clist_selected_->rows().size(); ++i ) { if (i > 0) contents += ", "; contents += clist_selected_->cell(i, 0).get_text(); } params.setContents( contents ); params.setOptions( text_after_->get_entry()->get_text() ); if( inset_ != 0 ) { // Only update if contents have changed if( params != inset_->params() ) { inset_->setParams( params ); lv_->view()->updateInset( inset_, true ); } } else { lv_->getLyXFunc()->Dispatch( LFUN_CITATION_INSERT, params.getAsString().c_str() ); } } void FormCitation::sortBibList(gint col) { clist_bib_->set_sort_column(col); clist_bib_->sort(); } // looking for entries which contain all the words specified in search_text entry void FormCitation::search() { vector searchwords; string tmp, stext( search_text_->get_entry()->get_text() ); stext = frontStrip( strip( stext ) ); stext = frontStrip( split(stext, tmp, ' ') ); while( !tmp.empty() ) { searchwords.push_back(tmp); stext = frontStrip( split(stext, tmp, ' ') ); } // populating clist_bib_ clist_bib_->rows().clear(); clist_bib_->freeze(); int i, sz; bool additem; for ( i = 0, sz = bibkeys.size(); i < sz; ++i ) { string data = bibkeys[i] + bibkeysInfo[i]; additem = true; int j, szs; for (j = 0, szs = searchwords.size(); additem && j < szs; ++j ) if ( data.find(searchwords[j]) == string::npos ) additem = false; if ( additem ) addItemToBibList(i); } clist_bib_->sort(); clist_bib_->thaw(); // clist_bib_: done updateButtons(); }