fix cocoa menu-bar set menu label

This commit is contained in:
Matthew Flatt 2010-08-05 07:18:28 -06:00
parent f8ba0a65d2
commit a4c036b50a
3 changed files with 26 additions and 16 deletions

View File

@ -1,14 +1,13 @@
#lang scheme/base #lang scheme/base
(require scheme/class (require racket/class
scheme/foreign ffi/unsafe
ffi/objc ffi/unsafe/objc
(only-in racket/list take drop)
"../../syntax.rkt" "../../syntax.rkt"
"utils.rkt" "utils.rkt"
"types.rkt" "types.rkt"
"const.rkt" "const.rkt"
"queue.rkt") "queue.rkt")
(unsafe!)
(objc-unsafe!)
(provide menu-bar%) (provide menu-bar%)
@ -113,7 +112,6 @@
(defclass menu-bar% object% (defclass menu-bar% object%
(define menus null) (define menus null)
(def/public-unimplemented set-label-top)
(def/public-unimplemented number) (def/public-unimplemented number)
(def/public-unimplemented enable-top) (def/public-unimplemented enable-top)
@ -130,7 +128,9 @@
(public [append-menu append]) (public [append-menu append])
(define (append-menu menu title) (define (append-menu menu title)
(set! menus (append menus (list (cons menu title)))) (set! menus (append menus (list (cons menu title))))
(send menu set-parent this)) (send menu set-parent this)
(when (eq? current-mb this)
(send menu install cocoa-mb title)))
(define/public (install) (define/public (install)
(let loop () (let loop ()
@ -148,6 +148,15 @@
(define/public (get-top-window) (define/public (get-top-window)
top-wx) top-wx)
(define/public (set-label-top pos str)
(set! menus (append
(take menus pos)
(list (cons (car (list-ref menus pos)) str))
(drop menus (add1 pos))))
(when (eq? current-mb this)
(tellv (tell cocoa-mb itemAtIndex: #:type _NSInteger 1)
setTitle: #:type _NSString (clean-menu-label str))))
(define/public (do-on-menu-click) (define/public (do-on-menu-click)
(let ([es (send top-wx get-eventspace)]) (let ([es (send top-wx get-eventspace)])
(when es (when es

View File

@ -17,9 +17,6 @@
(define-struct mitem (item)) (define-struct mitem (item))
(define (clean-label str)
(regexp-replace* #rx"&(.)" str "\\1"))
(defclass menu% object% (defclass menu% object%
(init-field label (init-field label
callback callback
@ -37,13 +34,13 @@
(set! cocoa (set! cocoa
(as-objc-allocation (as-objc-allocation
(tell (tell NSMenuItem alloc) (tell (tell NSMenuItem alloc)
initWithTitle: #:type _NSString (clean-label label) initWithTitle: #:type _NSString (clean-menu-label label)
action: #:type _SEL #f action: #:type _SEL #f
keyEquivalent: #:type _NSString ""))) keyEquivalent: #:type _NSString "")))
(set! cocoa-menu (set! cocoa-menu
(as-objc-allocation (as-objc-allocation
(tell (tell NSMenu alloc) (tell (tell NSMenu alloc)
initWithTitle: #:type _NSString (clean-label label)))) initWithTitle: #:type _NSString (clean-menu-label label))))
(tellv cocoa-menu setAutoenablesItems: #:type _BOOL #f) (tellv cocoa-menu setAutoenablesItems: #:type _BOOL #f)
(tellv cocoa setSubmenu: cocoa-menu) (tellv cocoa setSubmenu: cocoa-menu)
(for-each (lambda (item) (for-each (lambda (item)
@ -137,9 +134,9 @@
(define/public (set-label item label) (define/public (set-label item label)
(adjust item (adjust item
(lambda (item-cocoa) (lambda (item-cocoa)
(tellv item-cocoa setTitle: #:type _NSString (clean-label label))) (tellv item-cocoa setTitle: #:type _NSString (clean-menu-label label)))
(lambda (mitem) (lambda (mitem)
(send (mitem-item mitem) set-label (clean-label label))))) (send (mitem-item mitem) set-label (clean-menu-label label)))))
(define/public (check item on?) (define/public (check item on?)
(adjust item (adjust item

View File

@ -13,7 +13,8 @@
define-mz define-mz
as-objc-allocation as-objc-allocation
retain release retain release
with-autorelease) with-autorelease
clean-menu-label)
(define cocoa-lib (ffi-lib (format "/System/Library/Frameworks/Cocoa.framework/Cocoa"))) (define cocoa-lib (ffi-lib (format "/System/Library/Frameworks/Cocoa.framework/Cocoa")))
(define cf-lib (ffi-lib (format "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"))) (define cf-lib (ffi-lib (format "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")))
@ -46,3 +47,6 @@
(begin0 (begin0
(thunk) (thunk)
(release pool)))) (release pool))))
(define (clean-menu-label str)
(regexp-replace* #rx"&(.)" str "\\1"))