please click the following URL to view the original text I posted.
This article will teach you how to create pop-up menu, sub menu, check
menu item, radio menu item, separator menu item, and make a menu item
grayed by GTK. the reason for I write this article is I did not found
any document about pop-up menu, check menu item, radio menu item in GTK
2.0 reference manual. I hope the article will be useful for you.Thank
you for viewing.I will be so sorry if my bad English writting confused
you.
In order to create a menu, please call the gtk_menu_new() function;this function returns a pointer to a menu widget.
GtkWidget *gtk_menu_new( void );
In order to create a menu item, please call one of the below three functions:
GtkWidget *gtk_menu_item_new( void );
GtkWidget *gtk_menu_item_new_with_label( const char *label );
GtkWidget *gtk_menu_item_new_with_mnemnonic( const char *label );
the only parameter of the gtk_menu_item_new_with_label()
functionrequired is menu text to display;the parameter of
thegtk_menu_item_new_with_mnemnonic() function is same to that of
thegtk_menu_item_new_with_label() function, but this
gtk_menu_item_new_with_mnemnonic() function create a menu item with an underlined accelerator.all of the three functions return a pointer to a menu item widget;
In order to create a check menu item, please call the
gtk_check_menu_item_new_with_label() function;the only parameter this
function required is check menu item text todisplay,this function
returns a pointer to a check menu item widget.
GtkWidget *gtk_check_menu_item_new_with_lab(const char *label);
In order to create a radio menu item, please call the
gtk_radio_menu_item_new_with_label() function;the only parameter this
function required is radio menu item text todisplay,this function
returns a pointer to a radio menu item widget.
GtkWidget *gtk_radio_menu_item_new_with_lab(const char *label);
In order to create a separator menu item, please call the
gtk_separator_menu_item_new() function;this function returns a pointer
to a separator menu item widget.
GtkWidget* gtk_separator_menu_item_new(void);
In order to make a menuitem grayed, please call the
gtk_widget_set_sensitive() function;this function enable or disable a
widget, the first parameter is awidget to enable or disable, the second
parameter takes a TRUE or FALSEvalue, which decide enable or disable
the widget.
void gtk_widget_set_sensitive(GtkWidget* widget, gboolean sensitive);
In order to append a menuitem to a menu, please call the
gtk_menu_shell_append() function;this function append a menu item to a
menu, the first parameter is a menu widget, the second parameter is a
menu item to append.
void gtk_menu_shell_append (GetWidget* menu, GetWidget* menu_item);
In order to pop up a menu, please call the gtk_menu_popup()
function;this function pop up a menu, the first parameter is a menu to
pop up.the second parameter is parent menu shell, which usually takes a
NULLvalue. the third parameter is parent menu item, which usually takes
aNULL value, the fourth parameter is menu position function,
whichusually takes a NULL value, the fifths parameter is a pointer to
theparameter of the fourth parameter, which usually takes a NULL
value,the sixth parameter takes the value of the member "button" of
theGdkEventButton structure, the seventh parameter takes the member
"time"of the GdkEventButton structure.
void gtk_menu_popup(GtkMenu *menu, GtkWidget *parent_menu_shell,
GtkWidget *parent_menu_item, GtkMenuPositionFunc func, gpointer data, guint button, guint32 activate_time);
In order to append a submenu to a menu item, please call the
gtk_menu_item_set_submenu() function;this function append a submenu to
a menu item,the first parameter is parent menu item, the second
parameter is a menu to append.
void gtk_menu_item_set_submenu(GtkMenuItem *menu_item,GtkWidget *submenu);
#ifndef __GTKMENU_H__
#define __GTKMENU_H__
#include <gtk/gtk.h>
void on_left_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_top_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_right_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_bottom_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_toolbar_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_statusbar_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_normal_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_normal_menuitem2_active(GtkMenuItem *menuitem, gpointer data);
void on_high_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_middle_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_low_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_popup_menuitem_active(GtkMenuItem *menuitem, gpointer data);
GtkWidget* CreateMenuItem(gchar* menuitemtext, GCallback func);
GtkWidget* CreateRadioMenuItem(GSList* group, gchar* menuitemtext, GCallback func);
GtkWidget* CreateSeparatorMenuItem();
GtkWidget* CreateCheckMenuItem(gchar* menuitemtext, GCallback func, gboolean checked);
GtkWidget* CreateSubMenu();
GtkWidget* CreatePopupMenu();
#endif
|
#include "GtkMenu.h"
#include <stdio.h>
void on_left_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Left\n");
}
void on_top_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Top\n");
}
void on_right_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Right\n");
}
void on_bottom_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Bottom\n");
}
void on_toolbar_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Toolbar\n");
}
void on_statusbar_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Statusbar\n");
}
void on_normal_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Normal menuitem\n");
}
void on_normal_menuitem2_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Normal menuitem2\n");
}
void on_high_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("High\n");
}
void on_middle_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("middle\n");
}
void on_low_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
printf("Low\n");
}
void on_popup_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
}
/*
* 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;
}
|
#include "GtkMenu.h"
/*
* 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;
}
|
阅读(3251) | 评论(0) | 转发(0) |