chiark / gitweb /
Added multi-threading support
[clg] / gtk / gtk.lisp
index 6d0c9efba4f85160d950a9cbc3bd7365f315f59f..f054449cfc6652899b1605ac3862e3e509a8ca61 100644 (file)
@@ -1,21 +1,26 @@
-;; Common Lisp bindings for GTK+ v2.0
-;; Copyright (C) 1999-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
+;; Common Lisp bindings for GTK+ v2.x
+;; Copyright 1999-2005 Espen S. Johnsen <espen@users.sf.net>
 ;;
-;; This library is free software; you can redistribute it and/or
-;; modify it under the terms of the GNU Lesser General Public
-;; License as published by the Free Software Foundation; either
-;; version 2 of the License, or (at your option) any later version.
+;; Permission is hereby granted, free of charge, to any person obtaining
+;; a copy of this software and associated documentation files (the
+;; "Software"), to deal in the Software without restriction, including
+;; without limitation the rights to use, copy, modify, merge, publish,
+;; distribute, sublicense, and/or sell copies of the Software, and to
+;; permit persons to whom the Software is furnished to do so, subject to
+;; the following conditions:
 ;;
-;; This library is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-;; Lesser General Public License for more details.
+;; The above copyright notice and this permission notice shall be
+;; included in all copies or substantial portions of the Software.
 ;;
-;; You should have received a copy of the GNU Lesser General Public
-;; License along with this library; if not, write to the Free Software
-;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-;; $Id: gtk.lisp,v 1.41 2005-04-19 08:11:39 espen Exp $
+;; $Id: gtk.lisp,v 1.61 2006-04-25 13:37:29 espen Exp $
 
 
 (in-package "GTK")
@@ -39,7 +44,8 @@ (defun gtk-version ()
        (format nil "Gtk+ v~A.~A" major minor) 
       (format nil "Gtk+ v~A.~A.~A" major minor micro))))
 
-(defbinding get-default-language () (copy-of pango:language))
+(defun clg-version ()
+  "clg 0.92.1")
 
 
 ;;;; Initalization
@@ -51,6 +57,11 @@ (defbinding (gtk-init "gtk_parse_args") () boolean
 
 (defun clg-init (&optional display)
   "Initializes the system and starts the event handling"
+  #+sbcl(when (and 
+              (find-package "SWANK")
+              (eq (symbol-value (find-symbol "*COMMUNICATION-STYLE*" "SWANK")) :spawn))
+         (error "When running clg in Slime the communication style :spawn can not be used. See the README file and <http://common-lisp.net/project/slime/doc/html/slime_45.html> for more information."))
+
   (unless (gdk:display-get-default)
     (gdk:gdk-init)
     (unless (gtk-init)
@@ -62,6 +73,29 @@ (defun clg-init (&optional display)
       (setq *max-event-to-sec* 0)
       (setq *max-event-to-usec* 1000))))
 
+#+sbcl   
+(defun clg-init-with-threading (&optional display)
+  "Initializes the system and starts the event handling"
+  (unless (gdk:display-get-default)
+    (gdk:gdk-init)
+    (gdk:threads-set-lock-functions)
+    (unless (gtk-init)
+      (error "Initialization of GTK+ failed."))
+    (sb-thread:make-thread 
+     #'(lambda () 
+        (gdk:display-open display)
+        (gdk:with-global-lock (main)))
+     :name "gtk event loop")))
+
+
+;;; Generic functions 
+
+(defgeneric add-to-radio-group (item1 item2))
+(defgeneric activate-radio-widget (item))
+(defgeneric (setf tool-item-tip-text) (tip-text tool-item))
+(defgeneric (setf tool-item-tip-private) (tip-private tool-item))
+
+
 
 ;;; Misc
 
@@ -73,23 +107,25 @@ (defbinding grab-get-current () widget)
 (defbinding grab-remove () nil
   (widget widget))
 
