gui/collects/mred/private/wx/cocoa/clipboard.rkt
Matthew Flatt e4ffd5e6c1 default buttons and Cocoa clipboard
original commit: 0723c4f647921b3d3342e531d017b414d0f5530e
2010-11-05 15:54:03 -06:00

82 lines
2.5 KiB
Racket

#lang scheme/base
(require scheme/class
ffi/unsafe
ffi/unsafe/objc
"utils.rkt"
"types.rkt"
"../common/bstr.rkt"
"../../syntax.rkt")
(provide clipboard-driver%
has-x-selection?)
(import-class NSPasteboard NSArray NSData)
(import-protocol NSPasteboardOwner)
(define (has-x-selection?) #f)
(define (map-type s)
(cond
[(string=? s "TEXT") "public.utf8-plain-text"]
[else (string-append "org.racket-lang." s)]))
(define (unmap-type s)
(cond
[(string=? s "public.utf8-plain-text") "TEXT"]
[(regexp-match #rx"^org[.]racket-lang[.](.*)$" s)
=> (lambda (m) (cadr m))]
[else s]))
(defclass clipboard-driver% object%
(init x-selection?) ; always #f
(super-new)
(define client #f)
(define counter -1)
(define/public (clear-client)
;; called in event-pump thread
(set! client #f))
(define/public (get-client)
(and client
(let ([c (tell #:type _NSInteger (tell NSPasteboard generalPasteboard)
changeCount)])
(if (= c counter)
client
(begin
(set! client #f)
#f)))))
(define/public (set-client c types)
(let ([pb (tell NSPasteboard generalPasteboard)]
[a (tell NSArray arrayWithObjects:
#:type (_list i _NSString) (map map-type types)
count: #:type _NSUInteger (length types))])
(set! counter (tell #:type _NSInteger pb clearContents))
(set! client c)
(for ([type (in-list types)])
(let* ([bstr (send c get-data type)]
[data (tell NSData
dataWithBytes: #:type _bytes bstr
length: #:type _NSUInteger (bytes-length bstr))])
(tellv (tell NSPasteboard generalPasteboard)
setData: data
forType: #:type _NSString (map-type type))))))
(define/public (get-data-for-type type)
(log-error "didn't expect clipboard data request"))
(define/public (get-text-data)
(let ([bstr (get-data "TEXT")])
(and bstr
(bytes->string/utf-8 bstr #\?))))
(define/public (get-data type)
(let* ([pb (tell NSPasteboard generalPasteboard)]
[data (tell pb dataForType: #:type _NSString (map-type type))])
(and data
(let ([len (tell #:type _NSUInteger data length)]
[bstr (tell #:type _pointer data bytes)])
(scheme_make_sized_byte_string bstr len 1))))))