[Openmcl-devel] Questions about Cocoa app development]
mevins at mac.com
Thu Jan 24 22:43:33 UTC 2008
On Jan 24, 2008, at 13:10:08 +0100, Didier Verna
<didier at lrde.epita.fr> wrote:
> I'm interested in developing some Cocoa/GUI applications in CL, and it
> seems to me that CCL is the best tool for that right now. So I tried
> for the first time and went through the Currency Converter tutorial. I
> must admit I was quite puzzled by the simplicity of delivering a Mac
> application !!
> I have some general questions and also some about the tutorial itself:
> 1/ How can you develop a GUI application interactively, or at least
> a GUI application combined with a listener so that you can inspect
> things during development ?
You can use EasyGUI:
...or you can use Cocoa and put up with some amount of inconvenience.
You can use Cocoa classes and method calls to create GUI elements and
manipulate them. You might have to do a certain amount of crawling
around Apple's documentation to figure out the specific things you
want to do. Over time, I hope I'll be adding some more HOWTO docs that
show how to do various things of that nature.
As for interacting with your application, well, you can build a
Listener into it. It used to be that my Bosco framework built a SLIME
interface into applications automatically. When I folded Bosco into
OpenMCL proper, I omitted that feature in order to get things done and
working more quickly. But making it easier to interact with the Lisp
in a built application is still on my list of things to do. Feel free
to send suggestions for how you'd like it to work.
> 2/ How do you execute specific Lisp code when the application starts,
> both before and after the GUI is setup ?
One Cocoa-flavored way to do it is to create an application delegate
and implement the methods applicationWillFinishLaunching: and
applicationDidFinishLaunching:. You can implement these in Lisp and
have them evaluate arbitrary Lisp forms. Apple documentation of these
and other delegate methods is here:
You can define the class and methods the same way it's done in the
Lisp code in the Currency Converter example.
> And about the tutorial itself:
> 1/ I don't quite understand the point in having an instance of the
> converter class visible at the Cocoa layer.
> The way I see it (correct me
> if I'm wrong), I want the Lisp layer to perform the actual work, and
> the Cocoa layer only to display stuff and to get input. So the
> converter-controller should be the only interface between the two
> Hypothesis: having the controller visible down below, as an outlet in
> the converter-controller class, lets the cocoa layer instanciate it at
> the right time, and avoids the need for a global variable to access it
> from the controller's methods ??
The main point is to make the example as much as possible like Apple's
example. Apple does it that way, so I do it that way.
In addition, we expose an Objective-C "Converter" class because that
way we can instantiate it in the nibfile; having instantiated it in
the nibfile, we can then create outlet connections to it. That means
that when the application launches, it will automatically instantiate
the class and store references to it in the appropriate outlets, which
means we don't have to write code to do any of that.
That's kind of convenient, and might be a good reason to do it that
way, but the real reason I did it that way is because that's the way
Apple's example does it.
> 2/ In the same vein, why having the actual work, ie the
> method defined in the Objc layer at all ? This sort of defeats the
> point of having CL on top of it: now it's here to define the classes,
> but performs no real job at all.
Well, to be frank, the application as a whole performs no real job at
all; it just does a simple multiplication. The (trivial) work
performed by the Currency Converter isn't the point of the example at
all. The point is to show how the pieces of a Model-View-Controller
application are implemented using the AppKit. The point of the Lisp
version is to show how you can do exactly the same thing using Lisp
instead of Objective-C as the implementation language.
> Which brings me to the last, more general question:
> if I want to perform the work in CL (here: to the math on single-float
> CL values), I'd have to go between CL and Objc types. How do you do
Well, in this example you are in fact performing the work in CL. The
actual multiplication is performed in
(objc:defmethod (#/convertCurrency:atRate: :float)
((self converter) (currency :float) (rate :float))
(* currency rate))
...which is Common Lisp code. The bridge has thoughfully arranged for
a Lisp form to be evaluated when the AppKit sends
convertCurrency:atRate: to an instance of Converter. It also handles
the conversions of the float values when they are transferred between
Objective-C and Lisp code.
If you want to understand and control the gritty details of passing
data back and forth across the bridge, you might want to look at:
If this reply omits information you wanted, or confuses more than it
clarifies, don't hesitate to ask for more clarification.
More information about the Openmcl-devel