Bonjour,

j'ai fait un petit programme en GTK+ avec trois fichiers : main.c, fonctions.c et header.h (Source ci-dessous)
Le programme fonctionne mais j'ai deux soucis :

- si j'ai le malheur d'ajouter un champs, voire augmenter la taille d'un des tableaux de ma structure GADGET, mon programme ne démarre pas.. je pense à un problème de mémoire mais je n'arrive pas à le résoudre . Est ce du à l'appel périodique d'une fonction. J'ai toujours utilisé cette structure et jusqu'ici je n'ai jamais rencontré ce problème.

- J'aurais aimé jouer un son quand le chrono arrive à 0. Comment faire ?

Merci pour vos réponses.

Exécutable pour Linux (sans runtime) compressé ZIP
header.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#ifndef DEF_CHRONO
#define DEF_CHRONO
 
#define LARGEUR     150
#define HAUTEUR     150
 
//structures
typedef struct GADGET GADGET;
 struct GADGET
 {
   GtkWidget * fenetre[1];
   GtkWidget * VBox[1];
   GtkWidget * HBox[2];
   GtkWidget * bouton[4];
   GtkWidget * entree[3];
   time_t temps;
   gboolean chrono; //valeur retournée dans la fct° périodique
   guint test; //numéro du processus
 };
 
 //fonctions
 void cadreFenetre(GADGET *);
 void accueil(GADGET *);
 void chrono(GtkWidget *, gpointer);
 void chrono_start(GtkWidget *, gpointer);
 void chrono_stop(GtkWidget *, gpointer);
 void retour_accueil(GtkWidget *, gpointer);
 void quitter(GtkWidget *, gpointer);
 void decompte(GtkWidget *, gpointer);
 gboolean  calcul_temps (gpointer);
 void decompte_start(GtkWidget *, gpointer);
 gboolean  calcul_decompte (gpointer);
 
#endif
main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>
#include "header.h"
 
int main(int argc,char **argv)
{
    /* Declaration du widget */
    GADGET gadget;
 
    gtk_init(&argc,&argv);
 
    /* Creation de la fenetre */
    gadget.fenetre[0] = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    cadreFenetre(&gadget);
    accueil(&gadget);
   /* Affichage de la fenetre */
 
    gtk_main();
 
    return EXIT_SUCCESS;
}
fonctions.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
 
#include <stdlib.h>
#include <gtk/gtk.h>
#include <time.h>
#include "header.h"
 
//Dessiner la fenêtre principale
void cadreFenetre(GADGET *p_gadget)
{
    /* Definition de la position */
    gtk_window_set_position(GTK_WINDOW(p_gadget->fenetre[0]), GTK_WIN_POS_CENTER);
    /* Taille de la Fenêtre */
    gtk_window_set_default_size  (GTK_WINDOW(p_gadget->fenetre[0]),LARGEUR,HAUTEUR );
    /* barre des titres*/
    gtk_window_set_decorated (GTK_WINDOW(p_gadget->fenetre[0]),TRUE);
    /*Icone*/
    //gtk_window_set_default_icon_from_file(ADRESSE_ICONE,NULL);
    /*espace de 1 autour de la fenêtre*/
    gtk_container_set_border_width(GTK_CONTAINER(p_gadget->fenetre[0]),1 );
}
 
//choix chrono ou décompte
void accueil(GADGET *p_gadget)
{
  if (p_gadget->VBox[0])
  {
      gtk_widget_destroy(p_gadget->VBox[0]);
  }
  p_gadget->VBox[0]=gtk_vbox_new(TRUE,0);
  gtk_container_add (GTK_CONTAINER(p_gadget->fenetre[0]),p_gadget->VBox[0]);
  p_gadget->bouton[0]=  gtk_button_new_with_label("Chronomètre") ;
  p_gadget->bouton[1]=  gtk_button_new_with_label("Décompte") ;
  gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->bouton[0],TRUE,TRUE,0);
  gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->bouton[1],TRUE,TRUE,0);
  gtk_widget_show_all(p_gadget->fenetre[0]);
 
  g_signal_connect(G_OBJECT(p_gadget->bouton[0]), "clicked", G_CALLBACK(chrono), (gpointer)p_gadget);
  g_signal_connect(G_OBJECT(p_gadget->bouton[1]), "clicked", G_CALLBACK(decompte), (gpointer)p_gadget);
  g_signal_connect(G_OBJECT(p_gadget->fenetre[0]), "destroy", G_CALLBACK(quitter), NULL);
 
}
 
