Re: gobject



        I've been learning / coding widgets for about 6 months now
(And I'm very impressed by the do-it-yourself OOP style).

What I suggest is that you start from an exising object that inherits
from the same class as you inherit from and "modify / add / remove" from
that objects
functionality.

If I'm not mixed up with gtk specific stuff I think you 
need to specify the size of your instance in your "xxx_get_type()"
function and that you need your "static parent_class" to be the first
member
of your class. (its g_object_new that will use this information to
allocate space
for your object and build it). 

Have fun!
        -Tristan


Parrish M Myers wrote:

Hello,

I am not sure this is the right list to email... if not please forgive
me.

I am attempting to learn gobject (in glib2).  I have scoured the net
looking for any tutorial I can find, and have come up empty.  I have
studdied the API documentation for gobject... But, considering I don't
understand what the basic elements look like the API documentation does
me no good.  I am well versed with OO programming, I know java, C++,
C#, bla bla bla...  But, for some reason I can't seem to grasp the
syntacs of gobject.  Figuring, a good example bit of code should get
the rest of the way, I have studdied libgsf, metacity, and gthumb, and
have come up with what I think is a basic gobject class.

Cany anyone comment on my code and tell me what I have done wrong or
have not understood?  I would appreciate any constructive criticism...
I am very willing to learn, I just need a little guidance from the
masters :)

#####################################################################
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8
-*- */

/*
 * timekeeper.h:
 *
 * Copyright (C) 2002 Parrish Myers (parrishmyers yahoo com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifndef TIMEPIECE_H
#define TIMEPIECE_H

#include <glib.h>
#include <glib-object.h>
#include "sw.h"

G_BEGIN_DECLS

#define SW_TIMEPIECE_TYPE  (sw_timepiece_get_type ())
#define SW_TIMEPIECE(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o),
SW_TIMEPIECE_TYPE, SwTimepiece))
#define SW_IS_TIMEPIECE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o),
SW_TIMEPIECE_TYPE))

typedef struct _SwTimepiece SwTimepiece;

struct _SwTimepiece {
        GObject g_object;
        Timepiece_Mode mode;
        Timepiece_Time time;

        /* protected */
        guint32 time_start;
        guint32 time_now;

};

typedef struct {
        GObjectClass g_object_class;

        /*< signals >*/

        void  (*Start_Error) (SwTimepiece *timepiece);

} SwTimepieceClass;

#define SW_TIMEPIECE_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST ((k),
SW_TIMEPIECE_TYPE, SwTimepieceClass))
#define SW_IS_TIMEPIECE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k),
SW_TIMEPIECE_TYPE))

GType        sw_timepiece_get_type (void);
SwTimepiece *sw_timepiece_new      (void);

/* protected */
guint  sw_time_function  (SwTimepiece *timepiece);
void   sw_set_mode       (SwTimepiece *timepiece, Timepiece_Mode
*mode);
void   sw_set_start_time (SwTimepiece *timepiece, Timepiece_Time
*time);
void   sw_start          (SwTimepiece *timepiece);
void   sw_stop           (SwTimepiece *timepiece);
void   sw_reset          (SwTimepiece *timepiece);

G_END_DECLS

#endif /* TIMEPIECE_H */

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8
-*- */

/*
 * timekeeper.c:
 *
 * Copyright (C) 2002 Parrish Myers (parrishmyers yahoo com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <glib.h>
#include "sw.h"
#include "timepiece.h"

static guint start_error_signal;

/* SwTimepiece */

static void
sw_timepiece_finalize (GObject *object)
{
        g_return_if_fail (object != NULL);
        g_return_if_fail (SW_IS_TIMEPIECE (object));

        G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
sw_timepiece_class_init (SwTimepieceClass *class)
{
        GObjectClass *object_class = G_OBJECT_CLASS (class);

        parent_class = g_type_class_peek_parent (class);

        /* register signals */
        start_error_signal = g_signal_new ("Start_Error",
                                           G_TYPE_FROM_CLASS (class),
                                           G_SIGNAL_RUN_LAST,
                                           G_STRUCT_OFFSET (SwTimepieceClass, Start_Error),
                                           NULL, NULL,
                                           sw_marshal_VOID__VOID,
                                           G_TYPE_NONE,
                                           0);

        object_class->finalize = sw_timepiece_finalize;

        class->Start_Error = NULL;
}

static void
sw_timepiece_init (SwTimepiece *timepiece)
{
        Timepiece_Time *time = {0, 0, 0};

        timepiece->mode       = MODE_STOPWATCH;
        timepiece->time       = time;
        timepiece->time_start = sw_pack_time(time);
        timepiece->time_now   = 0;
}

GType
sw_timepiece_get_type ()
{
        static guint type = 0;

        if (! type) {
                GTypeInfo type_info ={
                        sizeof (SwTimepieceClass),
                        NULL,
                        NULL,
                        (GClassInitFunc) sw_timepiece_class_init,
                        NULL,
                        NULL,
                        sizeof (SwTimpiece),
                        0,
                        (GInstanceInitFunc) sw_timepiece_init
                };

                type = g_type_register_static (G_TYPE_OBJECT,
                                               "SwTimepiece"
                                               &type_info,
                                               0);
        }

        return type;
}

SwTimepiece *
sw_timepiece_new ()
{
        GObject *object;
        object = g_object_new (SW_TIMEPIECE_TYPE, NULL);
        return SW_TIMEPIECE (object);
}

guint
sw_time_function (SwTimepiece *timepiece)
{
}

void
sw_set_mode (SwTimepiece *timepiece, Timepiece_Mode *mode)
{
}

void
sw_set_start_time (SwTimepiece *timepiece, Timepiece_Time *time)
{
}

void
sw_start (SwTimepiece *timepiece)
{
}

void
sw_stop (SwTimepiece *timepiece)
{
}

void
sw_reset (SwTimepiece *timepiece)
{
}

guint32
sw_pack_time (Timepiece_Time *time)
{
        guint32 packed_time = 0;

        packed_time += time->hours   * 3600;
        packed_time += time->minutes * 60;
        packed_time += time->seconds;

        return packed_time;
}

Timepiece_Time *
sw_unpack_time (guint32 packed_time);
{
        Timepiece_Time *time = {0, 0, 0};

        time->hours   = packed_time / 3600;
        time->minutes = packed_time / 60 % 60;
        time->seconds = packed_time % 60;

        return time;
}

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8
-*- */

/*
 * timekeeper.h:
 *
 * Copyright (C) 2002 Parrish Myers (parrishmyers yahoo com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifndef SW_H
#define SW_H

#include <glib.h>
#include <glib-object.h>

G_BEGIN_DECLS

typedef struct {
        gint hours;
        gint minutes;
        gint seconds;
} Timepiece_Time;

typedef enum {
        MODE_STOPWATCH,
        MODE_TIMER
} Timepiece_Mode;

guint32         sw_pack_time   (Timepiece_Time *time);
Timepiece_Time *sw_unpack_time (guint32 time);

G_END_DECLS

#endif /* SW_H */

=====
-----------------------------------------------------------
Academia is a little like child   | Parrish M. Myers
rearing, it provides a chance at  | The Wacked Jester
immortality without the stretch   | parrishmyers yahoo com
marks  -- (unknown source)        |
-----------------------------------------------------------

__________________________________________________
Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.com
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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