On this page:

16 Snip Canvases

 (require mrlib/snip-canvas) package: gui-lib


snip-canvas% : class?

  superclass: editor-canvas%

A canvas that contains a single snip.
Snips cannot be placed directly on dialogs, frames and panels. To use an interactive snip in a GUI, it must be inserted into an editor, which itself must be placed on a special canvas, which can be placed in a GUI container. To provide a seamless user experience, the editor should be enabled but not writable, not be able to receive focus, not have scrollbars, and other small details.
The snip-canvas% class handles these details, making it easy to use interactive snips as normal GUI elements.


(new snip-canvas% 
    [parent parent] 
    [make-snip make-snip] 
    [[style style] 
    [label label] 
    [horizontal-inset horizontal-inset] 
    [vertical-inset vertical-inset] 
    [enabled enabled] 
    [vert-margin vert-margin] 
    [horiz-margin horiz-margin] 
    [min-width min-width] 
    [min-height min-height] 
    [stretchable-width stretchable-width] 
    [stretchable-height stretchable-height]]) 
  (is-a?/c snip-canvas%)
  parent : 
(or/c (is-a?/c frame%) (is-a?/c dialog%)
      (is-a?/c panel%) (is-a?/c pane%))
  make-snip : ((integer-in 0 10000) (integer-in 0 10000) . -> . snip%)
  style : 
(listof (one-of/c 'no-border 'control-border 'combo
                  'resize-corner 'no-focus 'deleted
   = null
  label : (or/c label-string? false/c) = #f
  horizontal-inset : (integer-in 0 1000) = 5
  vertical-inset : (integer-in 0 1000) = 5
  enabled : any/c = #t
  vert-margin : (integer-in 0 1000) = 0
  horiz-margin : (integer-in 0 1000) = 0
  min-width : (integer-in 0 10000) = 0
  min-height : (integer-in 0 10000) = 0
  stretchable-width : any/c = #t
  stretchable-height : any/c = #t
Unlike instances of editor-canvas%, each instance of this class creates and manages its own editor. The editor contains one object: a snip% instance created by make-snip.

The make-snip function receives the requested width and height of the snip, which are calculated from the size of the snip canvas. It is called the first time the snip canvas is resized, which most likely coincides with the first time the snip canvas is shown. The snip is thus created lazily: only when needed, at the size needed. See on-size for more details and an example.

The style list is prepended with 'no-hscroll and 'no-vscroll before being passed to the editor-canvas% constructor. The other constructor arguments are passed untouched.


(send a-snip-canvas get-snip)  (or/c (is-a?/c snip%) #f)

Returns the wrapped snip, or #f if make-snip has not been called yet.


(send a-snip-canvas on-size width height)  void?

  width : (integer-in 0 10000)
  height : (integer-in 0 10000)
Overrides on-size in editor-canvas%.
This is called when the snip canvas is resized.

On the first call, on-size calls make-snip with width and height arguments respectively (max 0 (- width (* 2 horizontal-inset))) and (max 0 (- height (* 2 vertical-inset))). It then inserts the resulting snip into its editor.

On subsequent calls, on-size calls the snip’s resize method, calculating the width and height arguments the same way.

When a snip-canvas% instance is intended to wrap an existing snip% instance, make-snip should simply resize it and return it.

Example: functions from plot create snips and call a function similar to the following to place plots in a frame:
(define (make-snip-frame snip w h label)
  (define (make-snip width height)
    (send snip resize width height)
  (define frame
    (new frame%
         [label label]
         [width (+ 5 5 5 5 w)]
         [height (+ 5 5 5 5 h)]))
  (new snip-canvas%
       [parent frame]
       [make-snip make-snip]
       [horiz-margin 5] [vert-margin 5]
       [horizontal-inset 5] [vertical-inset 5])