#lang scheme/gui
(require "properties.ss"
"mred-plugin.ss"
"tooltip.ss"
"widgets.ss"
"mreddesigner-misc.ss"
"mreddesigner-properties-widget.ss"
"default-values.ss"
)
(define/provide (field-id-properties->widgets parent field-props)
(dict-map field-props
(λ(field-id field-id-prop)
(and (not (send field-id-prop get-hidden))
(make-property-widget parent field-id-prop)))
))
(define/provide (make-property-widget parent prop)
(let ([pwig
(let* ([val (send prop get-value)]
[common%% (λ(c%)(class c%
(super-new [parent parent]
[prop prop])))]
[pwig-text (λ(val->text text->val)
(new (common%% pwig:text-field%)
[val->text val->text]
[text->val text->val]))]
)
(cond [(is-a? prop prop:atom%)
(cond [(boolean? val) (new (common%% pwig:check-box%))]
[(string? val) (pwig-text (λ(x)x) (λ(x)x))]
[(symbol? val) (pwig-text symbol->string string->symbol)]
[(number? val) (pwig-text number->string (λ(x)(or (string->number x) 0)))]
[(list? val) (new (common%% pwig:list%))]
)]
[(is-a? prop prop:boolean%) (new (common%% pwig:check-box%))]
[(is-a? prop prop:file%) (new (common%% pwig:file%))]
[(is-a? prop prop:field-id%) (new (common%% pwig:field-id%))]
[(is-a? prop prop:one-of%) (new (common%% pwig:one-of%))]
[(is-a? prop prop:some-of%) (new (common%% pwig:some-of%))]
[(is-a? prop prop:group%) (new (common%% pwig:group%))]
[(is-a? prop prop:popup%) (new (common%% pwig:popup%))]
[(is-a? prop prop:code%) (new (common%% pwig:code%))]
[(is-a? prop prop:font%) (new (common%% pwig:font%))]
[(is-a? prop prop:proc%) (new (common%% pwig:proc%))]
[else (printf "make-property-widget: Don't know what to do with ~a\n"
prop)
#f])
)])
(when (is-a? pwig property-widget<%>)
(send pwig update))
pwig
))
(define/provide property-widget<%>
(interface ()))
(define/provide (property-widget%% c%)
(class* c% (property-widget<%>)
(init-field prop)
(super-new [vert-margin 0])
(send prop set-update-callback
(λ(prop)(send this update prop)))
(define/public (update [prop prop]) #f)
(define/public (commit) (printf "commit\n"))
))
(define pwig:field-id%
(class (property-widget%% horizontal-panel%)
(inherit-field prop)
(super-new [alignment '(center top)])
(define cb
(new (tooltip%% check-box%) [parent this]
[tooltip-text "Is this field an option in the generated code?"]
[label ""] [value (send prop get-option)]
[vert-margin 0]
[horiz-margin 0]
[min-width 0] [min-height 0]
[stretchable-width #f]
[stretchable-height #f]
))
(new text-field% [parent this]
[label ""]
[horiz-margin 0]
[vert-margin 0]
[min-width 120]
[enabled #f]
[stretchable-width #f]
[init-value (to-string (send prop get-field-id))])
(define prop-widget
(make-property-widget this (send prop get-prop)))
(define/override (commit)
(send prop set-option (send cb get-value))
(send prop-widget commit)
)
))
(define pwig:check-box%
(class (property-widget%% check-box%)
(inherit-field prop)
(super-new [label ""]
[stretchable-width #t]
[stretchable-height #t]
[horiz-margin 2]
)
(send this set-label
(if (field-bound? label prop)
(get-field label prop) ""))
(define/override (update [prop prop])
(send this set-value (send prop get-value)))
(define/override (commit)
(send prop set-value (send this get-value)))
))
(define pwig:text-field%
(class (property-widget%% text-field%)
(inherit-field prop)
(init-field text->val val->text)
(super-new [label ""]
[horiz-margin 0]
[stretchable-width #t]
)
(define/override (update [prop prop])
(send this set-value (val->text (send prop get-value))))
(define/override (commit)
(send prop set-value (text->val (send this get-value))))
))
(define pwig:file%
(class (property-widget%% file-button%)
(inherit-field prop)
(super-new [label "Choose Image..."]
[stretchable-width #t]
)
(define/override (update [prop prop])
(send this set-value (send prop get-value)))
(define/override (commit)
(send prop set-value (->string (send this get-value))))
))
(define pwig:one-of%
(class (property-widget%% choice%)
(inherit-field prop)
(super-new [label""]
[stretchable-width #t]
[choices '()]
)
(for-each (λ(p)(send this append (to-string p)))
(send prop get-prop-choices))
(define/override (update [prop prop])
(send this set-string-selection
(to-string (send prop get-value))))
(define/override (commit)
(send prop set-value
(list-ref (send prop get-prop-choices)
(send this get-selection))))
))
(define pwig:some-of%
(class (property-widget%% vertical-panel%)
(inherit-field prop)
(super-new [alignment '(left top)]
[stretchable-width #t])
(define gbox (new group-box-panel% [parent this]
[label ""]
[alignment '(left top)]
[stretchable-width #t]
[vert-margin 0]
[horiz-margin 2]))
(define check-boxes
(let ([vals (send prop get-value)]
[choices (send prop get-choices)])
(map (λ(c)(new check-box% [label (to-string c)]
[parent gbox]
[value (member c vals)]
))
choices)))
(define/override (update [prop prop])
(let ([vals (send prop get-value)])
(for-each (λ(v cb)(send cb set-value
(if (member v vals) #t #f)))
(send prop get-choices)
check-boxes)))
(define/override (commit)
(let ([choices (send prop get-choices)])
(send prop set-value
(append-map
(λ(cb c)(if (send cb get-value)
(list c)
'()))
check-boxes
choices))))
))
(define pwig:group%
(class (property-widget%% vertical-panel%)
(inherit-field prop)
(super-new [stretchable-width #t]
[alignment '(left top)])
(field [widgets
(map (λ(p)(make-property-widget this p))
(send prop get-props))])
(define/override (commit)
(for-each (λ(w)(send w commit)) widgets))
))
(define pwig:popup%
(class (property-widget%% button%)
(inherit-field prop)
(super-new
[label " ... "]
[stretchable-width #t]
[callback (λ(b e)
(let-values ([(h) (send this get-height)]
[(w) (send this get-width)]
[(x y) (send this client->screen
(send this get-x)
(send this get-y))
])
(send dial move (- x w) y)
(send dial show #t)))]
)
(define dial (new dialog% [label ""]
[parent (send this get-top-level-window)]
[min-width (send this get-width)]
))
(field [sub-widget (make-property-widget dial (send prop get-prop))])
(new button% [parent dial]
[label "Ok"]
[style '(border)]
[callback (λ(b e)(send dial show #f)
)])
(define/override (commit)
(send sub-widget commit))
))
(define pwig:list%
(class (property-widget%% button%)
(inherit-field prop)
(super-new
[label " ... "]
[stretchable-width #t]
[callback (λ _
(send edit-list show #t)
)]
)
(define edit-list
(new property-edit-list% [parent (send this get-top-level-window)]
[label "Edit List values"]
[empty-allowed? #f]
))
(define/override (update [prop prop])
(let ([vals (send prop get-value)])
(send edit-list set-choices (send prop get-value)))
(void))
(define/override (commit)
(let ([vals (send edit-list get-choices)])
(when vals
(send prop set-value vals)
)))
))
(define pwig:code%
(class (property-widget%% pane%)
(super-new [stretchable-width #t])
))
(define pwig:proc%
(class (property-widget%% panel%)
(inherit-field prop)
(super-new [stretchable-width #t])
(field [sub-widget
(make-property-widget this (send prop get-prop))])
(define/override (commit)
(send sub-widget commit))
))
(define pwig:font%
(class (property-widget%% button%)
(inherit-field prop)
(super-new [stretchable-width #t]
[label " Choose Font..."]
[callback (λ _
(let* ([ft the-font] [new-ft (get-font-from-user #f #f ft)])
(when new-ft
(set! the-font new-ft))))]
)
(field [the-font (send prop get-value)])
(define/override (commit)
(send (send prop get-prop) set-value
(font->list the-font)))
))