Cocoa tip: using custom outline view cells designed in IB

Cocoa, Development, Objective C No Comments

I only started learning Objective-C and Cocoa in mid-May, and for the first time I think I actually have a tip to contribute to the wider community. It’s about using custom cells in NSOutlineView, but those which you want to design inside Interface Builder rather than drawing manually.

If you’re an iOS developer, you’ll be wondering why this deserves a blog post – it’s easy to do in Cocoa Touch! Well, yes it is easy on iOS, because Apple have specifically allowed you to design table view cells in Interface Builder. When targetting Mac OS X though, it’s actually pretty awkward, and here’s why: in Cocoa Touch, the class which draws the cells in a table is UITableViewCell, which is a subclass of UIView – meaning you can drag one onto the canvas in Interface Builder and lay stuff out right there. In Cocoa, in contrast, the cell is simply represented by NSCell, which is not an NSView subclass. This means Interface Builder will not let you play with them, you draw them by implementing drawWithFrame:inView: instead. I think Apple realised the problem with this design in time for Cocoa Touch but obviously felt they couldn’t break the existing Cocoa interfaces. There are also many differences between the instantiation of NSCell versus UITableViewCell – there’s only one NSCell for all rows in a table / outline view, compared to a type-indexed pool in Cocoa Touch.

The problem boiled down to this: if the contents of your table / outline cell is non-trivial, or if you just don’t want to write a bunch of layout code, it’s a PITA to implement a custom look in NSOutlineView, such as the one in the picture, especially if you want custom controls embedded in it. For my current Cocoa app, I really wanted to design my cells (and I have 2 different styles for group levels and detail levels) in Interface Builder to save me hassle, including using Cocoa Bindings to hook up some dynamic fields within. Many internet searches later, and mostly the answer I found was that it couldn’t be done. Luckily, I’m too stubborn to take no for an answer, and eventually I figured out a way to do it. Details are after the jump, with an example project to show it in action.

Read the rest of this entry »

OS X Localisation: incremental genstrings and UTF-8 files

Development, OS X No Comments

I came across a couple of interesting issues when I came to do the first pass of writing the text for the user-visible strings I’d been setting up for a Cocoa app I’m writing (painfully slowly as I learn the nuances of the environment), and I thought I’d share them. Full details are after the jump, since I’ve embedded a large script in the post.

The basic principle for text localisation on OS X is that, like most systems, you externalise your user-visible strings in string tables and reference them by keyed aliases in code – in this case using NSLocalizedString. Apple provide a tool called ‘genstrings’ which extracts all these into a template strings file called Localizable.strings which you can then populate per language – localised files are kept in folders called en.lproj, fr.lproj etc and helpfully they’re picked up by default like this.  So far, so good.

Read the rest of this entry »

Taking a bite of the Apple

Development, OS X, Personal 13 Comments

Giving up the leadership of OGRE was a sad moment for me, but in hindsight it has also been rather liberating. For 10 years I’d spent most of my energy on OGRE or on projects that were related to OGRE. There was an implicit understanding both from the community and from myself that everything I embarked on would in some way tie into OGRE – and indeed my business has always been based on a constant balancing act between how I can make a living while also promoting and advancing OGRE. I’d tended to major on the latter rather than the former most of the time as it happens, because I had an emotional attachment to the project and a feeling of responsibility and custodianship that I took very seriously. So when I finally admitted to myself that my back couldn’t take the ongoing demands of being an open source leader as well as making a living, the big question was: what next?

Over the years I’ve learned a couple of things about choosing what projects to work on – follow your gut, and work on things you’d do even if there was no money in it. Yes, you need to do a business case and convince yourself that there’s a viable market for what you have in mind, but all that’s irrelevant if you don’t feel strongly about what you’re doing, because it’s passion and enthusiasm that will get you through the difficult times. So I sat down and gave some thought to what really excited me these days, what I liked using and what technical directions piqued my interest. I still find 3D and games fascinating, but they’re far from my only interests.

