It seems that GtkDialog automatically sets the focus on the left-most button (which is "Cancel" in my case). I want to change this default focus to another button, but I cannot go the route of "gtk_dialog_set_default_response" because I have packed the buttons manually into the dialogs action area.
GtkDialog似乎自动将焦点放在最左边的按钮(在我的例子中是“Cancel”)上。我想将这个默认焦点更改为另一个按钮,但是我不能使用“gtk_dialog_set_default_response”的路径,因为我已经将按钮手工打包到dialogs操作区域。
Then, while searching the API doc up and down, I realized that GtkDialog is a descendent of GtkWindow, and thus tried "gtk_window_set_default", which at first gave me some sort of "assertion `gtk_widget_get_can_default (default_widget)' failed" warning. To comply, I used "gtk_widget_set_can_default" on the button, and the warning disappeared.. BUT: the focus is still being set on the "Cancel" button.
然后,在上下搜索API doc时,我意识到GtkDialog是GtkWindow的后代,因此尝试了“gtk_window_set_default”,这首先给了我某种“断言”gtk_widget_can_default (default_widget)“失败”警告。为了遵守,我在按钮上使用了“gtk_widget_set_can_default”,警告消失了。但是:焦点仍然在“取消”按钮上。
Is there really no way other than having to use "gtk_dialog_add_action_widget"?
难道真的只有使用“gtk_dialog_add_action_widget”才能解决问题吗?
1 个解决方案
#1
3
Just use gtk_widget_grab_focus
on the widget which you want to have focus on. The widget has to be focusable, which is true by default in case of a button. Here is a sample code for your reference:
只需将gtk_widget_grab_focus放在您想要关注的小部件上。小部件必须是可调焦的,这在按钮的情况下是默认的。这里有一个示例代码供您参考:
#include <gtk/gtk.h>
/* Uncomment the below macro to see the default focus */
//#define DEFAULT_FOCUS
int main(void)
{
gtk_init (NULL, NULL);
#ifdef DIALOG_WITH_BUTTONS
GtkWidget * dialog = gtk_dialog_new_with_buttons ("Dialog",
NULL,
GTK_DIALOG_MODAL,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
NULL);
#ifndef DEFAULT_FOCUS
gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK));
#endif
#else
GtkWidget *dialog = gtk_dialog_new();
gtk_window_set_title(GTK_WINDOW(dialog), "Dialog");
GtkWidget *action_area = gtk_dialog_get_action_area(GTK_DIALOG(dialog));
GtkWidget *ok_button = gtk_button_new_with_label("OK");
GtkWidget *cancel_button = gtk_button_new_with_label("Cancel");
gtk_container_add(GTK_CONTAINER(action_area), cancel_button);
gtk_container_add(GTK_CONTAINER(action_area), ok_button);
gtk_widget_show_all(dialog);
#ifndef DEFAULT_FOCUS
gtk_widget_grab_focus(ok_button);
#endif
#endif
g_signal_connect(dialog, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_main();
return 0;
}
Hope this helps!
希望这可以帮助!
#1
3
Just use gtk_widget_grab_focus
on the widget which you want to have focus on. The widget has to be focusable, which is true by default in case of a button. Here is a sample code for your reference:
只需将gtk_widget_grab_focus放在您想要关注的小部件上。小部件必须是可调焦的,这在按钮的情况下是默认的。这里有一个示例代码供您参考:
#include <gtk/gtk.h>
/* Uncomment the below macro to see the default focus */
//#define DEFAULT_FOCUS
int main(void)
{
gtk_init (NULL, NULL);
#ifdef DIALOG_WITH_BUTTONS
GtkWidget * dialog = gtk_dialog_new_with_buttons ("Dialog",
NULL,
GTK_DIALOG_MODAL,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
NULL);
#ifndef DEFAULT_FOCUS
gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK));
#endif
#else
GtkWidget *dialog = gtk_dialog_new();
gtk_window_set_title(GTK_WINDOW(dialog), "Dialog");
GtkWidget *action_area = gtk_dialog_get_action_area(GTK_DIALOG(dialog));
GtkWidget *ok_button = gtk_button_new_with_label("OK");
GtkWidget *cancel_button = gtk_button_new_with_label("Cancel");
gtk_container_add(GTK_CONTAINER(action_area), cancel_button);
gtk_container_add(GTK_CONTAINER(action_area), ok_button);
gtk_widget_show_all(dialog);
#ifndef DEFAULT_FOCUS
gtk_widget_grab_focus(ok_button);
#endif
#endif
g_signal_connect(dialog, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_main();
return 0;
}
Hope this helps!
希望这可以帮助!