Why is realize screwing things up???



Greetings. I have been trying to create a widget that inherits from
the 'gtktreeitem' widget that allows the user to specify a label and
pixmaps to be displayed for both the expanded and collapsed states
in addition to the '+' and '-' pixmaps for navigating the tree. I have
spent a good 3 hours trying to get it to work and can not understand
why my 'realize' method does not work even though I chain up to the
parent class 'realize' method. Help is very much appreciated. Thanks.


Steve
#ifndef __GTK_TREE_ITEM_PIX_H__
#define __GTK_TREE_ITEM_PIX_H__


#include <gdk/gdk.h>
#include <gtk/gtktreeitem.h>
#include <gtk/gtkpixmap.h>


#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


/* Flags used to specify the location of the
 * label in the item container.
 */
typedef enum
{
  GTK_TREE_ITEM_PIX_LABEL_FIRST,
  GTK_TREE_ITEM_PIX_LABEL_LAST
} GtkTreeItemPixLabelPos;


#define GTK_TYPE_TREE_ITEM_PIX              (gtk_tree_item_pix_get_type ())
#define GTK_TREE_ITEM_PIX(obj)              (GTK_CHECK_CAST ((obj), GTK_TYPE_TREE_ITEM_PIX, GtkTreeItemPix))
#define GTK_TREE_ITEM_PIX_CLASS(klass)      (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_TREE_ITEM_PIX, GtkTreeItemPixClass))
#define GTK_IS_TREE_ITEM_PIX(obj)           (GTK_CHECK_TYPE ((obj), GTK_TYPE_TREE_ITEM_PIX))
#define GTK_IS_TREE_ITEM_PIX_CLASS(klass)   (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TREE_ITEM_PIX))

#define GTK_TREE_ITEM_PIX_SUBTREE(obj)      (GTK_TREE_ITEM_PIX(obj)->subtree)


typedef struct _GtkTreeItemPix       GtkTreeItemPix;
typedef struct _GtkTreeItemPixClass  GtkTreeItemPixClass;

struct _GtkTreeItemPix
{
  GtkTreeItem item;

  GtkWidget *collapsed_pix_widget;
  GtkWidget *expanded_pix_widget;
  GtkWidget *label_widget;
  gchar **collapsed_xpm;
  gchar **expanded_xpm;
  guint label_position;

  GList *pixmaps;

};

struct _GtkTreeItemPixClass
{
  GtkTreeItemClass parent_class;
};


GtkWidget* gtk_tree_item_pix_new (gchar *label,
                                  gchar **collapsed_xpm,
                                  gchar **expanded_xpm,
                                  guint location);


#ifdef __cplusplus
}
#endif /* __cplusplus */


#endif /* __GTK_TREE_ITEM_PIX_H__ */
#include <gtk/gtklabel.h>
#include <gtk/gtkpixmap.h>
#include <gtk/gtktable.h>
#include "gtktreeitempix.h"


typedef struct _GtkTreePixmaps GtkTreePixmaps;

struct _GtkTreePixmaps {
  gint refcount;
  GdkColormap *colormap;

  GdkPixmap *pixmap_plus;
  GdkPixmap *pixmap_minus;
  GdkBitmap *mask_plus;
  GdkBitmap *mask_minus;
};

static GList *pixmaps = NULL;
static GtkTreeItemClass *parent_class = NULL;

static void gtk_tree_item_pix_class_init (GtkTreeItemPixClass *klass);
static void gtk_tree_item_pix_init       (GtkTreeItemPix *tree_item_pix);
void tree_item_pix_collapse              (GtkWidget *item, gpointer data);
void tree_item_pix_expand                (GtkWidget *item, gpointer data);
static void gtk_tree_item_pix_add_pixmaps (GtkTreeItemPix *tree_item);
static void gtk_tree_item_pix_realize (GtkWidget *widget);