So, I realised that one area that I’ve been dying to get my teeth into properly for ages but had never found the time before, was coding specifically for Mac OS X. In 3 years I’ve gone from a total newcomer to the platform, to a staunch advocate of it. However until now I’d never really had time to play with developing on it, beyond porting cross-platform C++ code and providing / using intermediate libraries. One thing I learned in those 3 years as a user was how much better applications designed for OS X felt to use compared to those that were just ported via a common UI layer (like wxWidgets / Qt), and I’m convinced now that while cross-platform infrastructural code is great, user experiences are far better when designed with the specific platform in mind – increasingly that means OS and physical device now of course. Sure, cross-platform UIs save the developer time, but the result is often a watered-down experience – early on I liked OS X applications that felt like Windows, or ran the same on both platforms – now I  do not. Such carbon-copying applications were helpful while I was unfamiliar with the platform, but now it’s just glaring to me how basic their compatibility with the OS typically is, and how the UI styles clash with the expected standards.

So, I decided I wanted to learn how to target OS X specifically, and had a couple of ideas for projects I could do with it, which meant learning Objective-C. At first, I hated it and tried to escape via more familiar technologies like Objective-C++ and PyObjC. Ultimately I found shortcomings and limitations of those routes and returned to Objective-C, and the more I used it, the more my animosity toward it diminished. In the end I realised the problem was that I needed to adapt to the environment, rather than try to adapt it to my previously learned styles and behaviours. Sure, missing elements like namespaces might still nag me, but on balance the blend of static and dynamic language elements works very well for the intended use. And besides, I really didn’t want to be ‘that guy’ – the programmer that having decided one language / tech is ‘the best’, then tries to apply it everywhere, regardless of suitability; I like to think I’m a bit more flexible & multicultural than that.

I’ve also learned that Cocoa is a very, very smart system. Mad as a bison if you’re used to other systems beforehand, but persevere with it and resist the urge to hide it under some vanilla layer that you’re already familiar with, and you discover it’s really very powerful. Not to mention the Core Animation and Core Graphics frameworks are a lot of fun.

It’s funny, I’ve spent so many years concerning myself with providing compatibility across multiple OS’s, multiple GPUs, multiple render APIs, and multiple drivers, it’s a genuine joy to actually forget all that for a while, and concentrate on an end goal with a finite number of permutations for a change – and not to shy away from using platform-specific features.

While I’m still very much an advocate of open systems, I look at things slightly differently now – that data & protocols should be open, and that we should all re-use & collaborate on common, preferably open source infrastructure (like OGRE), but that the ‘last mile’ to the user is the least suitable for generalisation, because the more specific you can make that interface to what the user expects on their OS & device, the better that experience will be. And at the end of the day to the user, that experience is the application, and thus all that really matters – and I feel that Apple gets that, in a way that very few others do.

So, I’m having a great time learning to be an Apple developer so far, I’m going to see where this takes me for a while. My gut says it feels right, and I’ve learned to listen to my gut :) I love the platform, it’s a total change of pace and technology, it’s something I’ve had an interest in for a while, and the Mac has quite a thriving community of quality independent app developers that I can try to join – what’s not to like?

Open source is most important to producers rather than consumers

Development, Open Source 2 Comments

Gartner haven’t exactly been the sharpest tools in the box when it comes to predicting open source trends over the last few years, vastly underestimating it until about 2008, by which time it didn’t exactly take a professional analyst to tell you that it was popular. Still, now they’ve woken up to its potential, occasionally they post something useful. In particular, I liked a recent blog post about how open source is “trending towards customer obscurity” – that is to say that while open source is incredibly important to producers of software, the vast majority of consumers don’t really care how their software is made any more than they care how their car was made.

I support this view, and it’s one I’ve subscribed to for a while (although the somewhat condescending tone of the article is typical Gartner, the point is valid). My own open source software is aimed squarely at developers, where I think it adds value; since the users of my software are themselves making significant development investment in products using it, open source has significant advantages – the openness and participation-friendly nature of the development, the fact that the software can never be taken away from them either by company policy or acquisition, the fact that absolutely nothing is hidden behind any curtains so there aren’t any nasty surprises. When you’re investing your own time building on top of a foundation, there really is no substitute for being able to see all the working parts, should you want or need to.

Developers can be quite a broad church too – enterprises for example often have a need to modify and adapt software and that’s why open source has been very popular there too, even if they’re not actually making products of their own for publication.

But there’s also a vast group of people who are more traditional consumers (personally and in companies) – and they have no reason to care about open source if they’re never going to modify the software. There is a group of people who are philosophically dedicated to using free software (more so than open source) even if they never modify it themselves, but they’re a minority in the grand scheme of things. Most people that use open source do so because they feel it gives them what they need as users. Even personally, I use Linux on my servers not because I’m dedicated to using open source over the alternatives wherever I can (although I probably do have more bias in that direction than the average), but because it does exactly what I want in a server – it’s reliable, unobtrusive, cheap, has low hardware requirements and plenty of good software. Conversely, I don’t use Linux on the desktop much because personally I don’t feel it operates better than the commercial alternatives in that environment. I decide on a case-by-case basis what works best for me, and so do all but the most fanatical of users.

Now, of course open source regularly helps developers make better products (by using mature, reusable and adaptable components), which in the end can result in more users using software made from open source even if they don’t realise it. But the important thing to remember is that open source itself isn’t a marketing bullet point except to developers and the enthusiast minority. Like any gathering of like-minded people who mostly talk to each other, we open source developers / advocates can often forget that our enthusiasms aren’t necessarily shared by the rest of the populace. We have to remember that the end result is everything – and while open source definitely has a positive knock-on effect on product quality, it’s often a means to an end, not the end in and of itself. It’s obvious really, but worth keeping in mind.

Dependencies to build Git from source on [K]Ubuntu 9.04

Development, Linux 2 Comments

Git is picky when it comes to converting large, moderately complex Subversion repositories and so far the only option I’ve found that works reliably is using the very latest version on Linux. Forget about using 1.6.5 on Windows via msysGit, at least for the git-svn conversion it’s very, very unreliable. Similarly I found Git 1.5 on Linux very flaky for the svn conversion. This doesn’t give me the greatest confidence in Git but in order to properly explore all the angles, I’ve committed to making it work even if it means I have to monkey about a bit.

So, I installed a fresh Kubuntu 9.04 (and of course, 9.10 went stable a few days later) and tried to build Git 1.6.5 from source. The configure script is unfortunately a bit rubbish and doesn’t bother trying to detect the dependencies though, so for those that don’t want to go through the fail/retry build loop I went through, here are the packages you’ll want to install via apt from a clean version:

  • build-essential
  • curl
  • libcurl3
  • tk
  • subversion
  • libsvn-perl
  • cpio
  • zlib
  • zlibg1-dev
  • expat
  • perl
  • iconv

If like me you use the excellent wajig wrapper around apt, you can also do ‘wajig build-depend git-svn’, but that seems to install things that are not strictly necessary in the default build (but maybe are needed with some non-standard options).

sudo apt-get install curl
   41  sudo apt-get install libcurl
   49  sudo apt-get install tk8.4
   53  sudo apt-get install cpio expat
   55  sudo apt-get install zlib
   61  sudo apt-get install build-essential
   66  sudo apt-get install zlib1g-dev
   72  sudo apt-get install asciidoc
   75  sudo apt-get install xmlto

Yay, OgreSpeedTree 2.0b is done

Business, Development, OGRE 9 Comments

I’ve been pushing quite hard to get this done before I head off to Qt DevDays next week, and luckily it all came together in the last few days:

Some of the notable back-of-the-box (if there was a box) items:

  • Upgrade to SpeedTree v5 – supporting all the great new features. See the SpeedTree site for more details on this release.
  • More lighting options – Ambient Occlusion, Ambient Contrast, Specular Lighting, Transmission Lighting, Global Light Scalar, HDR.
  • Improved LOD fading – LOD transitions are now implemented via less perceptible techniques thanks to SpeedTree v5.
  • Dynamic soft self-shadowing via depth shadowmaps – (was also demonstrated on SpeedTree 4.2 in OgreSpeedTree 1.1.0)
  • Improved alpha to coverage support – alpha to coverage rendering has been significantly improved and is now enabled by default (you must render with at least 2xMSAA to see the effect), resulting in significantly smoother foliage edges and smoother fade transitions.

I’m just really, really glad to have got this done. SpeedTree v5 was quite a big change to handle, but hugely worthwhile, the guys at IDV have done a great job. I made the choice from v1 to do a ‘deep’ integration (ie all objects are first-party Ogre objects, everything uses Materials and RenderQueues properly, etc), which made the transition considerably more time consuming than using the higher level pass-through interfaces, so this took some effort. I like it this way though, it means it’s a ‘real’ integration as far as I’m concerned, not just a simplistic bolt-on.

