#include #include #include #include static GdkColor couleur[] = { {0, 0xFFFF, 0x0, 0x0}, {0, 0xFFFF, 0x0, 0x7530}, {0, 0xFFFF, 0x7530, 0xFDE8}, }; struct _MainWindow { GtkWidget *fenetre, *Vbox, *Hbox, *bouton[1], *area; time_t temps; gboolean appui; guint test; int tps, jour, heure, min; }; typedef struct _MainWindow MainWindow; int Rectangle(GtkWidget *area, gpointer data); int EchelleCouleur(GtkWidget *area, gpointer data); int chrono_start(GtkWidget *, gpointer data); int chrono_stop(GtkWidget *, gpointer data); gboolean chrono(gpointer data); int choix(gpointer data, int k); int main(int argc, char **argv) { MainWindow *pf; pf = (MainWindow*)g_malloc(sizeof(MainWindow)); gtk_init (&argc, &argv); pf->fenetre = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_container_set_border_width(GTK_CONTAINER(pf->fenetre), 5); gtk_window_set_title(GTK_WINDOW(pf->fenetre), "Simulateur hydraulique de Colonnes Pulsees"); gtk_window_set_default_size(GTK_WINDOW(pf->fenetre), 320, 200); g_signal_connect(G_OBJECT(pf->fenetre), "destroy", G_CALLBACK(gtk_main_quit), NULL); pf->Vbox = gtk_vbox_new(FALSE, 0); gtk_container_add(GTK_CONTAINER(pf->fenetre), pf->Vbox); pf->Hbox = gtk_hbox_new(TRUE, 0); gtk_box_pack_start(GTK_BOX(pf->Vbox), pf->Hbox, FALSE, FALSE, 0); pf->bouton[0] = gtk_button_new_with_label("Lecture/Reprise"); gtk_box_pack_start(GTK_BOX(pf->Hbox), pf->bouton[0], TRUE, TRUE, 0); pf->bouton[1] = gtk_button_new_with_label("Pause"); gtk_box_pack_start(GTK_BOX(pf->Hbox), pf->bouton[1], TRUE, TRUE, 0); pf->area = gtk_drawing_area_new(); gtk_drawing_area_size((GtkDrawingArea*)pf->area, 500, 500); gtk_box_pack_start(GTK_BOX(pf->Vbox), pf->area, TRUE, TRUE, 5); g_signal_connect(G_OBJECT(pf->area), "expose-event", G_CALLBACK(Rectangle), (gpointer)pf); g_signal_connect(G_OBJECT(pf->area), "expose-event", G_CALLBACK(EchelleCouleur), (gpointer)pf); g_signal_connect(G_OBJECT(pf->bouton[0]), "clicked", G_CALLBACK(chrono_start), (gpointer*)pf); g_signal_connect(G_OBJECT(pf->bouton[1]), "clicked", G_CALLBACK(chrono_stop), (gpointer*)pf); gtk_widget_show_all(pf->fenetre); gtk_main(); g_free(pf); return EXIT_SUCCESS; } int Rectangle(GtkWidget *area, gpointer data) { MainWindow *pf; pf = (MainWindow*)data; gdk_draw_rectangle(area->window, area->style->black_gc, FALSE, 225, 100, 50, 300); for(int i=0; i<6; i++) { gdk_draw_rectangle(area->window, area->style->white_gc, FALSE, 225, 100+50*i, 50, 50); } return 0; } int EchelleCouleur(GtkWidget *area, gpointer data) { MainWindow *pf; pf = (MainWindow*)data; GdkFont *font = NULL; font = gdk_font_load ("-Adobe-Helvetica-Bold-R-Normal--*-100-*-*-*-*-*-*"); GdkColormap *colormap; colormap = gdk_drawable_get_colormap(area->window); for(int i=0; i<3; i++) { gdk_colormap_alloc_color(colormap,&couleur[i],TRUE, FALSE); gdk_gc_set_foreground(area->style->fg_gc[GTK_WIDGET_STATE(area)], &couleur[i]); gdk_draw_rectangle(area->window, area->style->fg_gc[GTK_WIDGET_STATE(area)], TRUE, 50, 240+55*i, 50, 50); } gdk_draw_string (area->window, font, area->style->black_gc, 105, 270, "0"); gdk_draw_string (area->window, font, area->style->black_gc, 105, 320, "10"); gdk_draw_string (area->window, font, area->style->black_gc, 105, 375, "20"); return 0; } int chrono_start(GtkWidget *bouton, gpointer data) { MainWindow *pf; pf = (MainWindow*)data; pf->appui = TRUE; pf->temps=time(NULL); pf->test = g_timeout_add(1, chrono, (gpointer)pf); return 0; } int chrono_stop(GtkWidget *bouton, gpointer data) { MainWindow *pf; pf = (MainWindow*)data; pf->appui = FALSE; gdk_draw_rectangle(pf->area->window, pf->area->style->white_gc, TRUE, 0, 0, 50, 50); return 0; } gboolean chrono(gpointer data) { MainWindow *pf; pf = (MainWindow*)data; GdkColormap *colormap; colormap = gdk_drawable_get_colormap(pf->area->window); GdkFont *font = NULL; font = gdk_font_load ("-Adobe-Helvetica-Bold-R-Normal--*-100-*-*-*-*-*-*"); for(int i=0; i<6; i++) { gdk_colormap_alloc_color(colormap,&couleur[i],TRUE, FALSE); gdk_gc_set_foreground(pf->area->style->fg_gc[GTK_WIDGET_STATE(pf->area)], &couleur[i]); gdk_draw_rectangle(pf->area->window, pf->area->style->fg_gc[GTK_WIDGET_STATE(pf->area)], TRUE, 225, 100+50*i, 50, 50); } gchar buf[32]; time_t tA=0; if (pf->appui==FALSE) { return pf->appui; } tA=time(NULL); //définir les textes des trois GtkEntry pour les heures et les minutes les secondes pf->tps=tA-pf->temps; pf->jour =pf->tps/(24*60*60); pf->tps-= pf->jour*24*60*60; pf->heure= pf->tps/(60*60); pf->tps-=pf->heure*60*60; pf->min =pf->tps/60; pf->tps-=pf->min*60; sprintf(buf,"%d h %d min %d sec",pf->heure, pf->min, pf->tps); gdk_draw_string (pf->area->window, font, pf->area->style->black_gc, 300, 50, buf); gtk_widget_queue_draw(GTK_WIDGET(pf->area)); gtk_widget_show_all(pf->fenetre); return pf->appui; }