git.haldean.org grandmaster / dbfb5a8
start working on PGN view with hyperlinks Haldean Brown 8 months ago
1 changed file(s) with 52 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #lang racket/gui
1
2 (provide pgn-view%)
3
4 (define (pgn-parse-bits pgn)
5 (map (compose string-split string-trim)
6 (string-split pgn #rx"[0-9]+\\.")))
7
8
9 (define (pgn-build-text pgn-bits)
10 (define (pgn-build-ply txt ply-i ply-txt)
11 (define start (send txt last-position))
12 (send txt insert (format "~a" ply-txt))
13 (send txt set-clickback start (send txt last-position)
14 (λ (t s e) (println ply-i) (println ply-txt))
15 (send (new style-delta%) set-delta-background
16 (make-object color% 200 200 200))))
17
18 (define (pgn-build-line line i txt)
19 (send txt insert (format "~a." i))
20 (pgn-build-ply txt (- (* 2 i) 1) (first line))
21 (send txt insert " ")
22 (when (> (length line) 1)
23 (pgn-build-ply txt (* 2 i) (second line)))
24 (send txt insert "\n")
25 txt)
26 (foldl pgn-build-line (new text%) pgn-bits (range 1 (+ 1 (length pgn-bits)))))
27
28 (define pgn-view%
29 (class editor-canvas%
30 (super-new [style '(no-border no-hscroll auto-vscroll no-focus transparent)]
31 [horizontal-inset 10]
32 [stretchable-height #t] [stretchable-width #t])
33
34 (define/public (set-pgn pgn)
35 (send this set-editor (pgn-build-text (pgn-parse-bits pgn))))
36 ))
37
38 (define (main)
39 (define f (new frame% [label "pgnview test"] [width 250] [height 600]))
40 (define pgn (new pgn-view% [parent f]))
41 (send pgn set-pgn "1. e4 d6 2. d4 Nf6 3. Nc3 g6 4. Be3 Bg7 5. Qd2 c6 6. f3 b5
42 7. Nge2 Nbd7 8. Bh6 Bxh6 9. Qxh6 Bb7 10. a3 e5 11. O-O-O Qe7
43 12. Kb1 a6 13. Nc1 O-O-O 14. Nb3 exd4 15. Rxd4 c5 16. Rd1 Nb6
44 17. g3 Kb8 18. Na5 Ba8 19. Bh3 d5 20. Qf4+ Ka7 21. Rhe1 d4
45 22. Nd5 Nbxd5 23. exd5 Qd6 24. Rxd4 cxd4 25. Re7+ Kb6
46 26. Qxd4+ Kxa5 27. b4+ Ka4 28. Qc3 Qxd5 29. Ra7 Bb7 30. Rxb7
47 Qc4 31. Qxf6 Kxa3 32. Qxa6+ Kxb4 33. c3+ Kxc3 34. Qa1+ Kd2
48 35. Qb2+ Kd1 36. Bf1 Rd2 37. Rd7 Rxd7 38. Bxc4 bxc4 39. Qxh8
49 Rd3 40. Qa8 c3 41. Qa4+ Ke1 42. f4 f5 43. Kc1 Rd2 44. Qa7")
50 (send f show #t)
51 pgn)