There’s still more to do, not least of which a demo that’s more interesting than just a technical performance test :) But at least I can tick off one of the major things on my TODO now. I’d been putting in quite a few extra hours to try to handle this, and my Qt presentation preparation, and Ogre, and consultancy, and it has taken its toll a bit – I’ve been spending more time at the desk and think that was a contributing factor to me straining my back at the gym last weekend. Hopefully that’s just temporary, I’ve been a lot better these last few months.

Hope you like the shots anyway. I know, I’m pimping them all over the place in my Twitter, here and on the Ogre site, but a guys gotta make a living, right? ;)

Kaz doesn’t understand (most) developers

Development 12 Comments

Sony’s PR machine has been rather contrite of late, after some really quite stellar gaffes a few years ago, but comments from Kaz Harai in an interview recently are a firm return to the ‘what in Gods name were you thinking?’ school of PR. Ignoring the fanboy-baiting predictions of who’s going to ‘win’ (given the expansions in the game industry, does anyone have to ‘win’?), the bit that got my attention was when he talked about the (many would say unnecessary) complexity of developing for the PS3:

“We don’t provide the ‘easy to program for’ console that [developers] want, because ‘easy to program for’ means that anybody will be able to take advantage of pretty much what the hardware can do, so then the question is what do you do for the rest of the nine-and-a-half years?”

I had to read that twice to see whether he’d really, actually said that – it’s a pretty big insult to developers.

Developers are a problem-solving breed. But here’s the thing – unless you’re a student, or just hacking around on your own time for fun, you have deadlines. Time is money, and professional developers have people breathing down their necks for results, and having spent N times as long as estimated to utilise / optimise something doesn’t tend to go down too well. And as developers mature and become more savvy about the environment they work in, rather than just being geeky hackers, they generally start to look less for pointless puzzle boxes, and more for things that make them productive.

So what would they do with the other 9 1/2 years? They’d probably spend it on making new and exciting actual products rather than decoding an esoteric design.

Anyone can design something to be complicated. The real genius lies in making something powerful, and at the same time making it easy to access that power. You honestly shouldn’t be proud of a design that’s difficult to use – the layman’s way of looking at it might be “Hey, but developers will be squeezing new stuff out of it in 7 years time!”, but the more informed way of looking at it is “Developers would have been getting that power from day 1, if some idiot hadn’t buried it under so many layers of obscurity”.

Kaz, I’d advise skipping the GDC this year, or if you do, best to wear some projectile-proof clothing of some sort.

Qt 4.5 will be available under the LGPL

C++, Development, Open Source 7 Comments

I’m on the Qt (owned by Nokia now) mailing list since I have a commercial license for a client project, and I got a very interesting email today, telling me that on its release in March 2009, Qt 4.5 will be available under the LGPL.

This is really big news. Up until the current Qt 4.4, your only licensing options are a per-seat and per-platform commercial license (which adds up if you have multiple developers and multiple target platforms, which you will do if you’re using Qt anyway), or alternatively the free option which means you use it under the GPL – meaning all your own code has to also be GPL, with an exception that allows you to publish / use software under other open source licenses too, but nevertheless it all has to be public. There’s a pretty big chasm in between these two options that in my experience a lot of projects fell into.

So, offering an LGPL option from Qt 4.5 is a major development. Qt is a really excellent library, the best cross-platform, native code widget set I’ve seen, but the licensing cost often put people off; this changes everything. I’m actually quite surprised they’re going this far – I think just changing the commercial licensing structure to be a little less multiplicative would have made a lot of people happy, but the LGPL option really turns things upside down. Essentially it means that you get all the cost benefits of the GPL version, except there’s a ‘fire break’ of open source responsibility between Qt and your application, meaning a lot more people can consider it. Nokia’s stated aims are:

  • facilitate wider adoption of Qt across industries, desktop, web and embedded platforms
  • establish Qt as a de facto standard for application development
  • receive more valuable feedback and increased user contributions to ensure that Qt remains the best-in-class, cross-platform framework
  • extend Nokia’s existing platform commitment to the open source community

I’m pretty sure this move will achieve all of those goals. It may well take a little wind out of the sails of wxWidgets, which in my experience never seemed quite as polished as Qt, but the licensing was much more favourable – although of course the wxWidgets project is still a lot less ‘corporate’, so will continue to have an appeal from feeling a bit more open.

Interesting times anyway!