TweetFollow Us on Twitter

Print Dialogs
Volume Number:8
Issue Number:2
Column Tag:C Workshop

Related Info: Printing Manager Adding Items to the Print Dialogs

Modifying Print Dialogs

Tech Note #95 and beyond

By Greg Wilson, Etobicoke, Ontario, Canada

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

About the author

Greg Wilson is a Toronto area Macintosh consultant. He has been programming the Macintosh since 1985. His first article for MacTutor shows how to add Filter Routines and disable items in Print Dialogs.

The Problem

I programmed myself into a hole the other day. I was (am, seem to forever be) writing a game which can be played by several people over the network. Every time I passed through my main event loop I checked to see if there were any network messages to be processed. Unfortunately, there was a problem if the user selected a menu command which brought up a modal dialog, for example, when selecting the “About ” menu item. I had used the Alert function to handle the modal dialog like this:

/* 1*/

void ShowAboutMeDialog(void)
 short  itemHit;
 itemHit = Alert(rAboutAlert, nil);
 } /* ShowAboutMeDialog */

The program would sit on the line

/* 2 */

itemHit = Alert(rAboutAlert, nil);

This handles the window until the user hit an item, and so my main event loop network message check wouldn’t get executed and messages could be missed. I had to ensure that network messages weren’t missed (or timed out) while the user was sitting in a modal dialog.

The solution was to use a “Filter Proc” when calling ModalDialog directly, or indirectly through Alert, SFGetFile, etc which pass the FilterProc to ModalDialog. This Filter Proc would get called every time ModalDialog detected an event. My filter simply checked to see if there were any messages, processed them and then let ModalDialog handle the event in the usual way. Similar code has been seen in MacTutor numerous times in the past, my version (Net_filter) is shown in the sample code which follows the article. It gets invoked by changing the troublesome line to:

/* 3 */

itemHit = Alert(rAboutAlert, &Net_filter);

Pride Goeth

Before I got injured patting myself on the back, it became obvious that this would not work for Print Dialogs. There are two routines to conduct the modal print dialogs. The PrJobDialog and PrStlDialog functions conduct modal dialogs for defining the characteristics of the Print Job (quality, copies, page range, etc ) and the Page Setup (paper orientation, paper size etc) respectively. Neither of these accept a Filter Proc function. I could see no easy way around this, so I did the logical thing. I decided to leave it for later and hope the problem went away.

Of course things went from bad to worse. The BitMaps that I painstaking added to the display and printing didn’t print on an ImageWriter when the user selected Draft Mode. This was because Draft Mode is designed to just print the characters in the native character set of the device. BitMaps aren’t supported. PrGeneral was supposed to allow for printing of draft mode BitMaps under certain restrictions, but it seemed like it wouldn’t allow me to do what I needed. I wanted to disable the Draft Mode radio button; that way the user could never select Draft Mode printing, and so my BitMaps would always print. I knew from my experience, being unable to get a Filter Proc into these dialogs, that there was no way to patch a hook into these routines. Yet there must be some way to get at the dialogs since several commercial applications were disabling the button, and others were adding items to the dialogs. I now had two problems, so some action was required.

Salvation came in the form of Tech Note #95. This little beauty was built to explain how to add items to the Printing Manager’s dialogs. But more importantly for me, it showed the format of the data structures and described the procedures and functions required by PrStlDialog and PrJobDialog used to create and display the dialog information.

Inside PrJobDialog

When the PrJobDialog is called, it really only calls another function like this:

/* 4 */

ok = PrDlgMain(PrDlgStrctHndl,&PrJobinit);

(PrStlDialog operates similarly, except PrStlInit is used in the call to PrDlgMain. From now on I’ll just refer to PrJobDialog since that’s where Draft mode is used.)

PrDlgMain actually does all the work, and the first thing it does is call the PrJobInit routine. PrJobInit sets up a dialog record and puts it into a data structure. The data structure is defined in Printing.h like this:

/* 5 */

