Back to index

lightning-sunbird  0.9+nobinonly
PDEUtilities.c
Go to the documentation of this file.
00001 /*
00002 ********************************************************************************
00003     
00004     $Log: PDEUtilities.c,v $
00005     Revision 1.3  2005/08/08 03:08:43  smfr%smfr.org
00006     Fix bug 202014: add UI to the Mac Print Dialog Extension (PDE) that allows the user to select which headers and footers to print. Patch by Conrad Carlen, r=pinkerton, sr=me, a=asa.
00007 
00008     Revision 1.2  2003/04/03 19:20:05  ccarlen%netscape.com
00009     Bug 188508 - Upgrade print dialog PDE. r=pinkerton/sr=sfraser
00010 
00011 
00012 
00013     (c) Copyright 2002 Apple Computer, Inc.  All rights reserved.
00014     
00015     IMPORTANT: This Apple software is supplied to you by Apple Computer,
00016     Inc. ("Apple") in consideration of your agreement to the following
00017     terms, and your use, installation, modification or redistribution of
00018     this Apple software constitutes acceptance of these terms.  If you do
00019     not agree with these terms, please do not use, install, modify or
00020     redistribute this Apple software.
00021     
00022     In consideration of your agreement to abide by the following terms, and
00023     subject to these terms, Apple grants you a personal, non-exclusive
00024     license, under Apple's copyrights in this original Apple software (the
00025     "Apple Software"), to use, reproduce, modify and redistribute the Apple
00026     Software, with or without modifications, in source and/or binary forms;
00027     provided that if you redistribute the Apple Software in its entirety and
00028     without modifications, you must retain this notice and the following
00029     text and disclaimers in all such redistributions of the Apple Software.
00030     Neither the name, trademarks, service marks or logos of Apple Computer,
00031     Inc. may be used to endorse or promote products derived from the Apple
00032     Software without specific prior written permission from Apple.  Except
00033     as expressly stated in this notice, no other rights or licenses, express
00034     or implied, are granted by Apple herein, including but not limited to
00035     any patent rights that may be infringed by your derivative works or by
00036     other works in which the Apple Software may be incorporated.
00037     
00038     The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES
00039     NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE
00040     IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR
00041     A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
00042     OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
00043     
00044     IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
00045     OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00046     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00047     INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
00048     MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
00049     AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
00050     STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
00051     POSSIBILITY OF SUCH DAMAGE.
00052     
00053 ********************************************************************************
00054 */
00055 
00056 #include <Carbon/Carbon.h>
00057 #include <Print/PMPrintingDialogExtensions.h>
00058 
00059 #include "PDECore.h"
00060 #include "PDECustom.h"
00061 #include "PDEUtilities.h"
00062 
00063 // callback function to handle the 'help' event
00064 static OSStatus MyHandleHelpEvent (EventHandlerCallRef, EventRef, void *userData);
00065 
00066 
00067 /*
00068 --------------------------------------------------------------------------------
00069     MyDebugMessage
00070 --------------------------------------------------------------------------------
00071 */
00072 
00073 extern void MyDebugMessage (char *msg, SInt32 value)
00074 {
00075     char *debug = getenv ("PDEDebug");
00076     if (debug != NULL)
00077     {
00078         fprintf (stdout, "%s (%d)\n", msg, (int) value);
00079         fflush (stdout);
00080     }
00081 }
00082 
00083 /*
00084 --------------------------------------------------------------------------------
00085     MyCFAssign
00086 --------------------------------------------------------------------------------
00087 */
00088 
00089 extern CFTypeRef MyCFAssign(CFTypeRef srcRef, CFTypeRef dstRef)
00090 {
00091     if (srcRef)
00092         CFRetain(srcRef);
00093     if (dstRef)
00094         CFRelease(dstRef);
00095     dstRef = srcRef;
00096     return dstRef;
00097 }
00098 
00099 /*
00100 --------------------------------------------------------------------------------
00101     _MyGetBundle
00102 --------------------------------------------------------------------------------
00103 */
00104 
00105 static CFBundleRef _MyGetBundle (Boolean stillNeeded)
00106 {
00107     static CFBundleRef sBundle = NULL;
00108     
00109     if (stillNeeded)
00110     {
00111         if (sBundle == NULL)
00112         {
00113             sBundle = CFBundleGetBundleWithIdentifier (kMyBundleIdentifier);
00114             CFRetain (sBundle);
00115         }
00116     }
00117     else
00118     {
00119         if (sBundle != NULL)
00120         {
00121             CFRelease (sBundle);
00122             sBundle = NULL;
00123         }
00124     }
00125 
00126     return sBundle;
00127 }
00128 
00129 
00130 /*
00131 --------------------------------------------------------------------------------
00132     MyGetBundle
00133 --------------------------------------------------------------------------------
00134 */
00135 
00136 extern CFBundleRef MyGetBundle()
00137 {
00138     return _MyGetBundle (TRUE);
00139 }
00140 
00141 
00142 /*
00143 --------------------------------------------------------------------------------
00144     MyFreeBundle
00145 --------------------------------------------------------------------------------
00146 */
00147 
00148 extern void MyFreeBundle()
00149 {
00150     _MyGetBundle (FALSE);
00151 }
00152 
00153 
00154 #pragma mark -
00155 
00156 /*
00157 --------------------------------------------------------------------------------
00158     MyGetTitle
00159 --------------------------------------------------------------------------------
00160 */
00161 
00162 extern CFStringRef MyGetTitle()
00163 {
00164     return MyGetCustomTitle (TRUE);
00165 }
00166 
00167 
00168 /*
00169 --------------------------------------------------------------------------------
00170     MyFreeTitle
00171 --------------------------------------------------------------------------------
00172 */
00173 
00174 extern void MyFreeTitle()
00175 {
00176     MyGetCustomTitle (FALSE);
00177 }
00178 
00179 
00180 #pragma mark -
00181 
00182 /*
00183 --------------------------------------------------------------------------------
00184     MyGetTicket
00185 --------------------------------------------------------------------------------
00186 */
00187 
00188 extern OSStatus MyGetTicket (
00189     PMPrintSession  session,
00190     CFStringRef     ticketID,
00191     PMTicketRef*    ticketPtr
00192 )
00193 
00194 {
00195     OSStatus result = noErr;
00196     CFTypeRef type = NULL;
00197     PMTicketRef ticket = NULL;
00198     
00199     *ticketPtr = NULL;
00200 
00201     result = PMSessionGetDataFromSession (session, ticketID, &type);
00202 
00203     if (result == noErr)
00204     {    
00205         if (CFNumberGetValue (
00206             (CFNumberRef) type, kCFNumberSInt32Type, (void*) &ticket))
00207         {
00208             *ticketPtr = ticket;
00209         }
00210         else {
00211             result = kPMInvalidValue;
00212         }
00213     }
00214 
00215     return result;
00216 }
00217 
00218 
00219 /*
00220 --------------------------------------------------------------------------------
00221     MyEmbedControl
00222 --------------------------------------------------------------------------------
00223 */
00224 
00225 extern OSStatus MyEmbedControl (
00226     WindowRef nibWindow,
00227     ControlRef userPane,
00228     const ControlID *controlID,
00229     ControlRef* outControl
00230 )
00231 
00232 {
00233     ControlRef control = NULL;
00234     OSStatus result = noErr;
00235 
00236     *outControl = NULL;
00237 
00238     result = GetControlByID (nibWindow, controlID, &control);
00239     if (result == noErr)
00240     {
00241         SInt16 dh, dv;
00242         Rect nibFrame, controlFrame, paneFrame;
00243 
00244         (void) GetWindowBounds (nibWindow, kWindowContentRgn, &nibFrame);
00245         (void) GetControlBounds (userPane, &paneFrame);
00246         (void) GetControlBounds (control, &controlFrame);
00247         
00248         // find vertical and horizontal deltas needed to position the control
00249         // such that the nib-based interface is centered inside the dialog pane
00250 
00251         dh = ((paneFrame.right - paneFrame.left) - 
00252                 (nibFrame.right - nibFrame.left))/2;
00253 
00254         if (dh < 0) dh = 0;
00255 
00256         dv = ((paneFrame.bottom - paneFrame.top) - 
00257                 (nibFrame.bottom - nibFrame.top))/2;
00258 
00259         if (dv < 0) dv = 0;
00260                 
00261         OffsetRect (
00262             &controlFrame, 
00263             paneFrame.left + dh, 
00264             paneFrame.top + dv
00265         );
00266  
00267         (void) SetControlBounds (control, &controlFrame);
00268 
00269         // make visible
00270         result = SetControlVisibility (control, TRUE, FALSE);
00271 
00272         if (result == noErr) 
00273         {
00274             result = EmbedControl (control, userPane);
00275             if (result == noErr)
00276             {
00277                 // return the control only if everything worked
00278                 *outControl = control;
00279             }
00280         }
00281     }
00282 
00283     return result;
00284 }
00285 
00286 
00287 #pragma mark -
00288 
00289 /*
00290 --------------------------------------------------------------------------------
00291     MyReleaseContext
00292 --------------------------------------------------------------------------------
00293 */
00294 
00295 extern void MyReleaseContext (MyContext context)
00296 {
00297     if (context != NULL)
00298     {
00299         if (context->customContext != NULL) {
00300             MyReleaseCustomContext (context->customContext);
00301         }
00302 
00303         free (context);
00304     }
00305 }
00306 
00307 
00308 #pragma mark -
00309 
00310 /*
00311 --------------------------------------------------------------------------------
00312     MyInstallHelpEventHandler
00313 --------------------------------------------------------------------------------
00314 */
00315 
00316 #define kMyNumberOfEventTypes   1
00317 
00318 extern OSStatus MyInstallHelpEventHandler (
00319     WindowRef inWindow, 
00320     EventHandlerRef *outHandler,
00321     EventHandlerUPP *outHandlerUPP
00322 )
00323 
00324 {
00325     static const EventTypeSpec sEventTypes [kMyNumberOfEventTypes] =
00326     {
00327         { kEventClassCommand, kEventCommandProcess }
00328     };
00329 
00330     OSStatus result = noErr;
00331     EventHandlerRef handler = NULL;
00332     EventHandlerUPP handlerUPP = NewEventHandlerUPP (MyHandleHelpEvent);
00333 
00334     result = InstallWindowEventHandler (
00335         inWindow,
00336         handlerUPP,
00337         kMyNumberOfEventTypes,
00338         sEventTypes,
00339         NULL, 
00340         &handler
00341     );
00342 
00343     *outHandler = handler;
00344     *outHandlerUPP = handlerUPP;
00345     
00346     MyDebugMessage("InstallEventHandler", result);
00347     return result;
00348 }
00349 
00350 
00351 /*
00352 --------------------------------------------------------------------------------
00353     MyRemoveHelpEventHandler
00354 --------------------------------------------------------------------------------
00355 */
00356 
00357 extern OSStatus MyRemoveHelpEventHandler (
00358     EventHandlerRef *helpHandlerP, 
00359     EventHandlerUPP *helpHandlerUPP
00360 )
00361 
00362 {
00363     OSStatus result = noErr;
00364     
00365     // we remove the help handler if there is still one present
00366     if (*helpHandlerP != NULL)
00367     {
00368         MyDebugMessage("Removing event handler", result);
00369         result = RemoveEventHandler (*helpHandlerP);
00370         *helpHandlerP = NULL;
00371     }
00372 
00373     if (*helpHandlerUPP != NULL)
00374     {
00375         DisposeEventHandlerUPP (*helpHandlerUPP);
00376         *helpHandlerUPP = NULL;
00377     }
00378     return result;
00379 }
00380 
00381 
00382 /*
00383 --------------------------------------------------------------------------------
00384     MyHandleHelpEvent
00385 --------------------------------------------------------------------------------
00386 */
00387 
00388 static OSStatus MyHandleHelpEvent
00389 (
00390     EventHandlerCallRef call,
00391     EventRef event, 
00392     void *userData
00393 )
00394 
00395 {
00396     HICommand   commandStruct;
00397     OSStatus    result = eventNotHandledErr;
00398 
00399     GetEventParameter (
00400         event, kEventParamDirectObject,
00401         typeHICommand, NULL, sizeof(HICommand), 
00402         NULL, &commandStruct
00403     );
00404 
00405     if (commandStruct.commandID == 'help')
00406     {
00407         // result = MyDisplayHelp();
00408         // MyDebugMessage("handled help event", result);
00409     }
00410 
00411     return result;
00412 }
00413 
00414 
00415 // END OF SOURCE