Re: interrupting gtk_main



Marisa DeMeglio  wrote:

I am writing a GUI for an application whose events are
controlled by both the end user and a server-type
structure.  This server-type structure notifies the
GUI of new data by calling a class function.

My problem is that gtk_main() takes over and my
program  never receives anything until gtk_main_quit()
[at which point any new data is useless].

Is there a way that I can specify that a certain
function should have the authority to interrupt
gtk_main()?  I want gtk to listen for button presses,
etc, during the program execution, but I also want to
stop and update a text field when I get an outside
signal to do so.

Have a closer look at "Description" and "Creating new source types" at:

http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html

This page essentially describes the internals of a GTK main loop and
what features it offers. What you may want to do is making your called
class function send a custom message event to the main loop. I will try
to briefly describe 3 common approaches:

1.
using plain files, pipes or sockets as messengers. This kind of event
signalling is useful if you plan to transfer greater amounts of data
between your server and user application and / or if server and client
should be able to communicate via network. See also:

http://developer.gnome.org/doc/API/2.0/glib/glib-IO-Channels.html#g-io-add-watch
http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#GSource

2.
if your server-type structure just runs in another thread of the same
process as your frontend is in and if you need only basic signalling
then you may want to use GSignals. Those are the means which are
identified by names as you connect callback functions to them. They are
automatically invoked on emission of the signal (which would be
triggered by your server thread). For a description see:

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

3.
the simpliest way may be to use an existing GDK event, though. The GDK
Client Event may be used for propagation of custom messages. Your server
thread would send this event and the client thread (running the GTK main
loop) would handle it and thus be notified.

You should obtain a custom GdkAtom (the name of the action this signal
is used for, i.e. "myapp-server-signal"). This event type accepts a
payload of 20 bytes (or 10 shorts or 5 longs) which may be directly
delivered to the handler (callback) function of that signal. See:

http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventClient
http://developer.gnome.org/doc/API/2.0/gtk/GtkWidget.html#GtkWidget-client-event
http://developer.gnome.org/doc/API/2.0/gdk/gdk-Properties-and-Atoms.html#gdk-atom-intern
http://developer.gnome.org/doc/API/2.0/gtk/gtk-General.html#gtk-main-do-event
http://developer.gnome.org/doc/API/2.0/gtk/gtk-General.html#gtk-propagate-event

You should NOT use functions like gtk_timeout_add () or gtk_idle_add ()!
They don't belong to signalling based on asynchronous events, which is
most likely what you want. Those functions provide you with means of
active polling which is a) a waste of CPU resources and b) not as
accurate as using real signals (though c) maybe easier to implement).

Btw, you can update contents of text fields and other widgets basically
whenever you want to, if you use the correct widget functions to do so.



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