gtk_text_win.cc
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:7k
- /* 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 <gtk--.h>
- #include "movie_item.h"
- #include "gtk_text_win.h"
- const int TextWindow::topWindowInitSizeX = 600;
- const int TextWindow::topWindowInitSizeY = 400;
- const int TextWindow::movieTextColumns = 3;
- TextWindow::TextWindow( int id, Session &s ) : movieId( id ), session( s )
- {
- // make vertical box
- Gtk_VBox *top_vbox_layout = new Gtk_VBox();
- {
- // create menu bar
- menuBar = new Gtk_MenuBar();
- // create menu 'File'
- Gtk_MenuItem *fileMenuRootItem = new Gtk_MenuItem( "File" );
- menuBar->append( *fileMenuRootItem );
- menuBar->show();
- Gtk_Menu *fileMenu = new Gtk_Menu();
- fileMenuRootItem->set_submenu( *fileMenu );
- fileMenuRootItem->show();
-
- Gtk_MenuItem *searchMenuItem = new Gtk_MenuItem( "Search" );
- fileMenu->append( *searchMenuItem );
- searchMenuItem->show();
- Gtk_MenuItem *closeMenuItem = new Gtk_MenuItem( "Close" );
- fileMenu->append( *closeMenuItem );
- closeMenuItem->show();
-
- // connect entries
- connect_to_method( searchMenuItem->activate, &session,
- &Session::callback_search );
- connect_to_method( closeMenuItem->activate, &session,
- &Session::menu_close_text_window, movieId );
- // pack to vbox container
- top_vbox_layout->pack_start( *menuBar, FALSE, FALSE, 0 );
- // handle box
- Gtk_HandleBox *handleBox = new Gtk_HandleBox();
- top_vbox_layout->pack_start( *handleBox, FALSE, FALSE, 0 );
- handleBox->show();
- // toolbar for handle box
- Gtk_Toolbar *toolbar = new Gtk_Toolbar();
- handleBox->add( toolbar );
- toolbar->show();
- // buttons:
- // start button
- try
- {
- // if creation of funny buttons fails, try to create simple one
- Gtk_Button *bst =
- new Gtk_ImageTextButton( session.PlayPixmap.c_str(),"");
- bst->show();
- toolbar->add( bst );
- connect_to_method( bst->clicked, this,
- &TextWindow::callback_play );
- // stop button
- Gtk_Button *bsto =
- new Gtk_ImageTextButton( session.StopPixmap.c_str(),"");
- bsto->show();
- toolbar->add( bsto );
- connect_to_method( bsto->clicked, this,
- &TextWindow::callback_stop );
- }
- catch( BasicException e )
- {
- Gtk_Button *bst =
- new Gtk_Button( "Play" );
- bst->show();
- toolbar->add( bst );
- connect_to_method( bst->clicked, this,
- &TextWindow::callback_play );
- // stop button
- Gtk_Button *bsto =
- new Gtk_Button( "Stop" );
- bsto->show();
- toolbar->add( bsto );
- connect_to_method( bsto->clicked, this,
- &TextWindow::callback_stop );
- }
- // status bar
- statusBar = new Gtk_Statusbar();
- top_vbox_layout->pack_end( *statusBar, FALSE, FALSE, 0 );
- statusBar->show();
- // text
- static char *col_text[movieTextColumns] = { "", "text", "date" };
- GtkWidget *clist;
- clist = gtk_clist_new_with_titles( movieTextColumns, col_text );
- movieText = new Gtk_CList( GTK_CLIST (clist) );
- movieText->set_usize( topWindowInitSizeX, topWindowInitSizeY );
- // movieText->set_policy( GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
- movieText->set_column_width( 0, 10 );
- movieText->set_column_width( 1, topWindowInitSizeX *3/4);
- movieText->show();
- // movieText is inside the scrolled window
- Gtk_ScrolledWindow *scrolled_win = new Gtk_ScrolledWindow ();
- scrolled_win->show();
- scrolled_win->set_policy (GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
- scrolled_win->add( *movieText );
- top_vbox_layout->pack_start( *scrolled_win, TRUE, TRUE, 0 );
- // signals
- connect_to_method( movieText->select_row, this,
- &TextWindow::callback_text_select_row );
- // load the text of the movie
- const TextOfMovieT *text = session.get_movie_text( movieId );
- if( text )
- {
- char tbl_text[movieTextColumns][100];
- const gchar *tbl_texts[movieTextColumns];
- for( int i = 0; i < movieTextColumns; i++ )
- tbl_texts[i] = tbl_text[i];
- *tbl_text[0] = ' ';
- for( int i = 0; i < text->size(); i++ )
- {
- // insert new row
- string t = (*text)[i].get_text();
- strcpy( tbl_text[1], t.c_str() );
- const struct tm &tim = (*text)[i].get_time();
- sprintf( tbl_text[2], "%2d:%2d:%2d", tim.tm_hour,
- tim.tm_min, tim.tm_sec );
- int row = movieText->append( tbl_texts );
- movieText->set_row_data( row, (gpointer) (*text)[i].get_time_t() );
- }
- }
- // whatever have we text or not, currentSelectTime should be initiated
- // as the start time of the movie
- const MovieItem &mi = session.get_movie_by_id( movieId );
- // here const is not violated, arg is not modified
- currentSelectTime = mktime( ( tm * ) &mi.get_start() );
- }
- add( top_vbox_layout );
- top_vbox_layout->show();
- // selection only after text is filled - to force the selection
- // of first row
- movieText->set_selection_mode( GTK_SELECTION_BROWSE );
- movieText->select_row( 0, 0, NULL );
- const MovieItem &mov = session.get_movie_by_id( movieId );
- set_title( mov.get_name().c_str() );
- // set this window user data to id
- set_user_data( (gpointer) id );
- }
- int TextWindow::close_text_window( GdkEventAny *ev )
- {
- return 0;
- }
- gint TextWindow::delete_event_impl( GdkEventAny * )
- {
- destroy();
- session.notify_text_win_die( movieId );
- return 0;
- }
- TextWindow::~TextWindow()
- {
- cout << "text win destructorn";
- // session.notify_text_win_die( movieId );
- }
- void TextWindow::select_text_line_by_time( time_t destt )
- {
- const TextOfMovieT *text = session.get_movie_text( movieId );
- if( !text )
- return;
- // find by time
- int lowbound = 0;
- int highbound = (*text).size();
- int middle = 0;
- while( lowbound < highbound )
- {
- middle = ( lowbound + highbound ) / 2;
- if( lowbound + 1 == highbound )
- {
- middle = highbound;
- break;
- }
- if( (*text)[middle].get_time_t() == destt )
- break;
- if( (*text)[middle].get_time_t() > destt )
- highbound = middle;
- else
- lowbound = middle;
- }
- // and position selected text to middle
- movieText->moveto( middle, 0, 0.5, 0.0 );
- movieText->cause_select_row( middle, 0 );
- }
- void TextWindow::select_text_line( unsigned int linenum )
- {
- movieText->moveto( linenum, 0, 0.5, 0.0 );
- movieText->cause_select_row( linenum, 0 );
- }
- //-------------------------- call backs ---------------------------
- void TextWindow::callback_text_select_row
- ( gint row, gint col, GdkEvent *_button )
- {
- GdkEventButton *button = (GdkEventButton *)_button;
- time_t t = (time_t) movieText->get_row_data( row );
- if( t )
- currentSelectTime = t;
- }
- void TextWindow::callback_play()
- {
- // attention: currentSelectTime may not be valid
- struct tm t = *localtime( ¤tSelectTime );
- session.playVideo( *this, t, movieId );
- }
- void TextWindow::callback_stop()
- {
- session.stop();
- }
- // ------------------------------- Gtk_ImageTextButton ----------------------
- // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Gtk_ImageTextButton::Gtk_ImageTextButton( const gchar *xpmpath,
- const char *label )
- {
- Gtk_VBox *vbox_layout = new Gtk_VBox();
- vbox_layout->show();
- add( vbox_layout );
- Gtk_Pixmap *pixmapwid = new Gtk_Pixmap( *vbox_layout, xpmpath );
- pixmapwid->show();
- vbox_layout->pack_start( *pixmapwid, FALSE, FALSE, 0 );
- Gtk_Label *labelwid;
- if( strlen( label ) )
- {
- labelwid = new Gtk_Label( label );
- labelwid->show();
- vbox_layout->pack_start( *labelwid, FALSE, FALSE, 0 );
- }
- }