gtk_search_win.cc
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:8k
- /* Copyright (C) 1998, 1999 State University of New York at Stony Brook
- Author: Andrew V. Shuvalov ( andrew@ecsl.cs.sunysb.edu )
- Software license is located in file "COPYING"
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <gtk--.h>
- #include "session.h"
- #include "search.h"
- #include "gtk_search_win.h"
- const int SearchWindow::topWindowInitSizeX = 600;
- const int SearchWindow::topWindowInitSizeY = 400;
- const int SearchResults::col0width = 160;
- const int SearchResults::col1width = 150;
- const int SearchResults::col2width = 320;
- const int SearchResults::TextColumns = 3;
- SearchItem::SearchItem( Session &s, SearchWindow &w, int pos )
- : session( s ), searchWin( w ), position( pos ), Gtk_HBox( false, 14 )
- {
- // populate it
- if( pos == 0 )
- {
- Gtk_Label *lab1 = new Gtk_Label( "Search for specified word:" );
- pack_start( *lab1, FALSE, FALSE, 0 );
- lab1->show();
- }
- set_spacing( 10 );
- GList *cbitems = NULL;
- cbitems = g_list_append( cbitems, "and" );
- cbitems = g_list_append( cbitems, "and not" );
- cbitems = g_list_append( cbitems, "or" );
- cbitems = g_list_append( cbitems, "result and" );
- cbitems = g_list_append( cbitems, "result and not" );
- boolOpEntry = new Gtk_Combo();
- boolOpEntry->set_popdown_strings( cbitems );
- boolOpEntry->set_value_in_list( false, false );
- boolOpEntry->get_entry()->set_editable( false );
- // add it
- pack_start( *boolOpEntry, FALSE, FALSE, 0 );
- boolOpEntry->set_usize( boolOpEntry->width()/2, boolOpEntry->height() );
- textEntry = new Gtk_Combo();
- pack_start( *textEntry, FALSE, FALSE, 0 );
- textEntry->show();
- // open window in combobox
- connect_to_method( textEntry->get_entry()->changed, this,
- &SearchItem::callback_hint );
- // less
- lessButton = new Gtk_Button( "less" );
- pack_start( *lessButton, FALSE, FALSE, 0 );
- // signal
- connect_to_method( lessButton->clicked, &searchWin,
- &SearchWindow::callback_less, position );
- // more
- moreButton = new Gtk_Button( "more" );
- pack_start( *moreButton, FALSE, FALSE, 0 );
- // signal
- connect_to_method( moreButton->clicked, &searchWin,
- &SearchWindow::callback_more, position );
- // search
- searchButton = new Gtk_Button( "Search" );
- pack_start( *searchButton, FALSE, FALSE, 0 );
- // signal
- connect_to_method( searchButton->clicked, &searchWin,
- &SearchWindow::callback_search );
- show();
- }
- SearchItem::type SearchItem::get_type() const
- {
- string text = boolOpEntry->get_entry()->get_text();
- text.c_str();
- if( text == "and" )
- return and;
- if( text == "and not" )
- return andnot;
- if( text == "or" )
- return or;
- if( text == "result and" )
- return resultand;
- if( text == "result and not" )
- return resultandnot;
- return err;
- }
- void SearchItem::callback_hint()
- {
- // first add to list all similar words...
- string text = textEntry->get_entry()->get_text();
- GList *hints = NULL;
- hints = g_list_append( hints, (void*)text.c_str() );
- if( text.length() )
- {
- session.find_similar_words( text, hints );
- }
- // now let's search for some near-in-string words
- searchWin.find_hint_words( position, hints );
- if( hints )
- {
- textEntry->set_popdown_strings( hints );
- textEntry->show();
- }
- }
- // --------------------------------------------------------------------------
- SearchWindow::SearchWindow( Session &s )
- : session( s )
- {
- set_usize( topWindowInitSizeX, topWindowInitSizeY );
- // make vertical box
- Gtk_VBox *top_vbox_layout = new Gtk_VBox();
- top_vbox_layout->set_spacing( 10 );
- {
- searchItemsVBox = new Gtk_VBox();
- searchItemsVBox->set_spacing( 10 );
- {
- SearchItem *si = new SearchItem( session, *this, 0 );
- searchItems.push_back( si );
- si->buttonsVisible( true );
- searchItemsVBox->pack_start( *si, FALSE, FALSE, 0 );
- }
- top_vbox_layout->pack_start( *searchItemsVBox, FALSE, FALSE, 0 );
- searchItemsVBox->show();
- // result window
- searchResults = new SearchResults( session, *this );
- top_vbox_layout->pack_start( *searchResults, TRUE, TRUE, 2 );
- // button box
- Gtk_HButtonBox *hbb = new Gtk_HButtonBox();
- hbb->show();
- Gtk_Button *cb = new Gtk_Button( "Close" );
- cb->show();
- hbb->pack_start( *cb );
- connect_to_method( cb->clicked, this, &SearchWindow::callback_close );
- top_vbox_layout->pack_end( *hbb, FALSE, FALSE, 0 );
- }
- add( top_vbox_layout );
- top_vbox_layout->show();
- set_title( "Video client: Search" );
- }
- void SearchWindow::find_hint_words( int position, GList *&hints )
- {
- if( position == 0 && searchItems.size() <= 1 ) // nothing to do
- return;
- if( position > 0 )
- {
- const string &word = searchItems[ position - 1 ]->get_text();
- session.get_hint_words( word, hints );
- }
- }
- void SearchWindow::callback_less( int pos )
- {
- if( pos != searchItems.size() - 1 || pos == 0 )
- {
- // ups!
- return;
- }
- // delete
- searchItemsVBox->remove( searchItems.back() );
- delete searchItems.back();
- searchItems.pop_back();
- // adjust the last
- if( searchItems.size() > 1 )
- searchItems.back()->lessButVisible( true );
- searchItems.back()->buttonsVisible( true );
- }
- void SearchWindow::callback_more( int pos )
- {
- if( pos != searchItems.size() - 1 )
- {
- // ups!
- return;
- }
- // adjust old
- searchItems.back()->lessButVisible( false );
- searchItems.back()->buttonsVisible( false );
- // add new
- SearchItem *si = new SearchItem( session, *this, searchItems.size() );
- searchItems.push_back( si );
-
- si->boolOpVisible( true );
- si->buttonsVisible( true );
- si->lessButVisible( true );
- si->callback_hint();
- searchItemsVBox->pack_start( *si, FALSE, FALSE, 0 );
- }
- void SearchWindow::callback_close()
- {
- hide();
- }
- void SearchWindow::callback_search()
- {
- // build search structure from window input
- BoolSearchOperation *head = NULL;
- vector< SearchItem * >::iterator it;
- for( it = searchItems.begin(); it != searchItems.end(); it++ )
- {
- SearchItem &item = *(*it);
- string word = item.get_text();
- word.c_str(); // insert zero at the end
- if( head == NULL )
- {
- const WordOccurencesT &wo = session.find_word( word );
- head = new BoolSearchOperationResult( session.get_word_db(), wo );
- continue;
- }
- // not the first argument
- const WordOccurencesT &wo = session.find_word( item.get_text() );
- BoolSearchOperation *right =
- new BoolSearchOperationResult( session.get_word_db(), wo );
- head = head->merge_search_items( head, right, item );
- }
- if( !head )
- return;
- // now compute result
- const WordOccurencesT &result = head->get_result();
- // display results
- searchResults->display_results( result );
- }
- // --------------------------------------------------------------------------
- SearchResults::SearchResults( Session &s, SearchWindow &sw )
- : session( s ), searchWindow( sw ), Gtk_CList( 3 )
- {
- set_column_title( 0, "Movie" );
- set_column_title( 1, "Time" );
- set_column_title( 2, "Text" );
- column_titles_show();
- set_column_width( 0, col0width );
- set_column_width( 1, col1width );
- set_column_width( 2, col2width );
- show();
- connect_to_method( select_row, this, &SearchResults::callback_select_row );
- }
- void SearchResults::display_results( const WordOccurencesT &result )
- {
- WordOccurences = result;
- clear();
- freeze();
- WordOccurencesT::const_iterator it;
- for( it = result.begin(); it != result.end(); it++ )
- {
- gchar tbl_text[TextColumns][1];
- const gchar *tbl_texts[TextColumns];
- for( int i = 0; i < TextColumns; i++ )
- {
- tbl_texts[i] = tbl_text[i];
- tbl_text[i][0] = ' ';
- }
- int row = append( tbl_texts );
- const WordOccurence &wo = *it;
- const MovieItem &mi = session.get_movie_by_id( wo.get_movie_id() );
-
- set_text( row, 0, mi.get_name().c_str() );
- const TextOfMovieT *text = session.get_movie_text( wo.get_movie_id() );
- const struct tm &tim = (*text)[ wo.get_text_line() ].get_time();
- char buf[100];
- snprintf( buf, sizeof( buf ), "%2d/%2d/%4d %2d:%2d:%2d",
- tim.tm_mday, tim.tm_mon + 1, tim.tm_year + 1900, tim.tm_hour,
- tim.tm_min, tim.tm_sec );
-
- set_text( row, 1, buf );
- set_text( row, 2, (*text)[ wo.get_text_line() ].get_text().c_str() );
- }
- thaw();
- }
- void SearchResults::callback_select_row( gint row, gint col,
- GdkEvent *_button )
- {
- GdkEventButton *button = (GdkEventButton *)_button;
- const WordOccurence &wo = WordOccurences[ row ];
- session.display_movie_text_window_go_line( wo.get_movie_id(),
- wo.get_text_line() );
- }