免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4852 | 回复: 5
打印 上一主题 下一主题

[Gtk programming]Teach you to create check menu item, radio menu item [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-20 22:06 |只看该作者 |倒序浏览
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);






[ 本帖最后由 luojiafeng1984 于 2008-6-21 12:15 编辑 ]

RadioMenuItem.rar

1.65 KB, 下载次数: 85

RadioMenuItem.rar

2.28 KB, 下载次数: 59

论坛徽章:
0
2 [报告]
发表于 2008-06-21 12:00 |只看该作者
#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

论坛徽章:
0
3 [报告]
发表于 2008-06-21 12:06 |只看该作者
#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;
}

论坛徽章:
0
4 [报告]
发表于 2008-06-21 12:06 |只看该作者
#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;
}


[ 本帖最后由 luojiafeng1984 于 2008-6-21 12:08 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-06-21 12:12 |只看该作者
编译用
gcc RadioMenuItem.c GtkMenu.c `pkg-config --cflags --libs gtk+-2.0`

论坛徽章:
0
6 [报告]
发表于 2008-06-21 12:36 |只看该作者
这篇文章将教你如何用GTK创建弹出菜单,子菜单,复选菜单项,单选菜单项,分隔菜单项,以及如何让一个菜单项变灰。我在GTK 2.0参考手册上没有找到关于弹出菜单项,复选菜单项,单选菜单项的文档,所以就写了这篇文章。希望对大家有用,谢谢看贴。我的英文写作能力很差,希望不要把你们搞晕了。

    要创建一个菜单,请调用gtk_menu_new() 函数,这个函数返回一个指向菜单构件的指针。
GtkWidget *gtk_menu_new( void );

    要创建一个菜单项,请调用下列三个函数之一:
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 );
    gtk_menu_item_new_with_label()函数所需要的唯一参数是要显示的菜单文字。gtk_menu_item_new_with_mnemnonic()函数的参数和gtk_menu_item_new_with_label()函数的参观者数一样,但是这个函数将创建一个带有下划线的快捷菜单项,所有这三个函数都返回一个指向菜单项构件的指针。

    要创建复选菜单项,请调用gtk_check_menu_item_new_with_label()函数。这个函数需要的唯一参数是要显示的复选菜单项文字。这个函数返回一个指向复选菜单项构件的指针。
GtkWidget *gtk_check_menu_item_new_with_lab(const char *label);

    要创建单选菜单项,请调用gtk_radio_menu_item_new_with_label()函数。这个函数需要的唯一 参数是要显示的单选菜单项文字。 这个函数返回一个指向单选菜单项构件的指针。
GtkWidget *gtk_radio_menu_item_new_with_lab(const char *label);

    要创建一个分隔菜单项,请调用 gtk_separator_menu_item_new()函数。这个函数返回一个指向分隔菜单项构件的指针。
GtkWidget* gtk_separator_menu_item_new(void);


    要使一个菜单项变灰,请调用gtk_widget_set_sensitive()函数,这个函数启用或禁用一个构件。第一个参数是要启用或禁用的构件,第二个参数取TRUE或FALSE值,该值决定启用或禁用一个构件。
void gtk_widget_set_sensitive(GtkWidget* widget, gboolean sensitive);


    为了追加一个菜单项到菜单,请调用gtk_menu_shell_append() 函数,这个函数的第一个参数是菜单项构件指针,第二个参数是要追加的菜单项。
void gtk_menu_shell_append (GetWidget* menu, GetWidget* menu_item);

    要弹出一个菜单,请调用gtk_menu_popup() 函数。这个函数弹出一个菜单,第一个参数是要弹出的菜单,第二个参数是父菜单shell, 通常取NULL值。第三个参数是父菜单项,通常取NULL值。第四个参数是菜单位置函数,通常取NULL值。第五个参数是第四个参数的参数,通常取NULL值。第六个参数取GdkEventButton结构体的button成员值。第七个参数取GdkEventButton结构体的time成员值。

void gtk_menu_popup(GtkMenu *menu, GtkWidget *parent_menu_shell,
        GtkWidget *parent_menu_item, GtkMenuPositionFunc func, gpointer data,
        guint button, guint32 activate_time);


    为了追加一个子菜单到菜单项,请调用gtk_menu_item_set_submenu()函数。这个函数追加一个子菜单到菜单项。第一个参数是父菜单项,第二个参数是要追加的子菜单。
void gtk_menu_item_set_submenu(GtkMenuItem *menu_item,GtkWidget *submenu);




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP