Skip to content

Instantly share code, notes, and snippets.

@alex-hhh
Created February 15, 2019 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-hhh/20f03dcf2f7f340d20b95cb36da39f61 to your computer and use it in GitHub Desktop.
Save alex-hhh/20f03dcf2f7f340d20b95cb36da39f61 to your computer and use it in GitHub Desktop.
Dynamic Item Selection
#lang racket/gui
(define font-faces (get-face-list))
(define my-combo-field%
(class combo-field%
(define (construct-menu)
(let ((menu (send this get-menu))
(prefix (send this get-value)))
(for ([item (in-list (send menu get-items))])
(send item delete))
(for ([face (in-list font-faces)] #:when (string-prefix? face prefix))
(new menu-item% [parent menu]
[label face]
[callback (lambda (c e) (send this set-value face))]))))
(define/override (on-popup event)
(super on-popup event)
(construct-menu))
(init)
(super-new)
))
(define f (new frame% [label "Hello"] [width 400] [height 600]))
(define option1 (new group-box-panel% [parent f]
[label "Option 1"]
[border 10]
[stretchable-height #f]))
(define my-combo (new my-combo-field% [parent option1] [label "A combo"] [choices '()]))
(define option2 (new group-box-panel% [parent f]
[label "Option 2"]
[border 10]))
(define my-lb #f)
(define (on-filter c e)
(define prefix (send c get-value))
(when my-lb
(define candidates (filter (lambda (face) (string-prefix? face prefix)) font-faces))
(send my-lb set candidates)))
(define my-text-field (new text-field% [parent option2] [label "Filter"] [callback on-filter]))
(set! my-lb (new list-box% [parent option2] [label "Fonts"] [choices font-faces]))
(send f show #t)
@alex-hhh
Copy link
Author

dynamic-selection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment