guile-gtk-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: guile-gtk scribble demo


From: Marius Vollmer
Subject: Re: guile-gtk scribble demo
Date: Thu, 04 Nov 2004 15:51:38 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3 (gnu/linux)

Marcello Mathias Herreshoff <address@hidden> writes:

> I rewrote the GTK project's scribble.c in guile-gtk to familiarize
> myself with it and it occured to me that the guile-gtk project might
> want it as an example. 

Yes, thank you very much!  I have installed it in the guile-gtk-1.2
CVS.

I have also CCed the <address@hidden> list, so that more
people are aware of your program.

> I have documented it as best I can, and hope that it will be helpfull
> to people trying to understand the gdk module.
>
> It is attached to this email.  I hope it is usefull to you.
> -=+Marcello Mathias Herreshoff.
>
> #!/usr/bin/guile -s
> !#
> ;;;A translation of the GTK scribble example
> ;;;It features a resizable canvas that can be drawn on with the mouse
> ;;;by Marcello Mathias Herreshoff
> ;;;(C) 2004 GNU GPL
>
> (use-modules (gtk gtk) (gtk gdk))
>
> ;Variables holding the size of the pixmap & drawing area.
> (define width 200)
> (define height 200)
> (define brush-size 4)
>
> ;;The widgets:
> (define toplevel-window (gtk-window-new 'toplevel)) ; our toplevel window
> (define layout-box (gtk-vbox-new #f 0))
> (define drawing-area (gtk-drawing-area-new))  
> (define close-button (gtk-button-new-with-label "Close"))
> (define pixmap #f) ;to be created by recreate-pixmap
>
> ;We can't generate these until the window is visble,
> ; because we need the style object
> (define fore-gc #f)
> (define back-gc #f)
>
>
>
> ;Note: configure is GTKese for resize
> (define (configure-handler ev)
>   (recreate-pixmap))
>
> (define (recreate-pixmap)
>   (let ((widget (gtk-widget-window drawing-area))
>       (w (gtk-widget-allocation-width drawing-area))
>       (h (gtk-widget-allocation-height drawing-area)))
>     (set! width w)
>     (set! height h)
>     (set! pixmap (gdk-pixmap-new widget w h))
>     (gdk-draw-rectangle pixmap back-gc #t 0 0 w h))
>   (update-handler))
> ;Important note: we can't draw directly on the drawing area.
> ;the pixmap must use the drawing area's window.
>
>
> ;expose, that is, some other window is no longer covering us
> (define (expose-handler x)
>   (update-handler)) ;we just redraw everything.
>
>
> ;To redraw, we just write our backup pixmap onto the drawing area's window
> (define (update-handler) 
>   (gdk-draw-pixmap (gtk-widget-window drawing-area)
>                  back-gc pixmap 0 0 0 0 width height))
>
> ;How to draw on the pixmap:
> (define (draw-brush widget x y) 
>     (gdk-draw-rectangle pixmap fore-gc #t x y brush-size brush-size)
>     ;we just make a rectangle
>     (update-handler))
>
> ; if they click, call draw-brush with the exact position
> (define (click-handler ev)
>   (draw-brush drawing-area 
>             (inexact->exact (gdk-event-x ev))
>             (inexact->exact (gdk-event-y ev))))
>
> ;If they drag the mouse over the drawing area and the button is down,
> ;then we call draw-brush with the exact position
> (define (drag-handler ev)
>   (if (member 'button1-mask (gdk-event-state ev))
>       (draw-brush drawing-area
>             (inexact->exact (gdk-event-x ev))
>             (inexact->exact (gdk-event-y ev)))
>       #t))
>
> ;Set the events that the drawing-area can capture,
> ; so we can respond to click and drags.
> ;We need to to this before the widget is visible.
> (gtk-widget-set-events
>  drawing-area
>  '(button-press-mask pointer-motion-mask)) 
>
> ;Lay out the widgets
> (gtk-box-pack-start layout-box drawing-area)
> (gtk-box-pack-start layout-box close-button #f #f 0)
> (gtk-container-add toplevel-window layout-box)
>
> ;Set the size of the drawing area to something reasonable.
> (gtk-drawing-area-size drawing-area width height)
>
> ;make the window, and its contents visible
> (gtk-widget-show-all toplevel-window)
>
> ;Now we connect the events
> ;We need to do this things are visible, as some of these events might get
> ;called and panic.
> (gtk-signal-connect drawing-area "configure_event" configure-handler)
> (gtk-signal-connect drawing-area "expose_event" expose-handler)
> (gtk-signal-connect drawing-area "motion_notify_event" drag-handler)
> (gtk-signal-connect drawing-area "button_press_event" click-handler)
> (gtk-signal-connect close-button "clicked" gtk-exit)
>
> ;This also needs to happen after the window is visible, because
> ;we need its style
> (let ((style (gtk-widget-style toplevel-window)))
>   (set! fore-gc (gtk-style-fg-gc style 'normal))
>   (gdk-gc-set-foreground fore-gc "black") ;we draw in black
>   (set! back-gc (gtk-style-bg-gc style 'normal))
>   (gdk-gc-set-foreground back-gc "white") ;on a white background
> )
>
> ;We set up the pixmap
> (recreate-pixmap)
>
> (gtk-standalone-main toplevel-window) ;And finaly start the event loop.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]