How to add callback to tell owner something

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to add callback to tell owner something

rion10
How to tell the owner that something has done at one function in my class?
Just like as following:
main()
{
  TMyClass me;
  me.DoSomething(); //not done yet if the function return
  ...
}

void TMyClass::OnSomeEvent()
{
  if (something has done) {
    //Add a callback to tell owner that the transfer is done, how to do it?
  }
}

Thanks a lot
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

Tristan Van Berkom
rion10 (sent by Nabble.com) wrote:
> How to tell the owner that something has done at one function in my class?
> Just like as following:

After reading your mail, I think you are looking for
GSignal documentation... here it is:

http://developer.gnome.org/doc/API/2.0/gobject/gobject-Signals.html

GSignal is complex, I sugest starting by finding an
example (in the GTK+ source for instance) and working from
there.

Cheers,
                           -Tristan

_______________________________________________
gtk-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

rion10
could you give me a simple example, I'm a gtk+ beginner. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

Paul Davis
In reply to this post by Tristan Van Berkom
On Wed, 2005-11-30 at 10:38 -0500, Tristan Van Berkom wrote:

> rion10 (sent by Nabble.com) wrote:
> > How to tell the owner that something has done at one function in my class?
> > Just like as following:
>
> After reading your mail, I think you are looking for
> GSignal documentation... here it is:
>
> http://developer.gnome.org/doc/API/2.0/gobject/gobject-Signals.html
>
> GSignal is complex, I sugest starting by finding an
> example (in the GTK+ source for instance) and working from
> there.

if you are working in C++, forget GSignal; use libsigc++ and its very
very easy:

        class MyClass : public sigc::trackable
        {
            ...

            sigc::signal<void,int> SomethingHappened;

            void do_something () {
                int what;
                ....
                SomethingHappened (what);
            }
        };

 
        class MyOtherClass : public sigc::trackable
        {
            ...
 
            void when_something_happens (int what) {
                  ...
            }
        }

        MyClass mine;
        MyOtherClass myOther;

        mine.SomethingHappened.connect (mem_fun (myOther,
&MyOtherClass::when_something_happens);

        mine.do_something ();


please don't send email to me asking about this; the net is littered
with loads of sample code. and use gtkmm, not gtk+, your life will be
much easier.

--p


_______________________________________________
gtk-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

Tristan Van Berkom
In reply to this post by rion10
rion10 (sent by Nabble.com) wrote:
> could you give me a simple example, I'm a gtk+ beginner. Thanks.

Not really, like I said; GSignal is complex;

so heres a complex example:

First of all; you need to have a function to be called
by default on your class:

-----------------------------------
struct _MyObjectClass {
     GObjectClass parent_class;
     /* ... */

     void  (* event_happened)  (MyObject *, gint);
}
-----------------------------------

Now, you must keep track of all the signals you declare
in your object, in C; usually you do this globally:
-----------------------------------
enum {
     EVENT_HAPPENED,
     /* ... other signals ... */
     N_SIGNALS
}

guint my_object_signals[N_SIGNALS];
-----------------------------------

Now you need to actually declare them in your
class initializer:
-----------------------------------
void
my_object_class_init (MyObjectClass *class)
{
     /* ... */

     /* Assign a base class handler for the "event_happened"
      * signal
      */
     class->event_happened = my_object_event_happened;

     /* ... */

     /* Declare the signal that returns VOID and
      * takes one INT after its implicit OBJECT parameter.
      */
     my_object_signals[EVENT_HAPPENED] =
         g_signal_new ("event-happened",
                       G_TYPE_FROM_CLASS (object_class),
                       G_SIGNAL_RUN_LAST,
                       G_STRUCT_OFFSET (MyObjectClass,
                                        event_happened),
                       NULL, NULL,
                       g_cclosure_marshal_VOID__INT,
                       G_TYPE_NONE, 1, G_TYPE_INT);
}
-----------------------------------

Now you can fire the signal with:
-----------------------------------
     g_signal_emit (G_OBJECT (my_object),
                    my_object_signals[EVENT_HAPPENED],
                    0, 5);
-----------------------------------

And clients of your object can connect to the
signal using g_signal_connect.

Now that I've written a short book this morning,
you'll have to delve into the API docs to figure
out more precicely all the meanings of all these
parameters to all these functions, some pointers
for you are "marshalers"
(g_cclosure_marshal_VOID__INT is a marshaler,
generated by glib-genmarshal)

and accumulators.. which arent used in this example
but are used to handle signal return values; usually
these are written by hand.

You shouldn't need to delve into GClosure to use the API.

Ummm and also... read this:
http://developer.gnome.org/doc/API/2.0/gobject/index.html

Cheers,
                             -Tristan

_______________________________________________
gtk-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

Martyn Russell-3
In reply to this post by rion10
On Wed, 2005-11-30 at 06:41 -0800, rion10 (sent by Nabble.com) wrote:
> could you give me a simple example, I'm a gtk+ beginner. Thanks.

If you want an example of how to do GObject coding, perhaps you would
benefit form looking at Gossip or some other Gnome application which use
GObject.

Gossip uses GObject quite extensively and there are copious examples to
choose from (mostly in libgossip sub directory).  Some modules derive
from GtkComboBox, GtkTreeView and GtkButton and add Gossip specific
implementations and signals.

You can see the source here: http://cvs.gnome.org/viewcvs/gossip/

One of the simpler examples might be:
http://cvs.gnome.org/viewcvs/gossip/libgossip/gossip-event-manager.c
http://cvs.gnome.org/viewcvs/gossip/libgossip/gossip-event-manager.h

To see where this is used, look at:
http://cvs.gnome.org/viewcvs/gossip/src/gossip-app.c
 
--
Regards,
Martyn

_______________________________________________
gtk-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: How to add callback to tell owner something

rion10
In reply to this post by Tristan Van Berkom
sorry, I can only use glib1.2, so I can't use g_signal_emit(), g_signal_connect(), etc functions. Is there any other way for this problem?
Thanks