First we have to include the package symbols:
;; This is so we don't have to prefix everything with an ntw: (use-package :ntw)
This is a callback function. A callback function is run in response to a user interface event, like "button clicked" or "mouse moved." The server will pass information to the callback function, such as the widget in which caused the event, the type of event, and any extra data that the client provides. We'll learn more about callbacks later. For now, this one ends execution of the program.
;; This is our callback that should close things down. When deleting widgets is ;; implemented, this will delete the window and close the connection. (defun close-window (&optional widget event data userdata) (format t "Connection closed.~%"))
Here we've defined the widgets we are going to use. This is a simple application with just three -- a window with a button in it that says "Click to Quit". When the user clicks the button or uses the titlebar to close the window, the program will call the finished function we just defined, which will exit.
;; This is the function that gets called whenever a client connects to the server. (defun hello-world () ;; Define our three widgets here, setting attributes as appropriate (let* ((win (make-window :title "NTW Lisp Hello World" :width 400 :height 200)) (button (make-button :parent-id (id win))) (label (make-label :text "Click to Quit" :parent-id (id button))))
Before the client will send events, they have to be attached to callback functions as follows. A widget and event are specifed, along with the callback function that will run when that event occurs. In this case, the one callback is used for all three events.
;; Now add the callback function -- it will run when the button is pushed ;; or the window is closed. (add-callback button *button-pressed* #'close-window) (add-callback win *delete-event* #'close-window) (add-callback win *destroy-event* #'close-window)))
This last function tells the server to run the hello-world function when a client connects. Otherwise, the server will do nothing.
;; This is how the hello-world function is set to run at client connection time. (ntw-init #'hello-world)
And that's all there is to it. In order to see how it works, you'll have to load the ntw package into your Lisp and then load the helloworld.lisp file. Then start the server:
The ntw server will start. In a command window, start an ntw client. The default IP address and port should be ok when the client asks. After clicking OK, you should see the Hello World window appear. You can also try running the client from another machine by using the server machine's IP address. Have fun!