Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

gtask-window.c

Go to the documentation of this file.
00001 #include <gtask-ui/gtask-window.h>
00002 
00003 #include <gtk/gtkvbox.h>
00004 #include <gtk/gtklabel.h>
00005 
00006 #include <gconf/gconf-client.h>
00007 
00008 #include "gtask-internals.h"
00009 
00012 /* @{ */
00013 
00026 #define GTASK_GCONF_ROOT_KEY "/apps/gtask"
00027 
00028 enum {
00029     COL_VIEW_ID,
00030     COL_VIEW_NAME,
00031     N_VIEW_COLUMNS
00032 };
00033 
00034 static gpointer parent_class;
00035 
00036 static void
00037 gtask_window_init( GTaskWindow *window, gpointer g_class );
00038 
00039 static void
00040 gtask_window_class_init( GTaskWindowClass *klass );
00041 
00042 static void
00043 gtask_window_destroy( GtkObject *obj );
00044 
00045 static void
00046 gtask_window_finalize( GObject *obj );
00047 
00048 /* callbacks */
00049 void
00050 filter_text_clear( GtkButton *button, gpointer data );
00051 
00052 void
00053 filter_text_changed( GtkEntry *text, gpointer data );
00054 
00055 void
00056 view_mode_changed( GObject *widget, gpointer data );
00057 
00058 GType gtask_window_get_type( void ) {
00059     static GType type = 0;
00060 
00061     if( type == 0 ) {
00062         static const GTypeInfo info = {
00063             sizeof( GTaskWindowClass ),
00064             NULL,   /* base init */
00065             NULL,   /* base_finalize */
00066             (GClassInitFunc) gtask_window_class_init,   /* class_init */
00067             NULL,   /* class_finalize */
00068             NULL,   /* class_data */
00069             sizeof( GTaskWindow ),
00070             0,      /* n_preallocs */
00071             (GInstanceInitFunc) gtask_window_init
00072         };
00073     
00074         type = g_type_register_static( GTK_TYPE_WINDOW,
00075                                        "GTaskWindow",
00076                                        &info,
00077                                        0 );
00078     }
00079 
00080     return type;
00081 }
00082 
00083 static void
00084 gtask_window_init_ui( GTaskWindow *window ) {
00085     GTaskWindowPrivate *private = window->private;
00086 
00087     GtkWidget          *vbox = gtk_vbox_new( FALSE, 6 );
00088     GtkWidget          *header_hbox = gtk_hbox_new( FALSE, 6 );
00089 
00090     GtkWidget          *view_hbox = gtk_hbox_new( FALSE, 6 );
00091     GtkWidget          *filter_hbox = gtk_hbox_new( FALSE, 6 );
00092 
00093     GtkWidget          *app_label;
00094     GtkWidget          *app_icon;
00095 
00096     GtkWidget          *view_as_label;
00097     GtkWidget          *view_as_combo;
00098 
00099     GtkWidget          *filter_label;
00100     GtkWidget          *filter_entry;
00101     GtkWidget          *filter_clear_button;
00102 
00103     GtkListStore       *list_store;
00104     GtkTreeIter         iter;
00105     GtkCellRenderer    *rend;
00106 
00107     char               *title;
00108 
00109     private->task_view = gtask_view_new( );
00110 
00111     gtk_container_set_border_width( GTK_CONTAINER( vbox ), 12 );
00112 
00113     gtk_box_pack_start( (GtkBox *) vbox, header_hbox, FALSE, FALSE, 0 );
00114     gtk_box_pack_start( (GtkBox *) vbox, view_hbox, FALSE, FALSE, 0 );
00115     gtk_box_pack_start( (GtkBox *) vbox, GTK_WIDGET( private->task_view ), TRUE, TRUE, 0 );
00116     gtk_box_pack_start( (GtkBox *) vbox, filter_hbox, FALSE, FALSE, 0 );
00117 
00118     /* set up the header */
00119     app_icon = gtk_image_new( );
00120     app_label = gtk_label_new( NULL );
00121 
00122     title = g_strdup_printf( "<span weight=\"bold\" size=\"x-large\">%s</span>",
00123                              private->window_title );
00124     
00125     gtk_label_set_markup( GTK_LABEL( app_label ), title );
00126     
00127     g_free( title );
00128 
00129     gtk_misc_set_alignment( GTK_MISC( app_label ), 0, 0.5 );
00130 
00131     gtk_box_pack_start( GTK_BOX( header_hbox ), app_icon, FALSE, FALSE, 0 );
00132     gtk_box_pack_start( GTK_BOX( header_hbox ), app_label, TRUE, TRUE, 0 );
00133 
00134     view_as_label = gtk_label_new( "View As:" );
00135     gtk_misc_set_alignment( GTK_MISC( view_as_label ), 0, 0.5 );
00136 
00137     list_store = gtk_list_store_new( N_VIEW_COLUMNS,
00138                                      GTASK_VIEW_MODE_TYPE,
00139                                      G_TYPE_STRING );
00140 
00144     gtk_list_store_append( list_store, &iter );
00145     gtk_list_store_set( list_store, &iter,
00146                         COL_VIEW_ID, GTASK_VIEW_MODE_BASIC,
00147                         COL_VIEW_NAME, "Basic View", 
00148                         -1 );
00149 
00150     gtk_list_store_append( list_store, &iter );
00151     gtk_list_store_set( list_store, &iter,
00152                         COL_VIEW_ID, GTASK_VIEW_MODE_LIST,
00153                         COL_VIEW_NAME, "List View", 
00154                         -1 );
00155 
00156     view_as_combo = gtk_combo_box_new_with_model( GTK_TREE_MODEL( list_store ) );
00157 
00158     g_signal_connect( G_OBJECT( view_as_combo ),
00159                       "changed",
00160                       (GCallback) view_mode_changed,
00161                       window );
00162 
00163     rend = gtk_cell_renderer_text_new( );
00164     gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( view_as_combo ), rend, TRUE );
00165     gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( view_as_combo ), rend, 
00166                                     "text", COL_VIEW_NAME,
00167                                     NULL );
00168 
00169     private->view_combo = GTK_COMBO_BOX( view_as_combo );
00170 
00171     gtk_box_pack_start( GTK_BOX( view_hbox ), view_as_label, TRUE, TRUE, 0 );
00172     gtk_box_pack_start( GTK_BOX( view_hbox ), view_as_combo, FALSE, FALSE, 0 );
00173 
00174     /* set up the footer */
00175     filter_label = gtk_label_new( "Filter" );
00176     filter_entry = gtk_entry_new( );
00177     filter_clear_button = gtk_button_new_with_label( "Show All" );
00178 
00179     private->filter_entry = GTK_ENTRY( filter_entry );
00180 
00181     g_signal_connect( filter_clear_button,
00182                       "clicked",
00183                       G_CALLBACK( filter_text_clear ),
00184                       filter_entry );
00185 
00186     g_signal_connect( filter_entry,
00187                       "changed",
00188                       G_CALLBACK( filter_text_changed ),
00189                       window );
00190 
00191     gtk_box_pack_start( GTK_BOX( filter_hbox ), filter_label, FALSE, FALSE, 0 );
00192     gtk_box_pack_start( GTK_BOX( filter_hbox ), filter_entry, TRUE, TRUE, 0 );
00193     gtk_box_pack_start( GTK_BOX( filter_hbox ), filter_clear_button, FALSE, FALSE, 0 );
00194 
00195     gtk_container_add( GTK_CONTAINER( window ), vbox );
00196 }
00197 
00198 static char *
00199 gtask_window_get_gconf_root( GTaskWindow *window ) {
00200     const char *prgname = g_get_prgname( );
00201     char       *root;
00202 
00203     g_debug( "IN: gtask_window_get_gconf_root" );
00204 
00205     if( prgname ) {
00206         root = g_strdup_printf( "%s/windows/%s/%s",
00207                                 GTASK_GCONF_ROOT_KEY,
00208                                 prgname,
00209                                 window->private->name );
00210     } else {
00211         /* just in case someone has mistakenly not set their application
00212          * name, I suppose we should give them a chance, meh */
00213         root = g_strdup_printf( "%s/default_window/%s",
00214                                 GTASK_GCONF_ROOT_KEY,
00215                                 window->private->name );
00216 
00217     }
00218 
00219     g_debug( "LEAVING: gtask_window_get_gconf_root" );
00220 
00221     return root;
00222 }
00223 
00224 static void
00225 gtask_window_restore_preferences( GTaskWindow *window ) {
00226     GConfClient *client = window->private->gconf_client;
00227 
00228     char *top_level = gtask_window_get_gconf_root( window );
00229 
00230     GString *key = g_string_new( NULL );
00231 
00232     gint x, y;
00233     gint width, height;
00234 
00235     char *filter_string;
00236     gint view_mode;
00237 
00238     g_debug( "IN: gtask_window_restore_preferences" );
00239 
00240     g_string_printf( key, "%s/x", top_level );
00241     x = gconf_client_get_int( client, key->str, NULL );
00242 
00243     g_string_printf( key, "%s/y", top_level );
00244     y = gconf_client_get_int( client, key->str, NULL );
00245 
00246     g_string_printf( key, "%s/width", top_level );
00247     width = gconf_client_get_int( client, key->str, NULL );
00248 
00249     g_string_printf( key, "%s/height", top_level );
00250     height = gconf_client_get_int( client, key->str, NULL );
00251 
00252     g_string_printf( key, "%s/filter_string", top_level );
00253     filter_string = gconf_client_get_string( client, key->str, NULL );
00254 
00255     g_string_printf( key, "%s/view_mode", top_level );
00256     view_mode = gconf_client_get_int( client, key->str, NULL );
00257 
00258     if( x > 0 && y > 0 )
00259         gtk_window_move( GTK_WINDOW( window ), x, y );
00260 
00261     if( width > 0 && height > 0 )
00262         gtk_window_resize( GTK_WINDOW( window ), width, height );
00263 
00264     if( view_mode != GTASK_VIEW_MODE_BASIC && view_mode != GTASK_VIEW_MODE_LIST )
00265         view_mode = GTASK_VIEW_MODE_BASIC;
00266 
00267     //gtk_combo_box_set_active( window->private->view_combo, view_mode );
00268 
00269     gtask_view_set_view_mode( window->private->task_view, view_mode );
00270 
00271     g_string_free( key, TRUE );
00272     g_free( top_level );
00273 
00274     g_debug( "LEAVING: gtask_window_restore_preferences" );
00275 }
00276 
00277 static void
00278 gtask_window_save_preferences( GTaskWindow *window ) {
00279     GTaskWindowPrivate *private = window->private;
00280     GConfClient        *client;
00281     char               *top_level;
00282     GTaskViewMode       view_mode;
00283 
00284     GString *key = g_string_new( NULL );
00285 
00286     g_debug( "IN: gtask_window_save_preferences" );
00287 
00288     if( !private->save_prefs ) {
00289         g_debug( "LEAVING: gtask_window_save_preferences w/o save" );
00290         return;
00291     }
00292 
00293     client = private->gconf_client;
00294     top_level = gtask_window_get_gconf_root( window );
00295 
00296     g_string_printf( key, "%s/x", top_level );
00297     gconf_client_set_int( client, key->str, private->x, NULL );
00298 
00299     g_string_printf( key, "%s/y", top_level );
00300     gconf_client_set_int( client, key->str, private->y, NULL );
00301 
00302     g_string_printf( key, "%s/width", top_level );
00303     gconf_client_set_int( client, key->str, private->width, NULL );
00304 
00305     g_string_printf( key, "%s/height", top_level );
00306     gconf_client_set_int( client, key->str, private->height, NULL );
00307 
00308     view_mode = gtask_view_get_view_mode( private->task_view );
00309 
00310     g_string_printf( key, "%s/view_mode", top_level );
00311     gconf_client_set_int( client, key->str, view_mode, NULL );
00312 
00313     private->save_prefs = FALSE;
00314 
00315     g_string_free( key, TRUE );
00316     g_free( top_level );
00317 
00318     g_debug( "LEAVING: gtask_window_save_preferences w/ save" );
00319 }
00320 
00321 static gboolean
00322 gtask_window_save_prefs_timeout( gpointer data ) {
00323     GTaskWindow *window = GTASK_WINDOW( data );
00324 
00325     g_debug( "IN: gtask_window_save_prefs_timeout" );
00326 
00327     gtask_window_save_preferences( window );
00328 
00329     window->private->save_prefs_timeout = 0;
00330 
00331     g_debug( "LEAVING: gtask_window_save_prefs_timeout" );
00332 
00333     return FALSE;
00334 }
00335 
00336 static void
00337 gtask_window_queue_prefs_save( GTaskWindow *window ) {
00338     GTaskWindowPrivate *private = window->private;
00339 
00340     g_warning( "IN: gtask_window_queue_prefs_save" );
00341 
00342     if( !private->save_prefs_timeout ) {
00343         private->save_prefs_timeout = g_timeout_add( 5000,
00344                                                 gtask_window_save_prefs_timeout,
00345                                                 window );
00346     }
00347 
00348     g_warning( "LEAVING: gtask_window_queue_prefs_save" );
00349 }
00350 
00351 static gboolean
00352 gtask_window_configure_event( GtkWidget *widget,
00353                               GdkEventConfigure *config,
00354                               gpointer data ) {
00355     GTaskWindow        *window = GTASK_WINDOW( widget );
00356     GTaskWindowPrivate *private = window->private;
00357 
00358     g_warning( "IN: gtask_window_configure_event" );
00359 
00360     private->x      = config->x;
00361     private->y      = config->y;
00362     private->width  = config->width;
00363     private->height = config->height;
00364 
00365     private->save_prefs = TRUE;
00366 
00367     gtask_window_queue_prefs_save( window );
00368 
00369     g_warning( "LEAVING: gtask_window_configure_event" );
00370 
00371     return FALSE;
00372 }
00373 
00374 static void
00375 gtask_window_init( GTaskWindow *window, gpointer g_class ) {
00376     GTaskWindowPrivate *private = g_new0( GTaskWindowPrivate, 1 );
00377 
00378     window->private = private;
00379 
00380     private->gconf_client = gconf_client_get_default( );
00381 
00382     private->save_prefs_timeout = 0;
00383     private->save_prefs = FALSE;
00384 
00389 //    gtask_window_init_ui( window );
00390 
00391     g_signal_connect( window,
00392                       "configure-event",
00393                       (GCallback) gtask_window_configure_event,
00394                       NULL );
00395 }
00396 
00397 static void
00398 gtask_window_class_init( GTaskWindowClass *klass ) {
00399     GObjectClass *gobject_class = G_OBJECT_CLASS( klass );
00400     GtkObjectClass *gtk_object_class = GTK_OBJECT_CLASS( klass );
00401 
00402     parent_class = g_type_class_peek_parent( klass );
00403 
00404     gobject_class->finalize = gtask_window_finalize;
00405 
00406     gtk_object_class->destroy = gtask_window_destroy;
00407 }
00408 
00409 static void
00410 gtask_window_destroy( GtkObject *obj ) {
00411     GTaskWindow *window = GTASK_WINDOW( obj );
00412 
00413     gtask_window_save_preferences( window );
00414 
00415     g_object_unref( (GObject *) window->private->gconf_client );
00416 
00418 }
00419 
00420 static void
00421 gtask_window_finalize( GObject *obj ) {
00423 }
00424 
00425 
00426 GTaskWindow *
00427 gtask_window_new_with_name( const char *name, const char *window_title ) {
00428     char *n_window_title;
00429 
00430     GTaskWindow *window = g_object_new( GTASK_WINDOW_TYPE, NULL );
00431 
00432     if( !window_title ) {
00433         const char *tmp = g_get_application_name( );
00434 
00435         if( !tmp )
00436             n_window_title = g_strdup( "Application Tasks" );
00437         else
00438             n_window_title = g_strdup( tmp );
00439     } else
00440         n_window_title = g_strdup( window_title );
00441 
00442     window->private->name = g_strdup( ( name ) ? name : "default" );
00443     window->private->window_title = n_window_title;
00444 
00445     gtask_window_init_ui( window );
00446 
00447     gtask_window_restore_preferences( window );
00448 
00449     return window;
00450 }
00451 
00452 void
00453 gtask_window_add_task( GTaskWindow *window, GTaskGenericTask *task ) {
00454     g_return_if_fail( GTASK_IS_WINDOW( window ) );
00455     g_return_if_fail( GTASK_IS_GENERIC_TASK( task ) );
00456 
00457     g_debug( "IN: gtask_window_add_task" );
00458 
00459     gtask_view_add_task( window->private->task_view, task );
00460 
00461     g_debug( "LEAVING: gtask_window_add_task" );
00462 }
00463 
00464 GTaskView *
00465 gtask_window_get_view( GTaskWindow *window ) {
00466     g_return_val_if_fail( GTASK_IS_WINDOW( window ), NULL );
00467 
00468     g_object_ref( window->private->task_view );
00469 
00470     return window->private->task_view;
00471 }
00472 
00473 /* callbacks */
00474 void
00475 filter_text_clear( GtkButton *button, gpointer data ) {
00476     GtkEntry *entry = GTK_ENTRY( data );
00477 
00478     g_debug( "IN: filter_text_clear" );
00479 
00480     gtk_editable_delete_text( GTK_EDITABLE( entry ), 0, -1 );
00481     
00482     g_debug( "LEAVING: filter_text_clear" );
00483 }
00484 
00485 void
00486 filter_text_changed( GtkEntry *text, gpointer data ) {
00487     GTaskWindow        *window = GTASK_WINDOW( data );
00488     GTaskWindowPrivate *private = window->private;
00489 
00490     g_debug( "IN: filter_text_changed" );
00491 
00492     gtask_view_set_filter_string( private->task_view,
00493                                   gtk_entry_get_text( text ) );
00494 
00495     g_debug( "LEAVING: filter_text_changed" );
00496 }
00497 
00498 void
00499 view_mode_changed( GObject *widget, gpointer data ) {
00500     GTaskWindow   *window = GTASK_WINDOW( data );
00501     GTaskViewMode  view_mode;
00502     GtkTreeIter    iter;
00503     GtkTreeModel  *model;
00504     GValue         value = { 0, };
00505 
00506     g_debug( "IN: view_mode_changed" );
00507 
00508     if( !gtk_combo_box_get_active_iter( GTK_COMBO_BOX( widget ), &iter ) )
00509         return;
00510 
00511     window->private->save_prefs = TRUE;
00512 
00513     model = gtk_combo_box_get_model( GTK_COMBO_BOX( widget ) );
00514 
00515     gtk_tree_model_get_value( model, &iter, COL_VIEW_ID, &value );
00516 
00517     view_mode = g_value_get_enum( &value );
00518 
00519     g_value_unset( &value );
00520 
00521     gtask_view_set_view_mode( window->private->task_view, view_mode );
00522 
00523     gtask_window_queue_prefs_save( window );
00524 
00525     g_debug( "LEAVING: view_mode_changed" );
00526 }
00527 
00528 /* @} */

Generated on Mon Feb 2 21:33:26 2004 for libgtask-ui by doxygen 1.3.4