struct TPrDlg {
DialogRecord Dlg;
ModalFilterProcPtr pFltrProc;
PItemProcPtr pItemProc; 
THPrint hPrintUsr;
Boolean fDoIt;
Boolean fDone;
long lUser1;
long lUser2;
long lUser3;
long lUser4;
/*Plus more stuff needed by the particular printing dialog.*/

typedef struct TPrDlg TPrDlg;
typedef TPrDlg *TPPrDlg;

typedef pascal TPPrDlg (*PDlgInitProcPtr)(THPrint hPrint);

Then the PrJobInit function returns a pointer of type TPPrDlg which points to this structure. PrDlgMain then calls ShowWindow, passing the DialogRecord window (Dlg), and finally calls ModalDialog (passing, luckily enough, the ModalFilterProcPtr).

So, if I could get at this TPrDlg record, I could change the ModalFilterProcPtr and disable the Draft mode item in the DialogRecord window. Two bugs fixed for the price of one!

Today, On “How To Do It ”

As luck would have it, Tech Note #95 even showed how to get access to this TPrDlg record. Instead of using the PrJobDialog routine, I call PrDlgMain myself, substituting my own init routine in the call so it looks like this:

/* 6 */

if (!PrDlgMain(hPrintRec,&MyJobDlgInit))
 /* there was an error */

My init routine, MyJobDlgInit, calls the original init routine, PrJobInit, lets it set everything up, then modifies what we need.

/* 7 */

/* call PrJobInit to get pointer to the invisible job dialog */
PrtJobDialog = PrJobInit(hPrintRec);
if (PrError() != noErr)
 return nil;

/* set up the filter function */
PrtJobDialog->pFltrProc = (ModalFilterProcPtr)&Net_filter;
/* disable the draft mode now */

The routine DisableDraftMode is responsible for disabling the Draft Mode radio button of the print dialog record pointed to by PrtJobDialog. DisableDraftMode determines how many items in the list via ItemList, then loops for this number so each item can be checked. GetDitem is used to determine what type of item it is.

Type Checking

Types can have the high bit set if they are disabled, so I dropped this bit by ANDing the type with 7F (0111111). Radio buttons are controls, so the type check is made for ctrlItem + radCtrl (Control item and Radio Button Control).

The title is then found via the lowercase version of getctitle. I did it like this so that a C string is returned as opposed to Pascal format. By looping and checking for “Draft” as the title of the control, we aren’t relying on the Draft item being a certain item number in the Dialog. After all, Tech Note #95 has warned that “If you depend on the Draft button being a particular number and we change the Draft button’s item number, your program may not work”. When coding this for a real application, I would put the “Draft” string in the resource file to make localization easier.

Once the draft item is found, it is unselected by SetCtlValue and finally disabled by passing a value of 255 to HiliteControl.

Having disabled and possibly unselected the Draft item, the dialog is cleaned up by making sure that at least one of the buttons is selected. The item numbers of the Best and Faster modes are saved, so we try and turn one of these on.

SoapBox Derby

I should point out that there is a difference between a disabled item and an inactive control. If the item is disabled (and we could disable it by SetDItem) it cannot be selected. However, it is not drawn in dimmed text. The item looks like it can be clicked on, and if the user clicks on it, the radio button turns on then off right away. This means they might try again and again and start to think something is wrong with the mouse etc . When a control is set to inactive via HiLiteControl, it cannot be selected and IS dimmed. This gives feedback to the user and says “You can’t pick this, it isn’t a valid option right now”. Furthermore, the consistency of the Mac interface is preserved and the world is saved for peace, democracy and future generations. End of soapbox.

Adding Items to the ItemList

In fact, my code is a lot simpler to write than Tech Note #95’s. The Tech Note shows how to add items to the Dialog. The field Items in the Dialog Record contains a handle to a list of items. This list is documented briefly in Inside Macintosh, but the sample code in Tech Note #95 gives a more complete picture. The ItemList itself contains an item, dlgmaxindex, which tells the number of Items in the list (minus 1). Following dlgmaxindex is each of the DITLs. Each of these DITLs contains a Handle to the item, it’s rect, type and datalength followed by datalength bytes of variable data. There is no Trap to add a DITL to a DialogRecord’s ItemList and since each DITL is a variable length field within the Item List, the AppendDITL routine must do some fancy pointer manipulation. We will only look at and change the existing DITL’s so we can use the existing Dialog and Control Manager routines, but we will use the ItemList definition to get the variable. I’ve kept the ItemList structure and other definitions as in Tech Note #95 in case you wish to expand the sample to include a C version of the AppendDITL routine.

Example Source

The C source code which follows is composed of two main modules. The NoDraft.c source is the part of the basic boilerplate shell that I use. It originated on some developer CD somewhere and has been stripped down to the basics to conserve trees. It really just sets up a window and menubar then waits for the user to select “Print ”. The important stuff all happens in the Dialogs.c module. This module contains the DisableDraftMode, and MyJobDlgInit, as well as the routines needed to setup the print dialogs, the filter function, etc.


The example which follows shows how to include a filter function and how to disable the Draft Mode button on a print dialog. If you need to add items to a dialog, Tech Note #95 shows you how.

Listing NoDraft.h
#MultiFinder-Aware Simple NoDraft Application
#NoDraft.h- noDraft header manifest
#NoDraft is an example application that 
#demonstrates how to modify Print Dialogs.

#define kMinSize 128 /* min size (K) */

#define kPrefSize128 /* pref’d size (K) */

/* Indicies into STR# resources */
#define eWrongMachine1
#define eSmallSize 2
#define eNoMemory3
#define eNoWindow4
#define eUnknown 1

#define rMenuBar 128 /* app’s menu bar */
#define rAboutAlert128  /* about alert */
#define rUserAlert 129  /* error alert */
#define rWindow  128 /* A window */
#define kErrStrings128  /* error strings */

#define kSysEnvironsVersion 1

#define kOSEvent app4Evt  /* used by MF */
#define kSuspendResumeMessage 1  
#define kResumeMask1 
#define kMouseMovedMessage0xFA
#define kNoEvents0

#define mApple   128 /* Apple menu */
#define iAbout   1

#define mFile    129 /* File menu */
#define iNew1
#define iClose   4
#define iPSetUp  9
#define iPrint   10

#define iQuit    12

#define mEdit    130 /* Edit menu */
#define iUndo    1
#define iCut3
#define iCopy    4
#define iPaste   5
#define iClear   6

#define abs(x)   ( (x) < 0 ? -(x) : (x))
#define MAX(x, y)( (x) > (y) ? (x) : (y) )

#define kMinHeap 21 * 1024
#define kMinSpace8 * 1024

/* used to set enable/disable flags of a menu */

#define AllItems 0b1111111111111111111111111111111
/* 31 flags worth*/
#define NoItems  0b0000000000000000000000000000000
#define MenuItem10b0000000000000000000000000000001
#define MenuItem20b0000000000000000000000000000010
#define MenuItem30b0000000000000000000000000000100
#define MenuItem40b0000000000000000000000000001000
#define MenuItem50b0000000000000000000000000010000
#define MenuItem60b0000000000000000000000000100000
#define MenuItem70b0000000000000000000000001000000
#define MenuItem80b0000000000000000000000010000000
#define MenuItem90b0000000000000000000000100000000
#define MenuItem10 0b0000000000000000000001000000000
#define MenuItem11 0b0000000000000000000010000000000
#define MenuItem12 0b0000000000000000000100000000000

#define cr0x0d
#define enter  0x03

#define delay_length 1024
Listing NoDraft_types.h
#MultiFinder-Aware Simple NoDraft Application
#NoDraft_types.h - C type defs for NoDraft

struct DITLItem {
 Handle itmHndl;
 Rect   itmRect;
 SignedByte itmType;
 SignedByte itmData;

typedef struct DITLItem DITLItem;
typedef DITLItem *pDITLItem, **hDITLItem;

struct ItemList {
 short  dlgMaxIndex;
 DITLItem DITLItems;

typedef struct ItemList ItemList;
typedef ItemList *pItemList, **hItemList;

static TPPrDlg PrtJobDialog;
Listing Dialogs.c
#MultiFinder-Aware Simple NoDraft Application
#Dialogs.c -Dialog handling code
---------------------- */

#include <Values.h>
#include <Types.h>
#include <Resources.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Windows.h>
#include <Menus.h> 
#include <TextEdit.h>
#include <Dialogs.h>
#include <Desk.h>
#include <ToolUtils.h>
#include <Memory.h>
#include <SegLoad.h>
#include <OSUtils.h>
#include <OSEvents.h>
#include <DiskInit.h>
#include <Packages.h>
#include <Controls.h>
#include <Printing.h>
#include <Traps.h>
#include <NoDraft.h>
#include <Nodraft_types.h>

pascal TPPrDlg MyJobDlgInit();

THPrint hPrintRec; 

/*---------------------- */

void FlashDialogItem(the_dialog, theItem)

DialogPtr the_dialog;
short   theItem;

