1. RoundTransparentWindow
1.1 Find it on Apple's Developer Connection
RoundTransparentWindow is a fun little Cocoa example that you can find on Apple’s Developer Connection web site.
On the ADC web site, Apple provides a rich library of sample code to help developers learn how get the most out of their platform. The last time I counted, there were nearly 800 examples illustrating how to do everything from writing low level device drivers to how to make your software more accessible to people with restricted abilities.
RubyCocoa developers will find Apple’s Cocoa section especially useful. There you’ll find dozens of examples illustrating various aspects of Cocoa. All but a few of them are written in Objective-C and can be used to guide RubyCocoa-based projects. But although the RubyCocoa bridge is powerful, at times it can be narrow and dangerous, and some experience with it can be very helpful.
In this article, we’ll convert one of Apple’s Cocoa examples from Objective-C to Ruby. We’ll do it one class at a time and we will test and debug as we go.
As I worked through this project, I tripped over a few things; I’ll tell you about them so that hopefully we both can avoid them in the future. When we’re finished, we’ll have a nearly 100%-Ruby example of a fun user-interface technology from Apple.
So let’s get started. You can download the RoundTransparentWindow program here.
I’m assuming you have Xcode and RubyCocoa installed. If not, you can find instructions for getting Xcode here and RubyCocoa here.
1.2 Get acquainted with it
First, build and run the program. It shows how non-rectangular windows can be created with Cocoa. Transparency plays a key role in this. Windows are still defined with rectangles, but any region drawn transparently (with an alpha value of zero) is considered outside the window boundaries. Click and drag inside the circle, and the window moves; but click on the corners outside the circle and nothing happens—you are outside the window.
As you move the slider, you see another form of transparency: the alpha value for the window itself changes, ranging from opaque (with an alpha value of 1.0) to completely transparent. You’ll also see the window change shape. For alpha values below 0.7, the window is drawn as a pentagon. From the user interface perspective, the window only exists on points where its alpha is nonzero. So if you release the mouse button when the slider is completely to the left, the window will be gone and you’ll have no way to get it back! But have no fear, just use the menu to quit the application and then try it again.
Now look at the code. You’ll see that it consists of three classes:
- a Controller class that accepts messages from the slider and changes the window transparency,
- a CustomWindow class that sets up the window and handles mouse events, and
- a CustomView class that draws in the window.
These classes are implemented in Objective-C source and header files named after the classes and with extensions .m and .h, respectively. Nearly everything in those files can be converted to Ruby with no noticable effect on the application. But many programmers will find that the Ruby versions are much more concise and flexible.
We’ll convert these classes one at a time. But first, we have to get the application ready to run Ruby.
Copyright:
© Copyright 2001 Apple Computer, Inc. All rights reserved.
Disclaimer:
IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Did you find an error? Is something missing? Post your comment or suggestion below!
Comments (0) post