gtk_main.cc
上传用户:psq1974
上传日期:2007-01-06
资源大小:1195k
文件大小:14k
- /* 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 <assert.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <vector>
- #include <map>
- #include <gtk--.h>
- #include "movie_item.h"
- #include "gtk_main_win.h"
- #include "session.h"
- int main( int argc, char **argv )
- {
- Session s( &argc, &argv );
- gtk_debug_flags = 0xffffffff;
- s.run();
- return 0;
- }
- // ====================== Main window implementation =====================
- MainWindow::MainWindow( Session &s ) : session( s ), statusBarContextId( 0 )
- {
- // make vertical box
- Gtk_VBox *top_vbox_layout = new Gtk_VBox();
- {
- // create menu bar
- menuBar = new Gtk_MenuBar();
- menuBar->show();
- // create menu 'File'
- Gtk_MenuItem *fileMenuRootItem = new Gtk_MenuItem( "File" );
- menuBar->append( *fileMenuRootItem );
- 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 *exitMenuItem = new Gtk_MenuItem( "Exit" );
- fileMenu->append( *exitMenuItem );
- exitMenuItem->show();
-
- // connect entries
- connect_to_method( searchMenuItem->activate, &session,
- &Session::callback_search );
- connect_to_method( exitMenuItem->activate, this,
- &MainWindow::callback_exit );
- // create menu 'Edit'
- Gtk_MenuItem *editMenuRootItem = new Gtk_MenuItem( "Edit" );
- menuBar->append( *editMenuRootItem );
- // editMenu is the class member. Superuser will use it
- editMenu = new Gtk_Menu();
- editMenuRootItem->set_submenu( *editMenu );
- editMenuRootItem->show();
-
- Gtk_MenuItem *editPrefMenuItem = new Gtk_MenuItem( "Preferences..." );
- editMenu->append( *editPrefMenuItem );
- editPrefMenuItem->show();
-
- // connect entries
- connect_to_method( editPrefMenuItem->activate, this,
- &MainWindow::callback_preferences );
- // create menu 'Options'
- Gtk_MenuItem *optionsMenuRootItem = new Gtk_MenuItem( "Options" );
- menuBar->append( *optionsMenuRootItem );
- Gtk_Menu *optionsMenu = new Gtk_Menu();
- optionsMenuRootItem->set_submenu( *optionsMenu );
- optionsMenuRootItem->show();
- Gtk_MenuItem *suMenuItem =
- new Gtk_MenuItem( "Login as superuser..." );
- optionsMenu->append( *suMenuItem );
- suMenuItem->show();
- // connect entries
- connect_to_method( suMenuItem->activate, &session,
- &Session::callback_superuser );
- // create menu 'View'
- Gtk_MenuItem *viewMenuRootItem = new Gtk_MenuItem( "View" );
- menuBar->append( *viewMenuRootItem );
- Gtk_Menu *viewMenu = new Gtk_Menu();
- viewMenuRootItem->set_submenu( *viewMenu );
- viewMenuRootItem->show();
- Gtk_MenuItem *menuItemBr = new Gtk_MenuItem( "Broadcast..." );
- viewMenu->append( *menuItemBr );
- menuItemBr->show();
-
- // connect entries
- connect_to_method( menuItemBr->activate, &session,
- &Session::callback_broadcast );
- // pack to vbox container
- top_vbox_layout->pack_start( *menuBar, FALSE, FALSE, 0 );
- // status bar
- statusBar = new Gtk_Statusbar();
- top_vbox_layout->pack_end( *statusBar, FALSE, FALSE, 0 );
- statusBar->show();
- // scroll window for log
- Gtk_ScrolledWindow *scrw = new Gtk_ScrolledWindow();
- scrw->set_usize( session.topWindowInitSizeX,
- session.topWindowInitSizeY/4 );
- scrw->show();
- // log window
- logText = new Gtk_Text();
- logText->set_usize( session.topWindowInitSizeX,
- session.topWindowInitSizeY*10 );
- logText->show();
- logText->set_editable( false );
- scrw->add( logText );
- // make horizontal layout: movies list and short description
- Gtk_HBox *hbox_movie_layout = new Gtk_HBox();
- hbox_movie_layout->show();
- // pack hbox_movie_layout and logText to vertical pane
- Gtk_VPaned *vp = new Gtk_VPaned();
- top_vbox_layout->pack_start( *vp, TRUE, TRUE, 0 );
- vp->show();
- vp->add1( *hbox_movie_layout );
- vp->add2( *scrw );
- // block for hbox_movie_layout
- {
- Gtk_VBox *mov_box = new Gtk_VBox( FALSE, 10 );
- mov_box->set_spacing( 10 );
- mov_box->show();
- // list
- static char *text[movieListColumns] = { "", "Title", "date" };
- GtkWidget *clist;
- clist = gtk_clist_new_with_titles( movieListColumns, text );
- movieList = new Gtk_CList( GTK_CLIST (clist) );
- movieList->set_usize( session.topWindowInitSizeX/2,
- session.topWindowInitSizeY/2 );
- movieList->set_column_width( 0, 10 );
- movieList->set_column_width( 1, session.topWindowInitSizeX/4 );
- movieList->show();
- // movieList 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( *movieList );
- mov_box->pack_start( *scrolled_win, TRUE, TRUE, 0 );
- // callback
- connect_to_method( movieList->select_row, this,
- &MainWindow::callback_movie_select_row );
- connect_to_method( movieList->button_press_event, this,
- &MainWindow::callback_movie_button_press );
- movieList->set_selection_mode( GTK_SELECTION_BROWSE );
-
- // add paned
- Gtk_HPaned *hp = new Gtk_HPaned();
- hbox_movie_layout->pack_start( *hp, TRUE, TRUE, 0 );
- hp->show();
- // the right of paned vbox is located
- Gtk_VBox *vbox = new Gtk_VBox( FALSE, 0 );
- vbox->show();
- // paned childrens attached
- hp->add1( *mov_box );
- hp->add2( *vbox );
- {
- // caption
- dbNameLabel = new Gtk_Label( "Not connected" );
- vbox->pack_start( *dbNameLabel, FALSE, FALSE, 0 );
- dbNameLabel->show();
- // name
- dbMovieName = new Gtk_Text();
- dbMovieName->set_usize( session.topWindowInitSizeX/2,
- session.topWindowInitSizeY/12 );
- vbox->pack_start( *dbMovieName, FALSE, FALSE, 0 );
- dbMovieName->show();
- // comments
- comments = new Gtk_Text();
- comments->set_usize( session.topWindowInitSizeX/2,
- session.topWindowInitSizeY/6 );
- vbox->pack_start( *comments, FALSE, FALSE, 0 );
- comments->show();
- // recorded at
- dbStartTime = new Gtk_Label( "" );
- vbox->pack_start( *dbStartTime, FALSE, FALSE, 0 );
- dbStartTime->show();
- // duration
- dbDuration = new Gtk_Label( "" );
- vbox->pack_start( *dbDuration, FALSE, FALSE, 0 );
- dbDuration->show();
- // make horizontal layout
- Gtk_HBox *hbox_db1 = new Gtk_HBox();
- hbox_db1->show();
- vbox->pack_start( *hbox_db1, FALSE, FALSE, 0 );
- {
- Gtk_Label *l = new Gtk_Label( "Database name" );
- l->show();
- hbox_db1->pack_start( *l, FALSE, FALSE, 0 );
- Gtk_Button *b = new Gtk_Button( "Connect" );
- b->show();
- hbox_db1->pack_end( *b, FALSE, FALSE, 0 );
- connect_to_method( b->clicked, this,
- &MainWindow::callback_dbname );
- }
- // make horizontal layout
- Gtk_HBox *hbox_db2 = new Gtk_HBox();
- hbox_db2->show();
- vbox->pack_start( *hbox_db2, FALSE, FALSE, 0 );
- {
- // database name
- dbNameText = new Gtk_Entry();
- dbNameText->set_editable( true );
- dbNameText->show();
- hbox_db2->pack_start( *dbNameText, FALSE, FALSE, 0 );
- }
- // button to toggle if the log follows the last line
- toggleLogAuto = new Gtk_ToggleButton( "Auto scroll" );
- toggleLogAuto->show();
- toggleLogAuto->set_mode( true );
- vbox->pack_end( *toggleLogAuto, FALSE, FALSE, 0 );
- }
- }
- top_vbox_layout->show();
- }
- add( top_vbox_layout );
- top_vbox_layout->show();
- // callback to destroy session
- connect_to_method( destroy, &session,
- &Session::callback_destroy_session );
- // other...
- movieListCurrentlySelectedMovie = -1;
- set_title( "Video client" );
- // create pixmaps and bitmaps for bullets
- {
- string pmp_name_loaded = session.loadedPixmap.c_str();
- string pmp_name_notloaded = session.notLoadedPixmap.c_str();
- textLoadedPixmap = new Gtk_Pixmap( *movieList, pmp_name_loaded );
- textNotLoadedPixmap = new Gtk_Pixmap( *movieList, pmp_name_notloaded );
- // textLoadedBitmap = new Gdk_Bitmap();
- // textLoadedPixmap = new Gdk_Pixmap( *movieList->get_window(),
- // *textLoadedBitmap,
- // Gdk_Color( "white" ),
- // pmp_name_loaded );
- // textNotLoadedBitmap = new Gdk_Bitmap();
- // textLoadedPixmap = new Gdk_Pixmap( *movieList->get_window(),
- // *textNotLoadedBitmap,
- // Gdk_Color( "white" ),
- // pmp_name_notloaded );
- }
- }
- MainWindow::~MainWindow()
- {
- }
- gint MainWindow::delete_event_impl( GdkEventAny * )
- {
- session.cleanup();
- Gtk_Main::instance()->quit();
- return 0;
- }
- void MainWindow::callback_exit()
- {
- session.cleanup();
- Gtk_Main::instance()->quit();
- }
- void MainWindow::callback_movie_select_row
- ( gint row, gint col, GdkEvent *_button )
- {
- GdkEventButton *button = (GdkEventButton *)_button;
- int id = (int) movieList->get_row_data( row );
- const MovieItem &mi = session.get_movie_by_id( id );
- // if user click to first column, insert movie text into search
- // database
- if( col == 0 )
- {
- session.get_movie_text( id );
- update_pixmap( row );
- }
- movieListCurrentlySelectedMovie = id;
- // display this movie info
- dbMovieName->set_point( 0 );
- dbMovieName->forward_delete( dbMovieName->get_length() );
- dbMovieName->insert( Gdk_Font(), Gdk_Color( "black" ), Gdk_Color( "white" ),
- mi.get_name(), mi.get_name().length() );
- // comments
- comments->set_point( 0 );
- comments->forward_delete( comments->get_length() );
- comments->insert( Gdk_Font(), Gdk_Color( "black" ), Gdk_Color( "white" ),
- mi.get_comments(), mi.get_comments().length() );
- // display time
- const struct tm &t = mi.get_start();
- const struct tm &t2 = mi.get_stop();
- time_t tt1 = mktime( (struct tm *) &t );
- time_t tt2 = mktime( (struct tm *) &t2 );
- string s = string( "Recorded at " ) + asctime( &t );
- dbStartTime->set_text( s );
- int dt = (int) difftime( tt2, tt1 );
- char buf[200];
- sprintf( buf, "Duration is %2dh:%2dm:%2ds", dt / 3600, dt % 3600 / 60,
- dt % 60 );
- dbDuration->set_text( buf );
- }
- gint MainWindow::callback_movie_button_press( GdkEventButton *event )
- {
- if( event->type==GDK_2BUTTON_PRESS )
- {
- session.display_movie_text_window( movieListCurrentlySelectedMovie );
- }
- return FALSE;
- }
- void MainWindow::callback_preferences()
- {
- session.edit_preferences();
- }
- void MainWindow::callback_dbname()
- {
- string dbname = dbNameText->get_text();
- dbname.c_str();
- session.update_movie_list( dbname );
- }
- // ------------------------------------------------------
- void MainWindow::display_database_name( string &dbname )
- {
- dbNameLabel->set_text( string("Connected to ") + dbname );
- dbNameText->set_text( dbname );
- }
- void MainWindow::show_superuser_status( bool status )
- {
- if( statusBarContextId == 0 )
- statusBarContextId = statusBar->get_context_id( "superuser" );
- if( status )
- statusBar->push( statusBarContextId, "Superuser" );
- else
- statusBar->pop( statusBarContextId );
- }
- void MainWindow::status_changed_to_superuser( bool status )
- {
- Gtk_MenuItem *menuItem = new Gtk_MenuItem( "Server preferences..." );
- editMenu->append( *menuItem );
- menuItem->show();
-
- // connect entries
- connect_to_method( menuItem->activate, &session,
- &Session::callback_server_preferences );
- }
- void MainWindow::update_movie_list( CMapOfMoviesT &movies )
- {
- char text[movieListColumns][100];
- const gchar *texts[movieListColumns];
- for( int i = 0; i < movieListColumns; i++ )
- texts[i] = text[i];
- movieList->freeze();
- // destroy
- movieList->clear();
- // add
- CMapOfMoviesT::const_iterator it;
- for( it = movies.begin(); it != movies.end(); it ++ )
- {
- const MovieItem &mi = (*it).second;
- const struct tm &t = mi.get_start();
- // insert new row
- *text[0] = '';
- strcpy( text[1], (char *) mi.get_name().c_str() );
- sprintf( text[2], "%2d/%2d/%2d", t.tm_mon+1, t.tm_mday, t.tm_year );
- int row = movieList->append( texts );
- // pixmap
- Gdk_Pixmap pixmap;
- Gdk_Bitmap bitmap;
- string pmp_name;
- if( session.is_movie_text_loaded( (*it).first ) )
- textLoadedPixmap->get( pixmap, bitmap );
- else
- textNotLoadedPixmap->get( pixmap, bitmap );
- movieList->set_pixmap( row, 0, pixmap, bitmap );
- // row data
- movieList->set_row_data( row, (gpointer) (*it).first );
- }
- movieList->thaw();
- }
- void MainWindow::update_pixmap( int row )
- {
- int id = (int) movieList->get_row_data( row );
-
- // pixmap
- Gdk_Pixmap pixmap;
- Gdk_Bitmap bitmap;
- if( !session.is_movie_text_loaded( id ))
- return;
- textLoadedPixmap->get( pixmap, bitmap );
- movieList->set_pixmap( row, 0, pixmap, bitmap );
- }
- void MainWindow::update_pixmap_by_id( int id )
- {
- int row = movieList->find_row_from_data( (gpointer) id );
- if( row >= 0 )
- update_pixmap( row );
- }
- // ------------------------------------------------------
- void MainWindow::display_message( const string &msg )
- {
- logText->insert( Gdk_Font(), msg[0]=='#'? msg[1]=='!'? Gdk_Color( "red" ):
- Gdk_Color( "green" ): Gdk_Color( "black" ),
- Gdk_Color( "white" ), msg, msg.length() );
- if( toggleLogAuto->get_mode() == false )
- return;
- // auto scroll
- logText->set_point( logText->get_length() );
- }
- void MainWindow::display_messagenl( const string &msg )
- {
- display_message( msg );
- if( msg[ msg.length() - 1 ] != 'n' )
- display_message( "n" );
- }
- void MainWindow::display_messages( const vector< string > &msgs )
- {
- for( int i = 0; i < msgs.size(); i++ )
- display_messagenl( msgs[ i ] );
- }