 short  itemType;
 Rect   itemRect;
 Handle itemHdl;
 long   longdelay, longticks;
 GetDItem(the_dialog, theItem, &itemType, &itemHdl, &itemRect); 
  if ( itemHdl != nil) {
 HiliteControl( (ControlHandle) itemHdl, 1);
 longdelay = longticks = 6;
 Delay(longdelay, &longticks);

/*---------------------- */

void checknetwork()

 static int count = 0;  
 /*I use this routine to check for AppleTalk™
 messages rather than include all that code,
 I just beep every once in a while to let 
 you know it is working, you can change
 delay_length to increase/decrease
 freq of Beeps */
 if ( count++ > delay_length ) {
 count = 0;
 } /* end of checking the network */
/*---------------------- */

pascal Boolean  Net_filter(theDialog, theEvent, itemHit)

DialogPtr theDialog;
EventRecord *theEvent;
short   *itemHit;
 int    key;

 handled_event = false; /* did we handle event */
/* do whatever you have to for event */
 switch ( theEvent->what) {
 case nullEvent:
 case keyDown:
 key = theEvent->message & charCodeMask;
 if (( key == cr) || ( key == enter ))
 FlashDialogItem(theDialog, ok); 
 *itemHit = ok;
 handled_event = true;
 case updateEvt:
 /*we get all kinds of update events here
 some for our modal dialog and even some
 for the different windows ( in some cases) 
 rather than try and process these so that
 we do get real null events when
 we can check the network, just check the
 network and let the update events get
 processed later */

 case activateEvt:
 /* Don’t know what we got here, but */
 /* but check the network in case we */
 /* get stuck on this event like we */
 /* did with updates */
/* return boolean to tell modal dialog to */
/* handle the event or not */
 return (handled_event);

/*---------------------- */

void ShowAboutMeDialog(void)
/* my about  dialog */
 short  itemHit;

 itemHit = Alert(rAboutAlert, &Net_filter);

 } /* ShowAboutMeDialog */

/*---------------------- */

void DisableDraftMode(thePrintDialog)

TPPrDlg thePrintDialog;

/* disable the ability to print out draft */
/* mode since we can’t print our bitmaps */
 DialogPeek theDialogPtr;
 short  i;
 short  num_items; /* how many in list */
 hItemListhItems;/* Handle to DLOG’s list */

 short  item_no;
 short  draft_item_no;
 short  better_item_no;
 short  best_item_no;
 short  the_type;/* vars from GetDitem */
 Handle the_item;
 Rect   the_box;
 char   *the_title = “            “;
 short  the_and_type;
 char   *draft_title, *better_title;
 char   *best_title;
 short  draft_value;
 /* we are given a DialogPtr which is a ptr */
 /* to the print dialog */
 /* scan through all the items, and if you */
 /* get one called draft, disable it */
 /* how many items are there to scan */
 theDialogPtr = (DialogPeek)thePrintDialog;

 hItems = (hItemList) (theDialogPtr->items);
 num_items = (*hItems)->dlgMaxIndex + 1;

 draft_title = “Draft”;
 better_title = “Faster”;
 best_title = “Best”;
 draft_selected = false;
 for (i = 1; i <= num_items; i++ )
 /* get the item */
 item_no = i;
 GetDItem( (DialogPtr) theDialogPtr, item_no, &the_type, &the_item, &the_box);
 /* used to bit and with 7F */
 the_and_type = the_type & 0X7F;
 switch ( the_and_type )  { 
 /* is it a radio button */
 case ctrlItem + radCtrl:
 getctitle( (ControlHandle) the_item, the_title);
 if ((*the_title) == (*draft_title))
 draft_value = GetCtlValue((ControlHandle) the_item);
 if ( draft_value > 0 ) 
 draft_selected = true;
 draft_value = 0;
 SetCtlValue((ControlHandle) the_item, draft_value);
 HiliteControl( (ControlHandle) the_item, 255);
 /*comment the previous line and uncomment */
 /*the next two lines of code to see the */
 /*difference between disabling item */
 /*and inactivating control */

 /* the_and_type = the_type | 0X80; */
 /* SetDItem( (DialogPtr) theDialogPtr, item_no, the_and_type, the_item, 
&the_box); */
   draft_item_no = item_no;
 if ((*the_title) == (*better_title) ) 
   better_item_no = item_no;
 if ((*the_title) == (*best_title) ) 
  best_item_no = item_no;
 } /* end of switch */
 } /* of for loop */
 } /* of disabling the printing in draft mode */
/*---------------------- */

pascal TPPrDlg MyJobDlgInit(hPrint)
THPrint hPrint;
 /* call PrJobInit to get pointer to the */
 /* invisible job dialog */
 PrtJobDialog = PrJobInit(hPrintRec);
 if (PrError() != noErr)
 return nil;

