Feeds

Fresh effects for Cocoa apps

One file, many identities

Choosing a cloud hosting partner with confidence

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. ®

Intelligent flash storage arrays

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
Torvalds CONFESSES: 'I'm pretty good at alienating devs'
Admits to 'a metric ****load' of mistakes during work with Linux collaborators
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.