1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| /* Exemple simple de mise à jour en dynamique d'un label /*
#include <stdlib.h>
#include <gtk/gtk.h>
int i=0,flag_depart=0;
int main(int argc, char **argv)
{
/* déclaration des widgets */
GtkWidget *pWindow; /*fenetre principale */
GtkWidget *pLabel; /* un label */
GtkWidget *pTitre; /* un autre label */
GtkWidget *pVBox; /* la box verticale */
GtkWidget *pHBox; /* la box horizontale */
GtkWidget *pButtonReset; /* bouton reset */
GtkWidget *pButtonStart; /* bouton start */
GtkWidget *pButtonStop; /* bouton start */
gchar* sUtf8; /* pour formater des chaines de characteres */
guint period;
void OnDestroy(GtkWidget *pWidget, gpointer pData); /* fonction call back destroy */
gboolean OnExpose(gpointer pData); /* fonction call back affichage */
void Press_Start(GtkButton *button, gpointer user_data); /* fonction call back si start */
void Press_Reset(GtkButton *button, gpointer user_data); /* fonction call back si reset */
void Press_Stop(GtkButton *button, gpointer user_data); /* fonction call back si stop */
/* Initialisation de GTK+ */
gtk_init(&argc, &argv);
/* création de la fenetre */
pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/* définition des paramètres de la fenetre */
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(pWindow), 500, 500);
gtk_window_set_title(GTK_WINDOW(pWindow), "Test affichage en dynamique");
/* création de la box verticale */
pVBox = gtk_vbox_new(FALSE, 0);
/* création de la box horizontale */
pHBox = gtk_hbox_new(TRUE, 0);
/* création du label */
pLabel=gtk_label_new(NULL);
sUtf8 = g_locale_to_utf8("<span font_desc=\"25\"><b>0</b></span>", -1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(pLabel), sUtf8);
g_free(sUtf8);
/* création du titre */
pTitre=gtk_label_new(NULL);
sUtf8 = g_locale_to_utf8("<span font_desc=\"15\" background=\"#000000\" foreground=\"#FFFFFF\"><b>Test affichage en dynamique</b></span>", -1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(pTitre), sUtf8);
g_free(sUtf8);
/* création du bouton reset */
pButtonReset = gtk_button_new_with_label("Reset");
/* création du bouton start */
pButtonStart = gtk_button_new_with_label("Start");
/* création du bouton stop */
pButtonStop = gtk_button_new_with_label("Stop");
/* AJout du titre dans la GtkHBox */
gtk_box_pack_start(GTK_BOX(pVBox), pTitre, FALSE, TRUE, 10);
/* Ajout des boutons reset et start dans la GtkHBox */
gtk_box_pack_start(GTK_BOX(pHBox), pButtonStart, FALSE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(pHBox), pButtonStop, FALSE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(pHBox), pButtonReset, FALSE, TRUE, 0);
/* ajout de la boite horizontale dans la boite verticale */
gtk_box_pack_start(GTK_BOX(pVBox), pHBox, FALSE, TRUE, 0);
/* ajout du label dans la GtkVBox */
gtk_box_pack_start(GTK_BOX(pVBox), pLabel, TRUE, TRUE, 0);
/* ajout de la GtkVBox à la fenetre */
gtk_container_add(GTK_CONTAINER(pWindow), pVBox);
/* Connexion des signaux */
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(OnDestroy), NULL);
g_signal_connect(G_OBJECT(pButtonStart),"pressed", G_CALLBACK(Press_Start), NULL);
g_signal_connect(G_OBJECT(pButtonStop),"pressed", G_CALLBACK(Press_Stop), NULL);
g_signal_connect(G_OBJECT(pButtonReset),"pressed", G_CALLBACK(Press_Reset), GTK_LABEL(pLabel));
/* fonction g_timeout_add () ici */
period=(guint)1000;
g_timeout_add (period, OnExpose, GTK_LABEL(pLabel));
/*affichage de la fenetre */
gtk_widget_show_all(pWindow);
/* Demarrage de la boucle evenementielle */
gtk_main();
return EXIT_SUCCESS;
}
void OnDestroy(GtkWidget *pWidget, gpointer pData){
/* Arret de la boucle evenementielle */
gtk_main_quit();
}
gboolean OnExpose(gpointer pData){
/* Mise a jour du label */
char tempo[1000];
gchar* sUtf8;
if (flag_depart)
{
(void)sprintf(tempo, "<span font_desc=\"25\"><b>%d</b></span>", i);
sUtf8 = g_locale_to_utf8(tempo, -1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(pData), tempo);
i++;
}
g_free(sUtf8);
return TRUE;
}
void Press_Reset(GtkButton *button, gpointer user_data) {
/* reset compteur */
i=0;
char tempo[1000];
gchar* sUtf8;
(void)sprintf(tempo, "<span font_desc=\"25\"><b>%d</b></span>", 0);
sUtf8 = g_locale_to_utf8(tempo, -1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(user_data), tempo);
g_free(sUtf8);
}
void Press_Start(GtkButton *button, gpointer user_data) {
/* start compteur */
flag_depart=1;
}
void Press_Stop(GtkButton *button, gpointer user_data) {
/* stop compteur */
flag_depart=0;
} |
Partager