 /* set up the filter function */
 PrtJobDialog->pFltrProc = (ModalFilterProcPtr)&Net_filter;
 /* disable the draft mod now */

 /* PrDlgMain expects pointer to modified */
 /* dialog to be returned.... */
 return ( PrtJobDialog );
 } /* myJobDlgInit */


OSErr DoPrintDialog()

 hPrintRec = (THPrint) NewHandle(sizeof(TPrint));
 if (PrError() != noErr)
 return PrError();

 /* Here’s the line that does it all! */
 if (!PrDlgMain(hPrintRec,&MyJobDlgInit))
 return 2;

 if (PrError() != noErr)
 return PrError();

 /* that’s all for now */

Community Search:
MacTech Search:

Software Updates via MacUpdate

The best scanner app on mobile
People always say that the best camera is the one you have with you. Well, the same is true with scanners, and your phone can be a pretty great tool for scanning receipts and other documents while you're on the go. [Read more] | Read more »
MARVEL Avengers Academy guide - How to g...
MARVEL Avengers Academy lets you build your own superhero school and fill it with heroes from the Marvel universe. It can be a little slow going to get your school's attendance up though, so we've gathered together somesome tips to help you do this... | Read more »
Shadow Blade: Reload guide - How to hack...
Shadow Blade: Reload is the kind of action-platformer that would have happily sucked up hours of your time on a console a few years back.Now, you can take it with you wherever you go, and its mobile conversion is not too shabby at all. To help you... | Read more »
Tomb of the Mask guide - How to increase...
Tomb of the Mask is a great endless arcade game from Happymagenta in which quick reflexes and a persistent attitude can go a long way toward earning a top score. Check out these tips to see if you can give yourself an edge on the leaderboards. [... | Read more »
Smooth Operator! (Games)
Smooth Operator! 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Smooth Operator is a weird, weird two-player kissing game. Squeeze in for 2 player fun on a single iPad, creating awkward... | Read more »
Sinless: Remastered (Games)
Sinless: Remastered 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: | Read more »
_PRISM Guide - How to solve those puzzle...
_PRISM is a rather delightful puzzle game that’s been tailor made for touch screens. While part of the fun is figuring things out as you go along, we thought we’d offer you a helping hand at getting in the right mindset. Don’t worry about messing... | Read more »
Fractal Space (Games)
Fractal Space 1.3.1 Device: iOS Universal Category: Games Price: $.99, Version: 1.3.1 (iTunes) Description: Live the memorable experience of Fractal Space, a unique first person adventure & puzzle game by Haze Games! Will you... | Read more »
Set off on an adventure through the Cand...
Like match three puzzlers? If so, Jelly Blast, the innovative iOS and Android game which launched last year, is worth a look. Jelly Blast sees you head off on an epic adventure through the Candy Kingdom with your friends Lily, Mr. Hare, and Mr.... | Read more »
Ellipsis - Touch. Explore. Survive. (...
Ellipsis - Touch. Explore. Survive. 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: | Read more »

Price Scanner via

cb Hardcase – Handmade and Premium Protective...
Baden-Baden, Germany based company cb innovations has introduced the new cb Hardcase for iPhone. Featuring fine Italian Premium leather that makes for a unique look and feel, the cb Hardcase... Read more
Sale! B&H Photo offers 12-inch Retina Mac...
B&H Photo has 12″ Retina MacBooks on sale for $300 off MSRP for a limited time. Shipping is free, and B&H charges NY tax only: - 12″ 1.1GHz Gray Retina MacBook: $999 $300 off MSRP - 12″ 1.... Read more
App Annie Reveals Future of the App Economy:...
App Annie, a San Francisco based mobile app data and insights platform, has launched its first comprehensive app economy forecast. This new offering will provide brands, agencies, investors and app... Read more
Apple restocks Certified Refurbished Mac mini...
Apple has restocked Certified Refurbished 2014 Mac minis, with models available starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: - 1.4GHz Mac mini: $419 $... Read more
What iPad Pro Still Needs To Make It Truly Pr...
I love my iPad Air 2. So much that I’m grudgingly willing to put up with its compromises and limitations as a production tool in order to take advantage of its virtues. However, since a computer for... Read more
21-inch 3.1GHz 4K on sale for $1399, $100 off...
B&H Photo has the 21″ 3.1GHz 4K iMac on sale $1399 for a limited time. Shipping is free, and B&H charges NY sales tax only. Their price is $100 off MSRP: - 21″ 3.1GHz 4K iMac (MK452LL/A): $... Read more
Apple price trackers, updated continuously
Scan our Apple Price Trackers for the latest information on sales, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. We update the trackers continuously: - 15″... Read more
Save up to $240 with Apple Certified Refurbis...
Apple is now offering Certified Refurbished 12″ Retina MacBooks for up to $240 off the cost of new models. Apple will include a standard one-year warranty with each MacBook, and shipping is free. The... Read more
Apple refurbished 13-inch Retina MacBook Pros...
Apple has Certified Refurbished 13″ Retina MacBook Pros available for up to $270 off the cost of new models. An Apple one-year warranty is included with each model, and shipping is free: - 13″ 2.7GHz... Read more
Apple refurbished Time Capsules available for...
Apple has certified refurbished Time Capsules available for $120 off MSRP. Apple’s one-year warranty is included with each Time Capsule, and shipping is free: - 2TB Time Capsule: $179, $120 off - 3TB... Read more

Jobs Board

Infrastructure Engineer - *Apple* /Mac - Rem...
…part of a team Requires proven problem solving skills Preferred Additional: Apple Certified System Administrator (ACSA) Apple Certified Technical Coordinator (ACTC) Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Simply Mac *Apple* Specialist- Service Repa...
Simply Mac is the largest premier retailer of Apple products in the nation. In order to support our growing customer base, we are currently looking for a driven Read more
*Apple* Reporter - Business Insider, Inc. (U...
Business Insider is looking for a reporter to cover Apple , the biggest and arguably most important company in tech. As our primary Apple reporter, you will: * Read more
Infrastructure Engineer - *Apple* /Mac - Rem...
…part of a team Requires proven problem solving skills Preferred Additional: Apple Certified System Administrator (ACSA) Apple Certified Technical Coordinator (ACTC) Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.