Re: How to add new Signals in a class



On Fri, 2004-10-29 at 13:30, vasista sarma wrote:
ok let me explain the exact situation i am in i have a GtkButtonClass
which i have used for "clicked" and "pressed" events .
The code would look something like this

GtkButtonClass * button_class= (GtkButtonClass *)kclass;
button_class->clicked = button_clicked;
button_class->pressed = button_pressed;

staic void button_clicked (GtkButton *button)
{
blah.. blah..
}
same is for button_pressed.

The concept is somewhat bogus. What's emiting the signals is not the
button class, but the button object - I mean the object's instance, a
GObject with type GTK_TYPE_BUTTON in this case. 

These two functions are getting invoked for button pressed and Click
events.
Now i want to write something for drag motion as well
something like (this i want when other application is hovered over
button).

I don't see the point to extend the button class here. The button is a
GtkWidget and those signals are managed by the class structure on the
button's parent GtkWidget.

(this i want when other application is hovered over
button).

To drag a window over a widget will not emit the drag motion signal.

button_class->drag_motion= button_drag_motion();

Well, what you're doing with those classes is not - at all - the way GTK
works.

Those classes are to be inherited by other object's class when you
extended an object, not the be used as a container to override
functions.  

How can i achieve this. Please give an example on how to do this

To add new signal handler to an given GObject - in this case a GtkButton
- you should create a new GObject extending the GtkButton.

I mean you have to create a structure holding a pointer to the parent
class and proptotypes for the new signals functions.

You must create a init function (GClassInitFunc) and create your new
signals with g_signal_new().

You must create a get a new type and set a GtypeInfo containing a
pointer to your GClassInitFunc.

You must create a get_type() function returning your new type.

....

And finally you must get your signals emited anywhere.

Where will them be emited in this case ? The parent GtkWidget will emit
the drag motion signal, so all your work will be a nonsense.

Simply g_signal_connect(G_OBJECT(),"drag-motion", ..., ...) will do the
job.

I can't imagine a case where youd would have to create an object
extending other object, to override the the new object's class
functions.

I've uploaded an example for you. It's a GObject (called Test) that
extends GObject to add a couple of weird properties and signals.

http://iagorubio.com/gtk/gobject-extend-test.c.gz

Compile with:
gcc test.c -o test `pkg-config --cflags --libs glib-2.0 gobject-2.0`

Run with:
./test

A GtkWidget is a GObject, so to add new signals to GtkWidgets, is done 
exactly the same way, as it's done in the example.

I hope this helps.    
-- 
Iago Rubio   



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