4

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.

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.

Is there really no way other than having to use "gtk_dialog_add_action_widget"?

1 Answer 1

4

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:

#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!

2
  • Yes! Thank you very much, this was absolutely what I was looking for, although you didn't have to write that big of an example ;) I am still wondering though why my first attempt with gtk_window_set_default didn't work..
    – Ancurio
    Jan 4, 2012 at 18:40
  • 1
    It could be because it appears that the default widget does not receive focus if there is a focus widget. To make gtk_window_set_default work, make sure that the widget can default (which you have done) & set the focus of window to NULL, then it should work. So in the code above: gtk_widget_grab_focus(ok_button); can be replaced by gtk_window_set_focus(GTK_WINDOW(dialog), NULL); gtk_widget_set_can_default(ok_button,TRUE); gtk_window_set_default(GTK_WINDOW(dialog), ok_button); Jan 5, 2012 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.