diff --git a/.gitignore b/.gitignore index bd3ad3f..07d57cf 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ rktwebview/build/ /scrbl/*.js *.bak /scrbl/*.html +/doc diff --git a/example1/example.rkt b/example1/example.rkt index e217a31..1bcdcfc 100644 --- a/example1/example.rkt +++ b/example1/example.rkt @@ -235,6 +235,17 @@ (send this update-counter) (send this set-folder my-dir) + (when (eq? my-page 'first) + (let ((language-select + (send this element 'sel-lang))) + (send language-select + on-change! + (λ (value) + (displayln + (format + "Selected language: ~a" + (send language-select get))))))) + (ww-debug "CONNECTING BUTTONS") (send this bind! 'dialog-button 'click (λ (el evt data) (send this prefs))) diff --git a/scrbl/wv-input.scrbl b/scrbl/wv-input.scrbl index 4ea0581..7b957b2 100644 --- a/scrbl/wv-input.scrbl +++ b/scrbl/wv-input.scrbl @@ -384,4 +384,46 @@ Writes @racket[v] by delegating to: (webview-set-value! wv element-id v) ] } -} \ No newline at end of file +} + +@section{Class: wv-input/select%} + +@defclass[wv-input/select% wv-element% ()]{ + +Wrapper for an HTML select element. + +@defconstructor[([window (is-a?/c wv-window%)] + [element-id symbol?])]{ + +Creates a select-element wrapper. +} + +@defmethod[(get) (or/c string? boolean?)]{ + +Returns the value of the selected option by delegating to +@racket[webview-value]. +} + +@defmethod[(set! [v (or/c symbol? string? number? boolean? + g:date? g:time? g:datetime? rgba?)]) + any/c]{ + +Selects the option whose value equals @racket[v] by delegating to +@racket[webview-set-value!]. +} + +@defmethod[(set-options! [options list?] + [selected any/c #f]) + any/c]{ + +Replaces the option elements. Each entry in @racket[options] is a list +containing a value and a label. When @racket[selected] is not @racket[#f], +the option with that value is selected. +} + +@defmethod[(on-change! [callback-func procedure?]) any/c]{ + +Binds @racket[callback-func] to the HTML @tt{change} event. The callback +receives the newly selected value. +} +} diff --git a/scrbl/wv-window.scrbl b/scrbl/wv-window.scrbl index 46b1afd..dff0042 100644 --- a/scrbl/wv-window.scrbl +++ b/scrbl/wv-window.scrbl @@ -133,6 +133,7 @@ following classes is instantiated: @item{@racket[wv-input/check%] for @racket['checkbox]} @item{@racket[wv-input/radio%] for @racket['radio]} @item{@racket[wv-input/color%] for @racket['color]} + @item{@racket[wv-input/select%] for an HTML @tt{select} element} @item{@racket[wv-element%] otherwise}] Newly created wrappers receive this window through the @racket[window] init @@ -537,4 +538,4 @@ This module also re-exports the public APIs from: @item{@racketmodname[rgba]} @item{@racketmodname[wv-settings]}] -It also re-exports @racket[webview-version]. \ No newline at end of file +It also re-exports @racket[webview-version]. diff --git a/wv-input.rkt b/wv-input.rkt index 3725845..600263e 100644 --- a/wv-input.rkt +++ b/wv-input.rkt @@ -1,6 +1,7 @@ #lang racket/base (require racket/class + xml "wv-element.rkt" "racket-webview.rkt" ) @@ -14,7 +15,8 @@ wv-input/range% wv-input/check% wv-input/radio% - wv-input/color%) + wv-input/color% + wv-input/select%) (define-syntax mk-cl @@ -55,3 +57,41 @@ (mk-cl wv-input/check% webview-set-value! webview-value/boolean) (mk-cl wv-input/radio% webview-set-value! webview-value/boolean) (mk-cl wv-input/color% webview-set-value! webview-value/color) + +(define wv-input/select% + (class wv-element% + (inherit-field window + element-id) + + (super-new) + + (define wv (hash-ref (send window info) 'wv)) + + (define/public (get) + (webview-value wv element-id)) + + (define/public (set! value) + (webview-set-value! wv element-id value)) + + (define/public (set-options! options [selected #f]) + (send this + set-innerHTML! + (apply + string-append + (for/list ((option (in-list options))) + (let ((value (car option)) + (label (cadr option))) + (xexpr->string + (list 'option + (list + (list 'value + (format "~a" value))) + (format "~a" label))))))) + (when selected + (send this set! selected))) + + (define/public (on-change! callback-func) + (send window bind! element-id 'change + (λ (element event data) + (callback-func (send this get))))) + )) diff --git a/wv-window.rkt b/wv-window.rkt index d8134f0..bd44c8e 100644 --- a/wv-window.rkt +++ b/wv-window.rkt @@ -100,7 +100,7 @@ " return a;\n" " } else { \n" " if (a === null) {\n" - " return false;\n" + " return el.tagName;\n" " } else {\n" " return a;\n" " }" @@ -124,6 +124,7 @@ ((eq? type 'checkbox) wv-input/check%) ((eq? type 'radio) wv-input/radio%) ((eq? type 'color) wv-input/color%) + ((eq? type 'select) wv-input/select%) ((eq? type 'unknown) (begin (err-webview "Element with id '~a' not found" id) @@ -602,4 +603,3 @@ handler) ) - \ No newline at end of file