#lang scheme/gui
(require "mred-plugin.ss"
"plugin.ss"
"mreddesigner-misc.ss"
"mreddesigner-help.ss"
"tooltip.ss"
"templates.ss"
)
(define/provide toolbox-frame #f)
(define toolbox-frame-vertical-pane #f)
(define toolbox-plugin-button-callback #f)
(define lb-templates #f)
(define toolbox-frame%
(class frame%
(init-field on-close-callback)
(super-new)
(define closing? #f)
(define/augment (on-close)
(debug-printf "toolbox-frame%: on-close: closing:~a~n" closing?)
(unless closing?
(set! closing? #t)
(on-close-callback)))
))
(define/provide (make-toolbox-frame
#:exit-application-callback exit-application-callback
#:plugin-button-callback plugin-button-callback
#:generate-code-callback generate-code-callback
#:generate-code-to-console-callback generate-code-to-frame-callback
#:new-project-callback new-project-callback
#:load-project-callback load-project-callback
#:save-project-callback save-project-callback
#:close-project-callback close-project-callback
#:add-template-callback add-template-callback
#:save-template-callback save-template-callback
#:replace-template-callback replace-template-callback
#:delete-template-callback delete-template-callback
#:show-properties-callback show-properties-callback
#:show-hierarchy-callback show-hierarchy-callback
#:cut-callback cut-callback
#:copy-callback copy-callback
#:paste-callback paste-callback
[parent #f])
(set! toolbox-plugin-button-callback
plugin-button-callback)
(set! toolbox-frame
(new toolbox-frame%
[label application-name] [min-width 200]
[parent parent]
[x 5]
[y 5]
[on-close-callback exit-application-callback]
))
(let* ([menu (new menu-bar% [parent toolbox-frame])]
[make-menu (λ (label [help-str #f])
(new menu%
[parent menu]
[label label]
[help-string help-str]))]
[menu-file (make-menu "File")]
[menu-edit (make-menu "Edit")]
[menu-windows (make-menu "Windows")]
[menu-help (make-menu "Help")]
[current-menu (make-parameter menu-file)]
[make-menu-item (λ (label shortcut callback)
(new menu-item%
[parent (current-menu)]
[label label]
[shortcut shortcut]
[callback (λ _ (callback))]))]
[make-separator (λ ()
(new separator-menu-item%
[parent (current-menu)]))]
)
(current-menu menu-file)
(make-menu-item "&New Project" #\N new-project-callback)
(make-menu-item "&Open Project..." #\O load-project-callback)
(make-menu-item "&Save Project" #\S save-project-callback)
(make-menu-item "S&ave Project as..." 'f12 (λ _ (save-project-callback #t)))
(make-separator)
(make-menu-item "&Generate Scheme File..." 'f5 generate-code-callback)
(make-separator)
(make-menu-item "&Close Project" #\W close-project-callback)
(make-separator)
(make-menu-item "E&xit" #\Q exit-application-callback)
(current-menu menu-edit)
(make-menu-item "C&ut" #\X cut-callback)
(make-menu-item "&Copy" #\C copy-callback)
(make-menu-item "&Paste" #\V paste-callback)
(current-menu menu-windows)
(make-menu-item "Show/Hide &Properties" #f show-properties-callback)
(make-menu-item "Show/Hide &Hierarchy" #f show-hierarchy-callback)
(current-menu menu-help)
(make-menu-item "&Online Help" 'f1 help-online-help)
(make-menu-item "&PLT MrEd Help" #f help-mred-help)
(make-menu-item "&About MrEd Designer..." #f help-about-dialog)
)
(set! toolbox-frame-vertical-pane
(new vertical-pane%
[parent toolbox-frame]
[alignment '(right top)]
[border 3]))
(toolbox-frame-make-plugin-buttons)
(let* ([gb (new group-box-panel%
[label "Templates"]
[parent toolbox-frame-vertical-pane])]
[hp (new horizontal-panel% [parent gb]
[alignment '(center center)]
)]
[hp2 (new horizontal-panel% [parent gb]
[alignment '(center center)]
[stretchable-width #t]
)]
[hp3 (new horizontal-panel% [parent gb]
[alignment '(center center)]
[stretchable-width #t]
)]
)
(set! lb-templates
(new choice%
[parent hp]
[label #f]
[min-width 250]
[stretchable-width #t]
[choices '()]))
(toolbox-update-template-choices)
(new button%
[parent hp]
[label "Insert"]
[callback (λ _ (add-template-callback (get-selected-template)))]
)
(new button%
[parent hp2]
[label "Save"]
[callback (λ _ (save-template-callback
(get-text-from-user "Saving Template ..."
"Enter a name for the new template:")))]
)
(new button%
[parent hp2]
[label "Replace"]
[callback (λ _ (when (eq? 'yes (message-box "Replace?"
"Are you sure you want to replace the current template?"
#f '(yes-no)))
(replace-template-callback (get-selected-template))))]
)
(new button%
[parent hp2]
[label "Delete"]
[callback (λ _ (when (eq? 'yes (message-box "Delete?"
"Are you sure you want to delete the current template?"
#f '(yes-no)))
(delete-template-callback (get-selected-template))))]
)
)
(let* ([gbp (new group-box-panel%
[parent toolbox-frame-vertical-pane]
[label "Generate code..."])]
[hp (new horizontal-panel%
[parent gbp]
[alignment '(center center)]
)]
)
(new button%
[label "To frame"]
[parent hp]
[min-width 110]
[callback (λ _ (generate-code-to-frame-callback))])
(new button%
[label "To <project-id>.rkt"]
[parent hp]
[min-width 110]
[callback (λ _ (generate-code-callback #:ask #f))])
(new button%
[label "Save Project"]
[parent hp]
[min-width 110]
[callback (λ _ (save-project-callback))])
)
(update-toolbox-frame #f)
)
(define/provide (show-toolbox-frame)
(send toolbox-frame show #t))
(define (get-selected-template)
(let ([sel (send lb-templates get-selection)])
(and sel (car (list-ref template-dict sel)))))
(define/provide (toolbox-update-template-choices)
(send lb-templates clear)
(dict-for-each template-dict
(λ (k v)
(if v
(send lb-templates append v)
(printf "Warning: File ~a has a wrong format!\n" k)
)
))
(send lb-templates refresh)
)
(define plugin-panels (make-hash))
(define (new-plugin-panel label)
(let ([gbp (new group-box-panel%
(label label)
(parent toolbox-frame-vertical-pane))])
(new horizontal-pane%
(parent gbp))))
(define (get-button-panel plugin panel-name)
(hash-ref! plugin-panels panel-name
(λ () (new-plugin-panel panel-name))))
(define (add-plugin-button plugin)
(let ([button-group (send plugin get-button-group)])
(when button-group
(let ([pl-panel (get-button-panel plugin button-group)])
(new tooltip-button%
[label (make-object bitmap%
(build-path widget-plugins-path
(send plugin get-dir-name)
widget-icons-dir "24x24.png")
'png
(send the-color-database find-color "white"))]
[tooltip-text (send plugin get-tooltip)]
[parent pl-panel]
[style '(border)]
[callback (λ (b e) (toolbox-plugin-button-callback plugin))]
)))))
(define plugins-buttons '())
(define (toolbox-frame-make-plugin-buttons)
(set! plugins-buttons
(map (λ (p) (cons p (add-plugin-button p)))
(get-widget-plugins))))
(define/provide (update-toolbox-frame mid)
(dict-for-each plugins-buttons
(λ (p b)
(when (send p get-button-group) (send b enable (send p can-instantiate? mid))))
))