Newbie questions



Hi!

I'm newbie at gtk and I need a little help; I'm trying
to create a new Widget its based on a drawingarea, but
I have 2 problems:

1. If I declare another widget of the type I'm
creating, gtk returns me the following error:

Gtk-CRITICAL **: file gtkbin.c: line 217
(gtk_bin_add): assertion `bin->child == NULL' failed.

I don't understand the parent class and those
things...

2. I have a procedure that repaints the drawing area;
I connect a signal to the drawingarea, on the expose
event, but... How can I pass it the members of the
structure I'm creating? I'm trying to pass a gpointer
to the object but in the expose_event function the
gpointer data->any_member doesn't work...

Please give me any idea... I really apreciate this,
Thanks a lot

The little header code:


/***************************************************/


#ifndef __GTK_NEWLABEL_H__
#define __GTK_NEWLABEL_H__

#include <gdk/gdk.h>
#include <gtk/gtkvbox.h>

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

#define GTK_NEWLABEL(obj) GTK_CHECK_CAST (obj,
gtk_newlabel_get_type (), GtkNewLabel)
#define GTK_NEWLABEL_CLASS(klass) GTK_CHECK_CLASS_CAST
(klass, gtk_newlabel_get_type (), GtkNewLabelClass)
#define IS_GTK_NEWLABEL(obj) GTK_CHECK_TYPE (obj,
gtk_newlabel_get_type ())

typedef struct _GtkNewLabel       GtkNewLabel;
typedef struct _GtkNewLabelClass  GtkNewLabelClass;

struct _GtkNewLabel
  {
    GtkVBox vbox;
 
    GtkWidget *drawing_area;
    gchar *str;
    glong backred;
    glong backgreen;
    glong backblue;
    glong forered;
    glong foregreen;
    glong foreblue;
  };

struct _GtkNewLabelClass
  {
    GtkVBoxClass parent_class;
  };

guint          gtk_newlabel_get_type        ();
GtkWidget*     gtk_newlabel_new             (gchar
*caption);
void           gtk_newlabel_set_caption    
(GtkNewLabel *newlabel,
                                             gchar
*caption);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __GTK_NEWLABEL_H__ */


My Widget's code...

/****************************************************/

#include "gtk/gtktable.h"
#include "gtk/gtkdrawingarea.h"
#include "gtknewlabel.h"

#define arial
"-ult1mo-arial-medium-r-normal-*-*-160-*-*-p-*-adobe-fontspecific"

static void gtk_newlabel_class_init       
(GtkNewLabelClass *klass);
static void gtk_newlabel_init             
(GtkNewLabel      *newlabel);

void gtk_newlabel_expose (gpointer data);

GdkGC *GetPen (GdkColor *c, GdkDrawable *drawable);
GdkColor *NewColor (long red, long green, long blue);

guint gtk_newlabel_get_type ()
  {
    static guint newlabel_type = 0;

    if (!newlabel_type)
      {
        GtkTypeInfo newlabel_info =
          {
            "GtkNewLabel",
            sizeof (GtkNewLabel),
            sizeof (GtkNewLabelClass),
            (GtkClassInitFunc)
gtk_newlabel_class_init,
            (GtkObjectInitFunc) gtk_newlabel_init,
            (GtkArgSetFunc) NULL,
            (GtkArgGetFunc) NULL
          };

        newlabel_type = gtk_type_unique
(gtk_vbox_get_type (), &newlabel_info);
      }

    return newlabel_type;
  }

static void gtk_newlabel_class_init (GtkNewLabelClass
*class)
  {
  }

static void gtk_newlabel_init (GtkNewLabel *newlabel)
  {
    GtkWidget *table;
    long back_red = 0xb4b4;
    long back_green = 0xb2b2;
    long back_blue = 0xb0b0;
    long fore_red = 0x0000;
    long fore_green = 0x0000;
    long fore_blue = 0x0000;

    table = gtk_table_new (1, 1, TRUE);
    gtk_container_add (GTK_CONTAINER (newlabel),
table);
    gtk_widget_show (table);

    newlabel->drawing_area = gtk_drawing_area_new ();
    gtk_table_attach_defaults (GTK_TABLE(table),
newlabel->drawing_area, 0, 1, 0, 1);

    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "back_red",
                         (void *)back_red);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "back_green",
                         (void *)back_green);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "back_blue",
                         (void *)back_blue);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "fore_red",
                         (void *)fore_red);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "fore_green",
                         (void *)fore_green);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "fore_blue",
                         (void *)fore_blue);
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "caption", "");

    gtk_widget_show (newlabel->drawing_area);

    gtk_signal_connect (GTK_OBJECT
(newlabel->drawing_area), "expose_event",
                        GTK_SIGNAL_FUNC
(gtk_newlabel_expose), NULL);
  }

GtkWidget* gtk_newlabel_new (gchar *caption)
  {
    GtkNewLabel *newlabel;

    newlabel = gtk_type_new (gtk_newlabel_get_type
());

    gtk_newlabel_set_caption (newlabel, caption);

    return GTK_WIDGET (newlabel);
  }

void gtk_newlabel_set_caption (GtkNewLabel *newlabel,
gchar *caption)
  {
    gtk_object_set_data (GTK_OBJECT
(newlabel->drawing_area), "caption",
                         (void *)caption);
  }

void gtk_newlabel_expose (gpointer data)
  {
    GtkWidget *drawing_area = data;
    GdkDrawable *drawable = drawing_area->window;
    GdkGC *gc_back, *gc_fore;
    GdkFont *font;
    int x, y, height, width;
    long back_red = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                          "back_red");
    long back_green = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                            "back_green");
    long back_blue = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                           "back_blue");
    long fore_red = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                          "fore_red");
    long fore_green = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                            "fore_green");
    long fore_blue = (long)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                           "fore_blue");
    char *caption = (char *)gtk_object_get_data
(GTK_OBJECT (drawing_area),
                           "caption");

    gc_back = GetPen (NewColor (back_red, back_green,
back_blue), drawable);
    gc_fore = GetPen (NewColor (fore_red, fore_green,
fore_blue), drawable);

    gdk_draw_rectangle (drawable, gc_back, TRUE, 0, 0,
drawing_area->allocation.width,
                       
drawing_area->allocation.height);

    font = gdk_font_load (arial);

    height = gdk_text_height (font, caption,
strlen(caption));
    width = gdk_text_width (font, caption,
strlen(caption));
    x = ((drawing_area->allocation.width - width) /
2);
    y = ((drawing_area->allocation.height - height) /
2) + height;

    gdk_draw_string (drawable, font, gc_fore, x, y,
caption);
  }

GdkGC *GetPen (GdkColor *c, GdkDrawable *drawable)
  {
    GdkGC *gc;

    gc = gdk_gc_new (drawable);

    gdk_gc_set_foreground (gc, c);

    return (gc);
  }

GdkColor *NewColor (long red, long green, long blue)
  {
    GdkColor *c = (GdkColor *) g_malloc (sizeof
(GdkColor));

    c->red = red;
    c->green = green;
    c->blue = blue;

    gdk_color_alloc (gdk_colormap_get_system (), c);

    return (c);
  }



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/




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