Fresh effects for Cocoa apps

One file, many identities

Combat fraud and increase customer satisfaction

Mac Secrets I've decided to delve into Apple's CoreGraphics framework, to explore some of its undocumented features. Using these routines, you can add all sorts of wondrous effects to your Cocoa applications.

I ought to begin by saying that I'm claiming no originality for the undocumented nuggets. If you trawl Google, you'll find similar revelations of CoreGraphics naughtiness scattered all over the net. The key to understanding what functionality is available is a header file called CGSPrivate.h. This file is the work of many individuals, and it attempts to elucidate some of the undocumented routines and data types contained inside the framework.

You should also be aware that there are many different versions of this file, with subtle naming differences between them. For example, a connection between your application and the window server is variously referred to as a CGSConnection, a CGSConnectionID and a CGSConnectionRef. Confused? You will be.

In an effort to cut through the madness, I've included a fairly recent version of CGSPrivate.h as part of this month's project. As well as being relatively non-verbose - it uses the first of the three naming styles given above - it also makes a big effort to divide the various routines up into functional areas, and it includes some later additions such as CGSAddWindowFilter, about which more later.

I've made a few very minor changes to the file, all identified by my initials, DSJ. Despite being a true-blue Brit, I long ago gave up the fight regarding how to spell "colour", when programming! I've also added a new typedef to make clear when a transition handle should be supplied.

With the above in place, I created a new NSWindow category called CGSSecrets in order to demonstrate some of the functionality available through these undocumented CoreGraphics calls.

I should point out that in general I think it's easier to do this through a category rather than subclassing NSWindow. For one thing, the code is more immediately reusable, and for another, it can be applied to any window in your application. Moreover, you don't have to start introducing custom class names to Interface Builder. On the negative side, however, it does mean that you can't add new instance variables.

The category method below produces a window flipping effect that should be familiar to anyone who's used one of the many Dashboard widgets. As you'll know, many widgets have a little info button - usually in the lower, right corner - that, when clicked, causes the widget's window to flip over to reveal user-settable preferences or other information.

- (void) flipWithDuration: (float) duration forwards: (BOOL) forwards
  CGSTransitionSpec spec;
  CGSTransitionHandle transitionHandle;
  CGSConnection cid = CGSDefaultConnection;
  spec.type = CGSFlip;
  spec.option = 0x80 | (forwards ? 2 : 1);
  spec.wid = [self windowNumber];
  spec.backColor = nil;
  transitionHandle = -1;
  CGSNewTransition (cid, &spec, &transitionHandle);
  CGSInvokeTransition (cid, transitionHandle, duration);
  [[NSRunLoop currentRunLoop] runUntilDate: 
        [NSDate dateWithTimeIntervalSinceNow: duration]];
  CGSReleaseTransition (cid, transitionHandle);

I've seen Cocoa code fragments that attempt to do something similar without using the undocumented CoreGraphics routines but - in general - they don't perform well, especially if a complex graphic is on the window being flipped. This is a strong argument for Apple listening to the various enhancement requests and making more of this stuff official.

The flipWithDuration:forwards: routine takes a float specifying the animation duration - half a second seems about right - and a direction so that you can flip one way, and then flip the other way when returning to the original view.

The code works by using the undocumented CGSNewTransition routine to create a new transition according to the specification provided in the CGSTransitionSpec record.

The key thing here is the spec.type field, which is set to CGSFlip for a horizontal flip transition. Setting it to CGSWarpSwitch will give you a vortex-like transition, whereas CGSCube will give you the familiar cube transition - as when swapping from one user to another with fast user switching enabled.

Fliipping a complex graphic

Flipping a complex graphic doesn’t mean lost performance with the CoreGraphics routine

Incidentally, if you want to include the entire desktop - rather than just your application window - in the transition, then you can set spec.wid to zero. Typically, you wouldn't want to do that, but the paradigm obviously makes sense for user switching. Also bear in mind that, with a multi-display configuration, setting wid to zero will apply the transition to all displays individually. I'm not aware of any call that would make CoreGraphics treat the entire desktop surface as a single transition.

The demo program shows how to use a CoreGraphics transition and demonstrates an undocumented method of NSApplication called speakString:. Just pass any string to this method, and the speech synthesizer will produce it using the default voice.

Finally, the demo program also shows another interesting CoreGraphics routine called CGDisplayVisualBell. This is nothing to do with displaying visual bell symbols on the screen. Rather, it's a routine that attempts to apply a bell-shaped luminosity curve to the screen with respect to time. It simulates the rise, sustain and fall time of an old-style flash gun, and is useful when taking a photo, or producing some similar effect. Source code of the demo application can be downloaded from here.

More exploration of CoreGraphics next time. ®

High performance access to file storage

More from The Register

next story
Android engineer: We DIDN'T copy Apple OR follow Samsung's orders
Veep testifies for Samsung during Apple patent trial
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Batten down the hatches, Ubuntu 14.04 LTS due in TWO DAYS
Admins dab straining server brows in advance of Trusty Tahr's long-term support landing
Microsoft lobs pre-release Windows Phone 8.1 at devs who dare
App makers can load it before anyone else, but if they do they're stuck with it
Half of Twitter's 'active users' are SILENT STALKERS
Nearly 50% have NEVER tweeted a word
Internet-of-stuff startup dumps NoSQL for ... SQL?
NoSQL taste great at first but lacks proper nutrients, says startup cloud whiz
Windows 8.1, which you probably haven't upgraded to yet, ALREADY OBSOLETE
Pre-Update versions of new Windows version will no longer support patches
Microsoft TIER SMEAR changes app prices whether devs ask or not
Some go up, some go down, Redmond goes silent
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Ditch the sync, paddle in the Streem: Upstart offers syncless sharing
Upload, delete and carry on sharing afterwards?
prev story


Designing a defence for mobile apps
In this whitepaper learn the various considerations for defending mobile applications; from the mobile application architecture itself to the myriad testing technologies needed to properly assess mobile applications risk.
3 Big data security analytics techniques
Applying these Big Data security analytics techniques can help you make your business safer by detecting attacks early, before significant damage is done.
Five 3D headsets to be won!
We were so impressed by the Durovis Dive headset we’ve asked the company to give some away to Reg readers.
The benefits of software based PBX
Why you should break free from your proprietary PBX and how to leverage your existing server hardware.
Securing web applications made simple and scalable
In this whitepaper learn how automated security testing can provide a simple and scalable way to protect your web applications.