cocoa: control fonts

This commit is contained in:
Matthew Flatt 2010-10-19 14:50:58 -06:00
parent 42dc870c10
commit ca64c25cf9
5 changed files with 79 additions and 9 deletions

View File

@ -0,0 +1,48 @@
#lang racket/base
(require racket/class
racket/draw
ffi/unsafe
ffi/unsafe/objc
"../../lock.rkt"
"const.rkt"
"utils.rkt"
"types.rkt")
(provide font->NSFont)
(import-class NSFont NSFontManager)
(define NSItalicFontMask #x00000001)
(define NSBoldFontMask #x00000002)
(define (font->NSFont f)
(let* ([weight (send f get-weight)]
[style (send f get-style)]
[name (or (send f get-face)
(send the-font-name-directory
get-screen-name
(send the-font-name-directory
find-family-default-font-id
(send f get-family))
weight
style))])
(atomically
(with-autorelease
(let ([f (tell NSFont
fontWithName: #:type _NSString name
size: #:type _CGFloat (send f get-point-size))])
(if (and (eq? 'normal weight)
(eq? 'normal style))
(begin
(retain f)
f)
(let ([fm (tell NSFontManager sharedFontManager)])
(let ([f (tell fm
convertFont: f
toHaveTrait: #:type _int (bitwise-ior
(if (eq? weight 'bold) NSBoldFontMask 0)
(if (eq? style 'italic) NSItalicFontMask 0)))])
(begin
(retain f)
f)))))))))

View File

@ -2,10 +2,11 @@
(require scheme/class (require scheme/class
scheme/foreign scheme/foreign
ffi/objc ffi/objc
"../../syntax.rkt" "../../syntax.rkt"
"window.rkt" "window.rkt"
"const.rkt" "const.rkt"
"types.rkt") "types.rkt"
"font.rkt")
(unsafe!) (unsafe!)
(objc-unsafe!) (objc-unsafe!)
@ -17,7 +18,9 @@
systemFontOfSize: #:type _CGFloat 13)) systemFontOfSize: #:type _CGFloat 13))
(define (install-control-font cocoa font) (define (install-control-font cocoa font)
(tellv cocoa setFont: sys-font)) (if font
(tellv cocoa setFont: (font->NSFont font))
(tellv cocoa setFont: sys-font)))
(defclass item% window% (defclass item% window%
(inherit get-cocoa) (inherit get-cocoa)

View File

@ -10,6 +10,7 @@
"types.rkt" "types.rkt"
"const.rkt" "const.rkt"
"window.rkt" "window.rkt"
"font.rkt"
"../common/event.rkt") "../common/event.rkt")
(unsafe!) (unsafe!)
(objc-unsafe!) (objc-unsafe!)
@ -27,8 +28,12 @@
[-a _id (preparedCellAtColumn: [_NSInteger column] row: [_NSInteger row]) [-a _id (preparedCellAtColumn: [_NSInteger column] row: [_NSInteger row])
(let ([wx (->wx wxb)]) (let ([wx (->wx wxb)])
(tell (tell
(tell (tell NSCell alloc) initTextCell: #:type _NSString (let ([c (tell (tell NSCell alloc) initTextCell: #:type _NSString
(if wx (send wx get-row row) "???")) (if wx (send wx get-row row) "???"))]
[font (send wx get-cell-font)])
(when font
(tellv c setFont: font))
c)
autorelease))] autorelease))]
[-a _void (doubleClicked: [_id sender]) [-a _void (doubleClicked: [_id sender])
(queue-window*-event wxb (lambda (wx) (send wx clicked 'list-box-dclick)))] (queue-window*-event wxb (lambda (wx) (send wx clicked 'list-box-dclick)))]
@ -106,6 +111,10 @@
(def/public-unimplemented get-label-font) (def/public-unimplemented get-label-font)
(define cell-font (and font (font->NSFont font)))
(define/public (get-cell-font)
cell-font)
(define/public (get-selection) (define/public (get-selection)
(tell #:type _NSInteger content-cocoa selectedRow)) (tell #:type _NSInteger content-cocoa selectedRow))
(define/public (get-selections) (define/public (get-selections)

View File

@ -49,9 +49,14 @@
(define/private (default-font s) (define/private (default-font s)
(case s (case s
[(modern) "Monospace"] [(modern) "Monospace"]
[(roman) "Serif"] [(roman) (case (system-type)
[(windows) "Times New Roman"]
[(macosx) "Times"]
[else "Serif"])]
[(decorative swiss) "Helvetica"] [(decorative swiss) "Helvetica"]
[(script) "Chancery"] [(script) (case (system-type)
[(macosx) "Apple Chancery"]
[else "Chancery"])]
[(symbol) "Symbol"] [(symbol) "Symbol"]
[else (case (system-type) [else (case (system-type)
[(windows) "Tahoma"] [(windows) "Tahoma"]

View File

@ -50,6 +50,10 @@
20 'decorative 20 'decorative
'normal 'bold 'normal 'bold
#f)) #f))
(define italic-font (send the-font-list find-or-create-font
13 'roman
'italic 'normal
#f))
(define ($ font) (or font normal-control-font)) (define ($ font) (or font normal-control-font))
(define (make-h&s cp f) (define (make-h&s cp f)
@ -2253,7 +2257,7 @@
(make-radio-box "Stretchiness" '("Normal" "All Stretchy") (make-radio-box "Stretchiness" '("Normal" "All Stretchy")
p1 void)) p1 void))
(define font-radio (define font-radio
(make-radio-box "Label Font" '("Normal" "Small" "Tiny" "Big") (make-radio-box "Label Font" '("Normal" "Small" "Tiny" "Big" "Italic")
p1 void)) p1 void))
(define enabled-radio (define enabled-radio
(make-radio-box "Initially" '("Enabled" "Disabled") (make-radio-box "Initially" '("Enabled" "Disabled")
@ -2276,7 +2280,8 @@
(list-ref (list #f (list-ref (list #f
small-control-font small-control-font
tiny-control-font tiny-control-font
special-font) special-font
italic-font)
(send font-radio get-selection)) (send font-radio get-selection))
(positive? (send enabled-radio get-selection)) (positive? (send enabled-radio get-selection))
(positive? (send selection-radio get-selection)) (positive? (send selection-radio get-selection))