Re: How to put a pixmap on a GtkMenuItem ?



Hello,

Simply create a gtk_menu_item, in which you placed an hbox, which 
contains the pixmap and the associated label.
Here is an example :
/* CheckMenu.c */
#include <gtk/gtk.h>
static char *Icon[] = {
/*      width height num_colors char_per_pixel  */
  "	10	9	2	1",
/*      colors          */
  ". c None",
  "y c #ff1f00",
/*      pixels          */
  "..........",
  "........yy",
  ".......yy.",
  "......yy..",
  ".yyy.yy...",
  "..yyyy....",
  "...yy.....",
  "..........",
  "..........",
};

int
main (int argc, char *argv[])
{
  GtkWidget *Fenetre;
  GtkWidget *MenuBar;
  GtkWidget *Menu;
  GtkWidget *MenuItem;
  GtkWidget *Label;
  GtkWidget *hbox;

/*======================================================================
==*/
/*============== Pixmap declarations 
=====================================*/
/*======================================================================
==*/
  GtkWidget *Pixmap;
  GdkPixmap *PixmapIcon;
  GdkBitmap *MaskIcon;
  GdkColor transparent;
/*======================================================================
==*/
/*============== End Pixmap declarations 
=================================*/
/*======================================================================
==*/
/* Initialisation des bibliothèques */
  gtk_init (&argc, &argv);
/* Création de la fenêtre de dialogue */
  Fenetre = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* Connexion des signaux "delete" et "destroy" */
  gtk_signal_connect (GTK_OBJECT (Fenetre), "delete_event",
		      (GtkSignalFunc) gtk_exit, NULL);
  gtk_signal_connect (GTK_OBJECT (Fenetre), "destroy",
		      (GtkSignalFunc) gtk_exit, NULL);
/*======================================================================
==*/
/*============= Dealing with the pixmap 
==================================*/
/*======================================================================
==*/
  gtk_widget_realize (Fenetre);

  PixmapIcon = gdk_pixmap_create_from_xpm_d (Fenetre->window,
					     &MaskIcon, &transparent, Icon);
  Pixmap = gtk_pixmap_new (PixmapIcon, MaskIcon);
  gdk_pixmap_unref (PixmapIcon);
/*======================================================================
==*/
/*============= End Dealing with the pixmap 
==============================*/
/*======================================================================
==*/

/* Création et attachement de la barre de menu */
  MenuBar = gtk_menu_bar_new ();
  gtk_container_add (GTK_CONTAINER (Fenetre), MenuBar);
/* Création et attachement de la première entrée */
  MenuItem = gtk_menu_item_new_with_label ("Cases");
  gtk_menu_bar_append (GTK_MENU_BAR (MenuBar), MenuItem);
/* Création du menu associé à l'entrée "Cases" */
  Menu = gtk_menu_new ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (MenuItem), Menu);
/* L'entrée 1 */
  MenuItem = gtk_menu_item_new ();
/*======================================================================
==*/
/*============= Including the pixmap in the Menu Item 
====================*/
/*======================================================================
==*/
  hbox = gtk_hbox_new (FALSE, 5);
  gtk_box_pack_start_defaults (GTK_BOX (hbox), Pixmap);
  Label = gtk_label_new ("Case 1");
  gtk_box_pack_start_defaults (GTK_BOX (hbox), Label);
  gtk_container_add (GTK_CONTAINER (MenuItem), hbox);
/*======================================================================
==*/
/*============= End Including the pixmap in the Menu Item 
================*/
/*======================================================================
==*/
  gtk_menu_append (GTK_MENU (Menu), MenuItem);
/* L'entrée à cocher 2 */
  MenuItem = gtk_menu_item_new_with_label ("Case 2");
  gtk_menu_append (GTK_MENU (Menu), MenuItem);
/* Création et attachement de la dernière entrée */
  MenuItem = gtk_menu_item_new_with_label ("Quitter");
  gtk_menu_bar_append (GTK_MENU_BAR (MenuBar), MenuItem);
  gtk_signal_connect (GTK_OBJECT (MenuItem), "activate",
		      (GtkSignalFunc) gtk_exit, NULL);
  gtk_menu_item_right_justify (GTK_MENU_ITEM (MenuItem));
/* On affiche le tout */
  gtk_widget_show_all (Fenetre);
/* La boucle principale */
  gtk_main ();
}
Regards, Thierry Brichler



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]