Code by Kevin

Code by Kevin, Programming, code, business, and other pursuits

Your Host
Kevin Walzer, software developer.


Subscribe to RSS Feed
Get a syndicated feed of my weblog.



Privacy Policy

Site design: Skeleton


Sat, 07 Mar 2009

Tk-Cocoa, at last?

As readers of this blog know, one of my great preoccupations as a developer is the future of Tk on OS X. Tk currently is built on top of OS X's Carbon frameworks, which are more or less deprecated and will not be joining the great OS X migration to 64-bit capability. My concern has been simple: Will Tk become obsolete on the Mac if it does not run on top of Cocoa? Last year it appeared a developer was working on a port of Tk to run on top of Cocoa, which gave me great hope. But it appears that project never got off the ground.

Imagine my surprise, then, when recently running a Google search for "Tk Cocoa 64-bit" (which I idly do, from time to time), I ran across something interesting on page two of the search results:

das's tcl-tk at de-carbon-8-5

What caught my eye was this phrase: "This is a port of TkAqua 8.5 to Cocoa and 64bit for Mac OS X Leopard..."

My.What was this? Clicking on the link brought me to the main page. What awaited me there:

" TkAqua Cocoa/64bit Port *
* *
* Copyright (c) 2008-2009 Daniel A. Steffen <> *
* Copyright 2008-2009, Apple Inc. *
* *
* See the file "license.terms" for information on usage and redistribution *

This could scarely be better news for anyone concerned with Tk on the Mac. Daniel A. Steffen is the maintainer of Tk on the Mac--its lead developer, the one who makes sure it is up-to-date and continually improved with new features. No one knows more about Tk on OS X than Daniel. One reason I was feeling very discouraged about Tk's future on OS X is that last year Daniel had publicly ruled out undertaking a port of Tk to run on top of Cocoa. Since he works as an independent software consultant and is not paid for his maintainer work on Tk, his decision was understandable. Somehow, however, Daniel has apparently found time to work on the Tk-Cocoa project, and it appears that Apple itself is contributing resources to the project. Daniel has been conspicuously silent about his work on this project, but Apple is (in)famous for its secrecy about unreleased projects, so perhaps that's playing a role here.

Testing Tk-Cocoa

