Skip to content

Commit 55a99fb

Browse files
committed
add the right-click-menu argument to switchable-button%
1 parent e70f8c0 commit 55a99fb

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

gui-doc/mrlib/scribblings/switchable-button.scrbl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
[callback (-> (is-a?/c switchable-button%) any/c)]
2525
[alternate-bitmap (is-a?/c bitmap%) bitmap]
2626
[vertical-tight? boolean? #f]
27-
[min-width-includes-label? boolean? #f])]{
27+
[min-width-includes-label? boolean? #f]
28+
[right-click-menu (or/c #f (list/c string? (-> any)))])]{
2829
The @racket[callback] is called when the button
2930
is pressed. The @racket[label] and @racket[bitmap] are
3031
used as discussed above.
@@ -39,7 +40,14 @@
3940
If the @racket[min-width-includes-label?] is @racket[#t], then the minimum
4041
width includes both the bitmap and the label. Otherwise, it includes
4142
only the bitmap.
42-
}
43+
44+
If @racket[right-click-menu] is not @racket[#f], then right
45+
click (or control click on some platforms) opens a context
46+
sensitive menu under the button with one menu item whose
47+
label is the string and whose callback is the thunk.
48+
49+
@history[#:changed "1.76" @list{Added the @racket[right-click-menu] argument}]
50+
}
4351

4452
@defmethod[(set-label-visible [visible? boolean?]) void?]{
4553
Sets the visibility of the string part of the label.

gui-lib/info.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
(define pkg-authors '(mflatt robby))
3636

37-
(define version "1.75")
37+
(define version "1.76")
3838

3939
(define license
4040
'(Apache-2.0 OR MIT))

gui-lib/mrlib/switchable-button.rkt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#lang racket/base
22
(require racket/gui/base
3+
racket/contract
34
racket/class
45
"private/panel-wob.rkt")
56

@@ -63,7 +64,8 @@
6364
callback
6465
[alternate-bitmap bitmap]
6566
[vertical-tight? #f]
66-
[min-width-includes-label? #f])
67+
[min-width-includes-label? #f]
68+
[right-click-menu #f])
6769

6870
(define/public (get-button-label) label)
6971
(define/override (set-label l)
@@ -75,6 +77,12 @@
7577
(not (send label ok?)))
7678
(error 'switchable-button% "label bitmap is not ok?"))
7779

80+
(let ([rcb-pred (or/c #f (list/c string? (procedure-arity-includes/c 0)))])
81+
(unless (rcb-pred right-click-menu)
82+
(error 'switchable-button% "contract violation\n expected: ~s\n got: ~e"
83+
(contract-name rcb-pred)
84+
right-click-menu)))
85+
7886
(define/override (get-label) label)
7987

8088
(define disable-bitmap (make-dull-mask bitmap))
@@ -85,7 +93,7 @@
8593
(make-dull-mask alternate-bitmap)))
8694

8795
(inherit get-dc min-width min-height get-client-size refresh
88-
client->screen get-top-level-window)
96+
client->screen get-top-level-window popup-menu)
8997

9098
(define down? #f)
9199
(define in? #f)
@@ -130,6 +138,18 @@
130138
(not disabled?))
131139
(update-float #f)
132140
(callback this))]
141+
[(send evt button-up?)
142+
(set! down? #f)
143+
(refresh)]
144+
[(send evt button-down? 'right)
145+
(when right-click-menu
146+
(define m (new popup-menu%))
147+
(new menu-item%
148+
[label (list-ref right-click-menu 0)]
149+
[parent m]
150+
[callback (λ (_1 _2) ((list-ref right-click-menu 1)))])
151+
(define-values (cw ch) (get-client-size))
152+
(popup-menu m 0 ch))]
133153
[(send evt entering?)
134154
(set! in? #t)
135155
(update-float #t)

0 commit comments

Comments
 (0)