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
| #include <gtk/gtk.h>
gboolean
draw_background (GtkWidget *widget, cairo_t *cr, gpointer data)
{
/* Conversion du pointer en gint puis en double */
gdouble color = (gdouble)GPOINTER_TO_INT (data)/255;
/* Récupération de la taille du widget */
GtkAllocation allocation;
gtk_widget_get_allocation (widget, &allocation);
/* On fixe la couleur transmise, on efface un chemin existant et on remplit
* le widget. La subtilité pour le remplissage réside dans les coordonnées
* du widget.
* Si on affiche les coordonnées récupérées dans allocation elles ne
* correspondent pas. Chaque GtkMenuItem a pour coordonnées (0, 0).
*/
cairo_set_source_rgb (cr, color, color, color);
cairo_new_path (cr);
cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
cairo_fill (cr);
return FALSE;
}
gint
main (gint argc, gchar *argv[])
{
GtkWidget *window = NULL;
GtkWidget *box = NULL;
GtkWidget *menu = NULL;
GtkWidget *filemenu = NULL;
GtkWidget *submenu = NULL;
GtkWidget *menuitem = NULL;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), box);
menu = gtk_menu_bar_new();
gtk_box_pack_start (GTK_BOX (box), menu, FALSE, FALSE, 0);
filemenu = gtk_menu_item_new_with_label ("Fichier");
gtk_menu_shell_append (GTK_MENU_SHELL (menu), filemenu);
submenu = gtk_menu_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (filemenu), submenu);
/* Création de lignes exemple */
gint i;
gchar *text = NULL;
for (i=0; i<10; i++)
{
text = g_strdup_printf ("ligne %d", i+1);
menuitem = gtk_menu_item_new_with_label (text);
gtk_menu_shell_append (GTK_MENU_SHELL (submenu), menuitem);
g_free (text);
/* Ajout d'un callback à chaque ligne pour changer la couleur de fond */
if (i%2)
g_signal_connect (G_OBJECT (menuitem), "draw", (GCallback)draw_background, GINT_TO_POINTER (193));
else
g_signal_connect (G_OBJECT (menuitem), "draw", (GCallback)draw_background, GINT_TO_POINTER (152));
}
g_signal_connect (G_OBJECT (window), "destroy", (GCallback) gtk_main_quit, NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
} |
Partager