On this page:
Last Time
Two Sides of the Cursor
From Textbox to Chat  Client
Send It Already!
6.10

Lab 8: Textbox, Message, History

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 and lab 7. If you haven’t already completed labs 6 and 7, 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!

Last Time

In the previous two labs, we have designed data definitions for Message, History, and Textbox, and implemented a number of useful functions: message->image, add-message, textbox->image, and textbox-handle-key.

We finished lab 7 with a functioning textbox.

Two Sides of the Cursor

Ex 1: In assignment 4, you implemented a text editor with a two String fields, pre and post:

(define-struct editor (pre post))
; An Editor is a (make-editor String String)
; Interp: An Editor represents a text editor, where for some Editor e
; - (editor-pre  e) is the content before the cursor and
; - (editor-post e) is the content after the cursor.

Recall our data definition for Textboxes:

; A Natural is a non-negative integer.
(define-struct textbox (content cursor))
; 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.

Are the two representations, Textbox and Editor, in some sense equivalent? Justify why or why not in a comment in your definitions window.

Ex 2: Design the function editor->textbox that converts an Editor to a Textbox.

Ex 3: Design the function textbox->editor that converts a Textbox to an Editor.

From Textbox to ChatClient

Ex 4: Create a data definition ChatClient that includes a Textbox, History, and the String name of the user sending messages.

Ex 5: Create the template chat-template : ChatClient -> ??? for all functions that operate on ChatClients.

Hint: Remember our mantra for all composite data: What can we do with it?

Tear it apart!

Send It Already!

Swap Head and Hands!

Ex 6: Design a function create-client : String -> ChatClient that, given the user’s name, returns a new ChatClient with an empty Textbox, an empty History, and that user’s name.

Ex 7: Design a function draw-client : ChatClient -> Image that draws the chat client as a 500x500 pixel window with the Textbox on the bottom with the History above.

Ex 8: Design a function client-message : ChatClient -> Message that, given a ChatClient, creates a Message using the contents of the Textbox and the client’s user as the sender.

Ex 9: Design a function send-message : ChatClient -> ChatClient that adds the current client Message to the History. The Textbox should be emptied once the Message is sent.

Ex 10: Design a function handle-key : ChatClient KeyEvent -> ChatClient with all of the functionality of textbox-handle-key, but also with ability to send messages with send-message when given the return key (the KeyEvent "\r").

Ex 11: Using big-bang and your create-client, draw-client, and handle-key, send messages to yourself in your chat client!