#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-for-each field-props
(λ(field-id field-id-prop)
(unless (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 string->number)]
[(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)])
)])
(when (is-a? pwig property-widget<%>)
(send pwig update))
))
(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 pwig:field-id%
(class (property-widget%% horizontal-panel%)
(inherit-field prop)
(super-new [alignment '(center top)])
(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]
[callback (λ (cb e) (send prop set-option (send cb get-value)))]
)
(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))])
(make-property-widget this (send prop get-prop))
))
(define pwig:check-box%
(class (property-widget%% check-box%)
(inherit-field prop)
(super-new [label ""]
[stretchable-width #t]
[stretchable-height #t]
[horiz-margin 2]
[callback (λ(cb e)(send prop set-value
(send this get-value)))]
)
(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 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]
[callback (λ(cb e)(send prop set-value
(text->val (send this get-value))))]
)
(define/override (update [prop prop])
(send this set-value (val->text (send prop get-value))))
))
(define pwig:file%
(class (property-widget%% file-button%)
(inherit-field prop)
(super-new [label "Choose Image..."]
[stretchable-width #t]
[callback (λ(file)(send prop set-value (->string file)))]
)
(define/override (update [prop prop])
(send this set-value (send prop get-value)))
))
(define pwig:one-of%
(class (property-widget%% choice%)
(inherit-field prop)
(super-new [label""]
[stretchable-width #t]
[choices '()]
[callback (λ(cb e)(send prop set-value
(list-ref (send prop get-prop-choices)
(send this get-selection))))]
)
(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 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)]
[callback (λ(cb e)
(send prop set-value
(append-map
(λ(cb c)(if (send cb get-value)
(list c)
'()))
check-boxes
choices)))]
))
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 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 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-prop (make-property-widget dial (send prop get-prop))])
(new button% [parent dial]
[label "Ok"]
[style '(border)]
[callback (λ(b e)(send dial show #f)
)])
))
(define pwig:list%
(class (property-widget%% button%)
(inherit-field prop)
(super-new
[label " ... "]
[stretchable-width #t]
[callback (λ(b e)
(send edit-list show #t)
(let ([vals (send edit-list get-choices)])
(when vals
(send prop set-value vals)
)))]
)
(define edit-list
(new property-edit-list% [parent (send this get-top-level-window)]
[label "Edit List values"]
[empty-allowed? #f]
))
(send edit-list set-choices (send prop get-value))
))
(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-prop
(make-property-widget this (send prop get-prop))])
))
(define pwig:font%
(class (property-widget%% button%)
(inherit-field prop)
(super-new [stretchable-width #t]
[label " Choose Font..."]
[callback (λ _
(let* ([ft (send prop get-value)] [new-ft (get-font-from-user #f #f ft)])
(send (send prop get-prop) set-value
(font->list (or new-ft ft)))))]
)
))