Feeds

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

Combat fraud and increase customer satisfaction

More from The Register

next story
Ubuntu 14.04 LTS: Great changes, but sssh don't mention the...
Why HELLO Amazon! You weren't here last time
This time it's 'Personal': new Office 365 sub covers just two devices
Redmond also brings Office into Google's back yard
Got Windows 8.1 Update yet? Get ready for YET ANOTHER ONE – rumor
Leaker claims big release due this fall as Microsoft herds us into the CLOUD
Next Windows obsolescence panic is 450 days from … NOW!
The clock is ticking louder for Windows Server 2003 R2 users
OpenBSD founder wants to bin buggy OpenSSL library, launches fork
One Heartbleed vuln was too many for Theo de Raadt
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
Red Hat to ship RHEL 7 release candidate with a taste of container tech
Grab 'near-final' version of next Enterprise Linux next week
Apple inaugurates free OS X beta program for world+dog
Prerelease software now open to anyone, not just developers – as long as you keep quiet
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
prev story

Whitepapers

Mobile application security study
Download this report to see the alarming realities regarding the sheer number of applications vulnerable to attack, as well as the most common and easily addressable vulnerability errors.
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.
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.
Combat fraud and increase customer satisfaction
Based on their experience using HP ArcSight Enterprise Security Manager for IT security operations, Finansbank moved to HP ArcSight ESM for fraud management.