Friday, July 28, 2006

Using Accessory Views in Print Panel

Intended Audience


Intended for people having difficulties with accessory views in the print panel. Especially on how to handle option changes in this accessory view.
Level: Intermediate

Problem


Lately for a project I was quite stuck on how to let the user set some options in the print panel. Especially the apple docs were not very helpful, so that's why I decided to write this quick how-to. I also searched the web if there was any information available from other sources, but did not find anything.

So here's the deal: I have an application that lets teachers create exams. Now the teacher can choose whether (s)he wants to print solutions along with the questions in such an exam (e.g. for teaching assistants whom correct the exams). The most intuitive place to put this option is in the print panel itself. There we add a checkbox "Print solutions". You can see this on the picture below, note that the name of the application is Questions.



So now we know what our goal is, next we take a look on how we reach this goal.

Solution


The checkbox with "print solutions; is contained into a custom view, which is augmented to the print panel. This is also known as an accessory view. I simply add the view to my MainMenu.nib and bind the value of the checkbox to a custom key, named PrintSolutions, in the Shared Defaults. Don't worry if no Shared Defaults instance is shown in MainMenu.nib yet, it will be automatically added when you bind a value to it.



Now we can query the state of PrintSolutions from anywhere in our applications by asking the shared defaults. We could do this like this:

[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"PrintSolutions"];


To be able to access our PrintAccessoryView (which contains our checkbox) from code we add an outlet in our AppDelegate (or whichever class that catches the print action).

 IBOutlet NSView *printAccessoryView;


The next step is to add our view to the print panel. This is where my approach differs from Apple's documentation. But first how the docs of Apple do it:

- (void)print:(id)sender;
{
// create a printable view for our exam and create a print operation with this view
...
NSView *examView = [examPrintView printableView];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:examView];

[op setAccessoryView:printAccessoryView];
[op runOperation];
}

So first we create our view for the exam, not knowing whether the user wants the solutions or not, and create a printoperation that will print this view. Next, we add our accessory view to the panel, so that user can make his choice whether he wants the answers printed or not. Finally we fire the operation away by calling runOperation. When run operation is called the print panel shows up and then our user can click on the checkbox.

The problem is, that it is already too late now. The printable view for the exam has already been created and passed to the printoperation for printing. Whatever the user clicks in the printing panel will not have any effect. Thus, we need a solution where we first show the panel and afterwards create the view and fire up the printoperation.

Here is my solution:

- (void)print:(id)sender;
{
NSPrintPanel *printPanel = [NSPrintPanel printPanel];
[printPanel setAccessoryView:printAccessoryView];

[printPanel beginSheetWithPrintInfo:[NSPrintInfo sharedPrintInfo]
modalForWindow:[self window]
delegate:self
didEndSelector:@selector(printPanelDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}


In this approach we just create a print panel and fire away the panel with a callback to the
printPanelDidEnd:returnCode:contextInfo:
selector. And here's what this looks like:

- (void)printPanelDidEnd:(NSPrintPanel *)printPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
BOOL printSolutions = [[defaults valueForKey:@"PrintSolutions"] boolValue];

// Create the printable view with or without the solutions
...
NSView *examView = printSolutions ? [examPrintView printableView] : [examPrintView printableViewWithSolutions];

// PrintOperation
NSPrintOperation *printOperation;
printOperation = [NSPrintOperation printOperationWithView:examView printInfo:printInfo];
[printOperation setShowsPrintPanel:NO];
[printOperation setShowsProgressPanel:YES];
[printOperation runOperation];

// Clean up the sheet
[NSApp endSheet:[[self window] attachedSheet]];
[[[self window] attachedSheet] close];
}
}

Now we have control over our view before we run the printoperation. So we first can check the value of our defaults and build up a different view accordingly. Pay attention that you also turn off showing of the print panel in the print operation with setShowsPrintPanel:NO. Otherwise you will show the print panel twice.

Conclusion


So now we've learned how to read the options set by the user in the accessory view of the print panel, before running a print operation. I guess the approach found in Apple's docs also works, but is a little trickier and less efficient. When the user makes a change in the accessory view, this could fire up an action that changes the printable view somehow. For example when the user clicks the checkbox, an action
togglePrintSolutions:
could be fired. This method could in its turn then rebuild the view that was passed to printoperation, etc...

