New function UPDATE-USER-DATA and some bug fixes
[clg] / examples / testgtk.lisp
index 49856ec..c9849f2 100644 (file)
@@ -15,7 +15,7 @@
 ;; License along with this library; if not, write to the Free Software
 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-;; $Id: testgtk.lisp,v 1.11 2004/12/17 00:45:00 espen Exp $
+;; $Id: testgtk.lisp,v 1.14 2004/12/29 21:21:31 espen Exp $
 
 
 ;;; Some of the code in this file are really outdatet, but it is
    :label frame-label
    :child (make-instance class
           :border-width 5 :layout-style layout :spacing spacing
-;         :child-min-width width :child-min-height height
-          :child (make-instance 'button :label "gtk-ok" :use-stock t)
-          :child (make-instance 'button :label "gtk-cancel" :use-stock t)
-          :child (make-instance 'button :label "gtk-help" :use-stock t))))
+          :child (make-instance 'button :stock "gtk-ok")
+          :child (make-instance 'button :stock "gtk-cancel")
+          :child (make-instance 'button :stock "gtk-help"))))
 
 (define-toplevel create-button-box (window "Button Boxes")
   (make-instance 'v-box
   (declare (number n min-val max-val))
   (max (min n max-val) min-val))
 
+(defun set-cursor (spinner drawing-area label)
+  (let ((cursor
+        (glib:int-enum
+         (logand (clamp (spin-button-value-as-int spinner) 0 152) #xFE)
+         'gdk:cursor-type)))
+    (setf (label-label label) (string-downcase cursor))
+    (setf (widget-cursor drawing-area) cursor)))
+
+(defun cursor-expose (drawing-area event)
+  (declare (ignore event))
+  (multiple-value-bind (width height)
+      (widget-get-size-allocation drawing-area)
+    (let* ((window (widget-window drawing-area))
+          (style (widget-style drawing-area))
+          (white-gc (style-white-gc style))
+          (gray-gc (style-bg-gc style :normal))
+          (black-gc (style-black-gc style)))
+      (gdk:draw-rectangle window white-gc t 0 0 width (floor height 2))
+      (gdk:draw-rectangle window black-gc t 0 (floor height 2) width 
+                         (floor height 2))
+      (gdk:draw-rectangle window gray-gc t (floor width 3) 
+                         (floor height 3) (floor width 3) 
+                         (floor height 3))))
+  t)
+
+(define-simple-dialog create-cursors (dialog "Cursors")
+  (let ((spinner (make-instance 'spin-button 
+                 :adjustment (adjustment-new 
+                              0 0 
+                              (1- (enum-int :last-cursor 'gdk:cursor-type))
+                              2 10 0)))
+       (drawing-area (make-instance 'drawing-area
+                      :width-request 80 :height-request 80
+                      :events '(:exposure-mask :button-press-mask)))
+       (label (make-instance 'label :label "XXX")))
+
+    (signal-connect drawing-area 'expose-event #'cursor-expose :object t)
+
+    (signal-connect drawing-area 'button-press-event
+     #'(lambda (event)
+        (case (gdk:event-button event)
+          (1 (spin-button-spin spinner :step-forward 0.0))
+          (3 (spin-button-spin spinner :step-backward 0.0)))
+        t))
 
-;; (defun set-cursor (spinner drawing-area label)
-;;   (let ((cursor
-;;      (glib:int-enum
-;;       (logand (clamp (spin-button-value-as-int spinner) 0 152) #xFE)
-;;       'gdk:cursor-type)))   
-;;     (setf (label-text label) (string-downcase cursor))
-;;     (setf (widget-cursor drawing-area) cursor)))
-    
+    (signal-connect drawing-area 'scroll-event
+     #'(lambda (event)
+        (case (gdk:event-direction event)
+          (:up (spin-button-spin spinner :step-forward 0.0))
+          (:down (spin-button-spin spinner :step-backward 0.0)))
+        t))
+
+    (signal-connect spinner 'changed
+     #'(lambda ()
+        (set-cursor spinner drawing-area label)))
 
-; (define-standard-dialog create-cursors "Cursors"
-;   (setf (container-border-width main-box) 10)
-;   (setf (box-spacing main-box) 5)
-;   (let* ((hbox (hbox-new nil 0))
-;       (label (create-label "Cursor Value : "))
-;       (adj (adjustment-new 0 0 152 2 10 0))
-;       (spinner (spin-button-new adj 0 0)))
-;     (setf (container-border-width hbox) 5)
-;     (box-pack-start main-box hbox nil t 0)
-;     (setf (misc-xalign label) 0)
-;     (setf (misc-yalign label) 0.5)
-;     (box-pack-start hbox label nil t 0)
-;     (box-pack-start hbox spinner t t 0)
-
-;     (let ((frame (make-frame
-;                :shadow-type :etched-in
-;                :label-xalign 0.5
-;                :label "Cursor Area"
-;                :border-width 10
-;                :parent main-box
-;                :visible t))
-;        (drawing-area (drawing-area-new)))
-;       (setf (widget-width drawing-area) 80)
-;       (setf (widget-height drawing-area) 80)
-;       (container-add frame drawing-area)
-;       (signal-connect
-;        drawing-area 'expose-event
-;        #'(lambda (event)
-;         (declare (ignore event))
-;         (multiple-value-bind (width height)
-;             (drawing-area-size drawing-area)
-;           (let* ((drawable (widget-window drawing-area))
-;                  (style (widget-style drawing-area))
-;                  (white-gc (style-get-gc style :white))
-;                  (gray-gc (style-get-gc style :background :normal))
-;                  (black-gc (style-get-gc style :black)))
-;             (gdk:draw-rectangle
-;              drawable white-gc t 0 0 width (floor height 2))
-;             (gdk:draw-rectangle
-;              drawable black-gc t 0 (floor height 2) width (floor height 2))
-;             (gdk:draw-rectangle
-;              drawable gray-gc t (floor width 3) (floor height 3)
-;              (floor width 3) (floor height 3))))
-;           t))
-;       (setf (widget-events drawing-area) '(:exposure :button-press))
-;       (signal-connect
-;        drawing-area 'button-press-event
-;        #'(lambda (event)
-;         (when (and
-;                (eq (gdk:event-type event) :button-press)
-;                (or
-;                 (= (gdk:event-button event) 1)
-;                 (= (gdk:event-button event) 3)))
-;           (spin-button-spin
-;            spinner
-;            (if (= (gdk:event-button event) 1)
-;                :step-forward
-;              :step-backward)
-;            0)
-;           t)))
-;       (widget-show drawing-area)
-
-;     (let ((label (make-label
-;                :visible t
-;                :label "XXX"
-;                :parent main-box)))
-;       (setf (box-child-expand-p #|main-box|# label) nil)
-;       (signal-connect
-;        spinner 'changed
-;        #'(lambda ()
-;         (set-cursor spinner drawing-area label)))
-
-;       (widget-realize drawing-area)
-;       (set-cursor spinner drawing-area label)))))
+    (make-instance 'v-box
+     :parent dialog :border-width 10 :spacing 5 :show-all t
+     :child (list
+            (make-instance 'h-box
+             :border-width 5
+             :child (list
+                     (make-instance 'label :label "Cursor Value : ")
+                     :expand nil)
+             :child spinner)
+            :expand nil)
+     :child (make-instance 'frame
+;           :shadow-type :etched-in
+            :label "Cursor Area" :label-xalign 0.5 :border-width 10
+            :child drawing-area)
+     :child (list label :expand nil))
 
+    (widget-realize drawing-area)
+    (set-cursor spinner drawing-area label)))
 
 
 ;;; Dialog
   (dialog-add-button dialog "gtk-cancel" #'widget-destroy :object t)
   (dialog-add-button dialog "gtk-ok" 
    #'(lambda ()
-       (format t "Selected file: ~A~%" (file-chooser-filename dialog))
+       (if (slot-boundp dialog 'filename)
+          (format t "Selected file: ~A~%" (file-chooser-filename dialog))
+        (write-line "No files selected"))
        (widget-destroy dialog))))
 
 
@@ -665,38 +655,32 @@ This one is underlined (こんにちは) in quite a funky fashion"
 
 ;;; Layout
 
-;; (defun layout-expose (layout event)
-;;   (with-slots (window x-offset y-offset) layout
-;;     (with-slots (x y width height) event
-;;       (let ((imin (truncate (+ x-offset x) 10))
-;;         (imax (truncate (+ x-offset x width 9) 10))
-;;         (jmin (truncate (+ y-offset y) 10))
-;;         (jmax (truncate (+ y-offset y height 9) 10)))
-;;     (declare (fixnum imin imax jmin jmax))
-;;     (gdk:window-clear-area window x y width height)
-
-;;     (let ((window (layout-bin-window layout))
-;;           (gc (style-get-gc (widget-style layout) :black)))
-;;       (do ((i imin (1+ i)))
-;;           ((= i imax))
-;;         (declare (fixnum i))
-;;         (do ((j jmin (1+ j)))
-;;             ((= j jmax))
-;;           (declare (fixnum j))
-;;           (unless (zerop (mod (+ i j) 2))
-;;             (gdk:draw-rectangle
-;;              window gc t
-;;              (- (* 10 i) x-offset) (- (* 10 j) y-offset)
-;;              (1+ (mod i 10)) (1+ (mod j 10))))))))))
-;;   t)
-
+(defun layout-expose (layout event)
+  (when (eq (gdk:event-window event) (layout-bin-window layout))
+    (with-slots (gdk:x gdk:y gdk:width gdk:height) event
+      (let ((imin (truncate gdk:x 10))
+           (imax (truncate (+ gdk:x gdk:width 9) 10))
+           (jmin (truncate gdk:y 10))
+           (jmax (truncate (+ gdk:y gdk:height 9) 10)))
+
+       (let ((window (layout-bin-window layout))
+             (gc (style-black-gc (widget-style layout))))
+         (loop
+          for i from imin below imax
+          do (loop 
+              for j from jmin below jmax
+              unless (zerop (mod (+ i j) 2))
+              do (gdk:draw-rectangle
+                  window gc t (* 10 i) (* 10 j) 
+                  (1+ (mod i 10)) (1+ (mod j 10)))))))))
+  nil)
 
 (define-toplevel create-layout (window "Layout" :default-width 200
                                                :default-height 200)
   (let ((layout (make-instance 'layout
                 :parent (make-instance 'scrolled-window :parent window)
                 :width 1600 :height 128000 :events '(:exposure-mask)
-;;              :signal (list 'expose-event #'layout-expose :object t)
+                :signal (list 'expose-event #'layout-expose :object t)
                 )))
 
     (with-slots (hadjustment vadjustment) layout
@@ -836,7 +820,9 @@ This one is underlined (こんにちは) in quite a funky fashion"
            (menu-shell-append menu menu-item)
            (when (= i 3)
              (setf (widget-sensitive-p menu-item) nil))
-           (setf (menu-item-submenu menu-item) (create-menu (1- depth) t)))))
+           (let ((submenu (create-menu (1- depth) t)))
+             (when submenu
+               (setf (menu-item-submenu menu-item) submenu))))))
       menu)))
 
 
@@ -873,7 +859,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
 
 ;;; Notebook
 
-(defun create-notebook-page (notebook page-num)
+(defun create-notebook-page (notebook page-num book-closed)
   (let* ((title (format nil "Page ~D" page-num))
         (page (make-instance 'frame :label title :border-width 10))
         (v-box (make-instance 'v-box 
@@ -915,12 +901,12 @@ This one is underlined (こんにちは) in quite a funky fashion"
     (let ((label-box (make-instance 'h-box 
                      :show-all t
                      :child-args '(:expand nil)
-                     :child (make-instance 'image :pixmap book-closed-xpm)
+                     :child (make-instance 'image :pixbuf book-closed)
                      :child (make-instance 'label :label title)))
          (menu-box (make-instance 'h-box 
                     :show-all t
                     :child-args '(:expand nil)
-                    :child (make-instance 'image :pixmap book-closed-xpm)
+                    :child (make-instance 'image :pixbuf book-closed)
                     :child (make-instance 'label :label title))))
 
       (widget-show-all page)
@@ -929,24 +915,26 @@ This one is underlined (こんにちは) in quite a funky fashion"
 
 (define-simple-dialog create-notebook (dialog "Notebook")
   (let ((main (make-instance 'v-box :parent dialog)))
-    (let ((notebook (make-instance 'notebook 
+    (let ((book-open (gdk:pixbuf-new-from-xpm-data book-open-xpm))
+         (book-closed (gdk:pixbuf-new-from-xpm-data book-closed-xpm))
+         (notebook (make-instance 'notebook 
                     :border-width 10 :tab-pos :top :parent main)))
-      (flet ((set-image (page func xpm)
-              (image-set-from-pixmap-data 
-               (first (container-children (funcall func notebook page)))
-               xpm)))       
+      (flet ((set-image (page func pixbuf)
+              (setf
+               (image-pixbuf 
+                (first (container-children (funcall func notebook page))))
+               pixbuf)))
        (signal-connect notebook 'switch-page
         #'(lambda (pointer page)
             (declare (ignore pointer))
             (unless (eq page (notebook-current-page-num notebook))
-              (set-image page #'notebook-menu-label book-open-xpm)
-              (set-image page #'notebook-tab-label book-open-xpm)
-            
+              (set-image page #'notebook-menu-label book-open)
+              (set-image page #'notebook-tab-label book-open)
               (let ((curpage (notebook-current-page notebook)))
-                (when curpage
-                  (set-image curpage #'notebook-menu-label book-closed-xpm)
-                  (set-image curpage #'notebook-tab-label book-closed-xpm)))))))         
-      (loop for i from 1 to 5 do (create-notebook-page notebook i))
+                (when curpage
+                  (set-image curpage #'notebook-menu-label book-closed)
+                  (set-image curpage #'notebook-tab-label book-closed)))))))
+      (loop for i from 1 to 5 do (create-notebook-page notebook i book-closed))
 
       (make-instance 'h-separator :parent (list main :expand nil :padding 10))
        
@@ -977,7 +965,6 @@ This one is underlined (こんにちは) in quite a funky fashion"
        :child-args '(:expand nil)
        :child (make-instance 'label :label "Notebook Style: ")
        :child (let ((scrollable-p nil)) 
-               ;; option menu is deprecated, we should use combo-box
                (make-instance 'combo-box
                 :content '("Standard" "No tabs" "Scrollable") :active 0
                 :signal (list 'changed
@@ -1003,7 +990,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
                                   (setf (notebook-show-tabs-p notebook) t)
                                   (setf (notebook-scrollable-p notebook) t)
                                   (loop for i from 6 to 15 
-                                   do (create-notebook-page notebook i))))))
+                                   do (create-notebook-page notebook i book-closed))))))
                          :object t)))
        :child (make-instance 'button
               :label "Show all Pages"
@@ -1035,64 +1022,40 @@ This one is underlined (こんにちは) in quite a funky fashion"
 ;;; Panes
 
 (defun toggle-resize (child)
-  (let* ((paned (widget-parent child))
-        (is-child1-p (eq child (paned-child1 paned))))
-    (multiple-value-bind (child resize shrink)
-       (if is-child1-p
-           (paned-child1 paned)
-         (paned-child2 paned))
-      (container-remove paned child)
-      (if is-child1-p
-         (paned-pack1 paned child (not resize) shrink)
-       (paned-pack2 paned child (not resize) shrink)))))
+  (setf (paned-child-resize-p child) (not (paned-child-resize-p child))))
 
 (defun toggle-shrink (child)
-  (let* ((paned (widget-parent child))
-        (is-child1-p (eq child (paned-child1 paned))))
-    (multiple-value-bind (child resize shrink)
-       (if is-child1-p
-           (paned-child1 paned)
-         (paned-child2 paned))
-      (container-remove paned child)
-      (if is-child1-p
-         (paned-pack1 paned child resize (not shrink))
-       (paned-pack2 paned child resize (not shrink))))))
+  (setf (paned-child-shrink-p child) (not (paned-child-shrink-p child))))
 
 (defun create-pane-options (paned frame-label label1 label2)
-  (let* ((frame (make-instance 'frame :label frame-label :border-width 4))
-        (table (make-instance 'table :n-rows 3 :n-columns 2 :homogeneous t 
-                                     :parent frame)))
-
+  (let* ((table (make-instance 'table :n-rows 3 :n-columns 2 :homogeneous t)))
     (table-attach table (create-label label1) 0 1 0 1 :options '(:expand :fill))
     (let ((check-button (make-instance 'check-button :label "Resize")))
       (table-attach table check-button 0 1 1 2 :options '(:expand :fill))
-      (signal-connect
-       check-button 'toggled #'toggle-resize :object (paned-child1 paned)))
-    (let ((check-button (make-instance 'check-button :label "Shrink")))
+      (signal-connect check-button 'toggled 
+       #'toggle-resize :object (paned-child1 paned)))
+    (let ((check-button (make-instance 'check-button :label "Shrink" :active t)))
       (table-attach table check-button 0 1 2 3 :options '(:expand :fill))
-      (setf (toggle-button-active-p check-button) t)
-      (signal-connect
-       check-button 'toggled #'toggle-shrink :object (paned-child1 paned)))
+      (signal-connect check-button 'toggled 
+       #'toggle-shrink :object (paned-child1 paned)))
 
     (table-attach table (create-label label2) 1 2 0 1 :options '(:expand :fill))
-    (let ((check-button (make-instance 'check-button :label "Resize")))
+    (let ((check-button (make-instance 'check-button :label "Resize" :active t)))
       (table-attach table check-button 1 2 1 2 :options '(:expand :fill))
-      (setf (toggle-button-active-p check-button) t)
-      (signal-connect
-       check-button 'toggled #'toggle-resize :object (paned-child2 paned)))
-    (let ((check-button (make-instance 'check-button :label "Shrink")))
+      (signal-connect check-button 'toggled 
+       #'toggle-resize :object (paned-child2 paned)))
+    (let ((check-button (make-instance 'check-button :label "Shrink" :active t)))
       (table-attach table check-button 1 2 2 3 :options '(:expand :fill))
-      (setf (toggle-button-active-p check-button) t)
-      (signal-connect
-       check-button 'toggled #'toggle-shrink :object (paned-child2 paned)))
-    frame))
+      (signal-connect check-button 'toggled
+       #'toggle-shrink :object (paned-child2 paned)))
+    (make-instance 'frame :label frame-label :border-width 4 :child table)))
 
 (define-toplevel create-panes (window "Panes")
   (let* ((hpaned (make-instance 'h-paned
                  :child1 (make-instance 'frame
                           :width-request 60 :height-request 60
                           :shadow-type :in 
-                          :child (make-instance 'buttun :label "Hi there"))
+                          :child (make-instance 'button :label "Hi there"))
                  :child2 (make-instance 'frame                     
                           :width-request 80 :height-request 60
                           :shadow-type :in)))
@@ -1113,7 +1076,29 @@ This one is underlined (こんにちは) in quite a funky fashion"
 
 ;;; Progress bar
 
-     
+(define-simple-dialog create-progress-bar (dialog "Progress Bar")
+  (let* ((progress (make-instance 'progress-bar :pulse-step 0.05))
+        (activity-mode-button (make-instance 'check-button 
+                               :label "Activity mode"))
+        (timer (timeout-add 100
+                #'(lambda ()
+                    (if (toggle-button-active-p activity-mode-button)
+                        (progress-bar-pulse progress)
+                      (let ((fract (+ (progress-bar-fraction progress) 0.01)))
+                        (setf                
+                         (progress-bar-fraction progress)
+                         (if (> fract 1.0)
+                             0.0
+                           fract))))
+                    t))))
+
+    (make-instance 'v-box
+     :parent dialog :border-width 10 :spacing 10 :show-all t
+     :child progress
+     :child activity-mode-button)
+
+    (signal-connect dialog 'destroy 
+     #'(lambda () (when timer (timeout-remove timer))))))
 
 
 ;;; Radio buttons
@@ -1142,7 +1127,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
 (define-simple-dialog create-reparent (dialog "Reparent")
   (let ((main (make-instance 'h-box 
               :homogeneous t :spacing 10 :border-width 10 :parent dialog))
-       (label (make-instance 'label :label "Hellow World")))
+       (label (make-instance 'label :label "Hello World")))
 
     (flet ((create-frame (title)
             (let* ((frame (make-instance 'frame :label title :parent main))
@@ -1640,10 +1625,8 @@ This one is underlined (こんにちは) in quite a funky fashion"
 
 ;;; Toolbar test
 
-;; TODO: style properties
 (define-toplevel create-toolbar (window "Toolbar test" :resizable nil)
   (let ((toolbar (make-instance 'toolbar :parent window)))
-;    (setf (toolbar-relief toolbar) :none)
 
     ;; Insert a stock item
     (toolbar-append toolbar "gtk-quit"
@@ -1758,7 +1741,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
       (make-instance 'v-box
        :parent dialog :border-width 10 :spacing 10 :show-all t
        :child (create-button "button1" "This is button 1" "ContextHelp/button/1")
-       :child (create-button "button2" "This is button 2. This is also a really long tooltip which probably won't fit on a single line and will therefore need to be wrapped. Hopefully the wrapping will work correctly." "ContextHelp/button/2")))))
+       :child (create-button "button2" "This is button 2. This is also has a really long tooltip which probably won't fit on a single line and will therefore need to be wrapped. Hopefully the wrapping will work correctly." "ContextHelp/button/2")))))
 
 
 ;;; UI Manager
@@ -1850,7 +1833,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
            ("calendar" create-calendar)
            ("check buttons" create-check-buttons)
            ("color selection" create-color-selection)
-;;         ("cursors" #|create-cursors|#)
+           ("cursors" create-cursors)
            ("dialog" create-dialog)
 ;; ;       ("dnd")
            ("entry" create-entry)
@@ -1860,7 +1843,6 @@ This one is underlined (こんにちは) in quite a funky fashion"
 ;;         ("font selection")
 ;;         ("handle box" create-handle-box)
            ("image" create-image)
-;;         ("item factory")
            ("labels" create-labels)
            ("layout" create-layout)
            ("list" create-list)
@@ -1868,7 +1850,7 @@ This one is underlined (こんにちは) in quite a funky fashion"
 ;;         ("modal window")
            ("notebook" create-notebook)
            ("panes" create-panes)
-;;         ("progress bar" #|create-progress-bar|#)
+           ("progress bar" create-progress-bar)
            ("radio buttons" create-radio-buttons)
            ("range controls" create-range-controls)
 ;;         ("rc file")
@@ -1905,6 +1887,11 @@ This one is underlined (こんにちは) in quite a funky fashion"
                       :signal (list 'clicked #'widget-destroy 
                                     :object main-window)))) 
 
+    (let ((icon (gdk:pixbuf-load #p"clg:examples;gtk.png")))
+      (setf 
+       (window-icon main-window) 
+       (gdk:pixbuf-add-alpha icon t 254 254 252)))
+
     ;; Main box
     (make-instance 'v-box
      :parent main-window