/
frame.rkt
2950 lines (2658 loc) · 116 KB
/
frame.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket/unit
(require string-constants
racket/class
racket/contract/base
racket/path
"search.rkt"
"sig.rkt"
"../preferences.rkt"
"../gui-utils.rkt"
"gen-standard-menus.rkt"
"interfaces.rkt"
"srcloc-panel.rkt"
framework/private/focus-table
mrlib/close-icon
mred/mred-sig)
(import mred^
[prefix group: framework:group^]
[prefix preferences: framework:preferences^]
[prefix icon: framework:icon^]
[prefix handler: framework:handler^]
[prefix application: framework:application^]
[prefix panel: framework:panel^]
[prefix finder: framework:finder^]
[prefix keymap: framework:keymap^]
[prefix text: framework:text^]
[prefix pasteboard: framework:pasteboard^]
[prefix editor: framework:editor^]
[prefix canvas: framework:canvas^]
[prefix menu: framework:menu^]
[prefix racket: framework:racket^]
[prefix exit: framework:exit^]
[prefix comment-box: framework:comment-box^]
[prefix color-prefs: framework:color-prefs^])
(export (rename framework:frame^
[-editor<%> editor<%>]
[-pasteboard% pasteboard%]
[-text% text%]))
(init-depend mred^ framework:text^ framework:canvas^)
(define (reorder-menus frame)
(define items (send (send frame get-menu-bar) get-items))
(define (find-menu name)
(ormap (λ (i) (and (string=? (send i get-plain-label) name) i))
items))
(let* ([file-menu (find-menu (string-constant file-menu))]
[edit-menu (find-menu (string-constant edit-menu))]
[windows-menu (or (find-menu (string-constant windows-menu))
(find-menu (string-constant tabs-menu)))]
[help-menu (find-menu (string-constant help-menu))]
[other-items
(remq* (list file-menu edit-menu windows-menu help-menu) items)]
[re-ordered (filter values `(,file-menu ,edit-menu
,@other-items
,windows-menu ,help-menu))])
(for-each (λ (item) (send item delete)) items)
(for-each (λ (item) (send item restore)) re-ordered)))
(define (remove-empty-menus frame)
(define menus (send (send frame get-menu-bar) get-items))
(for-each (λ (menu) (send menu delete)) menus)
(for-each (λ (menu)
(when (pair? (send menu get-items)) (send menu restore)))
menus))
(define add-snip-menu-items
(lambda (edit-menu c% [func void])
(let* ([get-edit-target-object
(λ ()
(let ([menu-bar
(let loop ([p (send edit-menu get-parent)])
(cond
[(is-a? p menu-bar%)
p]
[(is-a? p menu%)
(loop (send p get-parent))]
[else #f]))])
(and menu-bar
(let ([frame (send menu-bar get-frame)])
(send frame get-edit-target-object)))))]
[edit-menu:do
(λ (const)
(λ (menu evt)
(let ([edit (get-edit-target-object)])
(when (and edit
(is-a? edit editor<%>))
(send edit do-edit-operation const)))
#t))]
[on-demand
(λ (menu-item)
(let ([edit (get-edit-target-object)])
(send menu-item enable (and edit (is-a? edit editor<%>)))))]
[insert-comment-box
(λ ()
(let ([text (get-edit-target-object)])
(when text
(let ([snip (make-object comment-box:snip%)])
(send text insert snip)
(send text set-caret-owner snip 'global)))))])
(let ([item
(new c%
[label (string-constant insert-comment-box-menu-item-label)]
[parent edit-menu]
[callback (λ (x y) (insert-comment-box))]
[demand-callback on-demand])])
(func item))
(define plain-insert-callback (edit-menu:do 'insert-image))
(define lowercase-imgs "*.png;*.jpg;*.jpeg;*.gif;*.xpm;*.bmp")
(define imgs (string-append
lowercase-imgs ";"
(string-upcase lowercase-imgs)))
(let ([item
(new c%
[label (string-constant insert-image-item)]
[parent edit-menu]
[callback (λ (menu evt)
(parameterize ([finder:default-filters
`(["Image" ,imgs]
["Any" "*.*"])])
(plain-insert-callback menu evt)))]
[demand-callback on-demand])])
(func item))
(void))))
(define frame-width 600)
(define frame-height 650)
(let ([window-trimming-upper-bound-width 20]
[window-trimming-upper-bound-height 50])
(let-values ([(w h) (get-display-size)])
(set! frame-width (min frame-width (- w window-trimming-upper-bound-width)))
(set! frame-height (min frame-height (- h window-trimming-upper-bound-height)))))
(define basic<%> frame:basic<%>)
(define focus-table<%> (interface (top-level-window<%>)))
(define focus-table-mixin
(mixin (top-level-window<%>) (focus-table<%>)
(inherit get-eventspace)
(define/override (show on?)
(define old (remove this (frame:lookup-focus-table (get-eventspace))))
(define new (if on? (cons this old) old))
(frame:set-focus-table (get-eventspace) new)
(super show on?))
(define/augment (on-close)
(frame:set-focus-table (get-eventspace) (remove this (frame:lookup-focus-table (get-eventspace))))
(inner (void) on-close))
(super-new)
(frame:set-focus-table (get-eventspace) (frame:lookup-focus-table (get-eventspace)))))
(define basic-mixin
(mixin ((class->interface frame%)) (basic<%>)
(define/override (show on?)
(if on?
(send (group:get-the-frame-group) insert-frame this)
(send (group:get-the-frame-group) remove-frame this))
(super show on?))
(define/override (can-exit?)
(and (exit:user-oks-exit)
(begin
(exit:set-exiting #t)
(let ([res (exit:can-exit?)])
(unless res
(exit:set-exiting #f))
res))))
(define/override (on-exit)
(exit:on-exit)
(queue-callback
(λ ()
(exit)
(exit:set-exiting #f))))
(define/public (make-visible filename) (void))
(define/public get-filename
(case-lambda
[() (get-filename #f)]
[(b) #f]))
(define/public (editing-this-file? filename) #f)
(define/override (on-superwindow-show shown?)
(send (group:get-the-frame-group) frame-shown/hidden this)
(super on-superwindow-show shown?))
(define after-init? #f)
(define/override on-drop-file
(λ (filename)
(handler:edit-file filename)))
;; added call to set label here to hopefully work around a problem in mac mred
(inherit set-label change-children)
(define/override after-new-child
(λ (child)
(when after-init?
(change-children (λ (l) (remq child l)))
(error 'frame:basic-mixin
(string-append
"do not add children directly to a frame:basic (unless using make-root-area-container); "
"use the get-area-container method instead")))))
(define/public get-area-container% (λ () vertical-panel%))
(define/public get-menu-bar% (λ () menu-bar%))
(define/public make-root-area-container
(λ (% parent)
(make-object % parent)))
(inherit on-close can-close?)
(define/public (close)
(when (can-close?)
(on-close)
(show #f)))
(inherit accept-drop-files)
(super-new)
(accept-drop-files #t)
(inherit set-icon)
(let ([icon (current-icon)])
(when icon
(if (pair? icon)
(let ([small (car icon)]
[large (cdr icon)])
(set-icon small (send small get-loaded-mask) 'small)
(set-icon large (send large get-loaded-mask) 'large))
(set-icon icon (send icon get-loaded-mask) 'both))))
(group:create-windows-menu (make-object (get-menu-bar%) this))
(reorder-menus this)
[define panel (make-root-area-container (get-area-container%) this)]
(define/public (get-area-container) panel)
(set! after-init? #t)))
(define current-icon (make-parameter #f))
(define size-pref<%>
(interface (basic<%>)
adjust-size-when-monitor-setup-changes?))
(define-local-member-name monitor-setup-changed)
(define size-pref-mixin
(mixin (basic<%>) (size-pref<%>)
(init-field size-preferences-key
[position-preferences-key #f])
(inherit is-maximized?)
(define/override (on-size w h)
;; if the monitor state is currently fluxuating, then
;; don't save preferences values, since the OS is doing
;; some adjustments for us.
(when (or (equal? (compute-current-monitor-information)
latest-monitor-information)
(not (adjust-size-when-monitor-setup-changes?)))
(define old-table (preferences:get size-preferences-key))
(define new-val
(cond
[(is-maximized?)
(define old (or (hash-ref old-table latest-monitor-information #f)
(hash-ref old-table #f)))
(cons #t (cdr old))]
[else
(list #f w h)]))
(preferences:set size-preferences-key
(hash-set*
old-table
latest-monitor-information new-val
#f new-val)))
(super on-size w h))
(define on-move-timer-arg-x #f)
(define on-move-timer-arg-y #f)
(define on-move-timer-arg-max? #f)
(define on-move-callback-running? #f)
(define/override (on-move x y)
(when position-preferences-key
(unless on-move-callback-running?
(set! on-move-callback-running? #t)
(queue-callback
(λ ()
(set! on-move-callback-running? #f)
(unless on-move-timer-arg-max?
;; if the monitor state is currently fluxuating, then
;; don't save preferences values, since the OS is doing
;; some adjustments for us.
(when (or (equal? (compute-current-monitor-information)
latest-monitor-information)
(not (adjust-size-when-monitor-setup-changes?)))
(define-values (monitor delta-x delta-y) (find-closest on-move-timer-arg-x on-move-timer-arg-y))
(define old-table (preferences:get position-preferences-key))
(define val (list monitor delta-x delta-y))
(preferences:set position-preferences-key
(hash-set*
old-table
latest-monitor-information val
#f val)))))
#f))
(set! on-move-timer-arg-x x)
(set! on-move-timer-arg-y y)
(set! on-move-timer-arg-max? (is-maximized?)))
(super on-move x y))
(define/augment (display-changed)
(restart-montior-information-timer)
(inner (void) display-changed))
;; if all of the offsets have some negative direction, then
;; just keep the thing relative to the original montior; otherwise
;; make it relative to whatever origin it is closest to.
(define/private (find-closest x y)
(define closest 0)
(define-values (delta-x delta-y dist) (find-distance x y 0))
(for ([m (in-range 1 (get-display-count))])
(define-values (new-delta-x new-delta-y new-dist)
(find-distance x y m))
(when (and new-delta-x
new-delta-y
(new-delta-x . >= . 0)
(new-delta-y . >= . 0))
(when (< new-dist dist)
(set! closest m)
(set!-values (delta-x delta-y dist) (values new-delta-x new-delta-y new-dist)))))
(values closest delta-x delta-y))
(define/private (find-distance x y mon)
(define-values (delta-x delta-y)
(let-values ([(l t) (get-display-left-top-inset #:monitor mon)])
(values (+ x l) (+ y t))))
(values delta-x
delta-y
(sqrt (+ (* delta-x delta-x)
(* delta-y delta-y)))))
(inherit maximize)
(define/private (get-sizes/maximzed)
(define size-table (preferences:get size-preferences-key))
(define-values (maximized? w h) (apply values (or (hash-ref size-table latest-monitor-information #f)
(hash-ref size-table #f))))
(define-values (x y origin-still-visible?)
(cond
[position-preferences-key
(define pos-table (preferences:get position-preferences-key))
(define-values (monitor delta-x delta-y) (apply values (or (hash-ref pos-table latest-monitor-information #f)
(hash-ref pos-table #f))))
(define-values (l t) (get-display-left-top-inset #:monitor monitor))
(define-values (mw mh) (get-display-size #:monitor monitor))
(if (and l t mw mh)
(values (- delta-x l)
(- delta-y t)
(and (<= 0 delta-x mw)
(<= 0 delta-y mh)))
(values #f #f #f))]
[else
(values #f #f #f)]))
(define (already-one-there? x y w h)
(for/or ([fr (in-list (get-top-level-windows))])
(and (equal? x (send fr get-x))
(equal? y (send fr get-y))
(equal? w (send fr get-width))
(equal? h (send fr get-height))
(equal? maximized? (send fr is-maximized?)))))
(cond
[(or (and (already-one-there? x y w h)
(not maximized?))
(not origin-still-visible?))
;; these are the situations where we look for a different position of the window
(let loop ([n 50]
[x 0]
[y 0])
(cond
[(zero? n)
(values #f #f #f #f maximized?)]
[(already-one-there? x y w h)
(define-values (dw dh) (get-display-size #:monitor 0))
(define sw (- dw w))
(define sh (- dh h))
(if (or (<= sw 0)
(<= sh 0))
(values #f #f #f #f maximized?)
(loop (- n 1)
(modulo (+ x 20) (- dw w))
(modulo (+ y 20) (- dh h))))]
[else
(values w h x y maximized?)]))]
[else
(values w h x y maximized?)]))
(define/public (adjust-size-when-monitor-setup-changes?) #f)
(inherit begin-container-sequence
end-container-sequence
move
resize)
(define/public (monitor-setup-changed)
(when (adjust-size-when-monitor-setup-changes?)
(define-values (w h x y maximized?) (get-sizes/maximzed))
(when (and w h x y)
(begin-container-sequence)
(move x y)
(resize w h)
(when maximized?
(maximize #t))
(end-container-sequence))))
(let-values ([(w h x y maximized?) (get-sizes/maximzed)])
(cond
[(and w h x y)
(super-new [width w] [height h] [x x] [y y])]
[else
(super-new)])
(when maximized?
(maximize #t)))))
(define (setup-size-pref size-preferences-key w h
#:maximized? [maximized? #f]
#:position-preferences [position-preferences-key #f])
(preferences:set-default size-preferences-key
(hash #f (list maximized? w h))
(hash/c (or/c (listof (list/c any/c any/c any/c any/c))
#f)
(list/c boolean?
exact-nonnegative-integer?
exact-nonnegative-integer?)
#:immutable #t
#:flat? #t))
(when position-preferences-key
(preferences:set-default position-preferences-key
(hash #f (list 0 0 0))
(hash/c (or/c (listof (list/c any/c any/c any/c any/c))
#f)
(list/c exact-nonnegative-integer?
exact-integer?
exact-integer?)
#:immutable #t
#:flat? #t))))
(define (compute-current-monitor-information)
(filter
values
(for/list ([m (in-range (get-display-count))])
(define-values (left top) (get-display-left-top-inset #:monitor m))
(define-values (width height) (get-display-size #:monitor m))
(and left top width height
(list left top width height)))))
(define latest-monitor-information (compute-current-monitor-information))
(define pending-monitor-information #f)
(define montior-information-timer
(new timer%
[notify-callback
(λ ()
(define new-monitor-information (compute-current-monitor-information))
(cond
[pending-monitor-information
(cond
[(equal? pending-monitor-information new-monitor-information)
(set! pending-monitor-information #f)
(set! latest-monitor-information new-monitor-information)
(queue-callback
(λ ()
(when (editor:get-change-font-size-when-monitors-change?)
(editor:set-current-preferred-font-size
(editor:get-current-preferred-font-size)))
(for ([frame (in-list (get-top-level-windows))])
(when (is-a? frame size-pref<%>)
(send frame monitor-setup-changed)))
#f))]
[else
(set! pending-monitor-information new-monitor-information)
(restart-montior-information-timer)])]
[else
(unless (equal? latest-monitor-information new-monitor-information)
(set! pending-monitor-information new-monitor-information)
(restart-montior-information-timer))]))]))
(define (restart-montior-information-timer)
(send montior-information-timer stop)
(send montior-information-timer start 250 #t))
(define register-group<%> (interface ()))
(define register-group-mixin
(mixin (basic<%>) (register-group<%>)
(define/augment (can-close?)
(and (inner #t can-close?)
(group:can-close-check)))
(define/augment (on-close)
(send (group:get-the-frame-group)
remove-frame
this)
(inner (void) on-close)
(group:on-close-action))
(define/override (on-activate on?)
(super on-activate on?)
(when on?
(send (group:get-the-frame-group) set-active-frame this)))
(super-new)
(send (group:get-the-frame-group) insert-frame this)))
(define locked-message (string-constant read-only))
(define lock-canvas%
(class canvas%
(field [locked? #f])
(inherit refresh)
(define/public (set-locked l)
(unless (eq? locked? l)
(set! locked? l)
(setup-sizes)
(refresh)))
(inherit get-client-size get-dc)
(define/override (on-paint)
(define dc (get-dc))
(define (draw str bg-color bg-style line-color line-style)
(send dc set-font small-control-font)
(define-values (w h) (get-client-size))
(define-values (tw th _1 _2) (send dc get-text-extent str #f 'grapheme))
(send dc set-pen (send the-pen-list find-or-create-pen line-color 1 line-style))
(send dc set-brush (send the-brush-list find-or-create-brush bg-color bg-style))
(send dc draw-rectangle 0 0 w h)
(send dc draw-text str
(- (/ w 2) (/ tw 2))
(- (/ h 2) (/ th 2))
'grapheme))
(when locked?
(draw locked-message "yellow" 'solid "black" 'solid)))
(inherit get-parent min-width min-height stretchable-width stretchable-height)
(define/private (setup-sizes)
(let ([dc (get-dc)])
(if locked?
(let-values ([(w h _1 _2) (send dc get-text-extent locked-message)])
(min-width (inexact->exact (floor (+ w 4))))
(min-height (inexact->exact (floor (+ h 2)))))
(begin
(min-width 0)
(min-height 0)))))
(super-new [style '(transparent no-focus)])
(send (get-dc) set-font small-control-font)
(setup-sizes)
(stretchable-width #f)
(stretchable-height #t)))
(define status-line<%>
(interface (basic<%>)
open-status-line
close-status-line
update-status-line))
;; status-line : (make-status-line symbol number)
(define-struct status-line (id count))
;; status-line-msg : (make-status-line-msg (is-a?/c message%) (union symbol #f))
(define-struct status-line-msg (message [id #:mutable]))
(define status-line-mixin
(mixin (basic<%>) (status-line<%>)
(field [status-line-container-panel #f]
;; status-lines : (listof status-line)
[status-lines null]
;; status-line-msgs : (listof status-line-msg)
[status-line-msgs null])
(define/override (make-root-area-container % parent)
(let* ([s-root (super make-root-area-container vertical-pane% parent)]
[r-root (make-object % s-root)])
(set! status-line-container-panel
(instantiate vertical-panel% ()
(parent s-root)
(stretchable-height #f)))
r-root))
(define/public (open-status-line id)
(do-main-thread
(λ ()
(when status-line-container-panel
(set! status-lines
(let loop ([status-lines status-lines])
(cond
[(null? status-lines)
(list (make-status-line id 1))]
[else (let ([status-line (car status-lines)])
(if (eq? id (status-line-id status-line))
(cons (make-status-line id (+ (status-line-count status-line) 1))
(cdr status-lines))
(cons status-line (loop (cdr status-lines)))))])))))))
(define/public (close-status-line id)
(do-main-thread
(λ ()
(when status-line-container-panel
;; decrement counter in for status line, or remove it if
;; counter goes to zero.
(set! status-lines
(let loop ([status-lines status-lines])
(cond
[(null? status-lines) (error 'close-status-line "status line not open ~e" id)]
[else (let* ([status-line (car status-lines)]
[this-line? (eq? (status-line-id status-line) id)])
(cond
[(and this-line? (= 1 (status-line-count status-line)))
(cdr status-lines)]
[this-line?
(cons (make-status-line id (- (status-line-count status-line) 1))
(cdr status-lines))]
[else (cons status-line (loop (cdr status-lines)))]))])))
;; make sure that there are only as many messages as different status lines, in total
(let ([status-line-msg (find-status-line-msg id)])
(when status-line-msg
(send (status-line-msg-message status-line-msg) set-label "")
(set-status-line-msg-id! status-line-msg #f)))
(let* ([msgs-that-can-be-removed (filter (λ (x) (not (status-line-msg-id x))) status-line-msgs)]
[max-to-include (length status-lines)]
[msgs-to-remove
(let loop ([n max-to-include]
[l msgs-that-can-be-removed])
(cond
[(null? l) l]
[(zero? n) l]
[else (loop (- n 1) (cdr l))]))])
(send status-line-container-panel
change-children
(λ (old-children)
(foldl (λ (status-line-msg l)
(remq (status-line-msg-message status-line-msg) l))
old-children
msgs-to-remove)))
(set! status-line-msgs
(let loop ([l msgs-to-remove]
[status-line-msgs status-line-msgs])
(cond
[(null? l) status-line-msgs]
[else (loop (cdr l)
(remq (car l) status-line-msgs))]))))))))
;; update-status-line : symbol (union #f string)
(define/public (update-status-line id msg-txt)
(do-main-thread
(λ ()
(unless (open-status-line? id)
(error 'update-status-line "unknown id ~e, other arg ~e" id msg-txt))
(if msg-txt
(cond
[(find-status-line-msg id)
=>
(λ (existing-status-line-msg)
(let ([msg (status-line-msg-message existing-status-line-msg)])
(unless (equal? (send msg get-label) msg-txt)
(send msg set-label msg-txt))))]
[(find-available-status-line-msg)
=>
(λ (available-status-line-msg)
(send (status-line-msg-message available-status-line-msg) set-label msg-txt)
(set-status-line-msg-id! available-status-line-msg id))]
[else
(set! status-line-msgs
(cons (make-new-status-line-msg id msg-txt)
status-line-msgs))])
(let ([status-line-msg (find-status-line-msg id)])
(when status-line-msg
(send (status-line-msg-message status-line-msg) set-label "")
(set-status-line-msg-id! status-line-msg #f)))))))
;; open-status-line? : symbol -> boolean
(define/private (open-status-line? id)
(let loop ([status-lines status-lines])
(cond
[(null? status-lines) #f]
[else
(let ([status-line (car status-lines)])
(or (eq? (status-line-id status-line) id)
(loop (cdr status-lines))))])))
;; find-available-status-line-msg : -> (union #f status-line-msg)
(define/private (find-available-status-line-msg)
(let loop ([status-line-msgs status-line-msgs])
(cond
[(null? status-line-msgs) #f]
[else (let ([status-line-msg (car status-line-msgs)])
(if (status-line-msg-id status-line-msg)
(loop (cdr status-line-msgs))
status-line-msg))])))
;; find-status-line-msg : symbol -> (union #f status-line-msg)
(define/private (find-status-line-msg id)
(let loop ([status-line-msgs status-line-msgs])
(cond
[(null? status-line-msgs) #f]
[else (let ([status-line-msg (car status-line-msgs)])
(if (eq? id (status-line-msg-id status-line-msg))
status-line-msg
(loop (cdr status-line-msgs))))])))
;; make-new-status-line-msg : symbol string -> status-line-msg
(define/private (make-new-status-line-msg id msg-txt)
(define msg
(new message%
[parent status-line-container-panel]
[stretchable-width #t]
[label ""]))
(send msg set-label msg-txt)
(make-status-line-msg msg id))
(inherit get-eventspace)
(define/private (do-main-thread t)
(let ([c-eventspace (current-eventspace)])
(if (and (eq? c-eventspace (get-eventspace))
(eq? (current-thread) (eventspace-handler-thread c-eventspace)))
(t)
(parameterize ([current-eventspace c-eventspace])
;; need high priority callbacks to ensure ordering wrt other callbacks
(queue-callback t #t)))))
(super-new)))
(define info<%> frame:info<%>)
(define magic-space 25)
(define-local-member-name get-memory-use-canvas)
(define memory-use-timer-cell (make-thread-cell #f))
(define (update-memory-use-canvases)
(define found-any? #f)
(for ([window (in-list (get-top-level-windows))])
(when (is-a? window frame:info<%>)
(set! found-any? #t)
(send (send window get-memory-use-canvas) update-memory-use-if-bigish-change)))
(unless found-any?
(send (thread-cell-ref memory-use-timer-cell) stop)
(thread-cell-set! memory-use-timer-cell #f)))
(define (maybe-create-memory-use-timer)
(unless (thread-cell-ref memory-use-timer-cell)
(when (eq? (current-thread) (eventspace-handler-thread (current-eventspace)))
(thread-cell-set! memory-use-timer-cell
(new timer%
[notify-callback update-memory-use-canvases]
[interval 1000])))))
(define info-mixin
(mixin (basic<%>) (info<%>)
[define rest-panel 'uninitialized-root]
[define super-root 'uninitialized-super-root]
(define/override (make-root-area-container % parent)
(let* ([s-root (super make-root-area-container
vertical-pane%
parent)]
[r-root (make-object % s-root)])
(set! super-root s-root)
(set! rest-panel r-root)
r-root))
(define info-canvas #f)
(define/public (get-info-canvas) info-canvas)
(define/public (set-info-canvas c) (set! info-canvas c))
(define/public (get-info-editor)
(and info-canvas
(send info-canvas get-editor)))
(define/public (determine-width string canvas edit)
(send edit set-autowrap-bitmap #f)
(send canvas call-as-primary-owner
(λ ()
(let ([lb (box 0)]
[rb (box 0)])
(send edit erase)
(send edit insert string)
(send edit position-location
(send edit last-position)
rb)
(send edit position-location 0 lb)
(send canvas min-width
(+ (get-client-width/view-delta edit canvas)
(- (inexact->exact (floor (unbox rb)))
(inexact->exact (floor (unbox lb))))))))))
(define outer-info-panel 'top-info-panel-uninitialized)
;; this flag is specific to this frame
;; the true state of the info panel is
;; the combination of this flag and the
;; 'framework:show-status-line preference
;; as shown in update-info-visibility
(define info-hidden? #f)
(define/public (hide-info)
(set! info-hidden? #t)
(update-info-visibility (preferences:get 'framework:show-status-line)))
(define/public (show-info)
(set! info-hidden? #f)
(update-info-visibility (preferences:get 'framework:show-status-line)))
(define/public (is-info-hidden?) info-hidden?)
(define/private (update-info-visibility pref-value)
(cond
[(or info-hidden? (not pref-value))
(send super-root change-children
(λ (l)
(if (memq outer-info-panel l)
(begin (unregister-collecting-blit gc-canvas)
(unregister-pref-save-callback)
(list rest-panel))
l)))]
[else
(send super-root change-children
(λ (l)
(if (memq outer-info-panel l)
l
(begin
(register-gc-blit)
(register-pref-save-callback)
(list rest-panel outer-info-panel)))))]))
[define close-panel-callback
(preferences:add-callback
'framework:show-status-line
(λ (p v)
(update-info-visibility v)))]
(define memory-cleanup void) ;; only for checkouts and nightly build users; used with memory-text
(define/augment (on-close)
(unregister-collecting-blit gc-canvas)
(unregister-pref-save-callback)
(close-panel-callback)
(memory-cleanup)
(inner (void) on-close))
(define icon-currently-locked? 'uninit)
(define/public (lock-status-changed)
(let ([info-edit (get-info-editor)])
(cond
[(not (object? lock-canvas))
(void)]
[(is-a? info-edit editor:file<%>)
(unless (send lock-canvas is-shown?)
(send lock-canvas show #t))
(let ([locked-now? (not (send info-edit get-read-write?))])
(unless (eq? locked-now? icon-currently-locked?)
(set! icon-currently-locked? locked-now?)
(when (object? lock-canvas)
(send lock-canvas set-locked locked-now?))))]
[else
(when (send lock-canvas is-shown?)
(send lock-canvas show #f))])))
(define/public (update-info) (lock-status-changed))
(super-new)
(set! outer-info-panel (make-object horizontal-panel% super-root))
(send outer-info-panel stretchable-height #f)
(define info-panel (new-horizontal-panel% [parent outer-info-panel]))
(new grow-box-spacer-pane% [parent outer-info-panel])
(define/public (get-info-panel) info-panel)
(define/public (get-memory-use-canvas) this-frames-memory-canvas)
(define pref-save-canvas #f)
(set! pref-save-canvas (new pref-save-canvas% [parent (get-info-panel)]))
[define lock-canvas (make-object lock-canvas% (get-info-panel))]
(define this-frames-memory-canvas #f)
; set up the memory use display in the status line
(let* ([panel (new-horizontal-panel%
[parent (get-info-panel)]
[stretchable-width #f]
[stretchable-height #f])])
(set! this-frames-memory-canvas
(new memory-position-canvas%
[parent panel]
[button-up
(λ (evt)
(cond
[(or (send evt get-alt-down)
(send evt get-control-down))
(dynamic-require 'framework/private/follow-log #f)]
[else
(collect-garbage)
(update-memory-text)]))]))
(set! memory-canvases (cons this-frames-memory-canvas memory-canvases))
(maybe-create-memory-use-timer)
(set! memory-cleanup
(λ ()
(set! memory-canvases (remq this-frames-memory-canvas memory-canvases))))
(send panel stretchable-width #f))
(define gc-canvas (new gc-off-canvas% [parent (get-info-panel)] [style '(border no-focus)]))
(define/private (register-gc-blit)
(let ([onb (icon:get-gc-on-bitmap)]
[offb (icon:get-gc-off-bitmap)])
(when (and (send onb ok?)
(send offb ok?))
(register-collecting-blit gc-canvas
0 0
(send onb get-width)
(send onb get-height)
onb offb))))
(define pref-save-callback-registration #f)
(inherit get-eventspace)
(define/private (register-pref-save-callback)
(when pref-save-canvas
(set! pref-save-callback-registration
(preferences:register-save-callback
(λ (start?)
(cond
[(eq? (current-thread) (eventspace-handler-thread (get-eventspace)))
(send pref-save-canvas set-on? start?)]
[else
(queue-callback
(λ ()
(send pref-save-canvas set-on? start?)))]))))))
(define/private (unregister-pref-save-callback)
(when pref-save-callback-registration
(preferences:unregister-save-callback pref-save-callback-registration)))
(register-pref-save-callback)
(unless (preferences:get 'framework:show-status-line)
(send super-root change-children
(λ (l)
(list rest-panel))))
(register-gc-blit)
(let* ([gcb (icon:get-gc-on-bitmap)]
[gc-width (if (send gcb ok?)
(send gcb get-width)
10)]
[gc-height (if (send gcb ok?)
(send gcb get-height)
10)])
(send* gc-canvas
(min-client-width (max (send gc-canvas min-width) gc-width))
(min-client-height (max (send gc-canvas min-height) gc-height))
(stretchable-width #f)
(stretchable-height #f)))
(send* (get-info-panel)
(set-alignment 'right 'center)
(stretchable-height #f)
(spacing 3)
(border 3))))
(define (ensure-enough-width editor-canvas text)
(send editor-canvas call-as-primary-owner
(λ ()
(let ([delta (get-client-width/view-delta text editor-canvas)]
[lb (box 0)]
[rb (box 0)])
(send text position-location
(send text last-position)
rb)
(send text position-location 0 lb)
(let ([nw
(+ delta (- (inexact->exact (floor (unbox rb)))
(inexact->exact (floor (unbox lb)))))])
(when (< (send editor-canvas min-client-width) nw)
(send editor-canvas min-client-width nw)))))))
(define (get-client-width/view-delta position-edit position-canvas)
(let ([admin (send position-edit get-admin)]
[wb (box 0)])
(send admin get-view #f #f wb #f)
(let-values ([(cw ch) (send position-canvas get-client-size)])
(inexact->exact (floor (- cw (unbox wb)))))))
(define position-canvas%
(class canvas%
(inherit min-client-height min-client-width get-dc get-client-size refresh)
(init init-width)
(init-field [button-up #f])
(init-field [char-typed void])
(define str "")
(define/public (set-str _str)
(set! str _str)
(update-client-width str)
(refresh))
(define/private (update-client-width str)
(define dc (get-dc))
(define-values (cw _4) (get-client-size))
(define-values (tw _1 _2 _3) (send dc get-text-extent str normal-control-font 'grapheme))
(when (< cw tw)
(min-client-width (inexact->exact (ceiling tw)))))