//callback du bouton chronomètre --> affichage des GtkEntry et boutons
void chrono(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    GtkWidget *label;
    gchar buf[32];
 
    gtk_widget_destroy(p_gadget->VBox[0]);
    p_gadget->VBox[0]=gtk_vbox_new(TRUE,0);
    gtk_container_add (GTK_CONTAINER(p_gadget->fenetre[0]),p_gadget->VBox[0]);
    //les boutons
    p_gadget->bouton[0]=  gtk_button_new_with_label("Lancer") ;
    p_gadget->bouton[1]=   gtk_button_new_with_label("Stop") ;
    p_gadget->bouton[2]=   gtk_button_new_with_label("Accueil") ;
    p_gadget->bouton[3]=   gtk_button_new_with_label("Quitter") ;
 
    //les gtkEntry
    p_gadget->entree[0]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[0]),3);
    gtk_editable_set_editable(GTK_EDITABLE(p_gadget->entree[0]),FALSE);
    p_gadget->entree[1]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[1]),3);
    gtk_editable_set_editable(GTK_EDITABLE(p_gadget->entree[1]),FALSE);
    p_gadget->entree[2]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[2]),3);
    gtk_editable_set_editable(GTK_EDITABLE(p_gadget->entree[2]),FALSE);
 
    p_gadget->HBox[0]=gtk_hbox_new(FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->HBox[0],FALSE,FALSE,0);
 
    //entrer les widgets
    label=gtk_label_new("              ");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[0],FALSE,FALSE,0);
    label=gtk_label_new(":");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[1],FALSE,FALSE,0);
    label=gtk_label_new(":");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[2],FALSE,FALSE,0);
 
    p_gadget->HBox[1]=gtk_hbox_new(TRUE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->HBox[1],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[0],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[1],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[2],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[3],FALSE,FALSE,0);
    //texte des GtkEntry
     sprintf(buf,"%d",0);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[0]),buf);
     sprintf(buf,"%d",0);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[1]),buf);
     sprintf(buf,"%d",0);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[2]),buf);
 
    g_signal_connect(G_OBJECT(p_gadget->bouton[0]), "clicked", G_CALLBACK(chrono_start), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[1]), "clicked", G_CALLBACK(chrono_stop), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[2]), "clicked", G_CALLBACK(retour_accueil), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[3]), "clicked", G_CALLBACK(quitter), (gpointer)p_gadget);
    gtk_widget_show_all(p_gadget->fenetre[0]);
 
}
 
void chrono_start(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    p_gadget->chrono=TRUE; //valeur revoyée dans calcul-temps
    p_gadget->temps=time(NULL);
    p_gadget->test=g_timeout_add(1000,calcul_temps,p_gadget);
}
 
void chrono_stop(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
 
    p_gadget->chrono=FALSE; //valeur revoyée dans calcul-temps
}
 
void retour_accueil(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    g_source_remove(p_gadget->test); //arrêter le chrono
    accueil(p_gadget); //retour au double choix du début
}
 
void quitter(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
 
    g_source_remove(p_gadget->test); //arrêter le chrono
    gtk_main_quit();
}
 
gboolean  calcul_temps (gpointer data)
{
    gchar buf[32];
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    time_t tA=0;
    int tps=0,jour=0,heure=0,min=0;
 
    if (p_gadget->chrono==FALSE)
    {
    return p_gadget->chrono;
    }
 
     tA=time(NULL);
 
     //définir les textes des trois GtkEntry pour  les heures et les minutes les secondes
     tps=tA-p_gadget->temps;
     jour =tps/(24*60*60);
     tps-= jour*24*60*60;
     heure= tps/(60*60);
     tps-=heure*60*60;
     min =tps/60;
     tps-=min*60;
 
     sprintf(buf,"%d",heure);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[0]),buf);
     sprintf(buf,"%d",min);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[1]),buf);
     sprintf(buf,"%d",tps);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[2]),buf);
     gtk_widget_show_all(p_gadget->fenetre[0]);
 
 return p_gadget->chrono;
}
 