GtkType
gtk_tree_item_pix_get_type (void)
{

  static GtkType tree_item_pix_type = 0;

  if (!tree_item_pix_type)
    {
      GtkTypeInfo tree_item_pix_info =
      {
        "GtkTreeItemPix",
        sizeof (GtkTreeItemPix),
        sizeof (GtkTreeItemPixClass),
        (GtkClassInitFunc) gtk_tree_item_pix_class_init,
        (GtkObjectInitFunc) gtk_tree_item_pix_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      tree_item_pix_type = gtk_type_unique (gtk_tree_item_get_type (),
                                            &tree_item_pix_info);
    }

  return tree_item_pix_type;
}

static void
gtk_tree_item_pix_class_init (GtkTreeItemPixClass *class)
{
  GtkWidgetClass *widget_class;

  widget_class = (GtkWidgetClass*) class;
  parent_class = gtk_type_class (gtk_item_get_type ());

  widget_class->realize = gtk_tree_item_pix_realize;
}

static void
gtk_tree_item_pix_init (GtkTreeItemPix *tree_item_pix)
{
  GtkWidget *pixmapwid, *tablewid, *labelwid;

  g_return_if_fail (tree_item_pix != NULL);
  g_return_if_fail (GTK_IS_TREE_ITEM_PIX (tree_item_pix));

  tablewid = gtk_table_new (1, 2, FALSE);
  gtk_widget_show (tablewid);
  gtk_container_add (GTK_CONTAINER (tree_item_pix), tablewid);

  labelwid = gtk_label_new(" ");
  gtk_misc_set_alignment (GTK_MISC (labelwid), 0.0, 0.5);
  gtk_widget_show (labelwid);
  gtk_table_attach (GTK_TABLE (tablewid), labelwid, 1, 2, 0, 1,
    GTK_SHRINK, GTK_SHRINK, 0, 0);
  tree_item_pix->label_widget = labelwid;

  pixmapwid = gtk_type_new (gtk_pixmap_get_type ());
  gtk_widget_show (pixmapwid);
  if (!GTK_TREE_ITEM (tree_item_pix)->expanded)
    gtk_table_attach (GTK_TABLE (tablewid), pixmapwid, 0, 1, 0, 1,
      GTK_SHRINK, GTK_SHRINK, 0, 0);
  tree_item_pix->collapsed_pix_widget = pixmapwid;
  gtk_widget_ref (tree_item_pix->collapsed_pix_widget);
  gtk_object_sink (GTK_OBJECT (tree_item_pix->collapsed_pix_widget));

  pixmapwid = gtk_type_new (gtk_pixmap_get_type ());
  gtk_widget_show (pixmapwid);
  if (GTK_TREE_ITEM (tree_item_pix)->expanded)
    gtk_table_attach (GTK_TABLE (tablewid), pixmapwid, 0, 1, 0, 1,
      GTK_SHRINK, GTK_SHRINK, 0, 0);
  tree_item_pix->expanded_pix_widget = pixmapwid;
  gtk_widget_ref (tree_item_pix->expanded_pix_widget);
  gtk_object_sink (GTK_OBJECT (tree_item_pix->expanded_pix_widget));

  gtk_signal_connect (GTK_OBJECT (tree_item_pix), "collapse",
                      GTK_SIGNAL_FUNC (tree_item_pix_collapse), NULL);
  gtk_signal_connect (GTK_OBJECT (tree_item_pix), "expand",
                      GTK_SIGNAL_FUNC (tree_item_pix_expand), NULL);
}

GtkWidget*
gtk_tree_item_pix_new (gchar *label,
                       gchar **collapsed_xpm,
                       gchar **expanded_xpm,
                       guint location)
{
  GtkWidget *tree_item_pix;
  GtkWidget *label_widget;
  GtkWidget *table_widget;
  GtkStyle *style;

  tree_item_pix = GTK_WIDGET (gtk_type_new (gtk_tree_item_pix_get_type ()));
  gtk_label_set (GTK_LABEL (GTK_TREE_ITEM_PIX (tree_item_pix)->label_widget),
    label);
  GTK_TREE_ITEM_PIX (tree_item_pix)->collapsed_xpm = collapsed_xpm;
  GTK_TREE_ITEM_PIX (tree_item_pix)->expanded_xpm = expanded_xpm;

  return tree_item_pix;
}

void tree_item_pix_collapse (GtkWidget *item, gpointer data)
{
  // gtk_pixmap_set (GTK_PIXMAP (GTK_TREE_ITEM_PIX (item)->current_pix_widget), GTK_TREE_ITEM_PIX (item)->collapsed_pixmap, GTK_TREE_ITEM_PIX (item)->collapsed_bitmap); 
}

void tree_item_pix_expand (GtkWidget *item, gpointer data)
{
  // gtk_pixmap_set (GTK_PIXMAP (GTK_TREE_ITEM_PIX (item)->current_pix_widget), GTK_TREE_ITEM_PIX (item)->expanded_pixmap, GTK_TREE_ITEM_PIX (item)->expanded_bitmap); 
}

static void
gtk_tree_item_add_pixmaps (GtkTreeItemPix *tree_item)
{
  GList *tmp_list;
  GdkColormap *colormap;
  GtkTreePixmaps *pixmap_node = NULL;

  g_return_if_fail (tree_item != NULL);
  g_return_if_fail (GTK_IS_TREE_ITEM_PIX (tree_item));

  if (tree_item->pixmaps)
    return;

  colormap = gtk_widget_get_colormap (GTK_WIDGET (tree_item));

  tmp_list = pixmaps;
  while (tmp_list)
    {
      pixmap_node = (GtkTreePixmaps *)tmp_list->data;

      if (pixmap_node->colormap == colormap)
        break;

      tmp_list = tmp_list->next;
    }

  if (tmp_list)
    {
      pixmap_node->refcount++;
      tree_item->pixmaps = tmp_list;
    }
  else
    {
      pixmap_node = g_new (GtkTreePixmaps, 1);

      pixmap_node->colormap = colormap;
      gdk_colormap_ref (colormap);

      pixmap_node->refcount = 1;

      /* create pixmaps for plus icon */
      pixmap_node->pixmap_plus =
        gdk_pixmap_create_from_xpm_d (GTK_WIDGET (tree_item)->window,
                                      &pixmap_node->mask_plus,
                                      NULL,
                                      tree_item->collapsed_xpm);

      /* create pixmaps for minus icon */
      pixmap_node->pixmap_minus =
        gdk_pixmap_create_from_xpm_d (GTK_WIDGET (tree_item)->window,
                                      &pixmap_node->mask_minus,
                                      NULL,
                                      tree_item->expanded_xpm);

      tree_item->pixmaps = pixmaps = g_list_prepend (pixmaps, pixmap_node);
    }

  gtk_pixmap_set (GTK_PIXMAP (tree_item->collapsed_pix_widget),
                  pixmap_node->pixmap_plus, pixmap_node->mask_plus);
  gtk_pixmap_set (GTK_PIXMAP (tree_item->expanded_pix_widget),
                  pixmap_node->pixmap_minus, pixmap_node->mask_minus);
}

static void
gtk_tree_item_pix_realize (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_TREE_ITEM_PIX (widget));

  if (GTK_WIDGET_CLASS (parent_class)->realize)
    (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);

  gdk_window_set_background (widget->window,
                             &widget->style->base[GTK_STATE_NORMAL]);

  gtk_tree_item_add_pixmaps (GTK_TREE_ITEM_PIX (widget));
}


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