Feeds

Fresh effects for Cocoa apps

One file, many identities

Secure remote control for conventional and virtual desktops

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

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.