//callback du bouton décompte --> GtkENtry et boutons
void decompte(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    GtkWidget *label;
 
    gtk_widget_destroy(p_gadget->VBox[0]);
    p_gadget->VBox[0]=gtk_vbox_new(TRUE,0);
    gtk_container_add (GTK_CONTAINER(p_gadget->fenetre[0]),p_gadget->VBox[0]);
 
    label=gtk_label_new("Entrer le temps du compte à rebour\n h/m/s :");
    gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),label,FALSE,FALSE,0);
 
    //les GtkEntry
    p_gadget->entree[0]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[0]),3);
    p_gadget->entree[1]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[1]),3);
    p_gadget->entree[2]=gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(p_gadget->entree[2]),3);
 
    p_gadget->HBox[0]=gtk_hbox_new(FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->HBox[0],FALSE,FALSE,0);
 
    label=gtk_label_new("              ");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[0],FALSE,FALSE,0);
    label=gtk_label_new(":");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[1],FALSE,FALSE,0);
    label=gtk_label_new(":");
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),label,FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[0]),p_gadget->entree[2],FALSE,FALSE,0);
 
    p_gadget->HBox[1]=gtk_hbox_new(TRUE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->VBox[0]),p_gadget->HBox[1],FALSE,FALSE,0);
 
    //les boutons
    p_gadget->bouton[0]=  gtk_button_new_with_label("Lancer") ;
    p_gadget->bouton[1]=   gtk_button_new_with_label("Stop") ;
    p_gadget->bouton[2]=   gtk_button_new_with_label("Accueil") ;
    p_gadget->bouton[3]=   gtk_button_new_with_label("Quitter") ;
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[0],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[1],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[2],FALSE,FALSE,0);
    gtk_box_pack_start(GTK_BOX(p_gadget->HBox[1]),p_gadget->bouton[3],FALSE,FALSE,0);
 
    g_signal_connect(G_OBJECT(p_gadget->bouton[0]), "clicked", G_CALLBACK(decompte_start), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[1]), "clicked", G_CALLBACK(chrono_stop), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[2]), "clicked", G_CALLBACK(retour_accueil), (gpointer)p_gadget);
    g_signal_connect(G_OBJECT(p_gadget->bouton[3]), "clicked", G_CALLBACK(quitter), (gpointer)p_gadget);
 
    gtk_widget_show_all(p_gadget->fenetre[0]);
 
}
 
void decompte_start(GtkWidget *p_Bouton, gpointer data)
{
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    const gchar* buf=NULL;
 
    //Entrer le texte du temps à décompter dans les GtkEntry
    p_gadget->chrono=TRUE;
    buf= gtk_entry_get_text(GTK_ENTRY(p_gadget->entree[0]));
    p_gadget->temps=strtol(buf,NULL,10)*60*60;
    buf= gtk_entry_get_text(GTK_ENTRY(p_gadget->entree[1]));
    p_gadget->temps+=strtol(buf,NULL,10)*60;
    buf= gtk_entry_get_text(GTK_ENTRY(p_gadget->entree[2]));
    p_gadget->temps+=strtol(buf,NULL,10);
 
    p_gadget->test=g_timeout_add(1000,calcul_decompte,p_gadget);
 
}
 
gboolean  calcul_decompte (gpointer data)
{
    gchar buf[32];
    GADGET *p_gadget=NULL;
    p_gadget=(GADGET*)data;
    int tps=0,jour=0,heure=0,min=0;
 
    if (p_gadget->chrono==FALSE)
    {
    return p_gadget->chrono;
    }
     p_gadget->temps--;
 
 
     //texte des GtkEntry pendant le décompte
     tps=p_gadget->temps;
     jour =tps/(24*60*60);
     tps-= jour*24*60*60;
     heure= tps/(60*60);
     tps-=heure*60*60;
     min =tps/60;
     tps-=min*60;
 
     sprintf(buf,"%d",heure);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[0]),buf);
     sprintf(buf,"%d",min);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[1]),buf);
     sprintf(buf,"%d",tps);
     gtk_entry_set_text(GTK_ENTRY(p_gadget->entree[2]),buf);
     gtk_widget_show_all(p_gadget->fenetre[0]);
 
    return p_gadget->chrono;
}