Package: gtk

Class gtk-drawing-area

Superclasses

gtk-widget, gtk-buildable, g-object, common-lisp:standard-object, common-lisp:t

Documented Subclasses

None

Direct Slots

None

Details

The gtk-drawing-area widget is used for creating custom user interface elements. It is essentially a blank widget. You can draw on it. After creating a drawing area, the application may want to connect to:
  • Mouse and button press signals to respond to input from the user. Use the gtk-widget-add-events function to enable events you wish to receive.
  • The "realize" signal to take any necessary actions when the widget is instantiated on a particular display. Create GDK resources in response to this signal.
  • The "configure-event" signal to take any necessary actions when the widget changes size.
  • The "draw" signal to handle redrawing the contents of the widget.
Draw signals are normally delivered when a drawing area first comes onscreen, or when it is covered by another window and then uncovered. You can also force an expose event by adding to the "damage region" of the drawing area's window. The gtk-widget-queue-draw-area and gdk-window-invalidate-rect functions are equally good ways to do this. You will then get a draw signal for the invalid region.

To receive mouse events on a drawing area, you will need to enable them with the gtk-widget-add-events function. To receive keyboard events, you will need to set the can-focus property on the drawing area, and you should probably draw some user visible indication that the drawing area is focused. Use the gtk-widget-has-focus function in your expose event handler to decide whether to draw the focus indicator. See the function for one way to draw focus.

Example

The following example demonstrates using a drawing area to display a circle in the normal widget foreground color.

Note that GDK automatically clears the exposed area before sending the expose event, and that drawing is implicitly clipped to the exposed area. If you want to have a theme-provided background, you need to call the gtk-render-background function in your "draw" signal handler.
(defun example-drawing-area ()
  (within-main-loop
    (let ((window (make-instance 'gtk-window
                                 :type :toplevel
                                 :title "Example Drawing Area"
                                 :default-width 400
                                 :default-height 300))
          ;; Create the drawing area
          (area (make-instance 'gtk-drawing-area)))
      ;; Signal handler for the drawing area
      (g-signal-connect area "draw"
          (lambda (widget cr)
            (let* ((cr (pointer cr))
                   (width (gtk-widget-allocated-width widget))
                   (height (gtk-widget-allocated-height widget))
                   (context (gtk-widget-style-context widget))
                   (color (gtk-style-context-color context :focused)))
                ;; Set the color from the style context of the widget
                (gdk-cairo-set-source-rgba cr color)
                ;; Draw and fill a circle on the drawing area
                (cairo-arc cr
                           (/ width 2.0)
                           (/ height 2.0)
                           (- (/ (min width height) 2.0) 12)
                           0.0
                           (* 2.0 pi))
                (cairo-fill cr)
                ;; Destroy the Cairo context
                (cairo-destroy cr))))
      ;; Signal handler for the window to handle the signal "destroy"
      (g-signal-connect window "destroy"
                        (lambda (widget)
                          (declare (ignore widget))
                          (leave-gtk-main)))
      ;; Show the window
      (gtk-container-add window area)
      (gtk-widget-show-all window))))    
 

Inherited Slot Access Functions

See also

*2021-11-30