Merge branch 'master' of http://git.sxemacs.org/sxemacs
[sxemacs] / lisp / gtk-widget-accessors.el
1 (globally-declare-fboundp
2  '(gtk-fundamental-type))
3
4 (require 'gtk-ffi)
5
6 (defconst GTK_TYPE_INVALID 0)
7 (defconst GTK_TYPE_NONE 1)
8 (defconst GTK_TYPE_CHAR 2)
9 (defconst GTK_TYPE_UCHAR 3)
10 (defconst GTK_TYPE_BOOL 4)
11 (defconst GTK_TYPE_INT 5)
12 (defconst GTK_TYPE_UINT 6)
13 (defconst GTK_TYPE_LONG 7)
14 (defconst GTK_TYPE_ULONG 8)
15 (defconst GTK_TYPE_FLOAT 9)
16 (defconst GTK_TYPE_DOUBLE 10)
17 (defconst GTK_TYPE_STRING 11)
18 (defconst GTK_TYPE_ENUM 12)
19 (defconst GTK_TYPE_FLAGS 13)
20 (defconst GTK_TYPE_BOXED 14)
21 (defconst GTK_TYPE_POINTER 15)
22 (defconst GTK_TYPE_SIGNAL 16)
23 (defconst GTK_TYPE_ARGS 17)
24 (defconst GTK_TYPE_CALLBACK 18)
25 (defconst GTK_TYPE_C_CALLBACK 19)
26 (defconst GTK_TYPE_FOREIGN 20)
27 (defconst GTK_TYPE_OBJECT 21)
28
29 (defconst gtk-value-accessor-names
30   '("INVALID" "NONE" "CHAR" "UCHAR" "BOOL" "INT" "UINT" "LONG" "ULONG" "FLOAT" "DOUBLE"
31     "STRING" "ENUM" "FLAGS" "BOXED" "POINTER" "SIGNAL" "ARGS" "CALLBACK" "C_CALLBACK"
32     "FOREIGN" "OBJECT"))
33
34 (defun define-widget-accessors (gtk-class
35                                 wrapper
36                                 prefix args)
37   "Output stub C code to access parts of a widget from lisp.
38 GTK-CLASS is the GTK class to grant access to.
39 WRAPPER is a fragment to construct GTK C macros for typechecking/etc. (ie: WIDGET)
40 ARGS is a list of (type . name) cons cells.
41 Defines a whole slew of functions to access & set the slots in the
42 structure."
43   (set-buffer (get-buffer-create "emacs-widget-accessors.c"))
44   (goto-char (point-max))
45   (let ((arg)
46         (base-arg-type nil)
47         (lisp-func-name nil)
48         (c-func-name nil)
49         (func-names nil))
50     (setq gtk-class (symbol-name gtk-class)
51           wrapper (upcase wrapper))
52     (while (setq arg (pop args))
53       (setq lisp-func-name (format "gtk-%s-%s" prefix (cdr arg))
54             lisp-func-name (replace-in-string lisp-func-name "_" "-")
55             c-func-name (concat "F" (replace-in-string lisp-func-name "-" "_")))
56       (insert
57        "DEFUN (\"" lisp-func-name "\", " c-func-name ", 1, 1, 0, /*\n"
58        "Access the `" (symbol-name (cdr arg)) "' slot of OBJ, a " gtk-class " object.\n"
59        "*/\n"
60        "\t(obj))\n"
61        "{\n"
62        (format "\t%s *the_obj = NULL;\n" gtk-class)
63        "\tGtkArg arg;\n"
64        "\n"
65        "\tCHECK_GTK_OBJECT (obj);\n"
66        "\n"
67        (format "\tif (!GTK_IS_%s (XGTK_OBJECT (obj)->object))\n" wrapper)
68        "\t{\n"
69        (format "\t\tsignal_simple_error (\"Object is not a %s\", obj);\n" gtk-class)
70        "\t};\n"
71        "\n"
72        (format "\tthe_obj = GTK_%s (XGTK_OBJECT (obj)->object);\n" wrapper)
73
74        (format "\targ.type = gtk_type_from_name (\"%s\");\n" (symbol-name (car arg))))
75 ;       (format "\targ.type = GTK_TYPE_%s;\n" (or
76 ;                                              (nth (gtk-fundamental-type (car arg))
77 ;                                                   gtk-value-accessor-names)
78 ;                                              (case (car arg)
79 ;                                                (GtkListOfString "STRING_LIST")
80 ;                                                (GtkListOfObject "OBJECT_LIST")
81 ;                                                (otherwise
82 ;                                                 "POINTER")))))
83
84       (setq base-arg-type (gtk-fundamental-type (car arg)))
85       (cond
86        ((= base-arg-type GTK_TYPE_OBJECT)
87         (insert
88          (format "\tGTK_VALUE_OBJECT (arg) = GTK_OBJECT (the_obj->%s);"
89                  (cdr arg))))
90        ((or (= base-arg-type GTK_TYPE_POINTER)
91             (= base-arg-type GTK_TYPE_BOXED))
92         (insert
93          (format "\tGTK_VALUE_%s (arg) = (void *)the_obj->%s;"
94                  (nth (gtk-fundamental-type (car arg)) gtk-value-accessor-names)
95                  (cdr arg))))
96        (t
97         (insert
98          (format "\tGTK_VALUE_%s (arg) = the_obj->%s;"
99                  (or (nth (gtk-fundamental-type (car arg)) gtk-value-accessor-names) "POINTER")
100                  (cdr arg)))))
101       (insert
102        "\n"
103        "\treturn (gtk_type_to_lisp (&arg));\n"
104        "}\n\n")
105       (push c-func-name func-names))
106     func-names))
107
108 (defun import-widget-accessors (file syms-function-name &rest description)
109   "Import multiple widgets, and emit a suitable vars_of_foo() function for them.\n"
110   (declare (special c-mode-common-hook c-mode-hook))
111   (let ((c-mode-common-hook nil)
112         (c-mode-hook nil))
113     (find-file file))
114   (erase-buffer)
115   (let ((c-funcs nil))
116     (while description
117       (setq c-funcs (nconc (define-widget-accessors
118                              (pop description) (pop description)
119                              (pop description) (pop description)) c-funcs)))
120     (goto-char (point-max))
121     (insert "void " syms-function-name " (void)\n"
122             "{\n\t"
123             (mapconcat (lambda (x)
124                          (concat "DEFSUBR (" x ");")) c-funcs "\n\t")
125             "\n}"))
126   (save-buffer))
127
128 ;; Because the new FFI layer imports GTK types lazily, we need to load
129 ;; up all of the gtk types we know about, or we get errors about
130 ;; unknown GTK types later on.
131 (mapatoms (lambda (sym)
132             (if (string-match "gtk-[^-]+-get-type" (symbol-name sym))
133                 (funcall sym))))
134
135 (import-widget-accessors
136  "../../src/emacs-widget-accessors.c"
137  "syms_of_widget_accessors "
138
139  'GtkAdjustment "ADJUSTMENT" "adjustment"
140  '((gfloat . lower)
141    (gfloat . upper)
142    (gfloat . value)
143    (gfloat . step_increment)
144    (gfloat . page_increment)
145    (gfloat . page_size))
146
147  'GtkWidget "WIDGET" "widget"
148  '((GtkStyle     . style)
149    (GdkWindow    . window)
150    (GtkStateType . state)
151    (GtkString    . name)
152    (GtkWidget    . parent))
153
154  'GtkButton "BUTTON" "button"
155  '((GtkWidget  . child)
156    (gboolean   . in_button)
157    (gboolean   . button_down))
158
159  'GtkCombo "COMBO" "combo"
160  '((GtkWidget  . entry)
161    (GtkWidget  . button)
162    (GtkWidget  . popup)
163    (GtkWidget  . popwin)
164    (GtkWidget  . list))
165
166  'GtkGammaCurve "GAMMA_CURVE" "gamma-curve"
167  '((GtkWidget  . table)
168    (GtkWidget  . curve)
169    (gfloat      . gamma)
170    (GtkWidget  . gamma_dialog)
171    (GtkWidget  . gamma_text))
172
173  'GtkCheckMenuItem "CHECK_MENU_ITEM" "check-menu-item"
174  '((gboolean   . active))
175
176  'GtkNotebook "NOTEBOOK" "notebook"
177  '((GtkPositionType . tab_pos))
178
179  'GtkText "TEXT" "text"
180  '((GtkAdjustment . hadj)
181    (GtkAdjustment . vadj))
182
183  'GtkFileSelection "FILE_SELECTION" "file-selection"
184  '((GtkWidget . dir_list)
185    (GtkWidget . file_list)
186    (GtkWidget . selection_entry)
187    (GtkWidget . selection_text)
188    (GtkWidget . main_vbox)
189    (GtkWidget . ok_button)
190    (GtkWidget . cancel_button)
191    (GtkWidget . help_button)
192    (GtkWidget . action_area))
193
194  'GtkFontSelectionDialog "FONT_SELECTION_DIALOG" "font-selection-dialog"
195  '((GtkWidget . fontsel)
196    (GtkWidget . main_vbox)
197    (GtkWidget . action_area)
198    (GtkWidget . ok_button)
199    (GtkWidget . apply_button)
200    (GtkWidget . cancel_button))
201
202  'GtkColorSelectionDialog "COLOR_SELECTION_DIALOG" "color-selection-dialog"
203  '((GtkWidget . colorsel)
204    (GtkWidget . main_vbox)
205    (GtkWidget . ok_button)
206    (GtkWidget . reset_button)
207    (GtkWidget . cancel_button)
208    (GtkWidget . help_button))
209
210  'GtkDialog "DIALOG" "dialog"
211  '((GtkWidget . vbox)
212    (GtkWidget . action_area))
213
214  'GtkInputDialog "INPUT_DIALOG" "input-dialog"
215  '((GtkWidget . close_button)
216    (GtkWidget . save_button))
217
218  'GtkPlug "PLUG" "plug"
219  '((GdkWindow . socket_window)
220    (gint      . same_app))
221
222  'GtkObject "OBJECT" "object"
223  '((guint     . flags)
224    (guint     . ref_count))
225
226  'GtkPaned "PANED" "paned"
227  '((GtkWidget . child1)
228    (GtkWidget . child2)
229    (gboolean  . child1_resize)
230    (gboolean  . child2_resize)
231    (gboolean  . child1_shrink)
232    (gboolean  . child2_shrink))
233
234  'GtkCList "CLIST" "clist"
235  '((gint . rows)
236    (gint . columns)
237    (GtkAdjustment . hadjustment)
238    (GtkAdjustment . vadjustment)
239    (GtkSortType   . sort_type)
240    (gint . focus_row)
241    (gint . sort_column))
242
243  'GtkList "LIST" "list"
244  '((GtkListOfObject . children)
245    (GtkListOfObject . selection))
246
247  'GtkTree "TREE" "tree"
248  '((GtkListOfObject . children)
249    (GtkTree         . root_tree)
250    (GtkWidget       . tree_owner)
251    (GtkListOfObject . selection))
252
253  'GtkTreeItem "TREE_ITEM" "tree-item"
254  '((GtkWidget       . subtree))
255
256  'GtkScrolledWindow "SCROLLED_WINDOW" "scrolled-window"
257  '((GtkWidget . hscrollbar)
258    (GtkWidget . vscrollbar)
259    (gboolean  . hscrollbar_visible)
260    (gboolean  . vscrollbar_visible))
261
262  )