This is an elegant solution and I am puzzled why Apple didn't put this in its Printing documentation. Or maybe I just missed it. Anyway, I hope this proves helpful to someone.

Acknowledgments


I did not come up with this solution entirely by myself. When I could not find any documentation on this, I started looking at open source projects, to see how they solved it. The first one I found which solved my problem was Smultron by Peter Borg. So I am very grateful to Peter for his code, and I owe the solution entirely to him. Incidentally, it even looks to be a nice text editor that is well coded. I advise everyone to check it out.

References


Tuesday, October 25, 2005

Graphical Editing Framework for Cocoa

For my apprenticeship I am working on a project in Java in the Eclipse framework. Because we're working on a visual editor we make use of GEF (Graphical Editing Framework). The documentation for this is rather poor, but that's not the point of my post.

Now I have been searching for a similar framework in Cocoa, but haven't found anything. So here comes my mad megalomanic idea. Let's start a project on sourceforge to make our own framework, based on GEF. Of course making use of the idiosyncrasies of Objective-C and Cocoa, instead of the Java stuff in GEF.

I really believe such a project could provide a boost in Cocoa application development. Some input from other developers would be nice.

Mind, that I currently don't have time to immediately start something like that. Just some thoughts and dreams for the moment.

References:
Eclipse: http://www.eclipse.org
Gef: http://www.eclipse.org/gef/

Tuesday, October 04, 2005

Localized strings with a specified localization

I have been searching for a way to get a localized string for a specified localization for some time now. Finally I have found the solution. Well to be honest, I was given the solution by a certain Douglas Davidson on the CocoaDev mailing list. Thank you very much Douglas!

Here's the problem: Assume you have an application which has multiple localizations, but needs to access the localizations besides the preferred localization of the user at runtime. A simple example could be an App that I am currently working on, which among other things prepares exams for teachers. Those exams could be in a different language than the one the program is currently running in. I would like to use something similar to NSLocalizedString for the static text on the exam.

After finding out this wasn't provided by NSBundle in Cocoa yet, I have decided to make a category which adds support for this.
The important thing here is the macro NSLocalizedStringForLocalization. This is essentially the same as NSLocalizedString, except that you can specify the localization you want. Check out the code:
NSBundle_Extensions.h
NSBundle_Extensions.m

[update] The strings dictionary is now cached
[update 2] Fixed bug explained in comments by Jonathon

Tuesday, August 30, 2005

Delirium Tremens




I've tried out a new beer today called Delirium Tremens. It's brewed in Melle (Belgium) which is actually only 10km from where I live. I must say it's a real shame that I didn't try it out any sooner. This is really A-class beer.

I like it so much I decided to do some research on it, and it turns out that this beer has been elected as best beer in the word in 1997. The description in the image is true poetry and I could only say amen to that.

Monday, August 29, 2005

Logging method invocations in Objective C

Here is a code fragment I use to log method invocations in Objective-C. The great advantage of Objective-C is that you can find out what the name of the method is that is executing. I don't believe this is possible in a language like Java.

QSLogMethodInvocation macro
// Prints a line in the console stating the name of the
// method that is invoked.
//
// For example if class Foo has a method bar, the definition
// of bar in the class Foo could contain QSLogMethodInvocation().
// The following message will be logged: Invoked bar from class Foo
#define QSLogMethodInvocation()\
NSLog(@"[LOG] Invoked %@ from class %@",\
[NSString stringWithCString:(char *)_cmd], [self class])


QSLogAbstractMethodInvocationError macro
// Prints a line in the console stating an abstract method
// was invoked.
//
// For example if class Foo has an abstract method bar,
// the definition of bar in the class Foo should contain
// QSLogAbstractMethodInvocationError().
// The following message will be logged:
// Error (Foo): abstract method bar invoked
#define QSLogAbstractMethodInvocationError()\
NSLog(@"[LOG] Error (%@): abstract method %@ invoked",\
[self class], [NSString stringWithCString:(char *)_cmd])

First post

Here is the classical first post.

Welcome to my blog! I will be writing some information about my private and professional life on this blog. This will mainly be about development of Cocoa applications and my studies.

Hopefully this will grow out to be a fun and resourceful venture for me and you readers.

I am pleasantly surprised by the clean interface of blogger.com. Last year I had a project to develop a blogging application, as a project for my studies, with a group. The results are visible on this site: se5_2005. Upon request I could start up the blog portal application itself on the server.