#include <stdio.h>
#include <gtk/gtk.h>void on_left_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Leftn");
}
/*
* Function: CreateMenuItem
* Description: the function create a normal menu item
* Parameters:menuitemtext[in] menu text to display
func [in] call back function of the menu item
* Returns: a pointer to a menu item widget
*/
GtkWidget* CreateMenuItem(gchar* menuitemtext, GCallback func)
{
GtkWidget *menuitem = gtk_menu_item_new_with_label (menuitemtext);
g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
return menuitem;
}
/*
* Function: CreateRadioMenuItem
* Description: the function create a radio menu item
* Parameters:group[in] the radio menu item will be created will append to this group
menuitemtext[in] menu text to display
func [in] call back function of the menu item
* Returns: a pointer to a radio menu item widget
*/
GtkWidget* CreateRadioMenuItem(GSList* group, gchar* menuitemtext, GCallback func)
{
GtkWidget *menuitem = gtk_radio_menu_item_new_with_label (group, menuitemtext);
g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
return menuitem;
}
/*
* Function: CreateSeparatorMenuItem
* Description: the function create a separator menu item
* Parameters:none
* Returns: a pointer to a separator menu item widget
*/
GtkWidget* CreateSeparatorMenuItem()
{
return gtk_separator_menu_item_new();
}
/*
* Function: CreateCheckMenuItem
* Description: the function create a check menu item
* Parameters:menuitemtext[in] menu text to display
func [in] call back function of the menu item
checked[in] which takes a TRUE or FALSE value and decide to check or uncheck the menu item
* Returns: a pointer to a check menu item widget
*/
GtkWidget* CreateCheckMenuItem(gchar* menuitemtext, GCallback func, gboolean checked)
{
GtkWidget* menuitem = gtk_check_menu_item_new_with_label (menuitemtext);
g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
gtk_check_menu_item_set_active((GtkCheckMenuItem *)menuitem, checked);
return menuitem;
}
/*
* Function: CreateSubMenu
* Description: the function create a submenu
* Parameter:none
* Returns: a pointer to a menu widget
*/
GtkWidget* CreateSubMenu()
{
GtkWidget* menu = gtk_menu_new ();
GtkWidget* menu_item = CreateRadioMenuItem(NULL, "High", GTK_SIGNAL_FUNC(on_high_menuitem_active));
GtkWidget* normal_menu_item;
GSList* group = gtk_radio_menu_item_get_group((GtkRadioMenuItem *)menu_item);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), menu_item);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Middle", GTK_SIGNAL_FUNC(on_middle_menuitem_active)));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Low", GTK_SIGNAL_FUNC(on_low_menuitem_active)));
gtk_widget_show_all(menu);
return menu;
}
/*
* Function: CreatePopupMenu
* Description: the function create a pop-up menu
* Parameter:none
* Returns: a pointer to a menu widget
*/
GtkWidget* CreatePopupMenu()
{
/* create a menu */
GtkWidget* menu = gtk_menu_new ();
GtkWidget* menu_item = CreateRadioMenuItem(NULL, "Left", GTK_SIGNAL_FUNC(on_left_menuitem_active));
GtkWidget* normal_menu_item;
GtkWidget* popmenuitem;
/* append radio menu items to the menu */
GSList* group = gtk_radio_menu_item_get_group((GtkRadioMenuItem *)menu_item);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), menu_item);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Top", GTK_SIGNAL_FUNC(on_top_menuitem_active)));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Right", GTK_SIGNAL_FUNC(on_right_menuitem_active)));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Bottom", GTK_SIGNAL_FUNC(on_bottom_menuitem_active)));
/* append separator menu item to the menu */
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateSeparatorMenuItem());
/* append check menu items to the menu */
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateCheckMenuItem("Toolbar", GTK_SIGNAL_FUNC(on_toolbar_menuitem_active), TRUE));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateCheckMenuItem("Statusbar", GTK_SIGNAL_FUNC(on_statusbar_menuitem_active), FALSE));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateSeparatorMenuItem());
/* append normal menu items to the menu */
normal_menu_item = CreateMenuItem("Normal menuitem", GTK_SIGNAL_FUNC(on_normal_menuitem_active));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), normal_menu_item);
/* disable a normal menu item */
gtk_widget_set_sensitive(GTK_WIDGET(normal_menu_item), FALSE);
gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateMenuItem("Normal menuitem2", GTK_SIGNAL_FUNC(on_normal_menuitem2_active)));
/* create a submenu and append to a menu item */
popmenuitem = CreateMenuItem("Popup", GTK_SIGNAL_FUNC(on_popup_menuitem_active));
gtk_menu_shell_append (GTK_MENU_SHELL(menu), popmenuitem);
gtk_menu_item_set_submenu((GtkMenuItem *)popmenuitem, CreateSubMenu());
gtk_widget_show_all(menu);
return menu;
}
/*
* Function:on_button_press_event
* Description: call back funtion of the button press event
* Returns: TRUE if the mouse right button be clicked, otherwise, retuns FALSE
*/
gboolean on_button_press_event(GtkWidget *widget, GdkEventButton *event)
{
if (event-> button ==3)
{
gtk_menu_popup (GTK_MENU(CreatePopupMenu()), NULL, NULL, NULL, NULL, event->button, event->time);
return TRUE;
}
return FALSE;
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *menu_items;
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "GTK Radio Menu Item Test");
gtk_widget_set_usize(GTK_WIDGET(window), 300, 300);
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
/* create a button*/
button = gtk_button_new_with_label( "right click here to display the pop-up menu" );
g_signal_connect (G_OBJECT (button), "button_press_event",G_CALLBACK (on_button_press_event), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
/* display the window */
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
发布评论