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?

VMWare Fusion 3 impressions

OS X, Tech, Windows 3 Comments

vmware_fusion3As soon as Macs started running on Intel, they became infinitely more attractive just because suddenly you had the option of using Windows on them too if you needed to. Because let’s face it, as lovely to use as OS X is, and as much as its popularity has grown, the majority of the world still runs Windows. Boot Camp is a great little tool provided by Apple which makes setting up a dual-boot into Windows generally a breeze, barring a few small niggles such as the slightly ropey support for the extended functions of the track pad (two-finger right-clicking and scrolling is very flaky).

I have Vista installed as a secondary boot on my 2007 MacBook Pro, something I’ve come to regret as it’s regularly far more hassle than XP would have been. In practice, I’ve found that I hardly use it, with weeks passing without me bothering to boot up Vista (which makes it worse, because when I do boot it up its intrusive and wholly unstoppable update process starts kicking in in the background, making me grind my teeth at the CPU / RAM loss). Nevertheless, it’s useful at times.

I’ve dallied with virtualisation occasionally, from Parallels to VMWare to VirtualBox, but have been generally disappointed – my day-to-day work and most of my recreation involves 3D acceleration in one form or another, and virtualisation has historically been pretty bad at supporting it, making it an impractical option. VMWare Fusion 3, however, claims support for Shader Model 3, which is good enough for the vast majority of the work that I do (Dx10+ is still a tiny, tiny niche that is still rarely in demand), so I thought I’d give it a shot.

One of the nice things about Fusion 3 is that it lets you boot your existing Boot Camp partitions in a VM, which meant I could test it quickly. Parallels supported this too but I found it didn’t work that well in practice when I tried it. Fusion 3’s support is excellent – just a few minutes of adaptation and the requisite install of VMWare Tools on the Windows partition and it was up and running. The main problem I initially encountered is that the default 1GB memory allocation is, of course, absolutely no use for running the sweating hunk of lard that is Vista. This is the OS that is capable of getting up to 1.2GB on my machine after first boot with only Explorer open, if you’re unlucky enough that it feels it needs to run update processes in the background. I only (only??) have 2GB on my laptop so I had limits, but a quick tweak of the VM to 1.6GB (and also letting it use multiple CPUs) made it run OK and still let me jump back to OS X, albeit a bit slower than usual (but show me a post-millennium Windows OS that can stay usable on 400MB!).

For general non-specialist use, Fusion 3 is as awesome as other virtualisation tools. Either in a window, full screen, or in ‘Unity’ mode, where Windows apps look like first-party OS X windows in your task list (a bizarre feeling to have Explorer and Visual Studio show up in Exposé), it works great. They even fix the track pad issues, it works much better than in Boot Camp natively. I’d definitely want to upgrade the RAM on my machine if I did this regularly, but it’s certainly solid. But, for me, it’s all about the 3D, so let’s get to that.

I ran through a few GPU tests, after having to wait an hour or so for Vista to go through its background update processes so that it stopped sucking CPU and RAM, distorting my performance sampling (grr), and sure enough all the OGRE demos ran ok. They did, however, run pretty slowly; simple fixed-function demos that usually run at 2000fps on this machine ran at about 300fps, and moderately complex SM2 demos usually pegged at 250fps here ran at about 40fps. Highly complex SM3 demos (e.g. OgreSpeedTree) were unfortunately almost unusable. So, you can expect a pretty steep performance penalty of at least a factor of 6 times running virtualised 3D in my experience. Clearly, at this performance degradation, this would be no good for me as a sole Windows option on this machine. But, it still is valuable, since it’s using the Boot Camp partition, I can use this to quickly test things for compile & run-time behaviour, albeit at unrealistic performance levels, and reboot ‘properly’ into Windows for the times where I want to performance test or demo something.

That is, I would be able to if it wasn’t for Windows Activation. As we all know, this is Microsoft’s anti-piracy strategy and involves requiring you to re-activate your copy of Windows within 3 days if a ’significant’ hardware change is detected. Since a VM basically emulates all of the core system processes, inevitably Windows sees that as a ’significant’ change and requires you to reactivate. You can do this online, but only 5 times, after which you have to phone someone every time you want to do it. If I used my Boot Camp partition in both modes, as is useful to me, I’d have to reactivate way too often.

There are some threads and articles about this issue on the VMWare site; they claim that provided you follow this article, then the Windows activation monster will be sated and leave you alone when switching between Boot Camp and Fusion 3. No such luck for me, I’ve rebooted many times between Boot Camp and VMWare and have had the VMWare Tools installed since the first boot, and all I continuously get is an activated product when I use Boot Camp, and a ‘2 days to activation expiry’ message in VMWare. I’m not the only one: people are reporting this issue in the forums, including with Windows 7.

