forked from soegaard/remacs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.rkt
108 lines (102 loc) · 4.26 KB
/
gui.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
#lang racket
(require racket/gui "core.rkt" "controller.rkt" "key-event-key.rkt")
(define (new-editor-frame controller)
(define min-width 800)
(define min-height 800)
(define text-color (make-object color% "black"))
(define font-size 16)
(define font-family 'modern) ; fixed width
(define fixed-font (make-object font% font-size font-family))
(define frame (new frame% [label "Editor"]))
(define subeditor-canvas%
(class canvas%
(define/override (on-char event)
(cond
[(ignored-key-event? event) (void)]
[else
(define key-symbol (key-event->key-symbol event))
(displayln (~e key-symbol))
(send controller on-char key-symbol)
(define b (send controller get-buffer))
(send status-line set-label (string-append (~v (Buffer-cur b)) " " (send controller get-status-line)))
(send canvas on-paint)]))
(define/override (on-paint)
(define b (send controller get-buffer))
(render-buffer b canvas 16 controller))
(define start-row 0)
(define end-row #f)
(define (render-buffer b canvas font-size controller)
(define dc (send canvas get-dc))
(send dc clear)
(send dc suspend-flush)
(send dc set-text-foreground text-color)
(send dc set-text-background "white")
(send dc set-font fixed-font)
;; Dimensions
(define-values (width height) (send canvas get-client-size))
(define fs font-size)
(define ls (+ fs 1)) ; linesize -- 1 pixel for spacing
;; Placement of point relative to lines on screen
(define num-lines-on-screen (max 0 (quotient height ls)))
(define-values (row col) (Point-row-col (Buffer-cur b)))
;(displayln (list 'before: 'row row 'start-row start-row 'end-row end-row 'n num-lines-on-screen))
(define n num-lines-on-screen)
(when (not end-row)
(set! end-row (+ start-row n -1)))
(when (<= (length (Buffer-lines b)) num-lines-on-screen)
(set! start-row 0)
(set! end-row (sub1 (length (Buffer-lines b)))))
(when (< row start-row)
(define new-start-row row)
(define new-end-row (+ new-start-row n -1))
(set! start-row new-start-row)
(set! end-row new-end-row)
;(displayln (list 'new-start-and-end start-row end-row))
)
(when (> row end-row)
(define new-end-row row)
(define new-start-row (- new-end-row n -1))
(set! start-row new-start-row)
(set! end-row new-end-row)
;(displayln (list 'new-start-and-end start-row end-row))
)
; draw-string : string real real -> real
; draw string t at (x,y), return point to draw next string
(define (draw-string t x y)
(send dc draw-text t x y)
(+ y ls))
; draw text
(define after-end-row (add1 end-row))
(define window-lines
(drop (take (Buffer-lines b) after-end-row) start-row))
;(displayln (~e 'window-lines window-lines))
(define ymin 0 #;(send canvas get-y))
(define xmin 0 #;(send canvas get-x))
(for/fold ([y ymin])
([l window-lines])
(draw-string l xmin y))
;(displayln (~v 'start-row start-row 'end-row end-row))
(send controller draw-points dc start-row)
; resume flush
(send dc resume-flush))
(super-new)))
(define canvas (new subeditor-canvas% [parent frame]))
(send canvas min-client-width 400)
(send canvas min-client-height 100)
(define status-line (new message% [parent frame] [label "No news"]))
(send status-line min-width min-width)
(send frame show #t))
(module+ test
(require (submod "core.rkt" test))
(define b (new-Buffer #;'("Sing, O goddess, the anger"
"of Achilles son"
"of Peleus, that brought"
", the anger"
"of Achilles son"
"x")
'("Sing, O goddess, the anger"
"of Achilles son"
"of Peleus, that brought")))
(define controller (new controller% [buffer b]))
(new-editor-frame controller)
)