Re: How to make a button look like a treeview column title? [Was: How to use the treeview column title style for a button?]



#include <gtk/gtk.h>

int main( int   argc,
         char *argv[] )
{
 gtk_init (&argc, &argv);

 GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 g_signal_connect (window, "destroy",
                   G_CALLBACK (gtk_widget_destroyed), &window);

 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
 gtk_container_add (GTK_CONTAINER (window), vbox);

 GtkListStore *store = gtk_list_store_new (1, G_TYPE_STRING);
 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
 GtkTreeViewColumn *column0 =
     gtk_tree_view_column_new_with_attributes ("TreeViewButton",
                                               renderer,
                                               "text",
                                               0,
                                               NULL);
GtkWidget *treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
 gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column0);
 gtk_box_pack_start_defaults (GTK_BOX (vbox), treeview);

 GtkWidget *button = gtk_button_new_with_label( "Ordinary button" );
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), button );

/* Almost the same as ordinary button. Under Windows the focus frame is few pixels narrower than on an ordinary button. Under Kubuntu the button is slightly broader than an ordinary button but has no prelight. Anyway it does not look like a
  TreeView button at all. */
GtkWidget *title1 = gtk_button_new_with_label( "style by path *.GtkTreeView.GtkButton" );
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title1 );
GtkStyle *style = gtk_rc_get_style_by_paths (gtk_widget_get_settings (title1),
                                              "*.GtkTreeView.GtkButton",
                                              "*.GtkTreeView.GtkButton",
                                              G_OBJECT_TYPE (title1));
 gtk_widget_set_style (title1, style);

/* Set to its own style. This has the surprising effect that the button seems
  to fall back to its default GTK style. */
 GtkWidget *title2 = gtk_button_new_with_label( "Set to its own style" );
 GtkStyle *style2 = gtk_widget_get_style(title2);
 gtk_widget_set_style (title2, style2);
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title2 );

/* Copy the style from title1 which we have previously set. This works as expected,
  so why did the style on title2 not work? */
 GtkWidget *title2a = gtk_button_new_with_label( "Set to style title1" );
 GtkStyle *style2a = gtk_widget_get_style(title1);
 gtk_widget_set_style (title2a, style2a);
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title2a );

/* Set the style from a totally different widget. The button seems to fall back
  to GTK style, same as when trying to set to its own style. */
 GtkWidget *title3 = gtk_button_new_with_label( "Set to window style" );
 GtkStyle *style3 = gtk_widget_get_style(window);
 gtk_widget_set_style (title3, style3);
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title3 );

 /* This works as expected. */
GtkWidget *title4 = gtk_button_new_with_label( "gtk_widget_set_style() button" );
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title4 );
GtkStyle *style4 = gtk_rc_get_style_by_paths (gtk_widget_get_settings (title4),
                                               "*Button",
                                               "*Button",
                                                G_OBJECT_TYPE (title4));
 gtk_widget_set_style (title4, style4);

/* Try a path like msw_style.c uses it. This looks exactly as an ordinary button. */ GtkWidget *title5 = gtk_button_new_with_label( "style by path *Treeview*Button*" );
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), title5 );
GtkStyle *style5 = gtk_rc_get_style_by_paths (gtk_widget_get_settings (title5),
                                               "*Treeview*Button*",
                                               "*Treeview*Button*",
                                               G_OBJECT_TYPE (title5));
 gtk_widget_set_style (title5, style5);

/* Under Windows this looks like title1. Under Kubuntu it does too, but with prelight. */
 GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
 gtk_box_pack_start_defaults( GTK_BOX( vbox ), hbox );
 GtkWidget *title6 = gtk_button_new_with_label ("msw-header-button");
 gtk_box_pack_start_defaults (GTK_BOX( hbox ), title6);
 gchar* class_path = NULL;
 gtk_widget_class_path(title6, NULL, &class_path, NULL);
 g_debug("button class path = %s", class_path);
 gchar buf[1024];
 g_snprintf (buf, sizeof(buf),
             "widget_class \"%s\" style \"msw-header-button\"\n",
             class_path );
 g_debug("%s", buf);
 gtk_rc_parse_string (buf);

 gtk_widget_show_all(window);
 gtk_main ();

 return 0;
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]