If they can’t get the activation issue fixed, so that I can choose to use Boot Camp and VMWare interchangeably, this effectively kills the product stone dead for me, as with every other virtualisation product before it. Which is a shame, because apart from this,  it works great. It’s Windows fault of course, rather than VMWare’s, but regardless the effect is the same. This is a perfect example of bad, customer-unfriendly DRM – it’s especially galling to have MS accuse me of stealing when in fact I’ve paid through the nose for many, many copies of Windows before, and am set to do so again with their expensive Windows 7 (Pro+) pricing – where they still seem to stick to this ridiculous principle. Another option would be to free up a spare Windows license from my collection and have separate Boot Camp and VMWare partitions, but I’d really prefer not to burn the time & disk space on maintaining two installs.

Fix the activation issue and I’ll be buying a copy of this plus a 4GB RAM upgrade immediately. Until then, it’s another nice product that’s scuppered by one small, but nevertheless major issue.

Edit: Ok, I’ve resolved this issue. Actually the original KB article on the VMWare site was correct, I just didn’t follow it correctly. In my defence, the key to success is in the small print at the bottom of the article rather than the ordered steps: you have to make sure that you’ve booted once into Boot Camp with the VMWare Tools installed, then afterwards to start the VM under OS X and reactivate Windows. Having done this, both my hosted VM and Boot Camp configuration show up as ‘activated’, which is precisely what I need.

So, now to buy a copy of this puppy and get 2 new sticks of RAM to shove in the laptop. :)

Accented characters on OS X

OS X, Tech 4 Comments

