{"size": 4166, "ext": "scrbl", "max_stars_repo_path": "gui-doc/mrlib/scribblings/snip-canvas.scrbl", "max_stars_repo_name": "jjsimpso/gui", "max_stars_repo_stars_event_min_datetime": "2015-03-17T22:11:52.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-12T15:09:34.000Z", "max_issues_repo_path": "gui-doc/mrlib/scribblings/snip-canvas.scrbl", "max_issues_repo_name": "jjsimpso/gui", "max_issues_repo_issues_event_min_datetime": "2015-01-09T15:53:25.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T16:21:29.000Z", "max_forks_repo_path": "gui-doc/mrlib/scribblings/snip-canvas.scrbl", "max_forks_repo_name": "jjsimpso/gui", "max_forks_repo_forks_event_min_datetime": "2015-01-08T13:03:07.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-26T21:49:09.000Z"}
{"max_stars_count": 65, "max_issues_count": 231, "max_forks_count": 89, "avg_line_length": 44.3191489362, "max_line_length": 154, "alphanum_fraction": 0.6461833893}
#lang scribble/manual

@(require (for-label racket/base
                     racket/class
                     racket/gui/base
                     mrlib/snip-canvas
                     racket/contract))

@title[#:tag "snip"]{Snip Canvases}

@defmodule[mrlib/snip-canvas]

@defclass[snip-canvas% 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 @racket[snip-canvas%] class handles these details, making it easy to use interactive snips as normal GUI elements.

@defconstructor[([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
                                          'transparent)) 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 @racket[editor-canvas%], each instance of this class creates and manages its own editor.
The editor contains one object: a @racket[snip%] instance created by @racket[make-snip].

The @racket[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 @italic{lazily}: only when needed, at the size needed.
See @method[snip-canvas% on-size] for more details and an example.

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

@defmethod[(get-snip) (or/c (is-a?/c snip%) #f)]{
Returns the wrapped snip, or @racket[#f] if @racket[make-snip] has not been called yet.
}

@defmethod[#:mode override 
           (on-size [width (integer-in 0 10000)]
                    [height (integer-in 0 10000)])
           void?]{
This is called when the snip canvas is resized.

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

On subsequent calls, @method[snip-canvas% on-size] calls the snip's @method[snip% resize] method, calculating the width and height arguments the same way.

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

Example: functions from @racketmodname[plot #:indirect]
create snips and call a function similar to the following to place plots in a frame:
@racketblock[
(define (make-snip-frame snip w h label)
  (define (make-snip width height)
    (send snip resize width height)
    snip)
  
  (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])
  
  frame)]
}

} @; defclass snip-canvas%
