Synopsis#include <gtk/gtk.h> gchar* gtk_set_locale (void); void gtk_disable_setlocale (void); PangoLanguage* gtk_get_default_language (void); gboolean gtk_parse_args (int *argc, char ***argv); void gtk_init (int *argc, char ***argv); gboolean gtk_init_check (int *argc, char ***argv); gboolean gtk_init_with_args (int *argc, char ***argv, char *parameter_string, GOptionEntry *entries, char *translation_domain, GError **error); GOptionGroup* gtk_get_option_group (gboolean open_default_display); void gtk_exit (gint error_code); gboolean gtk_events_pending (void); void gtk_main (void); guint gtk_main_level (void); void gtk_main_quit (void); gboolean gtk_main_iteration (void); gboolean gtk_main_iteration_do (gboolean blocking); void gtk_main_do_event (GdkEvent *event); void (*GtkModuleInitFunc) (gint *argc, gchar ***argv); void (*GtkModuleDisplayInitFunc) (GdkDisplay *display); gboolean gtk_true (void); gboolean gtk_false (void); void gtk_grab_add (GtkWidget *widget); GtkWidget* gtk_grab_get_current (void); void gtk_grab_remove (GtkWidget *widget); void gtk_init_add (GtkFunction function, gpointer data); void gtk_quit_add_destroy (guint main_level, GtkObject *object); guint gtk_quit_add (guint main_level, GtkFunction function, gpointer data); guint gtk_quit_add_full (guint main_level, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GDestroyNotify destroy); void gtk_quit_remove (guint quit_handler_id); void gtk_quit_remove_by_data (gpointer data); guint gtk_timeout_add_full (guint32 interval, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GDestroyNotify destroy); guint gtk_timeout_add (guint32 interval, GtkFunction function, gpointer data); void gtk_timeout_remove (guint timeout_handler_id); guint gtk_idle_add (GtkFunction function, gpointer data); guint gtk_idle_add_priority (gint priority, GtkFunction function, gpointer data); guint gtk_idle_add_full (gint priority, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GDestroyNotify destroy); void gtk_idle_remove (guint idle_handler_id); void gtk_idle_remove_by_data (gpointer data); guint gtk_input_add_full (gint source, GdkInputCondition condition, GdkInputFunction function, GtkCallbackMarshal marshal, gpointer data, GDestroyNotify destroy); void gtk_input_remove (guint input_handler_id); #define GTK_PRIORITY_REDRAW #define GTK_PRIORITY_RESIZE #define GTK_PRIORITY_HIGH #define GTK_PRIORITY_INTERNAL #define GTK_PRIORITY_DEFAULT #define GTK_PRIORITY_LOW guint gtk_key_snooper_install (GtkKeySnoopFunc snooper, gpointer func_data); gint (*GtkKeySnoopFunc) (GtkWidget *grab_widget, GdkEventKey *event, gpointer func_data); void gtk_key_snooper_remove (guint snooper_handler_id); GdkEvent* gtk_get_current_event (void); guint32 gtk_get_current_event_time (void); gboolean gtk_get_current_event_state (GdkModifierType *state); GtkWidget* gtk_get_event_widget (GdkEvent *event); void gtk_propagate_event (GtkWidget *widget, GdkEvent *event); Description
Before using GTK+, you need to initialize it; initialization connects
to the window system display, and parses some standard command line
arguments. The Like all GUI toolkits, GTK+ uses an event-driven programming model. When the user is doing nothing, GTK+ sits in the main loop and waits for input. If the user performs some action - say, a mouse click - then the main loop "wakes up" and delivers an event to GTK+. GTK+ forwards the event to one or more widgets.
When widgets receive an event, they frequently emit one or more
signals. Signals notify your program that
"something interesting happened" by invoking functions you've
connected to the signal with When your callbacks are invoked, you would typically take some action - for example, when an Open button is clicked you might display a GtkFileSelectionDialog. After a callback finishes, GTK+ will return to the main loop and await more user input. Example 1. Typical
int
main (int argc, char **argv)
{
/* Initialize i18n support */
gtk_set_locale ();
/* Initialize the widget set */
gtk_init (&argc, &argv);
/* Create the main window */
mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* Set up our GUI elements */
...
/* Show the application window */
gtk_widget_show_all (mainwin);
/* Enter the main event loop, and wait for user interaction */
gtk_main ();
/* The user lost interest */
return 0;
}
It's OK to use the GLib main loop directly instead of Detailsgtk_set_locale ()gchar* gtk_set_locale (void);
Initializes internationalization support for GTK+. If you are calling this function because you changed the locale after GTK+ is was initialized, then calling this function may help a bit. (Note, however, that changing the locale after GTK+ is initialized may produce inconsistent results and is not really supported.)
In detail - sets the current locale according to the
program environment. This is the same as calling the C library function
gtk_disable_setlocale ()void gtk_disable_setlocale (void);
Prevents Most programs should not need to call this function.
gtk_get_default_language ()PangoLanguage* gtk_get_default_language (void); Returns the PangoLanguage for the default language currently in effect. (Note that this can change over the life of an application.) The default language is derived from the current locale. It determines, for example, whether GTK+ uses the right-to-left or left-to-right text direction.
This function is equivalent to
gtk_parse_args ()gboolean gtk_parse_args (int *argc,
char ***argv);
Parses command line arguments, and initializes global
attributes of GTK+, but does not actually open a connection
to a display. (See
Any arguments used by GTK+ or GDK are removed from the array and
You shouldn't call this function explicitely if you are using
gtk_init ()void gtk_init (int *argc,
char ***argv);
Call this function before using any other GTK+ functions in your GUI
applications. It will initialize everything needed to operate the
toolkit and parses some standard command line options.
Note that there are some alternative ways to initialize GTK+:
if you are calling
Note
This function will terminate your program if it was unable to initialize
the GUI for some reason. If you want your program to fall back to a
textual interface you want to call
Note
gtk_init_check ()gboolean gtk_init_check (int *argc,
char ***argv);
This function does the same work as This way the application can fall back to some other means of communication with the user - for example a curses or command line interface.
gtk_init_with_args ()gboolean gtk_init_with_args (int *argc,
char ***argv,
char *parameter_string,
GOptionEntry *entries,
char *translation_domain,
GError **error);
This function does the same work as
Since 2.6 gtk_get_option_group ()GOptionGroup* gtk_get_option_group (gboolean open_default_display);
Returns a GOptionGroup for the commandline arguments recognized
by GTK+ and GDK. You should add this group to your GOptionContext
with
Since 2.6 gtk_exit ()void gtk_exit (gint error_code); Warning
Terminates the program and returns the given exit code to the caller. This function will shut down the GUI and free all resources allocated for GTK+.
gtk_events_pending ()gboolean gtk_events_pending (void); Checks if any events are pending. This can be used to update the GUI and invoke timeouts etc. while doing some time intensive computation. Example 2. Updating the GUI during a long computation.
/* computation going on */
...
while (gtk_events_pending ())
gtk_main_iteration ();
...
/* computation continued */
gtk_main ()void gtk_main (void);
Runs the main loop until gtk_main_level ()guint gtk_main_level (void);
Asks for the current nesting level of the main loop. This can be useful
when calling
gtk_main_quit ()void gtk_main_quit (void); Makes the innermost invocation of the main loop return when it regains control. gtk_main_iteration ()gboolean gtk_main_iteration (void);
Runs a single iteration of the mainloop. If no events are waiting to be
processed GTK+ will block until the next event is noticed. If you don't
want to block look at
gtk_main_iteration_do ()gboolean gtk_main_iteration_do (gboolean blocking);
Runs a single iteration of the mainloop. If no events are available either
return or block dependent on the value of
gtk_main_do_event ()void gtk_main_do_event (GdkEvent *event); Processes a single GDK event. This is public only to allow filtering of events between GDK and GTK+. You will not usually need to call this function directly. While you should not call this function directly, you might want to know how exactly events are handled. So here is what this function does with the event:
GtkModuleInitFunc ()void (*GtkModuleInitFunc) (gint *argc,
gchar ***argv);
Each GTK+ module must have a function
GtkModuleDisplayInitFunc ()void (*GtkModuleDisplayInitFunc) (GdkDisplay *display);
Since 2.2 gtk_true ()gboolean gtk_true (void);
All this function does it to return Example 3. A persistent window
##include <gtk/gtk.h>
int
main (int argc, char **argv)
{
GtkWidget *win, *but;
gtk_init( &argc, &argv );
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (win, "delete-event",
G_CALLBACK (gtk_true), NULL);
g_signal_connect (win, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
but = gtk_button_new_with_label ("Close yourself. I mean it!");
g_signal_connect_swapped (but, "clicked",
G_CALLBACK (gtk_object_destroy), win);
gtk_container_add (GTK_CONTAINER (win), but);
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
gtk_false ()gboolean gtk_false (void);
Analogical to
gtk_grab_add ()void gtk_grab_add (GtkWidget *widget);
Makes
If
gtk_grab_get_current ()GtkWidget* gtk_grab_get_current (void); Queries the current grab of the default window group.
gtk_grab_remove ()void gtk_grab_remove (GtkWidget *widget);
Removes the grab from the given widget. You have to pair calls to
If
gtk_init_add ()void gtk_init_add (GtkFunction function, gpointer data); Registers a function to be called when the mainloop is started.
gtk_quit_add_destroy ()void gtk_quit_add_destroy (guint main_level,
GtkObject *object);
Trigger destruction of
gtk_quit_add ()guint gtk_quit_add (guint main_level,
GtkFunction function,
gpointer data);
Registers a function to be called when an instance of the mainloop is left.
gtk_quit_add_full ()guint gtk_quit_add_full (guint main_level,
GtkFunction function,
GtkCallbackMarshal marshal,
gpointer data,
GDestroyNotify destroy);
Registers a function to be called when an instance of the mainloop is left.
In comparison to
The former can be used to run interpreted code instead of a compiled function
while the latter can be used to free the information stored in
gtk_quit_remove ()void gtk_quit_remove (guint quit_handler_id); Removes a quit handler by its identifier.
gtk_quit_remove_by_data ()void gtk_quit_remove_by_data (gpointer data);
Removes a quit handler identified by its
gtk_timeout_add_full ()guint gtk_timeout_add_full (guint32 interval,
GtkFunction function,
GtkCallbackMarshal marshal,
gpointer data,
GDestroyNotify destroy);
Warning
Registers a function to be called periodically. The function will be called
repeatedly after
gtk_timeout_add ()guint gtk_timeout_add (guint32 interval,
GtkFunction function,
gpointer data);
Warning
Registers a function to be called periodically. The function will be called
repeatedly after
gtk_timeout_remove ()void gtk_timeout_remove (guint timeout_handler_id); Warning
Removes the given timeout destroying all information about it.
gtk_idle_add ()guint gtk_idle_add (GtkFunction function, gpointer data); Warning
Causes the mainloop to call the given function whenever no events with
higher priority are to be processed. The default priority is
gtk_idle_add_priority ()guint gtk_idle_add_priority (gint priority,
GtkFunction function,
gpointer data);
Warning
Like
gtk_idle_add_full ()guint gtk_idle_add_full (gint priority,
GtkFunction function,
GtkCallbackMarshal marshal,
gpointer data,
GDestroyNotify destroy);
Warning
Like
gtk_idle_remove ()void gtk_idle_remove (guint idle_handler_id); Warning
Removes the idle function with the given id.
gtk_idle_remove_by_data ()void gtk_idle_remove_by_data (gpointer data); Warning
Removes the idle function identified by the user data.
gtk_input_add_full ()guint gtk_input_add_full (gint source,
GdkInputCondition condition,
GdkInputFunction function,
GtkCallbackMarshal marshal,
gpointer data,
GDestroyNotify destroy);
Warning
Registers a function to be called when a condition becomes true on a file descriptor.
gtk_input_remove ()void gtk_input_remove (guint input_handler_id); Warning
Removes the function with the given id.
GTK_PRIORITY_REDRAW#define GTK_PRIORITY_REDRAW (G_PRIORITY_HIGH_IDLE + 20) Warning
Use this priority for redrawing related stuff. It is used internally by
GTK+ to do pending redraws. This priority is lower than GTK_PRIORITY_RESIZE#define GTK_PRIORITY_RESIZE (G_PRIORITY_HIGH_IDLE + 10)
Use this priority for resizing related stuff. It is used internally by
GTK+ to compute the sizes of widgets. This priority is higher than
GTK_PRIORITY_HIGH#define GTK_PRIORITY_HIGH G_PRIORITY_HIGH Warning
Use this for high priority timeouts. This priority is never used inside GTK+ so everything running at this priority will be running before anything inside the toolkit. GTK_PRIORITY_INTERNAL#define GTK_PRIORITY_INTERNAL GTK_PRIORITY_REDRAW Warning
This priority is for GTK+ internal stuff. Don't use it in your applications. GTK_PRIORITY_DEFAULT#define GTK_PRIORITY_DEFAULT G_PRIORITY_DEFAULT_IDLE Warning
Default priority for idle functions. GTK_PRIORITY_LOW#define GTK_PRIORITY_LOW G_PRIORITY_LOW Warning
Priority for very unimportant background tasks. gtk_key_snooper_install ()guint gtk_key_snooper_install (GtkKeySnoopFunc snooper, gpointer func_data); Installs a key snooper function, which will get called on all key events before delivering them normally.
GtkKeySnoopFunc ()gint (*GtkKeySnoopFunc) (GtkWidget *grab_widget, GdkEventKey *event, gpointer func_data); Key snooper functions are called before normal event delivery. They can be used to implement custom key event handling.
gtk_key_snooper_remove ()void gtk_key_snooper_remove (guint snooper_handler_id); Removes the key snooper function with the given id.
gtk_get_current_event ()GdkEvent* gtk_get_current_event (void);
Obtains a copy of the event currently being processed by GTK+. For
example, if you get a "clicked" signal from GtkButton, the current
event will be the GdkEventButton that triggered the "clicked"
signal. The returned event must be freed with
gtk_get_current_event_time ()guint32 gtk_get_current_event_time (void);
If there is a current event and it has a timestamp, return that
timestamp, otherwise return
gtk_get_current_event_state ()gboolean gtk_get_current_event_state (GdkModifierType *state);
If there is a current event and it has a state field, place
that state field in
gtk_get_event_widget ()GtkWidget* gtk_get_event_widget (GdkEvent *event);
If
gtk_propagate_event ()void gtk_propagate_event (GtkWidget *widget, GdkEvent *event);
Sends an event to a widget, propagating the event to parent widgets
if the event remains unhandled. Events received by GTK+ from GDK
normally begin in
All that said, you most likely don't want to use any of these
functions; synthesizing events is rarely needed. Consider asking on
the mailing list for better ways to achieve your goals. For
example, use
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||