3 ** Description: Creating 'real' UIs from lisp.
5 ** Created by: William M. Perry <wmperry@gnu.org>
6 ** Copyright (c) 2000 William M. Perry <wmperry@gnu.org>
13 #include "console-gtk.h"
16 #include "glyphs-gtk.h"
17 #include "objects-gtk.h"
26 /* XEmacs specific GTK types */
29 Lisp_Object Qemacs_ffip;
30 Lisp_Object Qemacs_gtk_objectp;
31 Lisp_Object Qemacs_gtk_boxedp;
33 Lisp_Object Venumeration_info;
35 static GHashTable *dll_cache;
37 Lisp_Object gtk_type_to_lisp (GtkArg *arg);
38 int lisp_to_gtk_type (Lisp_Object obj, GtkArg *arg);
39 void describe_gtk_arg (GtkArg *arg);
40 guint symbol_to_enum (Lisp_Object obj, GtkType t);
41 static guint lisp_to_flag (Lisp_Object obj, GtkType t);
42 static Lisp_Object flags_to_list (guint value, GtkType t);
43 static Lisp_Object enum_to_symbol (guint value, GtkType t);
45 #define NIL_OR_VOID_P(x) (NILP (x) || EQ (x, Qvoid))
48 initialize_dll_cache (void)
52 dll_cache = g_hash_table_new (g_str_hash, g_str_equal);
54 g_hash_table_insert (dll_cache, "---XEmacs Internal Handle---", dll_open (NULL));
58 DEFUN ("dll-load", Fdll_load, 1, 1, 0, /*
59 Load a shared library DLL into XEmacs. No initialization routines are required.
60 This is for loading dependency DLLs into XEmacs.
68 initialize_dll_cache ();
70 /* If the dll name has a directory component in it, then we should
72 if (!NILP (Fstring_match (build_string ("/"), dll, Qnil, Qnil)))
73 dll = Fexpand_file_name (dll, Qnil);
75 /* Check if we have already opened it first */
76 h = g_hash_table_lookup (dll_cache, XSTRING_DATA (dll));
80 h = dll_open ((char *) XSTRING_DATA (dll));
84 g_hash_table_insert (dll_cache, g_strdup (XSTRING_DATA (dll)), h);
88 signal_simple_error ("dll_open error", build_string (dll_error (NULL)));
91 return (h ? Qt : Qnil);
95 /* Gtk object importing */
96 EXFUN (Fgtk_import_type, 1);
98 static struct hash_table *internal_type_hash;
101 type_hash_equal(const void *arg1, const void *arg2)
103 return ((GtkType) arg1 == (GtkType) arg2);
107 type_hash_hash(const void *arg)
109 return ((unsigned long) arg);
113 type_already_imported_p (GtkType t)
117 /* These are cases that we don't need to import */
118 switch (GTK_FUNDAMENTAL_TYPE (t))
128 case GTK_TYPE_DOUBLE:
129 case GTK_TYPE_STRING:
131 case GTK_TYPE_POINTER:
132 case GTK_TYPE_SIGNAL:
134 case GTK_TYPE_CALLBACK:
135 case GTK_TYPE_C_CALLBACK:
136 case GTK_TYPE_FOREIGN:
140 if (!internal_type_hash)
142 internal_type_hash = make_general_hash_table (163, type_hash_hash, type_hash_equal);
146 if (gethash ((void *)t, internal_type_hash, (const void **)&retval))
154 mark_type_as_imported (GtkType t)
156 if (type_already_imported_p (t))
159 puthash ((void *) t, (void *) 1, internal_type_hash);
162 static void import_gtk_type (GtkType t);
165 import_gtk_object_internal (GtkType the_type)
167 GtkType original_type = the_type;
177 GtkObjectClass *klass;
178 GtkSignalQuery *query;
183 /* Register the type before we do anything else with it... */
186 if (!type_already_imported_p (the_type))
188 import_gtk_type (the_type);
193 /* We need to mark the object type as imported here or we
194 run the risk of SERIOUS recursion when we do automatic
195 argument type importing. mark_type_as_imported() is
196 smart enough to be a noop if we attempt to register
199 mark_type_as_imported (the_type);
202 args = gtk_object_query_args(the_type,&flags,&n_args);
204 /* First get the arguments the object can accept */
205 for (i = 0; i < n_args; i++)
207 if ((args[i].type != original_type) && !type_already_imported_p (args[i].type))
209 import_gtk_type (args[i].type);
217 /* Now lets publish the signals */
218 klass = (GtkObjectClass *) gtk_type_class (the_type);
219 signals = klass->signals;
220 n_signals = klass->nsignals;
222 for (i = 0; i < n_signals; i++)
224 query = gtk_signal_query (signals[i]);
225 /* What do we want to do here? */
230 the_type = gtk_type_parent(the_type);
231 } while (the_type != GTK_TYPE_INVALID);
235 import_gtk_enumeration_internal (GtkType the_type)
237 GtkEnumValue *vals = gtk_type_enum_get_values (the_type);
238 Lisp_Object assoc = Qnil;
240 if (NILP (Venumeration_info))
242 Venumeration_info = call2 (intern ("make-hashtable"), make_int (100), Qequal);
245 while (vals && vals->value_name)
247 assoc = Fcons (Fcons (intern (vals->value_nick), make_int (vals->value)), assoc);
248 assoc = Fcons (Fcons (intern (vals->value_name), make_int (vals->value)), assoc);
252 assoc = Fnreverse (assoc);
254 Fputhash (make_int (the_type), assoc, Venumeration_info);
258 import_gtk_type (GtkType t)
260 if (type_already_imported_p (t))
265 switch (GTK_FUNDAMENTAL_TYPE (t))
269 import_gtk_enumeration_internal (t);
271 case GTK_TYPE_OBJECT:
272 import_gtk_object_internal (t);
278 mark_type_as_imported (t);
282 /* Foreign function calls */
283 static emacs_ffi_data *
284 allocate_ffi_data (void)
286 emacs_ffi_data *data = alloc_lcrecord_type (emacs_ffi_data, &lrecord_emacs_ffi);
288 data->return_type = GTK_TYPE_NONE;
290 data->function_name = Qnil;
291 data->function_ptr = 0;
298 mark_ffi_data (Lisp_Object obj)
300 emacs_ffi_data *data = (emacs_ffi_data *) XFFI (obj);
302 mark_object (data->function_name);
307 ffi_object_printer (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
312 error ("printing unreadable object #<ffi %p", XFFI (obj)->function_ptr);
314 write_c_string ("#<ffi ", printcharfun);
315 print_internal (XFFI (obj)->function_name, printcharfun, 1);
316 if (XFFI (obj)->n_args)
318 sprintf (buf, " %d arguments", XFFI (obj)->n_args);
319 write_c_string (buf, printcharfun);
321 sprintf (buf, " %p>", (void *)XFFI (obj)->function_ptr);
322 write_c_string (buf, printcharfun);
325 DEFINE_LRECORD_IMPLEMENTATION ("ffi", emacs_ffi,
326 mark_ffi_data, ffi_object_printer,
327 0, 0, 0, NULL, emacs_ffi_data);
329 typedef GtkObject * (*__OBJECT_fn) ();
330 typedef gint (*__INT_fn) ();
331 typedef void (*__NONE_fn) ();
332 typedef gchar * (*__STRING_fn) ();
333 typedef gboolean (*__BOOL_fn) ();
334 typedef gfloat (*__FLOAT_fn) ();
335 typedef void * (*__POINTER_fn) ();
336 typedef GList * (*__LIST_fn) ();
338 /* An auto-generated file of marshalling functions. */
339 #include "emacs-marshals.c"
341 #define CONVERT_SINGLE_TYPE(var,nam,tp) case GTK_TYPE_##nam: GTK_VALUE_##nam (var) = * (tp *) v; break;
342 #define CONVERT_RETVAL(a,freep) \
344 void *v = GTK_VALUE_POINTER(a); \
345 switch (GTK_FUNDAMENTAL_TYPE (a.type)) \
347 CONVERT_SINGLE_TYPE(a,CHAR,gchar); \
348 CONVERT_SINGLE_TYPE(a,UCHAR,guchar); \
349 CONVERT_SINGLE_TYPE(a,BOOL,gboolean); \
350 CONVERT_SINGLE_TYPE(a,INT,gint); \
351 CONVERT_SINGLE_TYPE(a,UINT,guint); \
352 CONVERT_SINGLE_TYPE(a,LONG,glong); \
353 CONVERT_SINGLE_TYPE(a,ULONG,gulong); \
354 CONVERT_SINGLE_TYPE(a,FLOAT,gfloat); \
355 CONVERT_SINGLE_TYPE(a,DOUBLE,gdouble); \
356 CONVERT_SINGLE_TYPE(a,STRING,gchar *); \
357 CONVERT_SINGLE_TYPE(a,ENUM,gint); \
358 CONVERT_SINGLE_TYPE(a,FLAGS,guint); \
359 CONVERT_SINGLE_TYPE(a,BOXED,void *); \
360 CONVERT_SINGLE_TYPE(a,POINTER,void *); \
361 CONVERT_SINGLE_TYPE(a,OBJECT,GtkObject *); \
363 GTK_VALUE_POINTER (a) = * (void **) v; \
366 if (freep) xfree(v); \
369 gpointer __allocate_object_storage (GtkType t)
374 switch (GTK_FUNDAMENTAL_TYPE (t))
378 s = (sizeof (gchar));
381 s = (sizeof (guchar));
384 s = (sizeof (gboolean));
390 s = (sizeof (guint));
393 s = (sizeof (glong));
396 s = (sizeof (gulong));
399 s = (sizeof (gfloat));
401 case GTK_TYPE_DOUBLE:
402 s = (sizeof (gdouble));
404 case GTK_TYPE_STRING:
405 s = (sizeof (gchar *));
409 s = (sizeof (guint));
412 case GTK_TYPE_POINTER:
413 s = (sizeof (void *));
416 /* base type of the object system */
417 case GTK_TYPE_OBJECT:
418 s = (sizeof (GtkObject *));
422 if (GTK_FUNDAMENTAL_TYPE (t) == GTK_TYPE_LISTOF)
424 s = (sizeof (void *));
433 memset (rval, '\0', s);
439 Lisp_Object type_to_marshaller_type (GtkType t)
441 switch (GTK_FUNDAMENTAL_TYPE (t))
444 return (build_string ("NONE"));
448 return (build_string ("CHAR"));
450 return (build_string ("BOOL"));
455 return (build_string ("INT"));
458 return (build_string ("LONG"));
460 case GTK_TYPE_DOUBLE:
461 return (build_string ("FLOAT"));
462 case GTK_TYPE_STRING:
463 return (build_string ("STRING"));
465 case GTK_TYPE_POINTER:
466 return (build_string ("POINTER"));
467 case GTK_TYPE_OBJECT:
468 return (build_string ("OBJECT"));
469 case GTK_TYPE_CALLBACK:
470 return (build_string ("CALLBACK"));
472 /* I can't put this in the main switch statement because it is a
473 new fundamental type that is not fixed at compile time.
476 if (GTK_FUNDAMENTAL_TYPE (t) == GTK_TYPE_ARRAY)
477 return (build_string ("ARRAY"));
479 if (GTK_FUNDAMENTAL_TYPE (t) == GTK_TYPE_LISTOF)
480 return (build_string ("LIST"));
485 struct __dll_mapper_closure {
486 void * (*func) (dll_handle, const char *);
487 const char *obj_name;
491 static void __dll_mapper (gpointer key, gpointer value, gpointer user_data)
493 struct __dll_mapper_closure *closure = (struct __dll_mapper_closure *) user_data;
495 if (*(closure->storage) == NULL)
497 /* Need to see if it is in this one */
498 *(closure->storage) = closure->func ((dll_handle) value, closure->obj_name);
502 DEFUN ("gtk-import-variable-internal", Fgtk_import_variable_internal, 2, 2, 0, /*
503 Import a variable into the XEmacs namespace.
510 if (SYMBOLP (type)) type = Fsymbol_name (type);
515 initialize_dll_cache ();
516 xemacs_init_gtk_classes ();
518 arg.type = gtk_type_from_name ((char *) XSTRING_DATA (type));
520 if (arg.type == GTK_TYPE_INVALID)
522 signal_simple_error ("Unknown type", type);
525 /* Need to look thru the already-loaded dlls */
527 struct __dll_mapper_closure closure;
529 closure.func = dll_variable;
530 closure.obj_name = XSTRING_DATA (name);
531 closure.storage = &var;
533 g_hash_table_foreach (dll_cache, __dll_mapper, &closure);
538 signal_simple_error ("Could not locate variable", name);
541 GTK_VALUE_POINTER(arg) = var;
542 CONVERT_RETVAL (arg, 0);
543 return (gtk_type_to_lisp (&arg));
546 DEFUN ("gtk-import-function-internal", Fgtk_import_function_internal, 2, 3, 0, /*
547 Import a function into the XEmacs namespace.
549 (rettype, name, args))
551 Lisp_Object rval = Qnil;
552 Lisp_Object marshaller = Qnil;
553 emacs_ffi_data *data = NULL;
558 ffi_marshalling_function marshaller_func = NULL;
559 ffi_actual_function name_func = NULL;
561 CHECK_SYMBOL (rettype);
565 initialize_dll_cache ();
566 xemacs_init_gtk_classes ();
568 /* Need to look thru the already-loaded dlls */
570 struct __dll_mapper_closure closure;
572 closure.func = dll_function;
573 closure.obj_name = XSTRING_DATA (name);
574 closure.storage = (void **) &name_func;
576 g_hash_table_foreach (dll_cache, __dll_mapper, &closure);
581 signal_simple_error ("Could not locate function", name);
584 data = allocate_ffi_data ();
593 Lisp_Object tail = Qnil;
594 Lisp_Object value = args;
595 Lisp_Object type = Qnil;
597 EXTERNAL_LIST_LOOP (tail, value)
600 Lisp_Object marshaller_type = Qnil;
602 CHECK_SYMBOL (XCAR (tail));
604 type = Fsymbol_name (XCAR (tail));
606 the_type = gtk_type_from_name ((char *) XSTRING_DATA (type));
608 if (the_type == GTK_TYPE_INVALID)
610 signal_simple_error ("Unknown argument type", type);
613 /* All things must be reduced to their basest form... */
614 import_gtk_type (the_type);
615 data->args[n_args] = the_type; /* GTK_FUNDAMENTAL_TYPE (the_type); */
617 /* Now lets build up another chunk of our marshaller function name */
618 marshaller_type = type_to_marshaller_type (data->args[n_args]);
620 if (NILP (marshaller_type))
622 signal_simple_error ("Do not know how to marshal", type);
624 marshaller = concat3 (marshaller, build_string ("_"), marshaller_type);
630 marshaller = concat3 (marshaller, build_string ("_"), type_to_marshaller_type (GTK_TYPE_NONE));
633 rettype = Fsymbol_name (rettype);
634 data->return_type = gtk_type_from_name ((char *) XSTRING_DATA (rettype));
636 if (data->return_type == GTK_TYPE_INVALID)
638 signal_simple_error ("Unknown return type", rettype);
641 import_gtk_type (data->return_type);
643 marshaller = concat3 (type_to_marshaller_type (data->return_type), build_string ("_"), marshaller);
644 marshaller = concat2 (build_string ("emacs_gtk_marshal_"), marshaller);
646 marshaller_func = (ffi_marshalling_function) find_marshaller ((char *) XSTRING_DATA (marshaller));
648 if (!marshaller_func)
650 signal_simple_error ("Could not locate marshaller function", marshaller);
653 data->n_args = n_args;
654 data->function_name = name;
655 data->function_ptr = name_func;
656 data->marshal = marshaller_func;
658 XSETFFI (rval, data);
662 DEFUN ("gtk-call-function", Fgtk_call_function, 1, 2, 0, /*
663 Call an external function.
667 GtkArg the_args[MAX_GTK_ARGS];
669 Lisp_Object retval = Qnil;
674 n_args = XINT (Flength (args));
676 #ifdef XEMACS_IS_SMARTER_THAN_THE_PROGRAMMER
677 /* #### I think this is too dangerous to enable by default.
678 ** #### Genuine program bugs would probably be allowed to
679 ** #### slip by, and not be very easy to find.
680 ** #### Bill Perry July 9, 2000
682 if (n_args != XFFI(func)->n_args)
684 Lisp_Object for_append[3];
686 /* Signal an error if they pass in too many arguments */
687 if (n_args > XFFI(func)->n_args)
689 return Fsignal (Qwrong_number_of_arguments,
690 list2 (func, make_int (n_args)));
693 /* If they did not provide enough arguments, be nice and assume
694 ** they wanted `nil' in there.
696 for_append[0] = args;
697 for_append[1] = Fmake_list (make_int (XFFI(func)->n_args - n_args), Qnil);
699 args = Fappend (2, for_append);
702 if (n_args != XFFI(func)->n_args)
704 /* Signal an error if they do not pass in the correct # of arguments */
705 return Fsignal (Qwrong_number_of_arguments,
706 list2 (func, make_int (n_args)));
712 Lisp_Object tail = Qnil;
713 Lisp_Object value = args;
718 /* First we convert all of the arguments from Lisp to GtkArgs */
719 EXTERNAL_LIST_LOOP (tail, value)
721 the_args[n_args].type = XFFI (func)->args[n_args];
723 if (lisp_to_gtk_type (XCAR (tail), &the_args[n_args]))
725 /* There was some sort of an error */
726 signal_simple_error ("Error converting arguments", args);
732 /* Now we need to tack on space for a return value, if they have
734 if (XFFI (func)->return_type != GTK_TYPE_NONE)
736 the_args[n_args].type = XFFI (func)->return_type;
737 GTK_VALUE_POINTER (the_args[n_args]) = __allocate_object_storage (the_args[n_args].type);
741 XFFI (func)->marshal ((ffi_actual_function) (XFFI (func)->function_ptr), the_args);
743 if (XFFI (func)->return_type != GTK_TYPE_NONE)
745 CONVERT_RETVAL (the_args[n_args - 1], 1);
746 retval = gtk_type_to_lisp (&the_args[n_args - 1]);
749 /* Need to free any array or list pointers */
752 for (i = 0; i < n_args; i++)
754 if (GTK_FUNDAMENTAL_TYPE (the_args[i].type) == GTK_TYPE_ARRAY)
756 g_free (GTK_VALUE_POINTER (the_args[i]));
758 else if (GTK_FUNDAMENTAL_TYPE (the_args[i].type) == GTK_TYPE_LISTOF)
760 /* g_list_free (GTK_VALUE_POINTER (the_args[i])); */
770 /* GtkObject wrapping for Lisp */
772 emacs_gtk_object_printer (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
777 error ("printing unreadable object #<GtkObject %p>", XGTK_OBJECT (obj)->object);
779 write_c_string ("#<GtkObject (", printcharfun);
780 if (XGTK_OBJECT (obj)->alive_p)
781 write_c_string (gtk_type_name (GTK_OBJECT_TYPE (XGTK_OBJECT (obj)->object)), printcharfun);
783 write_c_string ("dead", printcharfun);
784 sprintf (buf, ") %p>", (void *) XGTK_OBJECT (obj)->object);
785 write_c_string (buf, printcharfun);
789 object_getprop (Lisp_Object obj, Lisp_Object prop)
791 Lisp_Object rval = Qnil;
792 Lisp_Object prop_name = Qnil;
793 GtkArgInfo *info = NULL;
797 CHECK_SYMBOL (prop); /* Shouldn't need to ever do this, but I'm paranoid */
799 prop_name = Fsymbol_name (prop);
801 args[0].name = (char *) XSTRING_DATA (prop_name);
803 err = gtk_object_arg_get_info (GTK_OBJECT_TYPE (XGTK_OBJECT (obj)->object),
809 /* Not a magic symbol, fall back to just looking in our real plist */
812 return (Fplist_get (XGTK_OBJECT (obj)->plist, prop, Qunbound));
815 if (!(info->arg_flags & GTK_ARG_READABLE))
817 signal_simple_error ("Attempt to get write-only property", prop);
820 gtk_object_getv (XGTK_OBJECT (obj)->object, 1, args);
822 if (args[0].type == GTK_TYPE_INVALID)
824 /* If we can't get the attribute, then let the code in Fget know
825 so it can use the default value supplied by the caller */
829 rval = gtk_type_to_lisp (&args[0]);
831 /* Free up any memory. According to the documentation and Havoc's
832 book, if the fundamental type of the returned value is
833 GTK_TYPE_STRING, GTK_TYPE_BOXED, or GTK_TYPE_ARGS, you are
834 responsible for freeing it. */
835 switch (GTK_FUNDAMENTAL_TYPE (args[0].type))
837 case GTK_TYPE_STRING:
838 g_free (GTK_VALUE_STRING (args[0]));
841 g_free (GTK_VALUE_BOXED (args[0]));
844 g_free (GTK_VALUE_ARGS (args[0]).args);
853 object_putprop (Lisp_Object obj, Lisp_Object prop, Lisp_Object value)
855 GtkArgInfo *info = NULL;
856 Lisp_Object prop_name = Qnil;
860 prop_name = Fsymbol_name (prop);
862 args[0].name = (char *) XSTRING_DATA (prop_name);
864 err = gtk_object_arg_get_info (GTK_OBJECT_TYPE (XGTK_OBJECT (obj)->object),
870 /* Not a magic symbol, fall back to just storing in our real plist */
873 XGTK_OBJECT (obj)->plist = Fplist_put (XGTK_OBJECT (obj)->plist, prop, value);
877 args[0].type = info->type;
879 if (lisp_to_gtk_type (value, &args[0]))
881 signal_simple_error ("Error converting to GtkType", value);
884 if (!(info->arg_flags & GTK_ARG_WRITABLE))
886 signal_simple_error ("Attemp to set read-only argument", prop);
889 gtk_object_setv (XGTK_OBJECT (obj)->object, 1, args);
895 mark_gtk_object_data (Lisp_Object obj)
897 return (XGTK_OBJECT (obj)->plist);
901 emacs_gtk_object_finalizer (void *header, int for_disksave)
903 emacs_gtk_object_data *data = (emacs_gtk_object_data *) header;
908 XSETGTK_OBJECT (obj, data);
911 ("Can't dump an emacs containing GtkObject objects", obj);
916 gtk_object_unref (data->object);
920 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS ("GtkObject", emacs_gtk_object,
921 mark_gtk_object_data, /* marker function */
922 emacs_gtk_object_printer, /* print function */
923 emacs_gtk_object_finalizer, /* finalizer */
927 object_getprop, /* get prop */
928 object_putprop, /* put prop */
931 emacs_gtk_object_data);
933 static emacs_gtk_object_data *
934 allocate_emacs_gtk_object_data (void)
936 emacs_gtk_object_data *data = alloc_lcrecord_type (emacs_gtk_object_data,
937 &lrecord_emacs_gtk_object);
940 data->alive_p = FALSE;
946 /* We need to keep track of when the object is destroyed so that we
947 can mark it as dead, otherwise even our print routine (which calls
948 GTK_OBJECT_TYPE) will crap out and die. This is also used in the
949 lisp_to_gtk_type() routine to defend against passing dead objects
952 __notice_object_destruction (GtkObject *obj, gpointer user_data)
954 ungcpro_popup_callbacks ((GUI_ID) user_data);
957 Lisp_Object build_gtk_object (GtkObject *obj)
959 Lisp_Object retval = Qnil;
960 emacs_gtk_object_data *data = NULL;
963 id = (GUI_ID) gtk_object_get_data (obj, "xemacs::gui_id");
967 retval = get_gcpro_popup_callbacks (id);
972 data = allocate_emacs_gtk_object_data ();
975 data->alive_p = TRUE;
976 XSETGTK_OBJECT (retval, data);
979 gtk_object_set_data (obj, "xemacs::gui_id", (gpointer) id);
980 gcpro_popup_callbacks (id, retval);
981 gtk_object_ref (obj);
982 gtk_signal_connect (obj, "destroy", GTK_SIGNAL_FUNC (__notice_object_destruction), (gpointer)id);
989 __internal_callback_destroy (gpointer data)
991 Lisp_Object lisp_data;
993 VOID_TO_LISP (lisp_data, data);
995 ungcpro_popup_callbacks (XINT (XCAR (lisp_data)));
999 __internal_callback_marshal (GtkObject *obj, gpointer data, guint n_args, GtkArg *args)
1001 Lisp_Object arg_list = Qnil;
1002 Lisp_Object callback_fn = Qnil;
1003 Lisp_Object callback_data = Qnil;
1004 Lisp_Object newargs[3];
1005 Lisp_Object rval = Qnil;
1006 struct gcpro gcpro1;
1009 VOID_TO_LISP (callback_fn, data);
1011 /* Nuke the GUI_ID off the front */
1012 callback_fn = XCDR (callback_fn);
1014 callback_data = XCAR (callback_fn);
1015 callback_fn = XCDR (callback_fn);
1017 /* The callback data goes at the very end of the argument list */
1018 arg_list = Fcons (callback_data, Qnil);
1020 /* Build up the argument list, lisp style */
1021 for (i = n_args - 1; i >= 0; i--)
1023 arg_list = Fcons (gtk_type_to_lisp (&args[i]), arg_list);
1026 /* We always pass the widget as the first parameter at the very least */
1027 arg_list = Fcons (build_gtk_object (obj), arg_list);
1029 GCPRO1 ((arg_list));
1031 newargs[0] = callback_fn;
1032 newargs[1] = arg_list;
1034 rval = Fapply (2, newargs);
1035 signal_fake_event ();
1037 if (args[n_args].type != GTK_TYPE_NONE)
1038 lisp_to_gtk_type (rval, &args[n_args]);
1043 DEFUN ("gtk-signal-connect", Fgtk_signal_connect, 3, 6, 0, /*
1045 (obj, name, func, cb_data, object_signal, after_p))
1048 int c_object_signal;
1051 CHECK_GTK_OBJECT (obj);
1054 name = Fsymbol_name (name);
1056 CHECK_STRING (name);
1058 if (NILP (object_signal))
1059 c_object_signal = 0;
1061 c_object_signal = 1;
1069 func = Fcons (cb_data, func);
1070 func = Fcons (make_int (id), func);
1072 gcpro_popup_callbacks (id, func);
1074 gtk_signal_connect_full (XGTK_OBJECT (obj)->object, (char *) XSTRING_DATA (name),
1075 NULL, __internal_callback_marshal, LISP_TO_VOID (func),
1076 __internal_callback_destroy, c_object_signal, c_after);
1081 /* GTK_TYPE_BOXED wrapper for Emacs lisp */
1083 emacs_gtk_boxed_printer (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
1088 error ("printing unreadable object #<GtkBoxed %p>", XGTK_BOXED (obj)->object);
1090 write_c_string ("#<GtkBoxed (", printcharfun);
1091 write_c_string (gtk_type_name (XGTK_BOXED (obj)->object_type), printcharfun);
1092 sprintf (buf, ") %p>", (void *) XGTK_BOXED (obj)->object);
1093 write_c_string (buf, printcharfun);
1097 emacs_gtk_boxed_equality (Lisp_Object o1, Lisp_Object o2, int depth)
1099 emacs_gtk_boxed_data *data1 = XGTK_BOXED(o1);
1100 emacs_gtk_boxed_data *data2 = XGTK_BOXED(o2);
1102 return ((data1->object == data2->object) &&
1103 (data1->object_type == data2->object_type));
1106 static unsigned long
1107 emacs_gtk_boxed_hash (Lisp_Object obj, int depth)
1109 emacs_gtk_boxed_data *data = XGTK_BOXED(obj);
1110 return (HASH2 ((unsigned long)data->object, data->object_type));
1113 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS ("GtkBoxed", emacs_gtk_boxed,
1114 0, /* marker function */
1115 emacs_gtk_boxed_printer, /* print function */
1117 emacs_gtk_boxed_equality, /* equality */
1118 emacs_gtk_boxed_hash, /* hash */
1124 emacs_gtk_boxed_data);
1126 /* Currently defined GTK_TYPE_BOXED structures are:
1140 static emacs_gtk_boxed_data *
1141 allocate_emacs_gtk_boxed_data (void)
1143 emacs_gtk_boxed_data *data = alloc_lcrecord_type (emacs_gtk_boxed_data,
1144 &lrecord_emacs_gtk_boxed);
1146 data->object = NULL;
1147 data->object_type = GTK_TYPE_INVALID;
1152 Lisp_Object build_gtk_boxed (void *obj, GtkType t)
1154 Lisp_Object retval = Qnil;
1155 emacs_gtk_boxed_data *data = NULL;
1157 if (GTK_FUNDAMENTAL_TYPE (t) != GTK_TYPE_BOXED)
1160 data = allocate_emacs_gtk_boxed_data ();
1162 data->object_type = t;
1164 XSETGTK_BOXED (retval, data);
1170 /* The automatically generated structure access routines */
1171 #include "emacs-widget-accessors.c"
1173 /* The hand generated funky functions that we can't just import using the FFI */
1174 #include "ui-byhand.c"
1176 /* The glade support */
1180 /* Type manipulation */
1181 DEFUN ("gtk-fundamental-type", Fgtk_fundamental_type, 1, 1, 0, /*
1182 Load a shared library DLL into XEmacs. No initialization routines are required.
1183 This is for loading dependency DLLs into XEmacs.
1190 type = Fsymbol_name (type);
1192 CHECK_STRING (type);
1194 t = gtk_type_from_name ((char *) XSTRING_DATA (type));
1196 if (t == GTK_TYPE_INVALID)
1198 signal_simple_error ("Not a GTK type", type);
1200 return (make_int (GTK_FUNDAMENTAL_TYPE (t)));
1203 DEFUN ("gtk-object-type", Fgtk_object_type, 1, 1, 0, /*
1204 Return the GtkType of OBJECT.
1208 CHECK_GTK_OBJECT (object);
1209 return (make_int (GTK_OBJECT_TYPE (XGTK_OBJECT (object)->object)));
1212 DEFUN ("gtk-describe-type", Fgtk_describe_type, 1, 1, 0, /*
1213 Returns a cons of two lists describing the Gtk object TYPE.
1214 The car is a list of all the signals that it will emit.
1215 The cdr is a list of all the magic properties it has.
1219 Lisp_Object rval, signals, props;
1222 props = signals = rval = Qnil;
1226 type = Fsymbol_name (type);
1231 t = gtk_type_from_name (XSTRING_DATA (type));
1232 if (t == GTK_TYPE_INVALID)
1234 signal_simple_error ("Not a GTK type", type);
1243 if (GTK_FUNDAMENTAL_TYPE (t) != GTK_TYPE_OBJECT)
1245 signal_simple_error ("Not a GtkObject", type);
1248 /* Need to do stupid shit like this to get the args
1249 ** registered... damn GTK and its lazy loading
1253 GtkObject *obj = gtk_object_newv (t, 0, args);
1255 gtk_object_destroy(obj);
1262 /* Do the magic arguments first */
1268 args = gtk_object_query_args(t,&flags,&n_args);
1270 for (i = 0; i < n_args; i++)
1272 props = Fcons (Fcons (intern (gtk_type_name(args[i].type)),
1273 intern (args[i].name)), props);
1280 /* Now the signals */
1282 GtkObjectClass *klass;
1283 GtkSignalQuery *query;
1284 guint32 *gtk_signals;
1287 klass = (GtkObjectClass *) gtk_type_class (t);
1288 gtk_signals = klass->signals;
1289 n_signals = klass->nsignals;
1291 for (i = 0; i < n_signals; i++)
1293 Lisp_Object params = Qnil;
1295 query = gtk_signal_query (gtk_signals[i]);
1303 for (j = query->nparams - 1; j >= 0; j--)
1305 params = Fcons (intern (gtk_type_name (query->params[j])), params);
1309 signals = Fcons (Fcons (intern (gtk_type_name (query->return_val)),
1310 Fcons (intern (query->signal_name),
1318 t = gtk_type_parent(t);
1319 } while (t != GTK_TYPE_INVALID);
1321 rval = Fcons (signals, props);
1328 syms_of_ui_gtk (void)
1330 INIT_LRECORD_IMPLEMENTATION (emacs_ffi);
1331 INIT_LRECORD_IMPLEMENTATION (emacs_gtk_object);
1332 INIT_LRECORD_IMPLEMENTATION (emacs_gtk_boxed);
1333 defsymbol (&Qemacs_ffip, "emacs-ffi-p");
1334 defsymbol (&Qemacs_gtk_objectp, "emacs-gtk-object-p");
1335 defsymbol (&Qemacs_gtk_boxedp, "emacs-gtk-boxed-p");
1336 defsymbol (&Qvoid, "void");
1337 DEFSUBR (Fdll_load);
1338 DEFSUBR (Fgtk_import_function_internal);
1339 DEFSUBR (Fgtk_import_variable_internal);
1340 DEFSUBR (Fgtk_signal_connect);
1341 DEFSUBR (Fgtk_call_function);
1342 DEFSUBR (Fgtk_fundamental_type);
1343 DEFSUBR (Fgtk_object_type);
1344 DEFSUBR (Fgtk_describe_type);
1345 syms_of_widget_accessors ();
1346 syms_of_ui_byhand ();
1351 vars_of_ui_gtk (void)
1353 Fprovide (intern ("gtk-ui"));
1354 DEFVAR_LISP ("gtk-enumeration-info", &Venumeration_info /*
1355 A hashtable holding type information about GTK enumerations and flags.
1356 Do NOT modify unless you really understand ui-gtk.c.
1359 Venumeration_info = Qnil;
1364 /* Various utility functions */
1365 void describe_gtk_arg (GtkArg *arg)
1369 switch (GTK_FUNDAMENTAL_TYPE (a.type))
1373 stderr_out ("char: %c\n", GTK_VALUE_CHAR (a));
1375 case GTK_TYPE_UCHAR:
1376 stderr_out ("uchar: %c\n", GTK_VALUE_CHAR (a));
1379 stderr_out ("uchar: %s\n", GTK_VALUE_BOOL (a) ? "true" : "false");
1382 stderr_out ("int: %d\n", GTK_VALUE_INT (a));
1385 stderr_out ("uint: %du\n", GTK_VALUE_UINT (a));
1388 stderr_out ("long: %ld\n", GTK_VALUE_LONG (a));
1390 case GTK_TYPE_ULONG:
1391 stderr_out ("ulong: %lu\n", GTK_VALUE_ULONG (a));
1393 case GTK_TYPE_FLOAT:
1394 stderr_out ("float: %g\n", GTK_VALUE_FLOAT (a));
1396 case GTK_TYPE_DOUBLE:
1397 stderr_out ("double: %f\n", GTK_VALUE_DOUBLE (a));
1399 case GTK_TYPE_STRING:
1400 stderr_out ("string: %s\n", GTK_VALUE_STRING (a));
1403 case GTK_TYPE_FLAGS:
1404 stderr_out ("%s: ", (a.type == GTK_TYPE_ENUM) ? "enum" : "flag");
1406 GtkEnumValue *vals = gtk_type_enum_get_values (a.type);
1408 while (vals && vals->value_name && (vals->value != GTK_VALUE_ENUM(a))) vals++;
1410 stderr_out ("%s\n", vals ? vals->value_name : "!!! UNKNOWN ENUM VALUE !!!");
1413 case GTK_TYPE_BOXED:
1414 stderr_out ("boxed: %p\n", GTK_VALUE_BOXED (a));
1416 case GTK_TYPE_POINTER:
1417 stderr_out ("pointer: %p\n", GTK_VALUE_BOXED (a));
1420 /* structured types */
1421 case GTK_TYPE_SIGNAL:
1422 case GTK_TYPE_ARGS: /* This we can do as a list of values */
1424 case GTK_TYPE_CALLBACK:
1425 stderr_out ("callback fn: ...\n");
1427 case GTK_TYPE_C_CALLBACK:
1428 case GTK_TYPE_FOREIGN:
1431 /* base type of the object system */
1432 case GTK_TYPE_OBJECT:
1433 if (GTK_VALUE_OBJECT (a))
1434 stderr_out ("object: %s\n", gtk_type_name (GTK_OBJECT_TYPE (GTK_VALUE_OBJECT (a))));
1436 stderr_out ("object: NULL\n");
1444 Lisp_Object gtk_type_to_lisp (GtkArg *arg)
1446 switch (GTK_FUNDAMENTAL_TYPE (arg->type))
1451 return (make_char (GTK_VALUE_CHAR (*arg)));
1452 case GTK_TYPE_UCHAR:
1453 return (make_char (GTK_VALUE_UCHAR (*arg)));
1455 return (GTK_VALUE_BOOL (*arg) ? Qt : Qnil);
1457 return (make_int (GTK_VALUE_INT (*arg)));
1459 return (make_int (GTK_VALUE_INT (*arg)));
1460 case GTK_TYPE_LONG: /* I think these are wrong! */
1461 return (make_int (GTK_VALUE_INT (*arg)));
1462 case GTK_TYPE_ULONG: /* I think these are wrong! */
1463 return (make_int (GTK_VALUE_INT (*arg)));
1464 case GTK_TYPE_FLOAT:
1465 return (make_float (GTK_VALUE_FLOAT (*arg)));
1466 case GTK_TYPE_DOUBLE:
1467 return (make_float (GTK_VALUE_DOUBLE (*arg)));
1468 case GTK_TYPE_STRING:
1469 return (build_string (GTK_VALUE_STRING (*arg)));
1470 case GTK_TYPE_FLAGS:
1471 return (flags_to_list (GTK_VALUE_FLAGS (*arg), arg->type));
1473 return (enum_to_symbol (GTK_VALUE_ENUM (*arg), arg->type));
1474 case GTK_TYPE_BOXED:
1475 if (arg->type == GTK_TYPE_GDK_EVENT)
1477 return (gdk_event_to_emacs_event((GdkEvent *) GTK_VALUE_BOXED (*arg)));
1480 if (GTK_VALUE_BOXED (*arg))
1481 return (build_gtk_boxed (GTK_VALUE_BOXED (*arg), arg->type));
1484 case GTK_TYPE_POINTER:
1485 if (GTK_VALUE_POINTER (*arg))
1489 VOID_TO_LISP (rval, GTK_VALUE_POINTER (*arg));
1494 case GTK_TYPE_OBJECT:
1495 if (GTK_VALUE_OBJECT (*arg))
1496 return (build_gtk_object (GTK_VALUE_OBJECT (*arg)));
1500 case GTK_TYPE_CALLBACK:
1504 VOID_TO_LISP (rval, GTK_VALUE_CALLBACK (*arg).data);
1510 if (GTK_FUNDAMENTAL_TYPE (arg->type) == GTK_TYPE_LISTOF)
1512 if (!GTK_VALUE_POINTER (*arg))
1516 return (xemacs_gtklist_to_list (arg));
1519 stderr_out ("Do not know how to convert `%s' to lisp!\n", gtk_type_name (arg->type));
1522 /* This is chuck reminding GCC to... SHUT UP! */
1526 int lisp_to_gtk_type (Lisp_Object obj, GtkArg *arg)
1528 switch (GTK_FUNDAMENTAL_TYPE (arg->type))
1537 CHECK_CHAR_COERCE_INT (obj);
1539 GTK_VALUE_CHAR (*arg) = c;
1542 case GTK_TYPE_UCHAR:
1546 CHECK_CHAR_COERCE_INT (obj);
1548 GTK_VALUE_CHAR (*arg) = c;
1552 GTK_VALUE_BOOL (*arg) = NILP (obj) ? FALSE : TRUE;
1556 if (NILP (obj) || EQ (Qt, obj))
1558 /* For we are a kind mistress and allow sending t/nil for
1559 1/0 to stupid GTK functions that say they take guint or
1560 gint in the header files, but actually treat it like a
1563 GTK_VALUE_INT(*arg) = NILP (obj) ? 0 : 1;
1568 GTK_VALUE_INT(*arg) = XINT (obj);
1572 case GTK_TYPE_ULONG:
1574 case GTK_TYPE_FLOAT:
1575 CHECK_INT_OR_FLOAT (obj);
1576 GTK_VALUE_FLOAT(*arg) = extract_float (obj);
1578 case GTK_TYPE_DOUBLE:
1579 CHECK_INT_OR_FLOAT (obj);
1580 GTK_VALUE_DOUBLE(*arg) = extract_float (obj);
1582 case GTK_TYPE_STRING:
1584 GTK_VALUE_STRING (*arg) = NULL;
1588 GTK_VALUE_STRING (*arg) = (char *) XSTRING_DATA (obj);
1592 case GTK_TYPE_FLAGS:
1593 /* Convert a lisp symbol to a GTK enum */
1594 GTK_VALUE_ENUM(*arg) = lisp_to_flag (obj, arg->type);
1596 case GTK_TYPE_BOXED:
1599 GTK_VALUE_BOXED(*arg) = NULL;
1601 else if (GTK_BOXEDP (obj))
1603 GTK_VALUE_BOXED(*arg) = XGTK_BOXED (obj)->object;
1605 else if (arg->type == GTK_TYPE_STYLE)
1607 obj = Ffind_face (obj);
1609 GTK_VALUE_BOXED(*arg) = face_to_style (obj);
1611 else if (arg->type == GTK_TYPE_GDK_GC)
1613 obj = Ffind_face (obj);
1615 GTK_VALUE_BOXED(*arg) = face_to_gc (obj);
1617 else if (arg->type == GTK_TYPE_GDK_WINDOW)
1621 Lisp_Object window = Fselected_window (Qnil);
1622 Lisp_Object instance = glyph_image_instance (obj, window, ERROR_ME_NOT, 1);
1623 struct Lisp_Image_Instance *p = XIMAGE_INSTANCE (instance);
1625 switch (XIMAGE_INSTANCE_TYPE (instance))
1629 case IMAGE_SUBWINDOW:
1631 GTK_VALUE_BOXED(*arg) = NULL;
1634 case IMAGE_MONO_PIXMAP:
1635 case IMAGE_COLOR_PIXMAP:
1636 GTK_VALUE_BOXED(*arg) = IMAGE_INSTANCE_GTK_PIXMAP (p);
1640 else if (GTK_OBJECTP (obj) && GTK_IS_WIDGET (XGTK_OBJECT (obj)->object))
1642 GTK_VALUE_BOXED(*arg) = GTK_WIDGET (XGTK_OBJECT (obj))->window;
1646 signal_simple_error ("Don't know how to convert object to GDK_WINDOW", obj);
1650 else if (arg->type == GTK_TYPE_GDK_COLOR)
1652 if (COLOR_SPECIFIERP (obj))
1654 /* If it is a specifier, we just convert it to an
1655 instance, and let the ifs below handle it.
1657 obj = Fspecifier_instance (obj, Qnil, Qnil, Qnil);
1660 if (COLOR_INSTANCEP (obj))
1663 GTK_VALUE_BOXED(*arg) = COLOR_INSTANCE_GTK_COLOR (XCOLOR_INSTANCE (obj));
1665 else if (STRINGP (obj))
1667 signal_simple_error ("Please use a color specifier or instance, not a string", obj);
1671 signal_simple_error ("Don't know hot to convert to GdkColor", obj);
1674 else if (arg->type == GTK_TYPE_GDK_FONT)
1678 /* If it is a symbol, we treat that as a face name */
1679 obj = Ffind_face (obj);
1684 /* If it is a face, we just grab the font specifier, and
1685 cascade down until we finally reach a FONT_INSTANCE
1687 obj = Fget (obj, Qfont, Qnil);
1690 if (FONT_SPECIFIERP (obj))
1692 /* If it is a specifier, we just convert it to an
1693 instance, and let the ifs below handle it
1695 obj = Fspecifier_instance (obj, Qnil, Qnil, Qnil);
1698 if (FONT_INSTANCEP (obj))
1701 GTK_VALUE_BOXED(*arg) = FONT_INSTANCE_GTK_FONT (XFONT_INSTANCE (obj));
1703 else if (STRINGP (obj))
1705 signal_simple_error ("Please use a font specifier or instance, not a string", obj);
1709 signal_simple_error ("Don't know hot to convert to GdkColor", obj);
1714 /* Unknown type to convert to boxed */
1715 stderr_out ("Don't know how to convert to boxed!\n");
1716 GTK_VALUE_BOXED(*arg) = NULL;
1720 case GTK_TYPE_POINTER:
1722 GTK_VALUE_POINTER(*arg) = NULL;
1724 GTK_VALUE_POINTER(*arg) = LISP_TO_VOID (obj);
1727 /* structured types */
1728 case GTK_TYPE_SIGNAL:
1729 case GTK_TYPE_ARGS: /* This we can do as a list of values */
1730 case GTK_TYPE_C_CALLBACK:
1731 case GTK_TYPE_FOREIGN:
1732 stderr_out ("Do not know how to convert `%s' from lisp!\n", gtk_type_name (arg->type));
1737 /* This is not used, and does not work with union type */
1738 case GTK_TYPE_CALLBACK:
1743 obj = Fcons (Qnil, obj); /* Empty data */
1744 obj = Fcons (make_int (id), obj);
1746 gcpro_popup_callbacks (id, obj);
1748 GTK_VALUE_CALLBACK(*arg).marshal = __internal_callback_marshal;
1749 GTK_VALUE_CALLBACK(*arg).data = (gpointer) obj;
1750 GTK_VALUE_CALLBACK(*arg).notify = __internal_callback_destroy;
1755 /* base type of the object system */
1756 case GTK_TYPE_OBJECT:
1758 GTK_VALUE_OBJECT (*arg) = NULL;
1761 CHECK_GTK_OBJECT (obj);
1762 if (XGTK_OBJECT (obj)->alive_p)
1763 GTK_VALUE_OBJECT (*arg) = XGTK_OBJECT (obj)->object;
1765 signal_simple_error ("Attempting to pass dead object to GTK function", obj);
1770 if (GTK_FUNDAMENTAL_TYPE (arg->type) == GTK_TYPE_ARRAY)
1773 GTK_VALUE_POINTER(*arg) = NULL;
1776 xemacs_list_to_array (obj, arg);
1779 else if (GTK_FUNDAMENTAL_TYPE (arg->type) == GTK_TYPE_LISTOF)
1782 GTK_VALUE_POINTER(*arg) = NULL;
1785 xemacs_list_to_gtklist (obj, arg);
1790 stderr_out ("Do not know how to convert `%s' from lisp!\n", gtk_type_name (arg->type));
1799 /* This is used in glyphs-gtk.c as well */
1801 get_enumeration (GtkType t)
1805 if (NILP (Venumeration_info))
1807 Venumeration_info = call2 (intern ("make-hashtable"), make_int (100), Qequal);
1810 alist = Fgethash (make_int (t), Venumeration_info, Qnil);
1814 import_gtk_enumeration_internal (t);
1815 alist = Fgethash (make_int (t), Venumeration_info, Qnil);
1821 symbol_to_enum (Lisp_Object obj, GtkType t)
1823 Lisp_Object alist = get_enumeration (t);
1824 Lisp_Object value = Qnil;
1828 signal_simple_error ("Unkown enumeration", build_string (gtk_type_name (t)));
1831 value = Fassq (obj, alist);
1835 signal_simple_error ("Unknown value", obj);
1838 CHECK_INT (XCDR (value));
1840 return (XINT (XCDR (value)));
1844 lisp_to_flag (Lisp_Object obj, GtkType t)
1852 else if (SYMBOLP (obj))
1854 val = symbol_to_enum (obj, t);
1856 else if (LISTP (obj))
1860 val |= symbol_to_enum (XCAR (obj), t);
1872 flags_to_list (guint value, GtkType t)
1874 Lisp_Object rval = Qnil;
1875 Lisp_Object alist = get_enumeration (t);
1877 while (!NILP (alist))
1879 if (value & XINT (XCDR (XCAR (alist))))
1881 rval = Fcons (XCAR (XCAR (alist)), rval);
1882 value &= ~(XINT (XCDR (XCAR (alist))));
1884 alist = XCDR (alist);
1890 enum_to_symbol (guint value, GtkType t)
1892 Lisp_Object alist = get_enumeration (t);
1893 Lisp_Object cell = Qnil;
1897 signal_simple_error ("Unkown enumeration", build_string (gtk_type_name (t)));
1900 cell = Frassq (make_int (value), alist);
1902 return (NILP (cell) ? Qnil : XCAR (cell));