Out of curiosity, I downloaded the Tk-Cocoa source tree from the Github site (if you don't have the git source code control tool installed, you can download a zip archive of the source tree from the site, which is what I did). I spent some time browsing through the source code, which confirms what I hoped: this is an extensive rewrite of Tk to run on top of Cocoa, instead of Carbon. I also built Tk Cocoa from source, so I could give it a test drive.To build Tk Aqua, you should change to the source code directory in Terminal, and run these commands: make -C tcl/macosx; make -C tk/macosx; make -C tcl/macosx install-embedded INSTALL_ROOT=`pwd`/embedded/; make -C tk/macosx install-embedded INSTALL_ROOT=`pwd`/embedded/. This will build and install a standalone application bundle of the Tcl/Tk frameworks, which allows you to test the system without disturbing an existing Tcl/Tk installation. (Note: These instructions yielded a 32-bit Intel binary. I also did a test build of a 4-way universal binary: 32-bit PPC and Intel, and 64-bit PPC and Intel. I did that by exporting "export CFLAGS= -arch ppc -arch i386 -arch ppc64 -arch x86_64" and running ./configure --enable-framework from the tcl/unix directory, and ./configure --enable-framework --enable-aqua from the tk/unix directory. Running the "file" command on the resulting binaries showed a four-way universal binary, including 64-bit on both Mac platforms! I did not test that build, however.)

What follows is my overview of Tk-Cocoa: how it compares to the current Tk implementation of Tk on Carbon, what effect Cocoa has on Tk as a development toolkit, and the larger impact for Tk on OS X. Tk-Cocoa appears to be somewhere between an alpha and beta stage right now: most of it works, but there are undoubtedly some bugs, and some of it is not yet implemented. The final release, whenever it comes, will likely improve on my reaction below.


Tk-Cocoa does fix several annoying, long-standing warts in the implementation of Tk on Carbon. Images in menus is one of them. If you look at the two screenshots below, you'll see that the image in the Tk-Cocoa menu has some padding around it, while the image in the Tk-Carbon menu is smashed against the side of the menu. The Cocoa layout is the correct one. I'm not sure if this is a case of the Cocoa implementation "just working," but it certainly does work.

Tk-Cocoa menu. Tk-Carbon menu.

One extremely cool feature, which I discovered while rummaging around in the source code, is that Tk can now display Mac-native images. Tk had this ability on Mac Classic, but this feature has been broken every since Tcl/Tk was ported to Mac OS X in 2001. The screenshot below uses the "NSUser" system image. This feature, by itself, will make a huge difference in the polish of Tk applications on Mac OS X if it is intelligently used. (I will certainly be trying it out in my apps!)

Native Cocoa bitmaps.  

Some things, while not necessarily better, are different. The Cocoa dialog below looks very different from the Carbon. One item of note is that the Cocoa dialog does not display as a "sheet," that is, a window that slides down from the top of the main window. I hope this is functionality that will be added later, as I use this feature in all of my applications.

Tk-Cocoa dialog. Tk-Carbon dialog.

Tk-Cocoa also picks up the correct behavior of the "help" menu, and in fact displays something that annoys me about Cocoa programs: if the application lacks installed user help, by default it pops up a window that says, "Help is not available for this application." (Programmers should always provide user help, in my view, but that's a digression.)

The Status Quo

While there are some definite improvements and nice touches in Tk-Cocoa, what is especially interesting to me is how much is unchanged, at least on the surface. The two screenshots below are basically indistinguishable; both display old-fashioned Mac notebook tabs. (I'm not sure why Tk's themed "ttk" widgets use this Jaguar-era tab control, but that's been unchanged for five years now.)

Tk-Cocoa notebook. Tk-Carbon notebook.

Indeed, running the Tk widget demo with Tk-Cocoa, it's amazing how little seems to have changed. Most everything looks identical as before. Apart from the improvements noted above, there is almost nothing different between Tk-Cocoa and Tk-Carbon. The significance of this is clear: the goal is to preserve the user-level implementation of Tk as much as possible, even while there is a massive under-the-hood rewrite going on. It's a conservative approach, one that will not break existing packages that depend on this framework. It's a laudable goal, and one I've highlighted before in the work of other developers porting a toolkit to run on top of Cocoa.

Rough Edges

Of course, since the Tk-Cocoa port still appears to be a work in progress, not everything is done yet. The "to-do" list at the Github site mentions that native dialogs are not fully in place yet. Testing the widget demo, I can confirm this, as the screenshots below demonstrate.

Tk-Cocoa color dialog. Tk-Carbon color dialog.

The Tk-Cocoa screenshot is actually Tk's cross-platform color picker dialog, which is what Tk uses on Unix and serves as the fallback dialog on other platforms. The Tk-Carbon dialog uses the native system dialog; I assume this will be implemented at some point in Tk-Cocoa.

Conclusion: The Significance of Tk-Cocoa

Tk-Cocoa is a real milestone in the evolution of Tk on OS X. With better menu displays, native bitmaps, and other improvements, it offers a much higher degree of platform-specific Mac integration than it has in the past, while still preserving the API that makes Tk widely-used as a cross-platform toolkit. Cocoa purists may not be impressed, but I am definitely looking forward to using Tk-Cocoa in my applications; it will make the work I do to ensure a polished, high-quality Mac user experience with my programs much easier. I feel that Tk has always been a hidden gem, so to speak, and perhaps more developers on the Mac would be willing to give it a try now that it's moving to Cocoa.

Fundamentally, Tk-Cocoa ensures Tk's viability as a development toolkit on OS X for years to come. Apple has been moving slowly away from Carbon for years, and finally forced the break with Carbon by not making it a 64-bit development framework. That's how Apple has ensured a convergence of all Mac developers around Cocoa. Now, Tk is no longer left behind. It joins Qt, RealBasic, and wxWidgets as a major cross-platform toolkit with Cocoa as its foundation. And it's not just Tcl/Tk applications that benefit; major applications written in Python, Ruby, Perl, and other languages will also benefit, because they have Tk bindings.

All in all, it's a good time to be a Tk developer on the Mac. Daniel is doing a tremendous job, apparently almost single-handedly, in moving the Tk toolkit to Cocoa. And Apple, which sponsored the port of Tk to OS X back in 2001, is apparently contributing again to this important effort. I can't wait to see the final release!

[/general] permanent link