Funziona, ma sinceramente non ho capito perchè! Che differenza fa la modalità di allocazione con lo scope?
#include <gtk/gtk.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct { GtkWidget *wid; char * lab; } Btn;
//restituisce un carattere casuale seguito da /0...
char *rand_character()
{
char * c = calloc( 2, sizeof(char));
c[0] = 'a' + rand() % ('z'-'a') ;
c[1] = 'Funziona, ma sinceramente non ho capito perchè! Che differenza fa la modalità di allocazione con lo scope?
#include <gtk/gtk.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
typedef struct { GtkWidget *wid; char * lab; } Btn;
//restituisce un carattere casuale seguito da /0...
char *rand_character()
{
char * c = calloc( 2, sizeof(char));
c[0] = 'a' + rand() % ('z'-'a') ;
c[1] = '{parsed_message}';
return c;
}
static void grf_get_label( GtkWidget *widget, gpointer data )
{
printf("%s\n", ((Btn *)data)->lab);
}
//restituisce una tastiera 4*4 con dei caratteri aleatori come etichette, ad ogni pulsante è collegata
//una funzione "grf_get_label" che stampa in stdout il carattere d'etichetta del bottone premuto.
GtkWidget * grf_tastiera()
{
GtkWidget *table;
int rows, cols;
Btn **button;
button = (Btn **) calloc( 4, sizeof(Btn *));
for (rows=0; rows<4; rows++)
button[rows]= (Btn *) calloc( 4, sizeof(Btn));
table = gtk_table_new (4, 4, TRUE);
srand(time(NULL));
for ( rows = 0; rows < 4; rows++ )
for ( cols = 0; cols < 4; cols++ )
button[rows][cols].lab = rand_character();
for ( rows = 0; rows < 4; rows++ )
for ( cols = 0; cols < 4; cols++ )
{
button[rows][cols].wid = gtk_toggle_button_new_with_label(button[rows][cols].lab);
gtk_table_attach (GTK_TABLE (table), button[rows][cols].wid, cols, cols + 1,
rows, rows + 1, GTK_FILL | GTK_EXPAND,
GTK_FILL | GTK_EXPAND, 0, 0);
g_signal_connect (G_OBJECT (button[rows][cols].wid), "clicked",
G_CALLBACK (grf_get_label), &button[rows][cols]);
}
return table;
}
int main(int argc, char *argv[])
{
GtkWidget * window;
gtk_init (&argc, &argv);
//finestra principale
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Tastiera");
gtk_window_set_default_size(GTK_WINDOW(window) , 200 , 210);
gtk_window_set_position (GTK_WINDOW (window) , GTK_WIN_POS_CENTER);
gtk_container_set_border_width (GTK_CONTAINER (window), 9);
//posiziona una tastiera 4*4 nella finestra
gtk_container_add (GTK_CONTAINER (window), grf_tastiera());
gtk_widget_show_all (window);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
gtk_main ();
return 0;
}
';
return c;
}
static void grf_get_label( GtkWidget *widget, gpointer data )
{
printf("%s\n", ((Btn *)data)->lab);
}
//restituisce una tastiera 4*4 con dei caratteri aleatori come etichette, ad ogni pulsante è collegata
//una funzione "grf_get_label" che stampa in stdout il carattere d'etichetta del bottone premuto.
GtkWidget * grf_tastiera()
{
GtkWidget *table;
int rows, cols;
Btn **button;
button = (Btn **) calloc( 4, sizeof(Btn *));
for (rows=0; rows<4; rows++)
button[rows]= (Btn *) calloc( 4, sizeof(Btn));
table = gtk_table_new (4, 4, TRUE);
srand(time(NULL));
for ( rows = 0; rows < 4; rows++ )
for ( cols = 0; cols < 4; cols++ )
button[rows][cols].lab = rand_character();
for ( rows = 0; rows < 4; rows++ )
for ( cols = 0; cols < 4; cols++ )
{
button[rows][cols].wid = gtk_toggle_button_new_with_label(button[rows][cols].lab);
gtk_table_attach (GTK_TABLE (table), button[rows][cols].wid, cols, cols + 1,
rows, rows + 1, GTK_FILL | GTK_EXPAND,
GTK_FILL | GTK_EXPAND, 0, 0);
g_signal_connect (G_OBJECT (button[rows][cols].wid), "clicked",
G_CALLBACK (grf_get_label), &button[rows][cols]);
}
return table;
}
int main(int argc, char *argv[])
{
GtkWidget * window;
gtk_init (&argc, &argv);
//finestra principale
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Tastiera");
gtk_window_set_default_size(GTK_WINDOW(window) , 200 , 210);
gtk_window_set_position (GTK_WINDOW (window) , GTK_WIN_POS_CENTER);
gtk_container_set_border_width (GTK_CONTAINER (window), 9);
//posiziona una tastiera 4*4 nella finestra
gtk_container_add (GTK_CONTAINER (window), grf_tastiera());
gtk_widget_show_all (window);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
gtk_main ();
return 0;
}