I can’t believe this is the first time I’ve needed this on OS X, but it came about from needing to write a document for a European customer and suddenly realising I didn’t know how to make an umlaut on my Macbook Pro’s British keyboard. On Windows I might fire up the Character Map, but I didn’t know how to do it on OS X. Here’s what I discovered:

  1. OS X friendly apps like Mail, Safari, iCal and even Firefox have a ‘Special Characters’ entry on the Edit menu which brings up an equivalent of Character Map.
  2. For less OS X friendly apps (like Open Office), you can add a menu bar item to do the same everywhere under System Preferences, Language & Text, Input Sources – check the Keyboard and Character Viewer option and make sure the Show Input On Menu Bar is enabled. Then you just click the new icon on the menu bar every time you need the character browser.
  3. The most common ones have keyboard shortcuts which modify the next character you type afterward – Option-e puts an acute accent on the next character you type, Option-` is a grave accent, Option-u is an umlaut, Option-i a circumflex, Option-n that weird Spanish squiggle ;) While experimenting I found the ® and © symbols too (Option-r and Option-g respectively, Option-c is the cedilla)

So there you go – useful stuff if you’re on a British (or presumably US) keyboard and need to deal with non-English names from time to time. If you already knew this, great – this is just for people in my position who have to use these characters rarely and haven’t encountered it on a Mac yet.

Homeless Frank & Laptop Hunters

OS X, Personal, Tech, Windows 10 Comments

It’s always fun to watch Apple and Microsoft slug it out in the advertising space – here in the UK we mostly have to do this via YouTube, since apart from a short stint of amusing Mitchell and Webb Apple ads and those pretty bland “I’m A PC” ripostes, we don’t really see the front-line assaults which take place on US TV screens.

So I hear that MS have a new set of ads out, where “regular” people go and look for a laptop, whereby they look at the Mac and say “whoah, far too expensive!” and then go and buy a Dell instead. Fair enough, the 3rd party PC market certainly gives you a wider choice of blending specifications than Apple does – in practice, Macs aren’t actually much different in price to a similarly specced PC, it’s just that all the components are generally of similar ‘grade’ – so you can’t cut corners to save money like buying something with a big screen but a crappy GPU, or a large HD with a slow motherboard, or a fast CPU but crappy battery life.  Of course, many people don’t realise they’re making these sacrifices and just look at the price – but if you do know what you’re doing, you can tailor a machine closer to your needs. Anyway, I enjoyed the “Homeless Frank” spoof of these new ads:

A couple of years ago I would have made the same arguments against the Mac that MS makes with it’s Laptop Hunter series; and indeed I did, when a Mac-owning friend tried to convince me to buy one, despite being a .Net guru (who now works for Microsoft!). However, now that I’ve owned a Mac for almost 2 years, I feel completely different – in a laptop at least, I’m very willing to sacrifice a little configuration flexibility in favour of having a device that is of uniformly good quality, and is nice to use. After all, laptops are always compromised in terms of upgradability once you’ve bought them, so it’s generally better to buy something decent from the outset anyway.

I know that buying a Mac laptop is going to encourage me to spend a little more money than I otherwise might get away with. But, what I get for that is a really nice device, that has the added bonus of being able to run OS X as well as Windows. I still use Windows every day, almost exclusively because of Visual Studio these days (and some games) since everything else I use runs on the Mac too anyway, but running both Windows and OS X on the same machine merely serves to make me love OS X more, despite still being a newbie with it in many ways. Windows is fine to use and all, but there’s something about the way OS X just gets out of the way, doesn’t pester me with stupid warnings all the time, doesn’t need a virus scanner to slow it down, allows me to unmount my USB drives without hunting down every Explorer window that is using it first, and countless other little things that have slowly endeared me to it despite being a total skeptic to begin with. I’m a technical guy by nature, but even I can appreciate technology that doesn’t waste my time with trivial stuff I don’t want to care about – and at the end of the day Windows (and Linux) still feels like it’s designed for “PC users”, whilst OS X feels like it’s designed for people, specifically people with stuff to do other than worrying about keeping the computer happy. The day that XCode equals Visual Studio in functionality (it’s not far off, but it’s not there yet), and I can run Steam on OS X, is the day I might seriously consider not using Windows on a daily basis anymore. But, we’ll see whether Windows 7 changes that view.

Leaping Lately to Leopard

OS X, Tech 14 Comments

For a geek, I can exhibit luddite tendencies sometimes. I don’t run Vista yet on anything other than secondary test machines, because I really don’t like it very much  – I feel it’s burning additional machine resources in a way that adds little or no value to my productivity or user experience compared to XP, and I’ve had several usability / stability hassles with the test machines I’ve had. I also remain very skeptical about the relative importance of Dx10 given that the vast majority of users either don’t have Vista, or don’t have a card worthy of being called Dx10 compliant, even in gamer circles. In short, I love reading about and playing with new tech, but when it comes to spending my money and resources (particularly time), practicality and not hype tends to rule my mind.

Another example is that I only just upgraded my MacBook Pro to OS X Leopard, even though it’s been out for 9 months already. When it first came out I was reticent to upgade immediately, wary of early problems and also that I was only just getting used to OS X anyway. Since then, I’ve always seemed to have an excuse not to do it – some impending trip / presentation / deadline which meant it wasn’t convenient, and really just lack of time.

Nevertheless, I finally made some time to do it last night, mostly because a couple of people in my family are getting or likely to be getting Macs soon, and of course those will come with Leopard, so I’d like to be in sync. Also Snow Leopard isn’t going to be out for another year by the sounds of it, so it’s still worth me upgrading and not waiting for the next one.

My early impressions have been good – it’s very much an evolution rather than revolution, but then if it ain’t broke, don’t fix it. I like how once again the things that are good for flashy demos are actually very useful in practical terms too, in particular ‘Quick Look‘ is really very handy. Basically you can just press the Spacebar whenever you’re selecting an item in Finder or whatever, and it pops up a window allowing you to browse the content of the file, whether it’s a PDF, Powerpoint presentation, document, image, archive, whatever – and it does this without starting any applications, so it’s super-duper-fast. Incredibly handy for skimming through a bunch of documents when you’re not sure exactly which one you want just from the filename, without the overhead of firing up apps and loading everything fully.

Other things are handy too, Spaces are handy but nothing new if you’ve used multiple desktops on other systems before, and the fold-out stacks make shortcuts to common folders more convenient than before. I doubt I’ll use many of the other features – all my important stuff is backed up on a server so I don’t need Time Machine (although I will definitely be recommending that one to my parents), and I’m too attached to Firefox and Thunderbird to get excited about the Safari and Mail enhancements. Oh, I could see myself using the presentation features of iChat though, that’s something I normally have to use GoToMeeting or similar for.

As an upgrade I think Leopard is a bit expensive at £70 for what it is (another reason I put it off), but nevertheless it’s a nice increment to what has become my favourite desktop OS over the last 12 months. I really can’t see that changing in the near future either – with Snow Leopard, Apple seems to be taking the opposite approach of those in Redmond by concentrating on optimizing & further streamlining what they have rather than stuffing in more and more features, which sounds good to me. OS X already does everything I want, and is no slouch in the performance dept already, certainly compared to the hulking, sweating mass that is Vista, but if they can make it even faster, and smaller, then all the better.

Hmm, I’m definitely becoming an Apple cheerleader. A bit scary, but hey, they make stuff I really like to use. Sure you have to pay a little extra, but I’d rather choose to pay for something I can take pleasure in using, than be coerced into paying for something that at best leaves me apathetic, and at worst annoys the hell out of me.

Hex Colour Picker for OS X

OS X No Comments

If, like me, you were wondering why the excellent Pixelmator got out of the door without an easy way to see the colours you’ve picked in HTML-friendly hexidecimal form, the way you can in Photoshop – well, it’s actually that OS X has an altogether more elegant way of handling this kind of thing.

I didn’t know this, but the colour picker widget shown in PixelMator is a standard OS X one, and you can plug in new widgets for it. One such plugin is the Hex Colour Picker. Once installed, PixelMator picks it up as a new toolbar button on the colour picker widget (as will every other app using this widget) and you can copy the HTML-friendly colour from there. Simple, but very effective.