Index: /trunk/source/examples/cocoa/ui-elements/HOWTO.html
===================================================================
--- /trunk/source/examples/cocoa/ui-elements/HOWTO.html	(revision 8522)
+++ /trunk/source/examples/cocoa/ui-elements/HOWTO.html	(revision 8523)
@@ -3,5 +3,5 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
-    <title>Nib-Loading HOWTO</title>
+    <title>UI Elements HOWTO</title>
     <link rel="stylesheet" type="text/css" href="HOWTO_files/stylesheets/styles.css" />
   </head>
@@ -224,7 +224,141 @@
     specified, appears on the left lower corner of the screen.</p>
 
+    <div class="title">
+      <h2>Adding a Button</h2>
     </div>
 
-
+    <p>Once we have a window on the screen, we might like to put
+    something in it. Let's start by adding a button.</p>
+
+    <p>Creating a button object is as simple as creating a window
+    object; we simply allocate one:</p>
+
+    <pre>
+(setf my-button (#/alloc ns:ns-button))
+#&lt;NS-BUTTON &lt;NSButton: 0x13b7bec0&gt; (#x13B7BEC0)&gt;
+    </pre>
+
+    <p>As with the window, most of the interesting work is in
+    configuring the allocated button after it's allocated.</p>
+
+    <p>Instances of NSButton include pushbuttons with either text or
+    image labels (or both), checkboxes, and radio buttons. In order to
+    make a text pushbutton, we need to tell our button to use a
+    button-type of <code>NSMomentaryPushInButton</code>, an image
+    position of <code>NSNoImage</code>, and a border style
+    of <code>NSRoundedBezelStyle</code>. These style options are
+    represented by Cocoa constants.</p>
+    
+    <p>We also need to give the button a frame rectangle that defines
+    its size and position. We can once again
+    use <code>ns:with-ns-rect</code> to specify a temporary rectangle
+    for the purpose of initializing our button:</p>
+
+    <pre>
+(ns:with-ns-rect (frame 10 10 72 32)
+  (#/initWithFrame: my-button frame)
+  (#/setButtonType: my-button #$NSMomentaryPushInButton)
+  (#/setImagePosition: my-button #$NSNoImage)
+  (#/setBezelStyle: my-button #$NSRoundedBezelStyle))
+;Compiler warnings :
+;   Undeclared free variable MY-BUTTON (4 references), in an anonymous lambda form
+NIL
+    </pre>
+
+    <p>Now we just need to add the button to the window:</p>
+
+    <pre>
+(#/addSubview: (#/contentView my-window) my-button)
+    </pre>
+
+    <p>The button appears in the window with the rather uninspired
+    label "Button". Clicking it highlights the button but, since we
+    didn't give it any action to perform, does nothing else.</p>
+
+    <p>We can give the button a more interesting label and, perhaps
+    more importantly, an action to perform, by passing a string and an
+    action to it. First, let's set the button title:</p>
+
+    <pre>
+(let ((label (%make-nsstring "Hello!")))
+  (#/setTitle: my-button label)
+  (#/release label))
+;Compiler warnings :
+;   Undeclared free variable MY-BUTTON, in an anonymous lambda form
+NIL
+    </pre>
+
+    <p>The button changes to display the text "Hello!". Notice that we
+    are careful to save a reference to the label text and release it
+    after changing the button title. The normal memory-management
+    policy in Cocoa is that if we allocate an object (like the
+    NSString "Hello!") we are responsible for releasing it. Unlike
+    Lisp, Cocoa does not automatically garbage-collect all allocated
+    objects by default.</p>
+
+    <p>Giving the button an action is slightly more
+    complicated. Clicking a button causes the button object to send a
+    message to a target object. We haven't given our button a message
+    to send, nor a target object to send it to, so it doesn't do
+    anything. In order to get it do perform some kind of action, we
+    need to give it a target object and a message to send. Then, when
+    we click the button, it will send the message we specify to the
+    target we provide. Naturally, the target object had better be able
+    to respond to the message, or else we'll just see a runtime
+    error.</p>
+
+    <p>Let's define a class that knows how to respond to a greeting
+    message, and then make an object of that class to serve as our
+    button's target.</p>
+
+    <p>We can define a subclass of <code>NSObject</code> to handle
+    our button's message:</p>
+
+    <pre>
+(defclass greeter (ns:ns-object)
+  ()
+  (:metaclass ns:+ns-object))
+#&lt;OBJC:OBJC-CLASS GREETER (#x13BAF810)&gt;
+    </pre>
+
+    <p>We'll need to define a method to execute in response to the
+    button's message. Action methods accept one argument (inaddition
+    to the receiver): a sender. Normally Cocoa passes the button
+    object itself as the sender argument; the method can do anything
+    it likes (oor nothing at all) with the sender.</p>
+
+    <p>Here's a method that displays an alert dialog:</p>
+
+    <pre>
+(objc:defmethod #/greet: ((self greeter) (sender :id))
+  (declare (ignore sender))
+  (let ((title (%make-nsstring "Hello!"))
+        (msg (%make-nsstring "Hello, World!"))
+        (default-button (%make-nsstring "Hi!"))
+        (alt-button (%make-nsstring "Hello!"))
+        (other-button (%make-nsstring "Go Away")))
+    (#_NSRunAlertPanel title msg default-button alt-button other-button)
+    (#/release title)
+    (#/release msg)
+    (#/release default-button)))
+    </pre>
+
+    <p>Now we can create an instance of the Greeter class and use it
+    as the button's target:</p>
+
+    <pre>
+(setf my-greeter (#/init (#/alloc greeter)))
+#&lt;GREETER &lt;Greeter: 0x136c58e0&gt; (#x136C58E0)&gt;
+
+(#/setTarget: my-button my-greeter)
+NIL
+
+(#/setAction: my-button (@SELECTOR "greet:"))
+NIL
+    </pre>
+
+    <p>Now, if you click the button, an Alert panel appears.</p>
+
+    </div>
 
   </body>
