NOTE: Racket GUI framework and editor component

Dan's
Blog

Programming Language Theory • System Programming

NOTE: Racket GUI framework and editor component

framework based on racket/gui and provides some helpful components. This note is about editor, more precisely racket:text%.

Whole GUI system based on a class hierarchy, therefore, let's inherit racket:text%:

(define editor%
  (class racket:text%
    (init)
    (super-new)))

Keyboard Event

Override on-char can get key-event%:

(define editor%
  (class racket:text%
    (init)
    (super-new)
    (define/override (on-char e)
      (cond
        [(and (send e get-meta-down)
              (eq? (send e get-key-code) #\c))
         'copy-selected-text?]
        [(and (send e get-meta-down)
              (eq? (send e get-key-code) #\s))
         'save-to-file?]
        [else (super on-char e)]))))

An interesting thing was send key-event% get-x or send key-event% get-y won't get cursor position, but mouse position.

Mouse Event

Override on-local-event can get mouse-event%:

(define editor%
  (class racket:text%
    (init)
    (super-new)
    (define/override (on-local-event e)
      (cond
        [(and (send e get-meta-down)
              (send e button-down?))
         'jump-to-definition?]
        [else (super on-local-event e)]))))

author: Lîm Tsú-thuàn/林子篆/Danny

category:cs

tag:noteracketracket/guiguiframeworkeditor

Similar Articles

Buy Me A CoffeeCreative Commons Attribution-NonCommercial-NoDerivatives 4.0 InternationalAll works in this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
©2021 dannypsnl(林子篆)