1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gdkconfig.h"
31 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
41 #include <sys/types.h> /* For uid_t, gid_t */
49 #include <pango/pango-utils.h> /* For pango_split_file_list */
54 #include "gtkaccelmap.h"
57 #include "gtkversion.h"
60 #include "gtkselection.h"
61 #include "gtksettings.h"
62 #include "gtkwidget.h"
63 #include "gtkwindow.h"
64 #include "gtkprivate.h"
67 /* Private type definitions
69 typedef struct _GtkInitFunction GtkInitFunction;
70 typedef struct _GtkQuitFunction GtkQuitFunction;
71 typedef struct _GtkClosure GtkClosure;
72 typedef struct _GtkKeySnooperData GtkKeySnooperData;
73 typedef struct _GtkModuleInfo GtkModuleInfo;
75 struct _GtkInitFunction
81 struct _GtkQuitFunction
85 GtkCallbackMarshal marshal;
88 GtkDestroyNotify destroy;
93 GtkCallbackMarshal marshal;
95 GtkDestroyNotify destroy;
98 struct _GtkKeySnooperData
100 GtkKeySnoopFunc func;
105 struct _GtkModuleInfo
107 GtkModuleInitFunc init_func;
108 GtkModuleDisplayInitFunc display_init_func;
111 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
112 static void gtk_quit_destroy (GtkQuitFunction *quitf);
113 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
116 static void gtk_destroy_closure (gpointer data);
117 static gboolean gtk_invoke_idle_timeout (gpointer data);
118 static void gtk_invoke_input (gpointer data,
120 GdkInputCondition condition);
123 static void gtk_error (gchar *str);
124 static void gtk_warning (gchar *str);
125 static void gtk_message (gchar *str);
126 static void gtk_print (gchar *str);
129 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
131 const guint gtk_major_version = GTK_MAJOR_VERSION;
132 const guint gtk_minor_version = GTK_MINOR_VERSION;
133 const guint gtk_micro_version = GTK_MICRO_VERSION;
134 const guint gtk_binary_age = GTK_BINARY_AGE;
135 const guint gtk_interface_age = GTK_INTERFACE_AGE;
137 static GSList *gtk_modules;
139 /* Saved argc,argv for delayed module initialization
141 static gint gtk_argc = 0;
142 static gchar **gtk_argv = NULL;
144 static guint gtk_main_loop_level = 0;
145 static gint gtk_initialized = FALSE;
146 static GList *current_events = NULL;
148 static GSList *main_loops = NULL; /* stack of currently executing main loops */
150 static GList *init_functions = NULL; /* A list of init functions.
152 static GList *quit_functions = NULL; /* A list of quit functions.
154 static GMemChunk *quit_mem_chunk = NULL;
156 static GSList *key_snoopers = NULL;
158 guint gtk_debug_flags = 0; /* Global GTK debug flag */
160 #ifdef G_ENABLE_DEBUG
161 static const GDebugKey gtk_debug_keys[] = {
162 {"misc", GTK_DEBUG_MISC},
163 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
164 {"text", GTK_DEBUG_TEXT},
165 {"tree", GTK_DEBUG_TREE},
166 {"updates", GTK_DEBUG_UPDATES},
167 {"keybindings", GTK_DEBUG_KEYBINDINGS},
168 {"multihead", GTK_DEBUG_MULTIHEAD}
171 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
173 #endif /* G_ENABLE_DEBUG */
177 * @required_major: the required major version.
178 * @required_minor: the required major version.
179 * @required_micro: the required major version.
181 * Checks that the GTK+ library in use is compatible with the
182 * given version. Generally you would pass in the constants
183 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
184 * as the three arguments to this function; that produces
185 * a check that the library in use is compatible with
186 * the version of GTK+ the application or module was compiled
189 * Compatibility is defined by two things: first the version
190 * of the running library is newer than the version
191 * @required_major.required_minor.@required_micro. Second
192 * the running library must be binary compatible with the
193 * version @required_major.required_minor.@required_micro
194 * (same major version.)
196 * This function is primarily for GTK+ modules; the module
197 * can call this function to check that it wasn't loaded
198 * into an incompatible version of GTK+. However, such a
199 * a check isn't completely reliable, since the module may be
200 * linked against an old version of GTK+ and calling the
201 * old version of gtk_check_version(), but still get loaded
202 * into an application using a newer version of GTK+.
204 * Return value: %NULL if the GTK+ library is compatible with the
205 * given version, or a string describing the version mismatch.
206 * The returned string is owned by GTK+ and should not be modified
210 gtk_check_version (guint required_major,
211 guint required_minor,
212 guint required_micro)
214 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
215 gint required_effective_micro = 100 * required_minor + required_micro;
217 if (required_major > GTK_MAJOR_VERSION)
218 return "Gtk+ version too old (major mismatch)";
219 if (required_major < GTK_MAJOR_VERSION)
220 return "Gtk+ version too new (major mismatch)";
221 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
222 return "Gtk+ version too new (micro mismatch)";
223 if (required_effective_micro > gtk_effective_micro)
224 return "Gtk+ version too old (micro mismatch)";
228 /* This checks to see if the process is running suid or sgid
229 * at the current time. If so, we don't allow GTK+ to be initialized.
230 * This is meant to be a mild check - we only error out if we
231 * can prove the programmer is doing something wrong, not if
232 * they could be doing something wrong. For this reason, we
233 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
238 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
240 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
241 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
243 #ifdef HAVE_GETRESUID
244 /* These aren't in the header files, so we prototype them here.
246 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
247 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
249 if (getresuid (&ruid, &euid, &suid) != 0 ||
250 getresgid (&rgid, &egid, &sgid) != 0)
251 #endif /* HAVE_GETRESUID */
253 suid = ruid = getuid ();
254 sgid = rgid = getgid ();
259 if (ruid != euid || ruid != suid ||
260 rgid != egid || rgid != sgid)
262 g_warning ("This process is currently running setuid or setgid.\n"
263 "This is not a supported use of GTK+. You must create a helper\n"
264 "program instead. For further details, see:\n\n"
265 " http://www.gtk.org/setuid.html\n\n"
266 "Refusing to initialize GTK+.");
275 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
278 _gtk_get_datadir (void)
280 static char *gtk_datadir = NULL;
281 if (gtk_datadir == NULL)
282 gtk_datadir = g_win32_get_package_installation_subdirectory
283 (GETTEXT_PACKAGE, dll_name, "share");
289 _gtk_get_libdir (void)
291 static char *gtk_libdir = NULL;
292 if (gtk_libdir == NULL)
293 gtk_libdir = g_win32_get_package_installation_subdirectory
294 (GETTEXT_PACKAGE, dll_name, "lib");
300 _gtk_get_localedir (void)
302 static char *gtk_localedir = NULL;
303 if (gtk_localedir == NULL)
304 gtk_localedir = g_win32_get_package_installation_subdirectory
305 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
307 return gtk_localedir;
311 _gtk_get_sysconfdir (void)
313 static char *gtk_sysconfdir = NULL;
314 if (gtk_sysconfdir == NULL)
315 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
316 (GETTEXT_PACKAGE, dll_name, "etc");
318 return gtk_sysconfdir;
322 _gtk_get_data_prefix (void)
324 static char *gtk_data_prefix = NULL;
325 if (gtk_data_prefix == NULL)
326 gtk_data_prefix = g_win32_get_package_installation_directory
327 (GETTEXT_PACKAGE, dll_name);
329 return gtk_data_prefix;
332 #endif /* G_OS_WIN32 */
335 get_module_path (void)
337 const gchar *module_path_env;
338 const gchar *exe_prefix;
339 const gchar *home_dir;
340 gchar *home_gtk_dir = NULL;
343 static gchar **result = NULL;
348 home_dir = g_get_home_dir();
350 home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);
352 module_path_env = g_getenv ("GTK_PATH");
353 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
356 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
358 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);
360 if (module_path_env && home_gtk_dir)
361 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
362 module_path_env, home_gtk_dir, default_dir, NULL);
363 else if (module_path_env)
364 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
365 module_path_env, default_dir, NULL);
366 else if (home_gtk_dir)
367 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
368 home_gtk_dir, default_dir, NULL);
370 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
373 g_free (home_gtk_dir);
374 g_free (default_dir);
376 result = pango_split_file_list (module_path);
377 g_free (module_path);
383 * _gtk_get_module_path:
384 * @type: the type of the module, for instance 'modules', 'engines', immodules'
386 * Determines the search path for a particular type of module.
388 * Return value: the search path for the module type. Free with g_strfreev().
391 _gtk_get_module_path (const gchar *type)
393 gchar **paths = get_module_path();
398 for (path = paths; *path; path++)
401 result = g_new (gchar *, count * 4 + 1);
404 for (path = get_module_path (); *path; path++)
406 gint use_version, use_host;
408 for (use_version = TRUE; use_version >= FALSE; use_version--)
409 for (use_host = TRUE; use_host >= FALSE; use_host--)
413 if (use_version && use_host)
414 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
415 else if (use_version)
416 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
418 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
420 tmp_dir = g_build_filename (*path, type, NULL);
422 result[count++] = tmp_dir;
426 result[count++] = NULL;
431 /* Like g_module_path, but use .la as the suffix
434 module_build_la_path (const gchar *directory,
435 const gchar *module_name)
440 if (strncmp (module_name, "lib", 3) == 0)
441 filename = (gchar *)module_name;
443 filename = g_strconcat ("lib", module_name, ".la", NULL);
445 if (directory && *directory)
446 result = g_build_filename (directory, filename, NULL);
448 result = g_strdup (filename);
450 if (filename != module_name)
458 * @name: the name of the module
459 * @type: the type of the module, for instance 'modules', 'engines', immodules'
461 * Looks for a dynamically loadable module named @name of type @type in the
462 * standard GTK+ module search path.
464 * Return value: the pathname to the found module, or %NULL if it wasn't found.
465 * Free with g_free().
468 _gtk_find_module (const gchar *name,
473 gchar *module_name = NULL;
475 if (g_path_is_absolute (name))
476 return g_strdup (name);
478 paths = _gtk_get_module_path (type);
479 for (path = paths; *path; path++)
483 tmp_name = g_module_build_path (*path, name);
484 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
486 module_name = tmp_name;
491 tmp_name = module_build_la_path (*path, name);
492 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
494 module_name = tmp_name;
506 find_module (const gchar *name)
511 module_name = _gtk_find_module (name, "modules");
514 /* As last resort, try loading without an absolute path (using system
517 module_name = g_module_build_path (NULL, name);
520 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
527 load_module (GSList *module_list,
530 GtkModuleInitFunc modinit_func = NULL;
532 GModule *module = NULL;
534 if (g_module_supported ())
536 module = find_module (name);
538 g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
541 if (!g_slist_find (module_list, (gconstpointer) modinit_func))
543 g_module_make_resident (module);
544 info = g_new (GtkModuleInfo, 1);
546 info->init_func = modinit_func;
547 g_module_symbol (module, "gtk_module_display_init",
548 (gpointer *) &info->display_init_func);
550 module_list = g_slist_prepend (module_list, info);
554 g_module_close (module);
561 g_message ("Failed to load module \"%s\": %s",
562 module ? g_module_name (module) : name,
565 g_module_close (module);
572 load_modules (const char *module_str)
574 gchar **module_names = pango_split_file_list (module_str);
575 GSList *module_list = NULL;
578 for (i = 0; module_names[i]; i++)
579 module_list = load_module (module_list, module_names[i]);
581 module_list = g_slist_reverse (module_list);
583 g_strfreev (module_names);
588 static gboolean do_setlocale = TRUE;
591 * gtk_disable_setlocale:
593 * Prevents gtk_init() and gtk_init_check() from automatically
594 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
595 * want to use this function if you wanted to set the locale for
596 * your program to something other than the user's locale, or if
597 * you wanted to set different values for different locale categories.
599 * Most programs should not need to call this function.
602 gtk_disable_setlocale (void)
605 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
607 do_setlocale = FALSE;
610 #undef gtk_init_check
613 default_display_notify_cb (GdkDisplayManager *display_manager)
617 /* Initialize non-multihead-aware modules when the
618 * default display is first set to a non-NULL value.
620 static gboolean initialized = FALSE;
622 if (!gdk_display_get_default () || initialized)
627 for (slist = gtk_modules; slist; slist = slist->next)
631 GtkModuleInfo *info = slist->data;
633 if (!info->display_init_func)
634 info->init_func (>k_argc, >k_argv);
640 display_opened_cb (GdkDisplayManager *display_manager,
645 for (slist = gtk_modules; slist; slist = slist->next)
649 GtkModuleInfo *info = slist->data;
651 if (info->display_init_func)
652 info->display_init_func (display);
659 * @argc: a pointer to the number of command line arguments.
660 * @argv: a pointer to the array of command line arguments.
662 * Parses command line arguments, and initializes global
663 * attributes of GTK+, but does not actually open a connection
664 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
666 * Any arguments used by GTK or GDK are removed from the array and
667 * @argc and @argv are updated accordingly.
669 * You shouldn't call this function explicitely if you are using
670 * gtk_init(), or gtk_init_check().
672 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
675 gtk_parse_args (int *argc,
678 GString *gtk_modules_string = NULL;
680 GdkDisplayManager *display_manager;
681 const gchar *env_string;
686 if (!check_setugid ())
690 g_set_error_handler (gtk_error);
691 g_set_warning_handler (gtk_warning);
692 g_set_message_handler (gtk_message);
693 g_set_print_handler (gtk_print);
698 if (!setlocale (LC_ALL, ""))
699 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
702 gdk_parse_args (argc, argv);
703 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
705 #ifdef G_ENABLE_DEBUG
706 env_string = g_getenv ("GTK_DEBUG");
707 if (env_string != NULL)
709 gtk_debug_flags = g_parse_debug_string (env_string,
714 #endif /* G_ENABLE_DEBUG */
716 env_string = g_getenv ("GTK_MODULES");
718 gtk_modules_string = g_string_new (env_string);
724 for (i = 1; i < *argc;)
726 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
727 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
729 gchar *module_name = (*argv)[i] + 12;
731 if (*module_name == '=')
733 else if (i + 1 < *argc)
737 module_name = (*argv)[i];
741 if (module_name && *module_name)
743 if (gtk_modules_string)
744 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
746 gtk_modules_string = g_string_new (NULL);
748 g_string_append (gtk_modules_string, module_name);
751 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
753 GLogLevelFlags fatal_mask;
755 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
756 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
757 g_log_set_always_fatal (fatal_mask);
760 #ifdef G_ENABLE_DEBUG
761 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
762 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
764 gchar *equal_pos = strchr ((*argv)[i], '=');
766 if (equal_pos != NULL)
768 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
772 else if ((i + 1) < *argc && (*argv)[i + 1])
774 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
782 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
783 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
785 gchar *equal_pos = strchr ((*argv)[i], '=');
787 if (equal_pos != NULL)
789 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
793 else if ((i + 1) < *argc && (*argv)[i + 1])
795 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
803 #endif /* G_ENABLE_DEBUG */
807 for (i = 1; i < *argc; i++)
809 for (k = i; k < *argc; k++)
810 if ((*argv)[k] != NULL)
816 for (j = i + k; j < *argc; j++)
817 (*argv)[j-k] = (*argv)[j];
822 gtk_argv = g_malloc ((gtk_argc + 1) * sizeof (char*));
823 for (i = 0; i < gtk_argc; i++)
824 gtk_argv[i] = g_strdup ((*argv)[i]);
825 gtk_argv[gtk_argc] = NULL;
828 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
829 gdk_window_set_debug_updates (TRUE);
831 /* load gtk modules */
832 if (gtk_modules_string)
834 gtk_modules = load_modules (gtk_modules_string->str);
835 g_string_free (gtk_modules_string, TRUE);
839 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
840 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
841 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
842 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
843 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
848 /* Translate to default:RTL if you want your widgets
849 * to be RTL, otherwise translate to default:LTR.
850 * Do *not* translate it to "predefinito:LTR", if it
851 * it isn't default:LTR or default:RTL it will not work
853 char *e = _("default:LTR");
854 if (strcmp (e, "default:RTL")==0) {
855 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
856 } else if (strcmp (e, "default:LTR")) {
857 g_warning ("Whoever translated default:LTR did so wrongly.\n");
862 _gtk_accel_map_init ();
865 /* Set the 'initialized' flag.
867 gtk_initialized = TRUE;
869 display_manager = gdk_display_manager_get ();
870 g_signal_connect (display_manager, "notify::default-display",
871 G_CALLBACK (default_display_notify_cb), NULL);
872 g_signal_connect (display_manager, "display-opened",
873 G_CALLBACK (display_opened_cb), NULL);
875 /* initialize multhead aware gtk modules; for other modules,
876 * we wait until we have a display open;
878 for (slist = gtk_modules; slist; slist = slist->next)
882 GtkModuleInfo *info = slist->data;
884 if (info->display_init_func)
885 info->init_func (argc, argv);
892 #undef gtk_init_check
896 * @argc: Address of the <parameter>argc</parameter> parameter of your
897 * main() function. Changed if any arguments were handled.
898 * @argv: Address of the <parameter>argv</parameter> parameter of main().
899 * Any parameters understood by gtk_init() are stripped before return.
901 * This function does the same work as gtk_init() with only
902 * a single change: It does not terminate the program if the GUI can't be
903 * initialized. Instead it returns %FALSE on failure.
905 * This way the application can fall back to some other means of communication
906 * with the user - for example a curses or command line interface.
908 * Return value: %TRUE if the GUI has been successfully initialized,
912 gtk_init_check (int *argc,
915 if (!gtk_parse_args (argc, argv))
918 return gdk_display_open_default_libgtk_only () != NULL;
925 * @argc: Address of the <parameter>argc</parameter> parameter of your
926 * main() function. Changed if any arguments were handled.
927 * @argv: Address of the <parameter>argv</parameter> parameter of main().
928 * Any parameters understood by gtk_init() are stripped before return.
930 * Call this function before using any other GTK+ functions in your GUI
931 * applications. It will initialize everything needed to operate the
932 * toolkit and parses some standard command line options. @argc and
933 * @argv are adjusted accordingly so your own code will
934 * never see those standard arguments.
937 * This function will terminate your program if it was unable to initialize
938 * the GUI for some reason. If you want your program to fall back to a
939 * textual interface you want to call gtk_init_check() instead.
943 gtk_init (int *argc, char ***argv)
945 if (!gtk_init_check (argc, argv))
947 const char *display_name_arg = gdk_get_display_arg_name ();
948 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
953 #ifdef G_PLATFORM_WIN32
956 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
958 if (sizeof_GtkWindow != sizeof (GtkWindow))
959 g_error ("Incompatible build!\n"
960 "The code using GTK+ thinks GtkWindow is of different\n"
961 "size than it actually is in this build of GTK+.\n"
962 "On Windows, this probably means that you have compiled\n"
963 "your code with gcc without the -fnative-struct\n"
964 "(or -mms-bitfields) switch, or that you are using\n"
965 "an unsupported compiler.");
968 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
969 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
970 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
971 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
972 * -fnative-struct still matters. GtkBox is one such.
975 check_sizeof_GtkBox (size_t sizeof_GtkBox)
977 if (sizeof_GtkBox != sizeof (GtkBox))
978 g_error ("Incompatible build!\n"
979 "The code using GTK+ thinks GtkBox is of different\n"
980 "size than it actually is in this build of GTK+.\n"
981 "On Windows, this probably means that you have compiled\n"
982 "your code with gcc without the -fnative-struct\n"
983 "(or -mms-bitfields) switch, or that you are using\n"
984 "an unsupported compiler.");
987 /* These two functions might get more checks added later, thus pass
988 * in the number of extra args.
991 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
993 check_sizeof_GtkWindow (sizeof_GtkWindow);
995 check_sizeof_GtkBox (sizeof_GtkBox);
996 gtk_init (argc, argv);
1000 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1002 check_sizeof_GtkWindow (sizeof_GtkWindow);
1003 if (num_checks >= 2)
1004 check_sizeof_GtkBox (sizeof_GtkBox);
1005 return gtk_init_check (argc, argv);
1011 gtk_exit (gint errorcode)
1020 * Initializes internationalization support for GTK+. gtk_init()
1021 * automatically does this, so there is typically no point
1022 * in calling this function.
1024 * If you are calling this function because you changed the locale
1025 * after GTK+ is was initialized, then calling this function
1026 * may help a bit. (Note, however, that changing the locale
1027 * after GTK+ is initialized may produce inconsistent results and
1028 * is not really supported.)
1030 * In detail - sets the current locale according to the
1031 * program environment. This is the same as calling the C library function
1032 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
1033 * locale specific setup of the windowing system used by GDK.
1035 * Returns: a string corresponding to the locale set, typically in the
1036 * form lang_COUNTRY, where lang is an ISO-639 language code, and
1037 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
1038 * result of the setlocale(); it is also used on other machines, such as
1039 * Windows, where the C library returns a different result. The string is
1040 * owned by GTK+ and should not be modified or freed.
1043 gtk_set_locale (void)
1045 return gdk_set_locale ();
1049 * _gtk_get_lc_ctype:
1051 * Return the Unix-style locale string for the language currently in
1052 * effect. On Unix systems, this is the return value from
1053 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1054 * affect this through the environment variables LC_ALL, LC_CTYPE or
1055 * LANG (checked in that order). The locale strings typically is in
1056 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1057 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1058 * Swedish as written in Finland or pt_BR for Portuguese as written in
1061 * On Windows, the C library doesn't use any such environment
1062 * variables, and setting them won't affect the behaviour of functions
1063 * like ctime(). The user sets the locale through the Regional Options
1064 * in the Control Panel. The C library (in the setlocale() function)
1065 * does not use country and language codes, but country and language
1066 * names spelled out in English.
1067 * However, this function does check the above environment
1068 * variables, and does return a Unix-style locale string based on
1069 * either said environment variables or the thread's current locale.
1071 * Return value: a dynamically allocated string, free with g_free().
1075 _gtk_get_lc_ctype (void)
1078 /* Somebody might try to set the locale for this process using the
1079 * LANG or LC_ environment variables. The Microsoft C library
1080 * doesn't know anything about them. You set the locale in the
1081 * Control Panel. Setting these env vars won't have any affect on
1082 * locale-dependent C library functions like ctime(). But just for
1083 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1084 * it easier to test GTK and Pango in various default languages, you
1085 * don't have to clickety-click in the Control Panel, you can simply
1086 * start the program with LC_ALL=something on the command line.)
1090 p = getenv ("LC_ALL");
1092 return g_strdup (p);
1094 p = getenv ("LC_CTYPE");
1096 return g_strdup (p);
1098 p = getenv ("LANG");
1100 return g_strdup (p);
1102 return g_win32_getlocale ();
1104 return g_strdup (setlocale (LC_CTYPE, NULL));
1109 * gtk_get_default_language:
1111 * Returns the #PangoLanguage for the default language currently in
1112 * effect. (Note that this can change over the life of an
1113 * application.) The default language is derived from the current
1114 * locale. It determines, for example, whether GTK+ uses the
1115 * right-to-left or left-to-right text direction. See
1116 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
1118 * Return value: the default language as a #PangoLanguage, must not be
1122 gtk_get_default_language (void)
1125 PangoLanguage *result;
1128 lang = _gtk_get_lc_ctype ();
1129 p = strchr (lang, '.');
1132 p = strchr (lang, '@');
1136 result = pango_language_from_string (lang);
1147 GtkInitFunction *init;
1150 gtk_main_loop_level++;
1152 loop = g_main_loop_new (NULL, TRUE);
1153 main_loops = g_slist_prepend (main_loops, loop);
1155 tmp_list = functions = init_functions;
1156 init_functions = NULL;
1160 init = tmp_list->data;
1161 tmp_list = tmp_list->next;
1163 (* init->function) (init->data);
1166 g_list_free (functions);
1168 if (g_main_loop_is_running (main_loops->data))
1170 GDK_THREADS_LEAVE ();
1171 g_main_loop_run (loop);
1172 GDK_THREADS_ENTER ();
1178 GList *reinvoke_list = NULL;
1179 GtkQuitFunction *quitf;
1181 while (quit_functions)
1183 quitf = quit_functions->data;
1185 tmp_list = quit_functions;
1186 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1187 g_list_free_1 (tmp_list);
1189 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1190 gtk_quit_invoke_function (quitf))
1192 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1196 gtk_quit_destroy (quitf);
1203 work = g_list_last (reinvoke_list);
1205 quit_functions->prev = work;
1206 work->next = quit_functions;
1207 quit_functions = work;
1213 main_loops = g_slist_remove (main_loops, loop);
1215 g_main_loop_unref (loop);
1217 gtk_main_loop_level--;
1221 gtk_main_level (void)
1223 return gtk_main_loop_level;
1227 gtk_main_quit (void)
1229 g_return_if_fail (main_loops != NULL);
1231 g_main_loop_quit (main_loops->data);
1235 gtk_events_pending (void)
1239 GDK_THREADS_LEAVE ();
1240 result = g_main_context_pending (NULL);
1241 GDK_THREADS_ENTER ();
1247 gtk_main_iteration (void)
1249 GDK_THREADS_LEAVE ();
1250 g_main_context_iteration (NULL, TRUE);
1251 GDK_THREADS_ENTER ();
1254 return !g_main_loop_is_running (main_loops->data);
1260 gtk_main_iteration_do (gboolean blocking)
1262 GDK_THREADS_LEAVE ();
1263 g_main_context_iteration (NULL, blocking);
1264 GDK_THREADS_ENTER ();
1267 return !g_main_loop_is_running (main_loops->data);
1272 /* private libgtk to libgdk interfaces
1274 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1275 GdkWindow **grab_window,
1276 gboolean *owner_events);
1277 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1278 GdkWindow **grab_window,
1279 gboolean *owner_events);
1282 rewrite_events_translate (GdkWindow *old_window,
1283 GdkWindow *new_window,
1287 gint old_origin_x, old_origin_y;
1288 gint new_origin_x, new_origin_y;
1290 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1291 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1293 *x += old_origin_x - new_origin_x;
1294 *y += old_origin_y - new_origin_y;
1298 rewrite_event_for_window (GdkEvent *event,
1299 GdkWindow *new_window)
1301 event = gdk_event_copy (event);
1303 switch (event->type)
1306 rewrite_events_translate (event->any.window,
1308 &event->scroll.x, &event->scroll.y);
1310 case GDK_BUTTON_PRESS:
1311 case GDK_2BUTTON_PRESS:
1312 case GDK_3BUTTON_PRESS:
1313 case GDK_BUTTON_RELEASE:
1314 rewrite_events_translate (event->any.window,
1316 &event->button.x, &event->button.y);
1318 case GDK_MOTION_NOTIFY:
1319 rewrite_events_translate (event->any.window,
1321 &event->motion.x, &event->motion.y);
1324 case GDK_KEY_RELEASE:
1325 case GDK_PROXIMITY_IN:
1326 case GDK_PROXIMITY_OUT:
1333 g_object_unref (event->any.window);
1334 event->any.window = g_object_ref (new_window);
1339 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1340 * then what X11 does is deliver the event normally if it was going to this
1341 * client, otherwise, delivers it in terms of the grab window. This function
1342 * rewrites events to the effect that events going to the same window group
1343 * are delivered normally, otherwise, the event is delivered in terms of the
1347 rewrite_event_for_grabs (GdkEvent *event)
1349 GdkWindow *grab_window;
1350 GtkWidget *event_widget, *grab_widget;
1351 gboolean owner_events;
1352 GdkDisplay *display;
1354 switch (event->type)
1357 case GDK_BUTTON_PRESS:
1358 case GDK_2BUTTON_PRESS:
1359 case GDK_3BUTTON_PRESS:
1360 case GDK_BUTTON_RELEASE:
1361 case GDK_MOTION_NOTIFY:
1362 case GDK_PROXIMITY_IN:
1363 case GDK_PROXIMITY_OUT:
1364 display = gdk_drawable_get_display (event->proximity.window);
1365 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1371 case GDK_KEY_RELEASE:
1372 display = gdk_drawable_get_display (event->key.window);
1373 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1382 event_widget = gtk_get_event_widget (event);
1383 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1386 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1387 return rewrite_event_for_window (event, grab_window);
1393 gtk_main_do_event (GdkEvent *event)
1395 GtkWidget *event_widget;
1396 GtkWidget *grab_widget;
1397 GtkWindowGroup *window_group;
1398 GdkEvent *next_event;
1399 GdkEvent *rewritten_event = NULL;
1402 /* If there are any events pending then get the next one.
1404 next_event = gdk_event_peek ();
1406 /* Try to compress enter/leave notify events. These event
1407 * pairs occur when the mouse is dragged quickly across
1408 * a window with many buttons (or through a menu). Instead
1409 * of highlighting and de-highlighting each widget that
1410 * is crossed it is better to simply de-highlight the widget
1411 * which contained the mouse initially and highlight the
1412 * widget which ends up containing the mouse.
1415 if (((event->type == GDK_ENTER_NOTIFY) ||
1416 (event->type == GDK_LEAVE_NOTIFY)) &&
1417 ((next_event->type == GDK_ENTER_NOTIFY) ||
1418 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1419 (next_event->type != event->type) &&
1420 (next_event->any.window == event->any.window))
1422 /* Throw both the peeked copy and the queued copy away
1424 gdk_event_free (next_event);
1425 next_event = gdk_event_get ();
1426 gdk_event_free (next_event);
1432 gdk_event_free (next_event);
1434 if (event->type == GDK_SETTING)
1436 _gtk_settings_handle_event (&event->setting);
1440 /* Find the widget which got the event. We store the widget
1441 * in the user_data field of GdkWindow's.
1442 * Ignore the event if we don't have a widget for it, except
1443 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1444 * Though this happens rarely, bogus events can occour
1445 * for e.g. destroyed GdkWindows.
1447 event_widget = gtk_get_event_widget (event);
1450 /* To handle selection INCR transactions, we select
1451 * PropertyNotify events on the requestor window and create
1452 * a corresponding (fake) GdkWindow so that events get
1453 * here. There won't be a widget though, so we have to handle
1456 if (event->type == GDK_PROPERTY_NOTIFY)
1457 _gtk_selection_incr_event (event->any.window,
1463 /* If pointer or keyboard grabs are in effect, munge the events
1464 * so that each window group looks like a separate app.
1466 rewritten_event = rewrite_event_for_grabs (event);
1467 if (rewritten_event)
1469 event = rewritten_event;
1470 event_widget = gtk_get_event_widget (event);
1473 window_group = gtk_main_get_window_group (event_widget);
1475 /* Push the event onto a stack of current events for
1476 * gtk_current_event_get().
1478 current_events = g_list_prepend (current_events, event);
1480 /* If there is a grab in effect...
1482 if (window_group->grabs)
1484 grab_widget = window_group->grabs->data;
1486 /* If the grab widget is an ancestor of the event widget
1487 * then we send the event to the original event widget.
1488 * This is the key to implementing modality.
1490 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1491 gtk_widget_is_ancestor (event_widget, grab_widget))
1492 grab_widget = event_widget;
1496 grab_widget = event_widget;
1499 /* Not all events get sent to the grabbing widget.
1500 * The delete, destroy, expose, focus change and resize
1501 * events still get sent to the event widget because
1502 * 1) these events have no meaning for the grabbing widget
1503 * and 2) redirecting these events to the grabbing widget
1504 * could cause the display to be messed up.
1506 * Drag events are also not redirected, since it isn't
1507 * clear what the semantics of that would be.
1509 switch (event->type)
1515 g_object_ref (event_widget);
1516 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1517 !gtk_widget_event (event_widget, event))
1518 gtk_widget_destroy (event_widget);
1519 g_object_unref (event_widget);
1523 /* Unexpected GDK_DESTROY from the outside, ignore for
1524 * child windows, handle like a GDK_DELETE for toplevels
1526 if (!event_widget->parent)
1528 g_object_ref (event_widget);
1529 if (!gtk_widget_event (event_widget, event) &&
1530 GTK_WIDGET_REALIZED (event_widget))
1531 gtk_widget_destroy (event_widget);
1532 g_object_unref (event_widget);
1537 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1539 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1540 gtk_widget_send_expose (event_widget, event);
1541 gdk_window_end_paint (event->any.window);
1544 gtk_widget_send_expose (event_widget, event);
1547 case GDK_PROPERTY_NOTIFY:
1549 case GDK_FOCUS_CHANGE:
1553 case GDK_SELECTION_CLEAR:
1554 case GDK_SELECTION_REQUEST:
1555 case GDK_SELECTION_NOTIFY:
1556 case GDK_CLIENT_EVENT:
1557 case GDK_VISIBILITY_NOTIFY:
1558 case GDK_WINDOW_STATE:
1559 gtk_widget_event (event_widget, event);
1563 case GDK_BUTTON_PRESS:
1564 case GDK_2BUTTON_PRESS:
1565 case GDK_3BUTTON_PRESS:
1566 gtk_propagate_event (grab_widget, event);
1570 case GDK_KEY_RELEASE:
1573 if (gtk_invoke_key_snoopers (grab_widget, event))
1576 /* else fall through */
1577 case GDK_MOTION_NOTIFY:
1578 case GDK_BUTTON_RELEASE:
1579 case GDK_PROXIMITY_IN:
1580 case GDK_PROXIMITY_OUT:
1581 gtk_propagate_event (grab_widget, event);
1584 case GDK_ENTER_NOTIFY:
1585 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1587 g_object_ref (event_widget);
1589 gtk_widget_event (grab_widget, event);
1590 if (event_widget == grab_widget)
1591 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1593 g_object_unref (event_widget);
1597 case GDK_LEAVE_NOTIFY:
1598 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1600 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1601 gtk_widget_event (event_widget, event);
1603 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1604 gtk_widget_event (grab_widget, event);
1607 case GDK_DRAG_STATUS:
1608 case GDK_DROP_FINISHED:
1609 _gtk_drag_source_handle_event (event_widget, event);
1611 case GDK_DRAG_ENTER:
1612 case GDK_DRAG_LEAVE:
1613 case GDK_DRAG_MOTION:
1614 case GDK_DROP_START:
1615 _gtk_drag_dest_handle_event (event_widget, event);
1618 g_assert_not_reached ();
1622 tmp_list = current_events;
1623 current_events = g_list_remove_link (current_events, tmp_list);
1624 g_list_free_1 (tmp_list);
1626 if (rewritten_event)
1627 gdk_event_free (rewritten_event);
1642 static GtkWindowGroup *
1643 gtk_main_get_window_group (GtkWidget *widget)
1645 GtkWidget *toplevel = NULL;
1648 toplevel = gtk_widget_get_toplevel (widget);
1650 if (toplevel && GTK_IS_WINDOW (toplevel))
1651 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1653 return _gtk_window_get_group (NULL);
1658 GtkWidget *old_grab_widget;
1659 GtkWidget *new_grab_widget;
1663 check_is_grabbed (GtkWidget *widget,
1664 GtkWidget *grab_widget)
1667 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1673 gtk_grab_notify_foreach (GtkWidget *child,
1677 GrabNotifyInfo *info = data;
1678 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1679 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1681 if (was_grabbed != is_grabbed)
1683 g_object_ref (child);
1685 g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1687 if (GTK_IS_CONTAINER (child))
1688 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1690 g_object_unref (child);
1695 gtk_grab_notify (GtkWindowGroup *group,
1696 GtkWidget *grab_widget,
1697 gboolean was_grabbed)
1700 GrabNotifyInfo info;
1704 info.old_grab_widget = grab_widget;
1705 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1709 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1710 info.new_grab_widget = grab_widget;
1713 g_object_ref (group);
1714 g_object_ref (grab_widget);
1716 toplevels = gtk_window_list_toplevels ();
1717 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1721 GtkWindow *toplevel = toplevels->data;
1722 toplevels = g_list_delete_link (toplevels, toplevels);
1724 if (group == _gtk_window_get_group (toplevel))
1725 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1726 g_object_unref (toplevel);
1729 g_object_unref (group);
1730 g_object_unref (grab_widget);
1734 gtk_grab_add (GtkWidget *widget)
1736 GtkWindowGroup *group;
1737 gboolean was_grabbed;
1739 g_return_if_fail (widget != NULL);
1741 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1743 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1745 group = gtk_main_get_window_group (widget);
1747 was_grabbed = (group->grabs != NULL);
1749 g_object_ref (widget);
1750 group->grabs = g_slist_prepend (group->grabs, widget);
1752 gtk_grab_notify (group, widget, FALSE);
1757 gtk_grab_get_current (void)
1759 GtkWindowGroup *group;
1761 group = gtk_main_get_window_group (NULL);
1764 return GTK_WIDGET (group->grabs->data);
1769 gtk_grab_remove (GtkWidget *widget)
1771 GtkWindowGroup *group;
1773 g_return_if_fail (widget != NULL);
1775 if (GTK_WIDGET_HAS_GRAB (widget))
1777 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1779 group = gtk_main_get_window_group (widget);
1780 group->grabs = g_slist_remove (group->grabs, widget);
1782 g_object_unref (widget);
1784 gtk_grab_notify (group, widget, TRUE);
1789 gtk_init_add (GtkFunction function,
1792 GtkInitFunction *init;
1794 init = g_new (GtkInitFunction, 1);
1795 init->function = function;
1798 init_functions = g_list_prepend (init_functions, init);
1802 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1805 GtkKeySnooperData *data;
1806 static guint snooper_id = 1;
1808 g_return_val_if_fail (snooper != NULL, 0);
1810 data = g_new (GtkKeySnooperData, 1);
1811 data->func = snooper;
1812 data->func_data = func_data;
1813 data->id = snooper_id++;
1814 key_snoopers = g_slist_prepend (key_snoopers, data);
1820 gtk_key_snooper_remove (guint snooper_id)
1822 GtkKeySnooperData *data = NULL;
1825 slist = key_snoopers;
1829 if (data->id == snooper_id)
1832 slist = slist->next;
1837 key_snoopers = g_slist_remove (key_snoopers, data);
1843 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1847 gint return_val = FALSE;
1849 slist = key_snoopers;
1850 while (slist && !return_val)
1852 GtkKeySnooperData *data;
1855 slist = slist->next;
1856 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1863 gtk_quit_add_full (guint main_level,
1864 GtkFunction function,
1865 GtkCallbackMarshal marshal,
1867 GtkDestroyNotify destroy)
1869 static guint quit_id = 1;
1870 GtkQuitFunction *quitf;
1872 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1874 if (!quit_mem_chunk)
1875 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1876 512, G_ALLOC_AND_FREE);
1878 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1880 quitf->id = quit_id++;
1881 quitf->main_level = main_level;
1882 quitf->function = function;
1883 quitf->marshal = marshal;
1885 quitf->destroy = destroy;
1887 quit_functions = g_list_prepend (quit_functions, quitf);
1893 gtk_quit_destroy (GtkQuitFunction *quitf)
1896 quitf->destroy (quitf->data);
1897 g_mem_chunk_free (quit_mem_chunk, quitf);
1901 gtk_quit_destructor (GtkObject **object_p)
1904 gtk_object_destroy (*object_p);
1911 gtk_quit_add_destroy (guint main_level,
1914 GtkObject **object_p;
1916 g_return_if_fail (main_level > 0);
1917 g_return_if_fail (GTK_IS_OBJECT (object));
1919 object_p = g_new (GtkObject*, 1);
1921 g_signal_connect (object,
1923 G_CALLBACK (gtk_widget_destroyed),
1925 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1929 gtk_quit_add (guint main_level,
1930 GtkFunction function,
1933 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1937 gtk_quit_remove (guint id)
1939 GtkQuitFunction *quitf;
1942 tmp_list = quit_functions;
1945 quitf = tmp_list->data;
1947 if (quitf->id == id)
1949 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1950 g_list_free (tmp_list);
1951 gtk_quit_destroy (quitf);
1956 tmp_list = tmp_list->next;
1961 gtk_quit_remove_by_data (gpointer data)
1963 GtkQuitFunction *quitf;
1966 tmp_list = quit_functions;
1969 quitf = tmp_list->data;
1971 if (quitf->data == data)
1973 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1974 g_list_free (tmp_list);
1975 gtk_quit_destroy (quitf);
1980 tmp_list = tmp_list->next;
1985 gtk_timeout_add_full (guint32 interval,
1986 GtkFunction function,
1987 GtkCallbackMarshal marshal,
1989 GtkDestroyNotify destroy)
1993 GtkClosure *closure;
1995 closure = g_new (GtkClosure, 1);
1996 closure->marshal = marshal;
1997 closure->data = data;
1998 closure->destroy = destroy;
2000 return g_timeout_add_full (0, interval,
2001 gtk_invoke_idle_timeout,
2003 gtk_destroy_closure);
2006 return g_timeout_add_full (0, interval, function, data, destroy);
2010 gtk_timeout_add (guint32 interval,
2011 GtkFunction function,
2014 return g_timeout_add_full (0, interval, function, data, NULL);
2018 gtk_timeout_remove (guint tag)
2020 g_source_remove (tag);
2024 gtk_idle_add_full (gint priority,
2025 GtkFunction function,
2026 GtkCallbackMarshal marshal,
2028 GtkDestroyNotify destroy)
2032 GtkClosure *closure;
2034 closure = g_new (GtkClosure, 1);
2035 closure->marshal = marshal;
2036 closure->data = data;
2037 closure->destroy = destroy;
2039 return g_idle_add_full (priority,
2040 gtk_invoke_idle_timeout,
2042 gtk_destroy_closure);
2045 return g_idle_add_full (priority, function, data, destroy);
2049 gtk_idle_add (GtkFunction function,
2052 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2056 gtk_idle_add_priority (gint priority,
2057 GtkFunction function,
2060 return g_idle_add_full (priority, function, data, NULL);
2064 gtk_idle_remove (guint tag)
2066 g_source_remove (tag);
2070 gtk_idle_remove_by_data (gpointer data)
2072 if (!g_idle_remove_by_data (data))
2073 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2077 gtk_input_add_full (gint source,
2078 GdkInputCondition condition,
2079 GdkInputFunction function,
2080 GtkCallbackMarshal marshal,
2082 GtkDestroyNotify destroy)
2086 GtkClosure *closure;
2088 closure = g_new (GtkClosure, 1);
2089 closure->marshal = marshal;
2090 closure->data = data;
2091 closure->destroy = destroy;
2093 return gdk_input_add_full (source,
2095 (GdkInputFunction) gtk_invoke_input,
2097 (GdkDestroyNotify) gtk_destroy_closure);
2100 return gdk_input_add_full (source, condition, function, data, destroy);
2104 gtk_input_remove (guint tag)
2106 g_source_remove (tag);
2110 gtk_destroy_closure (gpointer data)
2112 GtkClosure *closure = data;
2114 if (closure->destroy)
2115 (closure->destroy) (closure->data);
2120 gtk_invoke_idle_timeout (gpointer data)
2122 GtkClosure *closure = data;
2125 gint ret_val = FALSE;
2126 args[0].name = NULL;
2127 args[0].type = G_TYPE_BOOLEAN;
2128 args[0].d.pointer_data = &ret_val;
2129 closure->marshal (NULL, closure->data, 0, args);
2134 gtk_invoke_input (gpointer data,
2136 GdkInputCondition condition)
2138 GtkClosure *closure = data;
2141 args[0].type = G_TYPE_INT;
2142 args[0].name = NULL;
2143 GTK_VALUE_INT (args[0]) = source;
2144 args[1].type = GDK_TYPE_INPUT_CONDITION;
2145 args[1].name = NULL;
2146 GTK_VALUE_FLAGS (args[1]) = condition;
2147 args[2].type = G_TYPE_NONE;
2148 args[2].name = NULL;
2150 closure->marshal (NULL, closure->data, 2, args);
2154 * gtk_get_current_event:
2156 * Obtains a copy of the event currently being processed by GTK+. For
2157 * example, if you get a "clicked" signal from #GtkButton, the current
2158 * event will be the #GdkEventButton that triggered the "clicked"
2159 * signal. The returned event must be freed with gdk_event_free().
2160 * If there is no current event, the function returns %NULL.
2162 * Return value: a copy of the current event, or %NULL if no current event.
2165 gtk_get_current_event (void)
2168 return gdk_event_copy (current_events->data);
2174 * gtk_get_current_event_time:
2176 * If there is a current event and it has a timestamp, return that
2177 * timestamp, otherwise return %GDK_CURRENT_TIME.
2179 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2182 gtk_get_current_event_time (void)
2185 return gdk_event_get_time (current_events->data);
2187 return GDK_CURRENT_TIME;
2191 * gtk_get_current_event_state:
2192 * @state: a location to store the state of the current event
2194 * If there is a current event and it has a state field, place
2195 * that state field in @state and return %TRUE, otherwise return
2198 * Return value: %TRUE if there was a current event and it had a state field
2201 gtk_get_current_event_state (GdkModifierType *state)
2203 g_return_val_if_fail (state != NULL, FALSE);
2206 return gdk_event_get_state (current_events->data, state);
2215 * gtk_get_event_widget:
2216 * @event: a #GdkEvent
2218 * If @event is %NULL or the event was not associated with any widget,
2219 * returns %NULL, otherwise returns the widget that received the event
2222 * Return value: the widget that originally received @event, or %NULL
2225 gtk_get_event_widget (GdkEvent *event)
2230 if (event && event->any.window)
2231 gdk_window_get_user_data (event->any.window, (void**) &widget);
2237 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2239 if (!quitf->marshal)
2240 return quitf->function (quitf->data);
2244 gint ret_val = FALSE;
2246 args[0].name = NULL;
2247 args[0].type = G_TYPE_BOOLEAN;
2248 args[0].d.pointer_data = &ret_val;
2249 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2257 * gtk_propagate_event:
2258 * @widget: a #GtkWidget
2261 * Sends an event to a widget, propagating the event to parent widgets
2262 * if the event remains unhandled. Events received by GTK+ from GDK
2263 * normally begin in gtk_main_do_event(). Depending on the type of
2264 * event, existence of modal dialogs, grabs, etc., the event may be
2265 * propagated; if so, this function is used. gtk_propagate_event()
2266 * calls gtk_widget_event() on each widget it decides to send the
2267 * event to. So gtk_widget_event() is the lowest-level function; it
2268 * simply emits the "event" and possibly an event-specific signal on a
2269 * widget. gtk_propagate_event() is a bit higher-level, and
2270 * gtk_main_do_event() is the highest level.
2272 * All that said, you most likely don't want to use any of these
2273 * functions; synthesizing events is rarely needed. Consider asking on
2274 * the mailing list for better ways to achieve your goals. For
2275 * example, use gdk_window_invalidate_rect() or
2276 * gtk_widget_queue_draw() instead of making up expose events.
2280 gtk_propagate_event (GtkWidget *widget,
2285 g_return_if_fail (GTK_IS_WIDGET (widget));
2286 g_return_if_fail (event != NULL);
2288 handled_event = FALSE;
2290 g_object_ref (widget);
2292 if ((event->type == GDK_KEY_PRESS) ||
2293 (event->type == GDK_KEY_RELEASE))
2295 /* Only send key events within Window widgets to the Window
2296 * The Window widget will in turn pass the
2297 * key event on to the currently focused widget
2302 window = gtk_widget_get_toplevel (widget);
2303 if (window && GTK_IS_WINDOW (window))
2305 /* If there is a grab within the window, give the grab widget
2306 * a first crack at the key event
2308 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2309 handled_event = gtk_widget_event (widget, event);
2313 window = gtk_widget_get_toplevel (widget);
2314 if (window && GTK_IS_WINDOW (window))
2316 if (GTK_WIDGET_IS_SENSITIVE (window))
2317 gtk_widget_event (window, event);
2321 handled_event = TRUE; /* don't send to widget */
2325 /* Other events get propagated up the widget tree
2326 * so that parents can see the button and motion
2327 * events of the children.
2335 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2336 tmp = widget->parent;
2337 g_object_unref (widget);
2341 if (!handled_event && widget)
2342 g_object_ref (widget);
2348 g_object_unref (widget);
2353 gtk_error (gchar *str)
2359 gtk_warning (gchar *str)
2365 gtk_message (gchar *str)
2371 gtk_print (gchar *str)
2373 static GtkWidget *window = NULL;
2374 static GtkWidget *text;
2375 static int level = 0;
2379 GtkWidget *hscrollbar;
2380 GtkWidget *vscrollbar;
2381 GtkWidget *separator;
2386 fputs (str, stdout);
2393 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2395 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2396 (GtkSignalFunc) gtk_widget_destroyed,
2399 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2401 box1 = gtk_vbox_new (FALSE, 0);
2402 gtk_container_add (GTK_CONTAINER (window), box1);
2403 gtk_widget_show (box1);
2406 box2 = gtk_vbox_new (FALSE, 10);
2407 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2408 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2409 gtk_widget_show (box2);
2412 table = gtk_table_new (2, 2, FALSE);
2413 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2414 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2415 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2416 gtk_widget_show (table);
2418 text = gtk_text_new (NULL, NULL);
2419 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2420 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2421 gtk_widget_show (text);
2422 gtk_widget_realize (text);
2424 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2425 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2426 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2427 gtk_widget_show (hscrollbar);
2429 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2430 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2431 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2432 gtk_widget_show (vscrollbar);
2434 separator = gtk_hseparator_new ();
2435 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2436 gtk_widget_show (separator);
2439 box2 = gtk_vbox_new (FALSE, 10);
2440 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2441 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2442 gtk_widget_show (box2);
2445 button = gtk_button_new_with_label ("close");
2446 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2447 (GtkSignalFunc) gtk_widget_hide,
2448 GTK_OBJECT (window));
2449 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2450 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2451 gtk_widget_grab_default (button);
2452 gtk_widget_show (button);
2456 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2459 if (!GTK_WIDGET_VISIBLE (window))
2460 gtk_widget_show (window);
2465 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2466 GValue *return_accu,
2467 const GValue *handler_return,
2470 gboolean continue_emission;
2471 gboolean signal_handled;
2473 signal_handled = g_value_get_boolean (handler_return);
2474 g_value_set_boolean (return_accu, signal_handled);
2475 continue_emission = !signal_handled;
2477 return continue_emission;