Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

CAUGuiTools.cpp

Go to the documentation of this file.
00001 /*
00002  *  CAUGuiTools.cpp
00003  *  CoreGraphics AudioUnit GUI framework
00004  *
00005  *  Created by Urs Heckmann on Mon Oct 28 2002.
00006  *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
00007  *
00008  *  Thanks to Franck Stauffer for the multifame code example
00009  *  (you won't recognize it anymore, but it's here, somewhere ;-)
00010  *
00011  */
00012 
00013 #include "CAUGuiTools.h"
00014 
00015 /***********************************************************************
00016 
00017     eRect
00018     
00019     a Rect class with better properties for layout issues...
00020     
00021     (sorry for the naming, but I just couldn't resist :-)
00022 
00023 ***********************************************************************/
00024 
00025 void eRect::align ( CAUGuiAlign align )
00026 {
00027     eRect r;
00028     
00029     r.set ( this );
00030     
00031     this->align ( &r, align );
00032 
00033 }
00034 
00035 
00036 
00037 void eRect::align ( eRect* r, CAUGuiAlign align )
00038 {
00039 
00040     int V = ((int) align & 7 );
00041     
00042     switch ( V )
00043     {
00044         case 2: // North
00045             
00046                 y = r->y;
00047                 break;
00048         
00049         case 4: // South
00050         
00051                 y = r->y + r->h - h;
00052                 break;
00053                 
00054         case 7:
00055         case 6: // Middle
00056         
00057                 y = r->y + ( r->h / 2 ) - ( h / 2 );
00058                 break;
00059         
00060         case 3: // Above
00061         
00062                 y = r->y - h;
00063                 break;
00064         
00065         case 5: // Below
00066                 
00067                 y = r->y + r->h;
00068                 break;
00069     
00070         default:
00071                 break;
00072     
00073     }
00074     
00075     int H = ((int) align & 56 ) / 8;
00076     
00077     switch ( H )
00078     {
00079         case 2: // West
00080             
00081                 x = r->x;
00082                 break;
00083         
00084         case 4: // East
00085         
00086                 x = r->x + r->w - w;
00087                 break;
00088                 
00089         case 7:
00090         case 6: // Center
00091         
00092                 x = r->x + ( r->w / 2 ) - ( w / 2 );
00093                 break;
00094         
00095         case 3: // Before
00096         
00097                 x = r->x - w;
00098                 break;
00099         
00100         case 5: // Behind
00101                 
00102                 x = r->x + r->w;
00103                 break;
00104     
00105         default:
00106                 break;  
00107     
00108     }
00109 
00110 
00111 
00112 }
00113 
00114 /***********************************************************************
00115 
00116     CAUGuiImage
00117     
00118     class for loading and containing images
00119 
00120 ***********************************************************************/
00121 
00122 CAUGuiImage::CAUGuiImage ( char* pngFileName )
00123 {
00124     
00125     loadImagePNG ( pngFileName );
00126     
00127     strcpy ( fileName, pngFileName );
00128     
00129     resourceID = 0;
00130     
00131     setType ( kCAUGui_PNG );
00132 
00133 }
00134 
00135 bool CAUGuiImage::isPNG ( char* pngFileName )
00136 {
00137 
00138     if ( strcmp ( fileName, pngFileName ) ) return false;
00139     
00140     return true;
00141 
00142 }
00143 
00144 void CAUGuiImage::loadImagePNG ( char* pngFileName )
00145 {
00146     
00147     CFBundleRef bundleRef = CFBundleGetBundleWithIdentifier(CFSTR( AU_BUNDLE_IDENTIFIER ));
00148     CGDataProviderRef   provider;
00149     
00150     if ( bundleRef )
00151     {
00152         SInt16 tmp = CurResFile();
00153     
00154         CFStringRef fileName = ::CFStringCreateWithCString( NULL, pngFileName, kCFStringEncodingASCII );
00155         if ( fileName != NULL )
00156         {
00157             CFURLRef url = ::CFBundleCopyResourceURL( bundleRef, fileName, NULL, NULL );
00158             
00159             provider = CGDataProviderCreateWithURL( url );
00160 
00161             Image = CGImageCreateWithPNGDataProvider( provider, NULL, false,  kCGRenderingIntentDefault );
00162             
00163             CGDataProviderRelease( provider );
00164             CFRelease( url );
00165             CFRelease( fileName );
00166         }
00167         else
00168         {
00169             Image = NULL;
00170         }
00171         
00172          UseResFile(tmp);
00173     }
00174     
00175 }
00176 
00177 /*
00178 
00179 void CAUGuiImage::loadImageRSRC ( char* pngFileName )
00180 {
00181     
00182     CFBundleRef bundleRef = CFBundleGetBundleWithIdentifier(CFSTR( AU_BUNDLE_IDENTIFIER ));
00183     CGDataProviderRef   provider;
00184     
00185     if ( bundleRef )
00186     {
00187         SInt16 tmp = CurResFile();
00188 
00189         PicHandle thePicture;
00190     
00191         thePicture = GetPicture( 134 );
00192         DetachResource( (Handle) thePicture );
00193     
00194             
00195     // reminder: open QuartzPrimer.pdf in Acrobat and Copy/Paste that damn code!!!          
00196                             
00197         
00198         if (thePicture)
00199         ReleaseResource( (Handle) thePicture );
00200 
00201     
00202         
00203          UseResFile(tmp);
00204     }
00205     
00206 }
00207 
00208 */
00209 
00210 /***********************************************************************
00211 
00212     CAUGuiGraphic
00213     
00214     class for loading and containing images
00215 
00216 ***********************************************************************/
00217 
00218 CAUGuiGraphic::CAUGuiGraphic ( char* pngFileName )
00219 {
00220     
00221     loadImagePNG ( pngFileName );   
00222     Frames = 1;
00223     FrameHeight = 1;
00224     
00225     if ( Image != NULL )
00226         FrameHeight = CGImageGetHeight (Image);
00227         
00228     setType ( kCAUGui_Graphic );
00229 
00230 }
00231 
00232 CAUGuiGraphic::CAUGuiGraphic ( char* pngFileName, UInt32 numFrames )
00233 {
00234     
00235     loadImagePNG ( pngFileName );
00236     if ( numFrames > 1 )    Frames = numFrames;
00237     else    Frames = 1;
00238     
00239     int height = Frames;
00240     
00241     if ( Image != NULL )
00242         height = CGImageGetHeight (Image);
00243     
00244     FrameHeight = height / numFrames;
00245     
00246     setType ( kCAUGui_Graphic );
00247 
00248 }
00249 
00250 SInt32 CAUGuiGraphic::getWidth()
00251 {
00252     SInt32 width = 0;
00253 
00254     if ( Image != NULL )
00255         width = CGImageGetWidth (Image); 
00256 
00257     return width;
00258 }
00259     
00260 SInt32 CAUGuiGraphic::getHeight()
00261 {
00262     return FrameHeight;
00263 }
00264 
00265 void CAUGuiGraphic::draw( CGContextRef context, UInt32 portHeight, eRect* r, float value )
00266 {
00267     // if Image has frames, the frame will be drawn
00268     // otherwise, the plain image will get out
00269     
00270     CGRect bounds;
00271     
00272     if ( Frames > 1 )
00273     {
00274      
00275         r->to( &bounds, portHeight );
00276         
00277         SInt32 offset = (SInt32)((float)(Frames-1) * value) * r->h + r->h;
00278         
00279         SInt32 height = Frames * r->h;
00280         
00281         bounds.size.height = height;
00282         
00283         bounds.origin.y += offset-height;
00284     
00285     }
00286     else
00287     {
00288         r->to( &bounds, portHeight );
00289     }
00290 
00291     if ( Image != NULL )
00292             CGContextDrawImage( context, bounds, Image );
00293 
00294 
00295 }
00296 
00297 
00298 void CAUGuiGraphic::loadImagePNG ( char* pngFileName )
00299 {
00300     
00301     CFBundleRef bundleRef = CFBundleGetBundleWithIdentifier(CFSTR( AU_BUNDLE_IDENTIFIER ));
00302     CGDataProviderRef   provider;
00303     
00304     if ( bundleRef )
00305     {
00306         SInt16 tmp = CurResFile();
00307     
00308         CFStringRef fileName = ::CFStringCreateWithCString( NULL, pngFileName, kCFStringEncodingASCII );
00309         if ( fileName != NULL )
00310         {
00311             CFURLRef url = ::CFBundleCopyResourceURL( bundleRef, fileName, NULL, NULL );
00312             
00313             provider = CGDataProviderCreateWithURL( url );
00314 
00315             Image = CGImageCreateWithPNGDataProvider( provider, NULL, false,  kCGRenderingIntentDefault );
00316             
00317             CGDataProviderRelease( provider );
00318             CFRelease( url );
00319             CFRelease( fileName );
00320         }
00321         else
00322         {
00323             Image = NULL;
00324         }
00325         
00326          UseResFile(tmp);
00327     }
00328     
00329 }
00330 
00331 
00332 /***********************************************************************
00333 
00334     CAUGuiLabel
00335     
00336     class for loading and containing images
00337 
00338 ***********************************************************************/
00339 
00340 CAUGuiLabel::CAUGuiLabel ( CAUGuiGraphic* theGraphic, eRect* theBounds )
00341 {
00342     
00343     Graphic = theGraphic;
00344     
00345     Bounds.set( theBounds );
00346     
00347     if ( Bounds.w == 0 )
00348         Bounds.w = theGraphic->getWidth();
00349         
00350     if ( Bounds.h == 0 )
00351         Bounds.h = theGraphic->getHeight();
00352         
00353     setType ( kCAUGui_Label );
00354 
00355 }
00356 
00357 void CAUGuiLabel::draw( CGContextRef context, UInt32 portHeight, eRect* rect, float value )
00358 {
00359 
00360     eRect* theBounds;
00361     
00362     theBounds->set( rect );
00363     
00364     theBounds->x += Bounds.x;
00365     theBounds->y += Bounds.y;
00366     theBounds->h = Bounds.h;
00367     theBounds->w = Bounds.w;
00368     
00369     Graphic->draw( context, portHeight, theBounds, value );
00370     
00371     
00372 }
00373 
00374 /***********************************************************************
00375 
00376     CAUGuiScheme
00377     
00378     class for automatically layouting controls
00379 
00380 ***********************************************************************/
00381 
00382 CAUGuiScheme::CAUGuiScheme ()
00383 {
00384     patternLength = 0;
00385     currentIteration = 0;
00386     currentPattern = 0;
00387     X = 0;
00388     Y = 0;
00389     startX = 0;
00390     startY = 0;
00391     offsetX = 0;
00392     offsetY = 0;
00393     notAligned = true;
00394 
00395 }
00396 
00397 void CAUGuiScheme::add( eRect* r, CAUGuiAlign align )
00398 {
00399     if ( patternLength < kCAUGuiSchemeMaxPatterns )
00400     {
00401         rects[ patternLength ].set( r );
00402         aligns[ patternLength ] = align;
00403         patternLength++;
00404     }
00405 }
00406     
00407 void CAUGuiScheme::at ( SInt32 x, SInt32 y )
00408 {
00409     X = x; Y = y;
00410     notAligned = true;
00411 }
00412     
00413 eRect* CAUGuiScheme::get()
00414 {
00415     currentPattern++;
00416     if ( currentPattern >= patternLength )
00417     {
00418         currentPattern = 0;
00419         X = startX + offsetX * currentIteration;
00420         Y = startY + offsetY * currentIteration;
00421         notAligned = true;
00422         currentIteration++;
00423     }
00424     
00425     
00426     if ( notAligned )
00427     {
00428         resulting.set( &rects[ currentPattern ] );
00429         resulting.offset( X, Y );
00430         notAligned = false;
00431     }
00432     else
00433     {
00434         resulting.align ( &rects[ currentPattern ], aligns[ currentPattern ] );
00435     
00436     }
00437     
00438     return &resulting;
00439 
00440 }
00441 
00442 eRect* CAUGuiScheme::getS( SInt32 w, SInt32 h )
00443 {
00444     eRect* ref = get ();
00445     
00446     ref->w = w;
00447     ref->h = h;
00448 
00449     return ref;
00450 }
00451 
00452 
00453 void CAUGuiScheme::reset()
00454 {
00455     currentPattern = 0;
00456     currentIteration = 0;
00457     notAligned = true;
00458     X = startX;
00459     Y = startY;
00460     
00461 }

(c) 2002 - 2003 by Urs Heckmann www.u-he.com
Generated on Thu May 6 15:13:14 2004 for CAUGui by doxygen 1.3.6