[4084] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | File: UIHandling.c
|
---|
| 4 |
|
---|
| 5 | Abstract: Implementation of Carbon UI portion of sample code.
|
---|
| 6 |
|
---|
| 7 | Version: <1.0>
|
---|
| 8 |
|
---|
| 9 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
|
---|
| 10 | Computer, Inc. ("Apple") in consideration of your agreement to the
|
---|
| 11 | following terms, and your use, installation, modification or
|
---|
| 12 | redistribution of this Apple software constitutes acceptance of these
|
---|
| 13 | terms. If you do not agree with these terms, please do not use,
|
---|
| 14 | install, modify or redistribute this Apple software.
|
---|
| 15 |
|
---|
| 16 | In consideration of your agreement to abide by the following terms, and
|
---|
| 17 | subject to these terms, Apple grants you a personal, non-exclusive
|
---|
| 18 | license, under Apple's copyrights in this original Apple software (the
|
---|
| 19 | "Apple Software"), to use, reproduce, modify and redistribute the Apple
|
---|
| 20 | Software, with or without modifications, in source and/or binary forms;
|
---|
| 21 | provided that if you redistribute the Apple Software in its entirety and
|
---|
| 22 | without modifications, you must retain this notice and the following
|
---|
| 23 | text and disclaimers in all such redistributions of the Apple Software.
|
---|
| 24 | Neither the name, trademarks, service marks or logos of Apple Computer,
|
---|
| 25 | Inc. may be used to endorse or promote products derived from the Apple
|
---|
| 26 | Software without specific prior written permission from Apple. Except
|
---|
| 27 | as expressly stated in this notice, no other rights or licenses, express
|
---|
| 28 | or implied, are granted by Apple herein, including but not limited to
|
---|
| 29 | any patent rights that may be infringed by your derivative works or by
|
---|
| 30 | other works in which the Apple Software may be incorporated.
|
---|
| 31 |
|
---|
| 32 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE
|
---|
| 33 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
|
---|
| 34 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
|
---|
| 35 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
|
---|
| 36 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
|
---|
| 37 |
|
---|
| 38 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
|
---|
| 39 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
| 40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
| 41 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
|
---|
| 42 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
|
---|
| 43 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
|
---|
| 44 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
|
---|
| 45 | POSSIBILITY OF SUCH DAMAGE.
|
---|
| 46 |
|
---|
| 47 | Copyright © 2006 Apple Computer, Inc., All Rights Reserved
|
---|
| 48 |
|
---|
| 49 | */
|
---|
| 50 |
|
---|
| 51 | #include <Carbon/Carbon.h>
|
---|
| 52 | #include "UIHandling.h"
|
---|
| 53 | #include "NavServicesHandling.h"
|
---|
| 54 | #include "AppDrawing.h"
|
---|
| 55 | #include "DoPrinting.h"
|
---|
| 56 |
|
---|
| 57 | /* Constants */
|
---|
| 58 | #define kMyHIViewSignature 'blDG'
|
---|
| 59 | #define kMyHIViewFieldID 128
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | /* Private Prototypes */
|
---|
| 63 | static OSStatus myDrawEventHandler(EventHandlerCallRef myHandler, EventRef event, void *userData);
|
---|
| 64 | static OSStatus myDoAppCommandProcess(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
|
---|
| 65 | static void DoAboutBox();
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | /* Global Data */
|
---|
| 69 | static OSType gCurrentCommand = kCommandStrokedAndFilledRects;
|
---|
| 70 | static WindowRef gWindowRef = NULL;
|
---|
| 71 | static HIViewRef gMyHIView = NULL;
|
---|
| 72 | static PMPageFormat gPageFormat = NULL;
|
---|
| 73 |
|
---|
| 74 | int main(int argc, char* argv[])
|
---|
| 75 | {
|
---|
| 76 | IBNibRef nibRef;
|
---|
| 77 |
|
---|
| 78 | OSStatus err;
|
---|
| 79 | static const HIViewID kMyViewID = { kMyHIViewSignature, kMyHIViewFieldID };
|
---|
| 80 | static const EventTypeSpec kMyViewEvents[] = { kEventClassControl, kEventControlDraw };
|
---|
| 81 | static const EventTypeSpec kMyCommandEvents[] = { kEventClassCommand, kEventCommandProcess };
|
---|
| 82 |
|
---|
| 83 | // Create a Nib reference passing the name of the nib file (without the .nib extension)
|
---|
| 84 | // CreateNibReference only searches into the application bundle.
|
---|
| 85 | err = CreateNibReference(CFSTR("main"), &nibRef);
|
---|
| 86 | require_noerr( err, CantGetNibRef );
|
---|
| 87 |
|
---|
| 88 | // Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
|
---|
| 89 | // object. This name is set in InterfaceBuilder when the nib is created.
|
---|
| 90 | err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar"));
|
---|
| 91 | require_noerr( err, CantSetMenuBar );
|
---|
| 92 |
|
---|
| 93 | // Then create a window. "MainWindow" is the name of the window object. This name is set in
|
---|
| 94 | // InterfaceBuilder when the nib is created.
|
---|
| 95 | err = CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &gWindowRef);
|
---|
| 96 | require_noerr( err, CantCreateWindow );
|
---|
| 97 |
|
---|
| 98 | // We don't need the nib reference anymore.
|
---|
| 99 | DisposeNibReference(nibRef);
|
---|
| 100 |
|
---|
| 101 | // Get the HIView associated with the window.
|
---|
| 102 | HIViewFindByID( HIViewGetRoot( gWindowRef ), kMyViewID, &gMyHIView );
|
---|
| 103 |
|
---|
| 104 | // Install the event handler for the HIView.
|
---|
| 105 | err = HIViewInstallEventHandler(gMyHIView,
|
---|
| 106 | NewEventHandlerUPP (myDrawEventHandler),
|
---|
| 107 | GetEventTypeCount(kMyViewEvents),
|
---|
| 108 | kMyViewEvents,
|
---|
| 109 | (void *) gMyHIView,
|
---|
| 110 | NULL);
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | // Install the handler for the menu commands.
|
---|
| 114 | InstallApplicationEventHandler(NewEventHandlerUPP(myDoAppCommandProcess), GetEventTypeCount(kMyCommandEvents),
|
---|
| 115 | kMyCommandEvents, NULL, NULL);
|
---|
| 116 |
|
---|
| 117 | // Initialize the current drawing command menu item
|
---|
| 118 | // SetMenuCommandMark(NULL, gCurrentCommand, checkMark);
|
---|
| 119 | SetMenuCommandMark(NULL, gCurrentCommand, TRUE);
|
---|
| 120 |
|
---|
| 121 | // The window was created hidden so show it.
|
---|
| 122 | ShowWindow( gWindowRef );
|
---|
| 123 |
|
---|
| 124 | // Call the event loop
|
---|
| 125 | RunApplicationEventLoop();
|
---|
| 126 |
|
---|
| 127 | CantCreateWindow:
|
---|
| 128 | CantSetMenuBar:
|
---|
| 129 | CantGetNibRef:
|
---|
| 130 | return err;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | static OSStatus myDrawEventHandler(EventHandlerCallRef myHandler, EventRef event, void *userData)
|
---|
| 135 | {
|
---|
| 136 | OSStatus status = noErr;
|
---|
| 137 | CGContextRef context;
|
---|
| 138 | HIRect bounds;
|
---|
| 139 |
|
---|
| 140 | // Get the CGContextRef. This context is only valid to draw to during the execution of this
|
---|
| 141 | // event handler.
|
---|
| 142 | status = GetEventParameter (event, kEventParamCGContextRef,
|
---|
| 143 | typeCGContextRef, NULL,
|
---|
| 144 | sizeof (CGContextRef),
|
---|
| 145 | NULL,
|
---|
| 146 | &context);
|
---|
| 147 |
|
---|
| 148 | if(status != noErr){
|
---|
| 149 | fprintf(stderr, "Got error %d getting the context!\n", status);
|
---|
| 150 | return status;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // Get the bounding rectangle.
|
---|
| 154 | HIViewGetBounds ((HIViewRef) userData, &bounds);
|
---|
| 155 |
|
---|
| 156 | // Flip the coordinates by translating and scaling. This produces a
|
---|
| 157 | // coordinate system that matches the Quartz default coordinate system
|
---|
| 158 | // with the origin in the lower-left corner with the y axis pointing up.
|
---|
| 159 | CGContextTranslateCTM(context, 0, bounds.size.height);
|
---|
| 160 | CGContextScaleCTM(context, 1.0, -1.0);
|
---|
| 161 |
|
---|
| 162 | myDispatchDrawing(context, gCurrentCommand);
|
---|
| 163 |
|
---|
| 164 | return status;
|
---|
| 165 |
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | static OSType printableCommandFromCommand(OSType command)
|
---|
| 169 | {
|
---|
| 170 | // Don't use pre-rendered drawing when printing or exporting data.
|
---|
| 171 | if(command == kCommandDoCGLayer)
|
---|
| 172 | return kCommandDoUncachedDrawing;
|
---|
| 173 |
|
---|
| 174 | return command;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static OSStatus myDoAppCommandProcess(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
|
---|
| 178 | {
|
---|
| 179 | #pragma unused (nextHandler, userData)
|
---|
| 180 | HICommand aCommand;
|
---|
| 181 | OSStatus result = eventNotHandledErr;
|
---|
| 182 |
|
---|
| 183 | GetEventParameter(theEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommand), NULL, &aCommand);
|
---|
| 184 |
|
---|
| 185 | switch (aCommand.commandID){
|
---|
| 186 | case kCommandStrokedAndFilledRects:
|
---|
| 187 | case kCommandAlphaRects:
|
---|
| 188 | case kCommandSimpleClip:
|
---|
| 189 | case kCommandDrawImageFile:
|
---|
| 190 | case kCommandDoUncachedDrawing:
|
---|
| 191 | case kCommandDoCGLayer:
|
---|
| 192 |
|
---|
| 193 | SetMenuCommandMark(NULL, gCurrentCommand, noMark);
|
---|
| 194 | gCurrentCommand = aCommand.commandID;
|
---|
| 195 | // SetMenuCommandMark(NULL, gCurrentCommand, checkMark);
|
---|
| 196 | SetMenuCommandMark(NULL, gCurrentCommand, TRUE);
|
---|
| 197 | if(gMyHIView){
|
---|
| 198 | HIViewSetNeedsDisplay(gMyHIView, true);
|
---|
| 199 | }
|
---|
| 200 | result = noErr;
|
---|
| 201 | break;
|
---|
| 202 |
|
---|
| 203 | case kHICommandPageSetup:
|
---|
| 204 | if(gPageFormat == NULL)
|
---|
| 205 | gPageFormat = CreateDefaultPageFormat();
|
---|
| 206 |
|
---|
| 207 | if(gPageFormat)
|
---|
| 208 | (void)DoPageSetup(gPageFormat);
|
---|
| 209 |
|
---|
| 210 | result = noErr;
|
---|
| 211 | break;
|
---|
| 212 |
|
---|
| 213 | case kHICommandPrint:
|
---|
| 214 | if(gPageFormat == NULL)
|
---|
| 215 | gPageFormat = CreateDefaultPageFormat();
|
---|
| 216 |
|
---|
| 217 | if(gPageFormat)
|
---|
| 218 | (void)DoPrint(gPageFormat, printableCommandFromCommand(gCurrentCommand));
|
---|
| 219 |
|
---|
| 220 | result = noErr;
|
---|
| 221 | break;
|
---|
| 222 |
|
---|
| 223 | case kHICommandAbout:
|
---|
| 224 | DoAboutBox();
|
---|
| 225 | result = noErr;
|
---|
| 226 | break;
|
---|
| 227 |
|
---|
| 228 | case kCommandExportPDF:
|
---|
| 229 | if(gWindowRef) // gUseQTForExport and dpi are ignored for PDF export.
|
---|
| 230 | (void)DoExport(gWindowRef, printableCommandFromCommand(gCurrentCommand), exportTypePDF);
|
---|
| 231 | break;
|
---|
| 232 |
|
---|
| 233 | case kCommandExportPNG:
|
---|
| 234 | if(gWindowRef)
|
---|
| 235 | (void)DoExport(gWindowRef, printableCommandFromCommand(gCurrentCommand), exportTypePNG);
|
---|
| 236 | break;
|
---|
| 237 |
|
---|
| 238 | case kHICommandQuit:
|
---|
| 239 | QuitApplicationEventLoop();
|
---|
| 240 | result = noErr;
|
---|
| 241 | break;
|
---|
| 242 |
|
---|
| 243 | default:
|
---|
| 244 | break;
|
---|
| 245 | }
|
---|
| 246 | HiliteMenu(0);
|
---|
| 247 | return result;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | static void DoStandardAlert(CFStringRef alertTitle, CFStringRef alertText)
|
---|
| 251 | {
|
---|
| 252 | AlertStdCFStringAlertParamRec param;
|
---|
| 253 | DialogRef dialog;
|
---|
| 254 | OSStatus err;
|
---|
| 255 | DialogItemIndex itemHit;
|
---|
| 256 |
|
---|
| 257 | GetStandardAlertDefaultParams( ¶m, kStdCFStringAlertVersionOne );
|
---|
| 258 |
|
---|
| 259 | param.movable = true;
|
---|
| 260 |
|
---|
| 261 | err = CreateStandardAlert( kAlertNoteAlert, alertText, NULL, ¶m, &dialog );
|
---|
| 262 | if(err){
|
---|
| 263 | fprintf(stderr, "Can't create alert!\n");
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | if(alertTitle)
|
---|
| 268 | SetWindowTitleWithCFString( GetDialogWindow( dialog ), alertTitle);
|
---|
| 269 |
|
---|
| 270 | RunStandardAlert( dialog, NULL, &itemHit );
|
---|
| 271 |
|
---|
| 272 | return;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | static void DoAboutBox()
|
---|
| 277 | {
|
---|
| 278 | CFStringRef alertMessage = CFCopyLocalizedString(kAboutBoxStringKey, NULL);
|
---|
| 279 | CFStringRef alertTitle = CFCopyLocalizedString(kAboutBoxTitleKey, NULL);
|
---|
| 280 |
|
---|
| 281 | if (alertMessage != NULL && alertTitle != NULL)
|
---|
| 282 | {
|
---|
| 283 | DoStandardAlert(alertTitle, alertMessage);
|
---|
| 284 | }
|
---|
| 285 | if(alertMessage)
|
---|
| 286 | CFRelease(alertMessage);
|
---|
| 287 |
|
---|
| 288 | if(alertTitle)
|
---|
| 289 | CFRelease(alertTitle);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | void DoErrorAlert(OSStatus status, CFStringRef errorFormatString)
|
---|
| 293 | {
|
---|
| 294 | if ((status != noErr) && (status != kPMCancel))
|
---|
| 295 | {
|
---|
| 296 | CFStringRef formatStr = NULL;
|
---|
| 297 | CFStringRef printErrorMsg = NULL;
|
---|
| 298 |
|
---|
| 299 | formatStr = CFCopyLocalizedString(errorFormatString, NULL);
|
---|
| 300 | if (formatStr != NULL){
|
---|
| 301 | printErrorMsg = CFStringCreateWithFormat(
|
---|
| 302 | NULL, NULL,
|
---|
| 303 | formatStr, status);
|
---|
| 304 | if (printErrorMsg != NULL)
|
---|
| 305 | {
|
---|
| 306 | DoStandardAlert(NULL, printErrorMsg);
|
---|
| 307 | CFRelease (printErrorMsg);
|
---|
| 308 | }
|
---|
| 309 | CFRelease (formatStr);
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | }
|
---|