+(defbinding get-default-language () (copy-of pango:language))
+
 
 ;;; About dialog
 
 #+gtk2.6
 (progn
-  (def-callback-marshal %about-dialog-activate-link-func 
-    (nil (dialog about-dialog) (link (copy-of string))))
+  (define-callback-marshal %about-dialog-activate-link-callback nil
+    (about-dialog (link string)))
 
   (defbinding about-dialog-set-email-hook (function) nil
-    ((callback %about-dialog-activate-link-func) pointer)
+    (%about-dialog-activate-link-callback callback)
     ((register-callback-function function) unsigned-int)
-    ((callback user-data-destroy-func) pointer))
+    (user-data-destroy-callback callback))
   
   (defbinding about-dialog-set-url-hook (function) nil
-    ((callback %about-dialog-activate-link-func) pointer)
+    (%about-dialog-activate-link-callback callback)
     ((register-callback-function function) unsigned-int)
-    ((callback user-data-destroy-func) pointer)))
+    (user-data-destroy-callback callback)))
 
 
 ;;; Acccel group
@@ -102,7 +138,7 @@ (defbinding %accel-group-connect () nil
   (gclosure gclosure))
 
 (defun accel-group-connect (group accelerator function &optional flags)
-  (multiple-value-bind (key modifiers) (accelerator-parse accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
     (let ((gclosure (make-callback-closure function)))
       (%accel-group-connect group key modifiers flags gclosure)
       gclosure)))
@@ -125,22 +161,45 @@ (defun accel-group-disconnect (group accelerator)
   (etypecase accelerator
     (gclosure (%accel-group-disconnect group accelerator))
     (string 
-     (multiple-value-bind (key modifiers) (accelerator-parse accelerator)
+     (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
        (%accel-group-disconnect-key group key modifiers)))))
 
+(defbinding %accel-group-query () (copy-of (vector (inlined accel-group-entry) n))
+  (accel-group accel-group)
+  (key unsigned-int)
+  (modifiers gdk:modifier-type)
+  (n int :out))
+
+(defun accel-group-query (accel-group accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
+    (%accel-group-query accel-group key modifiers)))
+
+(defbinding %accel-group-activate () boolean
+  (accel-group accel-group)
+  (acceleratable gobject)
+  (key unsigned-int)
+  (modifiers gdk:modifier-type))
+
+(defun accel-group-activate (accel-group acceleratable accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
+    (%accel-group-activate accel-group acceleratable key modifiers)))
+
 (defbinding accel-group-lock () nil
   (accel-group accel-group))
 
 (defbinding accel-group-unlock () nil
   (accel-group accel-group))
 
+(defbinding accel-group-from-accel-closure () accel-group
+  (closure gclosure))
+
 (defbinding %accel-groups-activate () boolean
   (object gobject)
   (key unsigned-int)
   (modifiers gdk:modifier-type))
 
 (defun accel-groups-activate (object accelerator)
-  (multiple-value-bind (key modifiers) (accelerator-parse accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
     (%accel-groups-activate object key modifiers)))
 
 (defbinding accel-groups-from-object () (gslist accel-groups)
@@ -155,12 +214,35 @@ (defbinding %accelerator-parse () nil
   (key unsigned-int :out)
   (modifiers gdk:modifier-type :out))
 
-(defun accelerator-parse (accelerator)
+(defgeneric parse-accelerator (accelerator))
+
+(defmethod parse-accelerator ((accelerator string))
   (multiple-value-bind (key modifiers) (%accelerator-parse accelerator)
     (if (zerop key)
        (error "Invalid accelerator: ~A" accelerator)
       (values key modifiers))))
 
+(defmethod parse-accelerator ((accelerator cons))
+  (destructuring-bind (key modifiers) accelerator
+    (values
+     (etypecase key
+       (integer key)
+       (string
+       (or 
+        (gdk:keyval-from-name key)
+        (error "Invalid key name: ~A" key)))
+       (character (parse-accelerator key)))
+     modifiers)))
+
+(defmethod parse-accelerator ((key integer))
+  key)
+
+(defmethod parse-accelerator ((key character))
+  (or
+   (gdk:keyval-from-name (string key))
+   (error "Invalid key name: ~A" key)))
+
+
 (defbinding accelerator-name () string
   (key unsigned-int)
   (modifiers gdk:modifier-type))
@@ -181,6 +263,9 @@ (defbinding (accelerator-default-modifier-mask "gtk_accelerator_get_default_mod_
 
 ;;; Acccel label
 
+(defbinding accel-label-get-accel-width () unsigned-int
+  (accel-label accel-label))
+
 (defbinding accel-label-refetch () boolean
   (accel-label accel-label))
 
@@ -188,7 +273,7 @@ (defbinding accel-label-refetch () boolean
 
 ;;; Accel map
 
-;(defbinding (accel-map-init "_gtk_accel_map_init") () nil)
+(defbinding (accel-map-init "_gtk_accel_map_init") () nil)
 
 (defbinding %accel-map-add-entry () nil
   (path string)
@@ -196,12 +281,20 @@ (defbinding %accel-map-add-entry () nil
   (modifiers gdk:modifier-type))
 
 (defun accel-map-add-entry (path accelerator)
-  (multiple-value-bind (key modifiers) (accelerator-parse accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
     (%accel-map-add-entry path key modifiers)))
 
-(defbinding accel-map-lookup-entry () boolean
+(defbinding %accel-map-lookup-entry () boolean
   (path string)
-  (key pointer)) ;accel-key))
+  ((make-instance 'accel-key) accel-key :return))
+
+(defun accel-map-lookup-entry (path)
+  (multiple-value-bind (found-p accel-key) (%accel-map-lookup-entry path)
+    (when found-p
+      (values 
+       (slot-value accel-key 'key)
+       (slot-value accel-key 'modifiers)
+       (slot-value accel-key 'flags)))))
 
 (defbinding %accel-map-change-entry () boolean
   (path string)
@@ -210,7 +303,7 @@ (defbinding %accel-map-change-entry () boolean
   (replace boolean))
 
 (defun accel-map-change-entry (path accelerator &optional replace)
-  (multiple-value-bind (key modifiers) (accelerator-parse accelerator)
+  (multiple-value-bind (key modifiers) (parse-accelerator accelerator)
     (%accel-map-change-entry path key modifiers replace)))
 
 (defbinding accel-map-load () nil
@@ -219,6 +312,27 @@ (defbinding accel-map-load () nil
 (defbinding accel-map-save () nil
   (filename pathname))
 
+(define-callback-marshal %accel-map-foreach-callback nil
+  ((accel-path string) (key unsigned-int) 
+   (modifiers gdk:modifier-type) (changed boolean)) :callback-id :first)
+
+(defbinding %accel-map-foreach (callback-id) nil
+  (callback-id unsigned-int)
+  (%accel-map-foreach-callback callback))
+
+(defbinding %accel-map-foreach-unfiltered (callback-id) nil
+  (callback-id unsigned-int)
+  (%accel-map-foreach-callback callback))
+
+(defun accel-map-foreach (function &optional (filter-p t))
+  (with-callback-function (id function)
+    (if filter-p                         
+       (%accel-map-foreach id)
+      (%accel-map-foreach-unfiltered id))))
+
+(defbinding accel-map-add-filter () nil
+  (filter string))
+
 (defbinding accel-map-get () accel-map)
 
 (defbinding accel-map-lock-path () nil
@@ -229,7 +343,7 @@ (defbinding accel-map-unlock-path () nil
 
 
 
-;;; Accessible
+;;; Accessibility
 
 (defbinding accessible-connect-widget-destroyed () nil
   (accessible accessible))
@@ -604,8 +718,8 @@ (defbinding alternative-dialog-button-order-p (&optional screen) boolean
 
 #+gtk2.6
 (defbinding (dialog-set-alternative-button-order 
-            "gtk_dialog_set_alternative_button_order_from_array") 
-    (dialog new-order)
+            "gtk_dialog_set_alternative_button_order_from_array")
+    (dialog new-order) nil
   (dialog dialog)
   ((length new-order) int)
   ((map 'vector #'(lambda (response)
@@ -613,6 +727,16 @@ (defbinding (dialog-set-alternative-button-order
        new-order) (vector int)))
 
 
+#+gtk2.8
+(progn
+  (defbinding %dialog-get-response-for-widget () int
+    (dialog dialog)
+    (widget widget))
+
+  (defun dialog-get-response-for-widget (dialog widget)
+    (dialog-find-response dialog (dialog-get-response-for-widget dialog widget))))
+
+
 (defmethod container-add ((dialog dialog) (child widget) &rest args)
   (apply #'container-add (dialog-vbox dialog) child args))
 
@@ -645,14 +769,14 @@ (defbinding entry-text-index-to-layout-index () int
 
 ;;; Entry Completion
 
-(def-callback-marshal %entry-completion-match-func
-    (boolean entry-completion string (copy-of tree-iter)))
+(define-callback-marshal %entry-completion-match-callback boolean 
+  (entry-completion string tree-iter))
 
 (defbinding entry-completion-set-match-func (completion function) nil
   (completion entry-completion)
-  ((callback %entry-completion-match-func) pointer)
+  (%entry-completion-match-callback callback)
   ((register-callback-function function) unsigned-int)
-  ((callback user-data-destroy-func) pointer))
+  (user-data-destroy-callback callback))
 
 (defbinding entry-completion-complete () nil
   (completion entry-completion))
@@ -789,14 +913,14 @@ (defbinding file-filter-add-pattern () nil
 (defbinding file-filter-add-pixbuf-formats () nil
   (filter file-filter))
 
-(def-callback-marshal %file-filter-func (boolean file-filter-info))
+(define-callback-marshal %file-filter-callback boolean (file-filter-info))
 
 (defbinding file-filter-add-custom (filter needed function) nil
   (filter file-filter)
   (needed file-filter-flags)
-  ((callback %file-filter-func) pointer)
+  (%file-filter-callback callback)
   ((register-callback-function function) unsigned-int)
-  ((callback user-data-destroy-func) pointer))
+  (user-data-destroy-callback callback))
 
 (defbinding file-filter-get-needed () file-filter-flags
   (filter file-filter))
@@ -837,6 +961,11 @@ (defun create-image-widget (source &optional mask)
     ((or list vector) (make-instance 'image :pixmap source))
     (gdk:pixmap (make-instance 'image :pixmap source :mask mask))))
 
+#+gtk2.8
+(defbinding image-clear () nil
+  (image image))
+
+
 
 ;;; Image menu item
 
@@ -988,13 +1117,14 @@ (defbinding menu-tool-button-set-arrow-tooltip () nil
 
 ;;; Message dialog
 
-(defmethod initialize-instance ((dialog message-dialog)
-                               &key (message-type :info) (buttons :close)
-                               flags text #+gtk 2.6 secondary-text 
-                               transient-parent)
-  (setf 
-   (slot-value dialog 'location)
-   (%message-dialog-new transient-parent flags message-type buttons))
+(defmethod allocate-foreign ((dialog message-dialog) &key (message-type :info)
+                            (buttons :close) flags transient-parent)
+  (%message-dialog-new transient-parent flags message-type buttons))
+
+
+(defmethod shared-initialize ((dialog message-dialog) names
+                             &key text #+gtk 2.6 secondary-text)
+  (declare (ignore names))
   (when text
     (message-dialog-set-markup dialog text))
   #+gtk2.6
@@ -1095,6 +1225,16 @@ (defmethod initialize-instance ((window window) &rest initargs
     (initial-add window #'window-add-accel-group 
      initargs :accel-group :accel-groups)))
 
+#-debug-ref-counting
+(defmethod print-object ((window window) stream)
+  (if (and 
+       (proxy-valid-p window) 
+       (slot-boundp window 'title) 
+       (not (zerop (length (window-title window)))))
+      (print-unreadable-object (window stream :type t :identity nil)
+        (format stream "~S at 0x~X" 
+        (window-title window) (sap-int (foreign-location window))))
+    (call-next-method)))
 
 (defbinding window-set-wmclass () nil
   (window window)
@@ -1182,9 +1322,24 @@ (defbinding window-propagate-key-event () boolean
   (window window)
   (event gdk:key-event))
 
+#-gtk2.8
 (defbinding window-present () nil
   (window window))
 
+#+gtk2.8
+(progn
+  (defbinding %window-present () nil
+    (window window))
+
+  (defbinding %window-present-with-time () nil
+    (window window)
+    (timespamp unsigned-int))
+
+  (defun window-present (window &optional timestamp)
+    (if timestamp
+       (%window-present-with-time window timestamp)
+      (%window-present window))))
+
 (defbinding window-iconify () nil
   (window window))
 
@@ -1248,7 +1403,7 @@ (defbinding window-get-frame-dimensions () nil
   (window window)
   (left int :out) (top int :out) (rigth int :out) (bottom int :out))
 
-(defbinding %window-get-icon-list () (glist gdk:pixbuf)
+(defbinding %window-get-icon-list () (glist (copy-of gdk:pixbuf))
   (window window))
 
 (defbinding window-get-position () nil
@@ -1468,7 +1623,7 @@ (defbinding notebook-prev-page () nil
 (defbinding notebook-reorder-child (notebook child position) nil
   (notebook notebook)
   (child widget)
-  ((%notebook-position notebook position) int))
+  ((%ensure-notebook-position notebook position) int))
 
 (defbinding notebook-popup-enable () nil
   (notebook notebook))
@@ -1535,7 +1690,7 @@ (defun (setf notebook-menu-label) (menu-label notebook page)
 
 (defbinding notebook-query-tab-label-packing (notebook page) nil
   (notebook notebook)
-  ((%notebook-child notebook page) widget)
+  ((%ensure-notebook-child notebook page) widget)
   (expand boolean :out)
   (fill boolean :out)
   (pack-type pack-type :out))
@@ -1543,7 +1698,7 @@ (defbinding notebook-query-tab-label-packing (notebook page) nil
 (defbinding notebook-set-tab-label-packing
     (notebook page expand fill pack-type) nil
   (notebook notebook)
-  ((%notebook-child notebook page) widget)
+  ((%ensure-notebook-child notebook page) widget)
   (expand boolean)
   (fill boolean)
   (pack-type pack-type))
@@ -1654,13 +1809,14 @@ (defbinding menu-attach () nil
   (top-attach unsigned-int)
   (bottom-attach unsigned-int))
 
-(def-callback-marshal %menu-position-func (nil (menu menu) (x int) (y int) (push-in boolean)))
+(define-callback-marshal %menu-position-callback nil 
+  (menu (x int) (y int) (push-in boolean)))
 
 (defbinding %menu-popup () nil
   (menu menu)
   (parent-menu-shell (or null menu-shell))
   (parent-menu-item (or null menu-item))
-  (callback-func (or null pointer))
+  (callback (or null callback))
   (callback-id unsigned-int)
   (button unsigned-int)
   (activate-time (unsigned 32)))
@@ -1671,7 +1827,7 @@ (defun menu-popup (menu button activate-time &key callback parent-menu-shell
       (with-callback-function (id callback)
        (%menu-popup 
         menu parent-menu-shell parent-menu-item 
-        (callback %menu-position-func) id button activate-time))
+        %menu-position-callback id button activate-time))
     (%menu-popup
      menu parent-menu-shell parent-menu-item nil 0 button activate-time)))
  
@@ -1699,13 +1855,13 @@ (defun (setf menu-active) (menu child)
   (%menu-set-active menu (%menu-position menu child))
   child)
   
-(defcallback %menu-detach-func (nil (widget widget) (menu menu))
+(define-callback %menu-detach-callback nil ((widget widget) (menu menu))
   (funcall (object-data menu 'detach-func) widget menu))
 
-(defbinding %menu-attach-to-widget () nil
+(defbinding %menu-attach-to-widget (menu widget) nil
   (menu menu)
   (widget widget)
-  ((callback %menu-detach-func) pointer))
+  (%menu-detach-callback callback))
 
 (defun menu-attach-to-widget (menu widget function)
   (setf (object-data menu 'detach-func) function)
@@ -2112,13 +2268,22 @@ (defbinding %stock-lookup () boolean
   (location pointer))
 
 (defun stock-lookup (stock-id)
-  (let ((location 
-        (allocate-memory (proxy-instance-size (find-class 'stock-item)))))
-    (unwind-protect
-       (when (%stock-lookup stock-id location)
-         (ensure-proxy-instance 'stock-item (%stock-item-copy location)))
-       (deallocate-memory location))))
+  (with-allocated-memory (stock-item (foreign-size (find-class 'stock-item)))
+    (when (%stock-lookup stock-id stock-item)
+      (ensure-proxy-instance 'stock-item (%stock-item-copy stock-item)))))
+
+#+gtk2.8
+(progn
+  (define-callback-marshal %stock-translate-callback string ((path string)))
+
+  (defbinding (stock-set-translate-function "gtk_stock_set_translate_func") 
+      (domain function) nil
+    (domain string)
+    (%stock-translate-callback callback)
+    ((register-callback-function function) unsigned-int)
+    (user-data-destroy-callback callback)))
 
+  
 
 ;;; Tooltips
 
@@ -2151,18 +2316,66 @@ (defbinding tooltips-get-info-from-tip-window () boolean
   (current-widget widget :out))
 
 
-;;; Rc
+;;; Resource Files
 
-(defbinding rc-add-default-file (filename) nil
-  ((namestring (truename filename)) string))
+(defbinding rc-get-style () style
+  (widget widget))
 
-(defbinding rc-parse (filename) nil
-  ((namestring (truename filename)) string))
+(defbinding rc-get-style-by-paths (&key path class-path class) style
+  (path (or null string))
+  (class-path (or null string))
+  (class gtype))
+
+(defbinding rc-parse () nil
+  (filename pathname))
 
 (defbinding rc-parse-string () nil
   (rc-string string))
 
-(defbinding rc-reparse-all () nil)
+(defbinding %rc-reparse-all () boolean)
 
-(defbinding rc-get-style () style
-  (widget widget))
+(defbinding %rc-reparse-all-for-settings () boolean
+  (settings settings)
+  (force-load-p boolean))
+
+(defun rc-reparse-all (&optional setting force-load-p)
+  (if setting
+      (%rc-reparse-all-for-settings setting force-load-p)
+    (%rc-reparse-all)))
+
+(defbinding rc-reset-styles () nil
+  (settings settings))
+
+(defbinding rc-add-default-file () nil
+  (filename pathname))
+
+(defbinding rc-get-default-files ()
+    (copy-of (null-terminated-vector (copy-of string))))
+
+(defbinding rc-get-module-dir () string)
+
+(defbinding rc-get-im-module-path () string)
+
+(defbinding rc-get-im-module-file () string)
+
+(defbinding rc-get-theme-dir () string)
+
+
+;;; Settings
+
+(defbinding (settings-get "gtk_settings_get_for_screen")
+    (&optional (screen (gdk:display-get-default-screen))) settings
+  (screen gdk:screen))
+
+
+;;; Plug and Socket
+
+(defbinding socket-add-id () nil
+  (socket socket)
+  (id gdk:native-window))
+
+(defbinding %plug-new () pointer
+  (id gdk:native-window))
+
+(defmethod allocate-foreign ((plug plug) &key id)
+  (%plug-new (or id 0)))