On this page:
Same Lab Time, Same Lab Channel!
Creating New Textboxes
A Working Textbox
6.10

Lab 7: Chatting in Action

Implement this lab with the Beginning Student Language. Require the HtDP2e image and universe libraries at the top of your definitions:
(require 2htdp/image)
(require 2htdp/universe)

Make sure you follow The Style we use for the {B,I,A}SL{,+} languages in this class.

Open your solutions to lab 6. If you haven’t already completed lab 6, do so before starting this lab.

Make sure you save and submit your definitions, we will be extending this program in future labs.

Choose the initial Head and Hands, and get started!

Same Lab Time, Same Lab Channel!

In the last lab we gave data definitions for Message, History, and Textbox:

(define-struct msg (name content))
; A Message is a (make-msg String String).
; Interp: A Message m' represents the chat message (msg-content m)'
; from the sender (msg-name m)'.
 
; message-template : Message -> ???
; Given any Message, we can pull out the sender's name and message content.
(define (message-template msg)
  (... (msg-name msg) ... (msg-content msg) ...))
 
(define msg0 (make-msg "Sam" "BSL is the best language ever!"))
(define msg1 (make-msg "Austin" "Nah, Coq is where it's at"))
(define msg2 (make-msg "DVH" "*head explodes*"))
 
; A History is an Image.
(define empty-history empty-image)
 
; history-template : History -> ???
(define (history-template hist)
  (... hist ...))
(define hist1 (add-message msg0 empty-history))
(define hist2 (add-message msg1 hist1))
(define hist3 (add-message msg2 hist2))
 
(define-struct textbox (content cursor))
 
; A Natural is a non-negative integer.
; A Textbox is a (make-textbox String Natural)
; Interp: a textbox (make-textbox str n) has its cursor
; immediately before the n'th character of str'. The
; cursor's value must never exceed the length of the
; string.
 
; textbox-template : Textbox -> ???
; Given any Textbox, we can pull out its content and
; cursor location.
(define (textbox-template tb)
  (... (textbox-content tb) ... (textbox-cursor tb) ...))
 
(define tb0 (make-textbox "foo " 0)) ; => |foo
(define tb1 (make-textbox "foo " 1)) ; => f|oo
(define tb2 (make-textbox "foo " 2)) ; => fo|o
(define tb3 (make-textbox "foo " 3)) ; => foo|
(define tb4 (make-textbox "foo " 4)) ; => foo |

We implemented the following functions:
  • message->image : Message -> Image

  • add-message : Message History -> History

  • before-cursor : Textbox -> String

  • after-cursor : Textbox -> String

  • textbox->image : Textbox -> Image

Creating New Textboxes

We need to be able to insert and remove content from our Textbox, as well as move around. We do that by creating new Textboxes with modified content from given Textboxes.

Ex 1: Design a function textbox-insert that, given a Textbox and a string, returns a new Textbox with the new content inserted at the appropriate location with an updated cursor.

(check-expect (textbox-insert tb2 "") (make-textbox "foo " 2))
(check-expect (textbox-insert tb2 "f") (make-textbox "fofo " 3))
(check-expect (textbox-insert tb2 "o") (make-textbox "fooo " 3))
(check-expect (textbox-insert tb4 "bar") (make-textbox "foo bar" 7))

Ex 2: A Direction is one of the strings "left" or "right". Design a function textbox-move that, given a Textbox and a Direction returns a new Textbox with the cursor moved one character to the left or right.

Ex 2.1: What does your textbox-move do when the Textbox cursor is at 0? What about when it is equal to the length of its content? Are you sure you’re satisfying the Textbox data definition?

Ex 3: Design a function textbox-backspace that, given a Textbox, returns a new Textbox with the last character before the cursor removed and the cursor properly updated. If the cursor is at the beginning of the Textbox, textbox-backspace should have no effect.

Ex 4: Design a function textbox-delete that works similarly to textbox-backspace, but for the character directly after the cursor.

A Working Textbox

Swap Head and Hands!

We will use big-bang to get our Textbox up and running.

Ex 5: Using only big-bang and the to-draw clause, display one of your example Textboxes in a window.

Without a key-handler, our Textbox is just about useless. The on-key clause to big-bang will let us react to KeyEvents.

Ex 6: What is a KeyEvent? Is it atomic or composite data? Is it an enumeration? Write your answers in a comment in the definitions window.

Ex 7: What keyboard key does the KeyEvent "a" represent? What about "left", "\b", and "\u007F"?

Ex 8: Design a function textbox-handle-key that given a Textbox and a KeyEvent, returns a new Textbox modified in the proper way.

Your key-handler should support inserting any lowercase character, number, or space, as well as proper movement with left/right arrow keys, delete, and backspace. The enter/return key should have no effect.