chiark / gitweb /
Introspected classes now defined in propper order
[clg] / gtk / gtkutils.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.no>
3 ;;
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.
8 ;;
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.
13 ;;
14 ;; You should have received a copy of the GNU Lesser General Public
15 ;; License along with this library; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 ;; $Id: gtkutils.lisp,v 1.6 2005-01-06 21:06:54 espen Exp $
19
20
21 (in-package "GTK")
22
23 (defun create-button (specs &optional callback &key object)
24   (destructuring-bind (label &rest initargs) (mklist specs)
25     (let ((button
26            (apply #'make-instance 'button :label label :visible t initargs)))
27       (if callback
28           (signal-connect button 'clicked callback :object object)
29         (setf (widget-sensitive-p button) nil))
30       button)))
31
32
33 (defun create-label (label &rest args)
34   (apply #'make-instance 'label :label label args))
35   
36
37 ;; TODO: same syntax as create-button
38 (defun %create-toggleable-button (class label callback initstate initargs)
39   (let ((button 
40          (apply #'make-instance class :label label :active initstate :visible t
41                 initargs)))
42     (signal-connect
43      button 'toggled
44      #'(lambda ()
45          (funcall (funcallable callback) (toggle-button-active-p button))))
46     (funcall (funcallable callback) initstate)
47     button))
48
49 (defun create-toggle-button (label callback &optional initstate &rest initargs)
50   (%create-toggleable-button 'toggle-button label callback initstate initargs))
51
52 (defun create-check-button (label callback &optional initstate &rest initargs)
53   (%create-toggleable-button 'check-button label callback initstate initargs))
54
55 (defun adjustment-new (value lower upper step-increment page-increment page-size)
56   (make-instance 'adjustment 
57    :value value :lower lower :upper upper :step-increment step-increment
58    :page-increment page-increment :page-size page-size))
59
60 (defun make-radio-group (type specs callback &rest initargs)
61   (let* ((active ())
62          (widgets
63           (loop
64            for spec in specs
65            as widget = (apply #'make-instance type (append spec initargs))
66            do (when callback
67                (apply #'add-activate-callback widget (mklist callback)))
68               (when (and (not active) (getf spec :active))
69                 (setq active widget))
70           collect widget)))
71
72     (let ((active (or active (first widgets))))
73       (loop
74        for widget in widgets
75        unless (eq widget active)
76        do (add-to-radio-group widget active))
77       (signal-emit active 'clicked))
78
79     widgets))
80
81
82 ;;;; The follwing code will probably be removed soon
83
84 (defun create-action (name &optional stock-id label accelerator tooltip 
85                       callback &rest initargs)
86   (let ((action (apply #'make-instance 'action
87                  :name (string name) :stock-id stock-id  :label label
88                  :tooltip tooltip :accelerator accelerator initargs)))
89     (when callback
90       (apply #'signal-connect action 'activate (mklist callback)))
91     action))
92
93 (defun create-toggle-action (name &optional stock-id label accelerator 
94                              tooltip active callback &rest initargs)
95   (let ((action (apply #'make-instance 'toggle-action 
96                  :name (string name) :stock-id stock-id :label label
97                  :tooltip tooltip :active active :accelerator accelerator
98                  initargs)))
99     (when callback
100       (destructuring-bind (function &key object after) (mklist callback)
101         (signal-connect action 'activate
102          (if object 
103              #'(lambda (object)
104                  (funcall function object (toggle-action-active-p action)))
105            #'(lambda ()
106                (funcall function (toggle-action-active-p action))))
107          :object object :after after)
108         ;(funcall callback active)
109         (when active
110           (action-activate action))))
111     action))
112
113 (defun create-radio-actions (specs &optional active callback &rest initargs)
114   (loop
115    with group = nil
116    for spec in specs
117    collect (destructuring-bind (name &optional stock-id label accelerator 
118                                 tooltip (value name)) 
119                (mklist spec)
120              (let ((action (apply #'make-instance 'radio-action 
121                             :name (string name) :stock-id stock-id 
122                             :label label :tooltip tooltip 
123                             :accelerator accelerator initargs)))
124                (when (equal active value)
125                  (setf (toggle-action-active-p action) t)
126                  (when callback
127                    (funcall callback value)))
128
129                (if (not group)
130                    (setq group action)
131                  (radio-action-add-to-group action group))
132                (when callback
133                  (signal-connect action 'activate
134                   #'(lambda ()
135                       (funcall callback value))))
136                action))))