Search code examples
clinuxwebkitgtk3

How can I set one element of the Gearmenu insensitive?


I wanted to set one element of the gearmenu insensitive if the "load-failed" signal is emmited.

struct GActionEntry {
  const gchar *name;

  void (* activate) (GSimpleAction *action,
                     GVariant      *parameter,
                     gpointer       user_data);

  const gchar *parameter_type;

  const gchar *state;

  void (* change_state) (GSimpleAction *action,
                         GVariant      *value,
                         gpointer       user_data);
};

this is what I found (https://developer.gnome.org/gio/stable/GActionMap.html#GActionEntry) and I also used it like this:

const GActionEntry app_actions[] = {
    { "setAsHome", set_clicked , NULL, NULL, NULL, {0, 0, 0} },
  { "about", about_clicked, NULL, NULL, NULL, {0, 0, 0} }
};

I now wanted to set the set_clicked inactive but I don't know how to do it. I mean I know how to make a callback funktion for the "load-failed"-signal but not how to set it inactive in it.

int main (int argc, char **argv)
{
    int status;
      struct widget *w = g_malloc (sizeof (struct widget));
    w->app = gtk_application_new ("org.gtk.dialog", G_APPLICATION_HANDLES_COMMAND_LINE);
    g_signal_connect (w->app, "command-line", G_CALLBACK (activate), (gpointer) w);
    g_action_map_add_action_entries (G_ACTION_MAP (w->app), app_actions,
                     G_N_ELEMENTS (app_actions), (gpointer) w);
    status = g_application_run (G_APPLICATION (w->app), argc, argv);
    g_object_unref (w->app);

    // free the memory for the widgets struct
    g_free (w);
    w = NULL;
    return status;
}
struct widget
{
  GtkApplication *app;
    GtkWidget *window;
  GtkWidget *box;
  GMenu *appmenu;
  GMenu *editmenu;
  GtkWidget *find;
    GtkWidget *back;
    GtkWidget *forward;
    GtkWidget *home;
  GtkWidget *entry;
    GtkWidget *header;
  GMenu *gearmenu;
    GtkWidget *gearmenubutton;
    GtkWidget *gearicon;
    GtkWidget *status;
  WebKitWebView *wv;
  guint id;
  const gchar *url;
  const gchar *homedir;
  gboolean success;
};
// create the gear menu button
      w->gearmenubutton = gtk_menu_button_new();
      w->gearicon = gtk_image_new_from_icon_name ("emblem-system-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
      gtk_button_set_image (GTK_BUTTON (w->gearmenubutton), w->gearicon);
  // create a menu for the gear button
      w->gearmenu = g_menu_new();
      g_menu_append (w->gearmenu, "_Set as Homepage", "app.setAsHome");
      w->editmenu = g_menu_new();
      g_menu_append (w->editmenu, "_About", "app.about");
      g_menu_append_section (w->gearmenu, NULL, G_MENU_MODEL (w->editmenu));
      gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (w->gearmenubutton),
                      G_MENU_MODEL (w->gearmenu));
      g_object_unref (w->editmenu);
      g_object_unref (w->gearmenu);

Solution

  • in the load-failed callback you need to remove the "setAsHome":

    g_action_map_remove_action(G_ACTION_MAP (w->app), "setAsHome"
    

    the load failed signal also emits when there is a failure and you would be redirectet to an error message page. Keep in mind that your load-change signal will be emitted 2 times once because the url can't be loaded and than to load the error page.

    In order to pake it "sensitive" again, you need to add it to your menu again.