了解事件是否发生(运行 gtk_dialog_run() 时)
如何通过检查 gtk_dialog_run() 的返回值来确定在对话框中单击了哪个控件/小部件
这就是我所拥有的
/*toolbar test thingy*/
#include <gtk/gtk.h>
#define MESSAGE_OK 1001
void mymessage (void){
GtkWidget *message = gtk_dialog_new_with_buttons("My dialog",
NULL,
GTK_DIALOG_MODAL,GTK_STOCK_OK,1001,NULL);
gtk_window_set_default_size(GTK_WINDOW(message),200,200);
gint result = gtk_dialog_run(GTK_DIALOG(message));
switch(result){
case MESSAGE_OK:
gtk_widget_destroy(message);
printf("you clicked message_ok");
break;
}
gtk_widget_destroy(message);
}
int main(int argc,char *argv[]){
GtkWidget *window;
GtkWidget *vertical_box;
GtkWidget *toolbar;
//GtkWidget *message;
GtkToolItem *tool_new;
gtk_init(&argc,&argv);
//setting up the main window.
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"rs-toolbar test");
gtk_window_set_default_size(GTK_WINDOW(window),300,350);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
//setting up the layout using vbox.
vertical_box = gtk_vbox_new(FALSE,0);
//adding the vbox layout to the Container window, window
gtk_container_add(GTK_CONTAINER(window),vertical_box);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
gtk_container_set_border_width(GTK_CONTAINER(toolbar),2);
tool_new = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),tool_new,-1);
//************************************************************************
//************************************************************************
gtk_box_pack_start(GTK_BOX(vertical_box),toolbar,FALSE,FALSE,0);
gtk_widget_show_all(window);
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(tool_new,"clicked",G_CALLBACK(mymessage),NULL);
gtk_main();
return 0;
}
是使用 gtk_dialog_new_with_buttons() 功能正确吗?我不完全确定如何为控件提供特定的 ID。我试图做的是将 1001 设置为 GTK_STOCK_OK 的 ID。
How do You determine what control/widget was clicked inside a dialog box by checking the return value of gtk_dialog_run()
This is what I have
/*toolbar test thingy*/
#include <gtk/gtk.h>
#define MESSAGE_OK 1001
void mymessage (void){
GtkWidget *message = gtk_dialog_new_with_buttons("My dialog",
NULL,
GTK_DIALOG_MODAL,GTK_STOCK_OK,1001,NULL);
gtk_window_set_default_size(GTK_WINDOW(message),200,200);
gint result = gtk_dialog_run(GTK_DIALOG(message));
switch(result){
case MESSAGE_OK:
gtk_widget_destroy(message);
printf("you clicked message_ok");
break;
}
gtk_widget_destroy(message);
}
int main(int argc,char *argv[]){
GtkWidget *window;
GtkWidget *vertical_box;
GtkWidget *toolbar;
//GtkWidget *message;
GtkToolItem *tool_new;
gtk_init(&argc,&argv);
//setting up the main window.
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"rs-toolbar test");
gtk_window_set_default_size(GTK_WINDOW(window),300,350);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
//setting up the layout using vbox.
vertical_box = gtk_vbox_new(FALSE,0);
//adding the vbox layout to the Container window, window
gtk_container_add(GTK_CONTAINER(window),vertical_box);
toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
gtk_container_set_border_width(GTK_CONTAINER(toolbar),2);
tool_new = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar),tool_new,-1);
//************************************************************************
//************************************************************************
gtk_box_pack_start(GTK_BOX(vertical_box),toolbar,FALSE,FALSE,0);
gtk_widget_show_all(window);
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(tool_new,"clicked",G_CALLBACK(mymessage),NULL);
gtk_main();
return 0;
}
Is the use of the gtk_dialog_new_with_buttons()
function correct?. I'm not entirely sure how to give a specific ID to a control. What I've tried to do there is make 1001 the ID of GTK_STOCK_OK
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将响应与错误的事物进行比较。
gtk_dialog_run
返回 GTK_RESPONSE 常量;尝试与开关中的按钮进行比较,看看按下了哪个按钮。You're comparing the response to the wrong thing.
gtk_dialog_run
returns one of the GTK_RESPONSE constants; try comparing against those in your switch to see which button was pressed.