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

CAUGuiControl.cpp

Go to the documentation of this file.
00001 /*
00002  *  CAUGuiControl.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  */
00009 
00010 #include "CAUGuiControl.h"
00011 
00012 CAUGuiCtrl::CAUGuiCtrl (
00013                     CAUGuiMan* theChief,
00014                     CAAUParameter &theAuvp,
00015                     eRect* theWhere,
00016                     CAUGuiResolution theRes
00017                 )
00018 {   
00019 
00020     Chief = theChief;
00021     where.set( theWhere );
00022     vizArea.set( theWhere );
00023     
00024     setDefaults();
00025     
00026     auvp = theAuvp;
00027     
00028     AUVPattached = true;
00029         
00030     where.set( theWhere );
00031     vizArea.set( theWhere );
00032     
00033     Resolution = theRes;
00034     
00035     Range = theAuvp.ParamInfo().maxValue - theAuvp.ParamInfo().minValue;
00036 
00037     //printf ("ctor CAUGuiCtrl\n");
00038 }
00039 
00040 CAUGuiCtrl::CAUGuiCtrl (
00041                     CAUGuiMan* theChief,
00042                     eRect* theWhere,
00043                     UInt32 theRange
00044                 )
00045 {
00046     Chief = theChief;
00047     where.set( theWhere );
00048     vizArea.set( theWhere );
00049 
00050     setDefaults();
00051     
00052     
00053     Resolution = kCAUGui_res_1;
00054     
00055     auvp = CAAUParameter(); // a NULLy auvp
00056     AUVPattached = false;
00057     
00058     Range = theRange;
00059 
00060     //printf ("ctor CAUGuiCtrl2\n");
00061 }
00062 
00063     
00064 CAUGuiCtrl::CAUGuiCtrl (
00065                     CAUGuiMan* theChief,
00066                     eRect* theWhere
00067                 )
00068 {
00069     Chief = theChief;
00070     where.set( theWhere );
00071     vizArea.set( theWhere );
00072     
00073     setDefaults();
00074         
00075     Resolution = kCAUGui_res_0;
00076     
00077     auvp = CAAUParameter(); // a NULLy auvp
00078     AUVPattached = false;
00079     
00080     Range = 0;
00081     
00082     //printf ("ctor CAUGuiCtrl2\n");
00083 }
00084     
00085 CAUGuiCtrl::~CAUGuiCtrl()
00086 {
00087     if (carbonControl) DisposeControl(carbonControl);
00088     if ( children != NULL ) delete children;
00089     //printf ("dtor CAUGuiCtrl\n");
00090 }
00091 
00092 void CAUGuiCtrl::setDefaults()
00093 {
00094     
00095     
00096     Daddy = NULL;
00097     
00098     
00099     
00100     id = 0;
00101     
00102     carbonControl = NULL;
00103     
00104     opaque = true;
00105     
00106     children = NULL;
00107     
00108     lastUpdatedValue = 0;
00109     
00110     tolerance = 1;
00111     
00112     becameVisible = false;
00113     
00114     wantsTrackingFlag = true;
00115     
00116     col_red = CAUGUI_FONT_RED;
00117     col_green = CAUGUI_FONT_GREEN;
00118     col_blue = CAUGUI_FONT_BLUE;
00119     col_alpha = CAUGUI_FONT_ALPHA;
00120     font_size = CAUGUI_FONT_SIZE;
00121     textalign = CAUGUI_DEFAULT_FONT_ALIGN;
00122     strcpy ( font_name, CAUGUI_FONT );
00123 
00124 
00125 
00126 }
00127 
00128 
00129 void CAUGuiCtrl::addCtrl( CAUGuiCtrl* theCtrl )
00130 {
00131 
00132     //printf ( "Huh, adding...\n" );
00133 
00134     if ( children == NULL )
00135             children = theCtrl;
00136         else
00137             children->append( theCtrl );
00138             
00139     theCtrl->setDaddy ( this );
00140     theCtrl->setOffset ( where.x, where.y );
00141     
00142     Chief->addCtrl( theCtrl );
00143 }
00144 
00145 void CAUGuiCtrl::setOffset ( SInt32 x, SInt32 y )
00146 {
00147     this->where.offset ( x, y );
00148     this->vizArea.offset ( x, y );
00149 
00150     if ( this->children != NULL )
00151     {
00152         CAUGuiCtrl* current = children;
00153         while ( current != NULL )
00154         {
00155             current->setOffset ( x, y );
00156             current = (CAUGuiCtrl*)(current->getNext());
00157         }
00158     }
00159 }
00160     
00161 void CAUGuiCtrl::clip ( bool drawing )
00162 {
00163 
00164 /*  
00165     if ( drawing )
00166         printf ( "drawing id%d type%d\n", (int)getID(), (int) getType() );
00167     else
00168         printf ( "clipping id%d type%d\n", (int)getID(), (int) getType() );
00169 */
00170 
00171     if ( opaque )
00172     {
00173         Rect r;
00174         where.to ( &r );
00175         FrameRect( &r );
00176         //printf ( "clipping opaque %d %d %d %d\n", r.left, r.top, r.right, r.bottom );
00177     }
00178     
00179     if ( !opaque || drawing )
00180     {       
00181         CAUGuiCtrl* current = children;
00182         while( current != NULL )
00183         {
00184             current->clip( false );
00185             current = (CAUGuiCtrl*)current->getNext();
00186         }
00187     }
00188 }
00189 
00190 void CAUGuiCtrl::setVisible ( bool viz )
00191 {
00192     if ( carbonControl != NULL )
00193     {
00194         if ( viz )
00195         {
00196             ShowControl ( carbonControl );
00197             becameVisible = true;
00198             Draw1Control ( carbonControl );
00199             
00200         }
00201         else
00202             HideControl ( carbonControl );
00203     }
00204     
00205     if ( children != NULL )
00206     {
00207         CAUGuiCtrl* current = children;
00208         while( current != NULL )
00209         {
00210             current->setVisible ( viz );
00211             current = (CAUGuiCtrl*)current->getNext();
00212         }
00213     }
00214 }
00215 
00216 void CAUGuiCtrl::setDefault()
00217 {
00218     if ( isAUVPattached() )
00219     {
00220         float fDefault = auvp.ParamInfo().defaultValue - auvp.ParamInfo().minValue;
00221     
00222         fDefault /= Range;
00223     
00224         ControlRef carbonControl = getCarbonControl();
00225     
00226         UInt32 max = GetControl32BitMaximum(carbonControl);
00227         
00228         UInt32 defaultValue = (UInt32)((float)max * fDefault);
00229         
00230         SetControl32BitValue ( carbonControl, defaultValue );
00231     }
00232 
00233 }
00234 
00235 
00236 void CAUGuiCtrl::idle()
00237 {
00238     if ( children != NULL )
00239         children->idle();
00240     if ( getNext() != NULL )
00241         ((CAUGuiCtrl*)getNext())->idle();
00242 }
00243     
00244 bool CAUGuiCtrl::isControlRef ( ControlRef theControl )
00245 {
00246     if (carbonControl == theControl) return true;
00247     if ( children != NULL )
00248     {
00249         CAUGuiCtrl* current = children;
00250         while( current != NULL )
00251         {
00252             if ( current->isControlRef ( theControl ) ) return true;
00253             current = (CAUGuiCtrl*)current->getNext();
00254         }
00255     }
00256     return false;
00257 }
00258 
00259 CAUGuiCtrl* CAUGuiCtrl::getChild(ControlRef theControl)
00260 {
00261     if ( carbonControl == theControl ) return this;
00262     if ( children != NULL )
00263     {
00264         CAUGuiCtrl* current = children;
00265         while( current != NULL )
00266         {
00267             if ( current->isControlRef ( theControl ) )
00268                 return current->getChild( theControl );
00269         
00270             current = (CAUGuiCtrl*)current->getNext();
00271         }
00272     }
00273     return NULL;
00274 }
00275 
00276 void CAUGuiCtrl::setForeBounds ( SInt32 x, SInt32 y, SInt32 w, SInt32 h )
00277 {
00278     vizArea.set ( x, y, w, h );
00279 
00280 }
00281 
00282 void CAUGuiCtrl::shrinkForeBounds ( SInt32 x, SInt32 y, SInt32 w, SInt32 h )
00283 {
00284     vizArea.grow ( -x, -y, -w, -h );
00285 }
00286 
00287 bool CAUGuiCtrl::mustUpdate ()
00288 {
00289     
00290     if ( carbonControl == NULL ) return true; // probably no User interaction
00291     
00292     SInt32 v = (SInt32 ) GetControl32BitValue( carbonControl );
00293 
00294     SInt32 diff = v - lastUpdatedValue;
00295     
00296     if ( Chief->isRelaxed() == false || becameVisible == true )
00297     {
00298         becameVisible = false;
00299         
00300         //printf ( "CAUGuiCtrl::mustUpdate () no relax!\n" );
00301         
00302         lastUpdatedValue = diff;
00303     
00304         return true;
00305     }
00306     
00307     
00308     if ( diff <= -tolerance || diff >= tolerance || Chief->isRelaxed() == false )
00309     {
00310         lastUpdatedValue = diff;
00311     
00312         return true;
00313     
00314     }
00315     
00316     //printf ( "CAUGuiCtrl::mustUpdate () draw prevented!\n" );
00317     
00318     return false;
00319 
00320 }
00321 
00322 
00323 static pascal OSStatus CAUGuiControlHandler ( EventHandlerCallRef myHandler, EventRef theEvent, void* userData )
00324 {
00325     #pragma unused (myHandler )
00326         
00327     OSStatus result = eventNotHandledErr;
00328     UInt32 whatHappened;
00329     
00330     GrafPtr thePort;
00331     CGrafPtr windowPort;
00332     WindowRef theWindow;
00333     ControlRef theControl;
00334     
00335     Rect controlBounds;
00336     Rect portBounds;
00337     Rect globalBounds; // Window Content Region...
00338     
00339     CGContextRef context;
00340    
00341     
00342     Point P;
00343     int doDaTrack = 1;
00344     UInt32 modifiers;
00345     bool with_option, with_shift;
00346          
00347     whatHappened = GetEventKind ( theEvent );
00348     
00349     
00350     GetEventParameter ( theEvent, kEventParamDirectObject, typeControlRef, NULL, sizeof( ControlRef ), NULL, &theControl );
00351         
00352     GetControlBounds ( theControl, &controlBounds );
00353     
00354     theWindow = GetControlOwner ( theControl );
00355         
00356     GetWindowBounds( theWindow, kWindowGlobalPortRgn, &globalBounds);
00357     
00358     CAUGuiMan* theChief = (CAUGuiMan*)userData;
00359     CAUGuiCtrl* theCtrl = NULL;
00360         
00361     if ( theChief != NULL )
00362     {
00363         theCtrl = theChief->getCtrlByControlRef ( theControl ); 
00364     }
00365         
00366     if ( theCtrl != NULL )
00367     {
00368         //printf ("Control exists!\n");
00369     
00370         switch ( whatHappened )
00371         {
00372             case kEventControlApplyBackground:
00373                 break; 
00374             case kEventControlDraw:
00375         
00376                 if ( theCtrl->mustUpdate () == false )
00377                 {
00378                     result = noErr;
00379                     break;
00380 
00381                 }
00382                 
00383         
00384                 GetPort ( &thePort );
00385                 windowPort = GetWindowPort(  theWindow );
00386                 SetPort(windowPort);
00387                 GetPortBounds ( windowPort, &portBounds );
00388                 
00389                 // C L I P P I N G
00390                 
00391                 RgnHandle clipRgn;
00392                 
00393                 clipRgn = NewRgn();
00394                 
00395                 OpenRgn();
00396             
00397                 //FrameRect( &portBounds );
00398                 
00399                 theCtrl->clip( true );
00400                 
00401                 CloseRgn( clipRgn );
00402                 
00403                 SetClip( clipRgn );
00404             
00405                 clipRgn = GetPortClipRegion( windowPort, clipRgn );
00406                 
00407                 // D R A W I N G
00408             
00409                 QDBeginCGContext ( windowPort, &context );            
00410             
00411                 ClipCGContextToRegion( context, &portBounds, clipRgn );
00412                 
00413                 SyncCGContextOriginWithPort( context, windowPort );
00414                 
00415                 CGContextSaveGState( context );
00416                             
00417                 theCtrl->draw( context, portBounds.bottom );
00418                                 
00419                 CGContextRestoreGState( context );
00420                 
00421                 CGContextSynchronize ( context );
00422                 
00423                 QDEndCGContext ( windowPort, &context );
00424                 
00425                 DisposeRgn ( clipRgn );
00426                 
00427                 SetPort ( thePort );
00428                                     
00429                 result = noErr;
00430                 break;
00431             
00432             case kEventControlHitTest:
00433             
00434                 result = noErr;
00435                 break;
00436     
00437             case kEventControlTrack:
00438             case kEventControlClick:
00439                 
00440                     
00441                 modifiers = GetCurrentEventKeyModifiers();
00442                 if ( modifiers & controlKey )
00443                 {
00444                 
00445                     GetGlobalMouse ( &P );
00446                                 
00447                     printf ("with control\n");
00448                     theCtrl->mouseRight( &P ); 
00449                 
00450                     result = noErr;
00451                     break;
00452                 
00453                 }
00454                 
00455                 if ( modifiers & cmdKey )
00456                 {
00457                     theCtrl->setDefault(); 
00458                 
00459                     result = noErr;
00460                     break;
00461                 
00462                 }
00463                 
00464                 
00465                 with_option = modifiers & optionKey ? true : false;
00466                 with_shift = modifiers & shiftKey ? true : false;
00467             
00468                 //GetMouse ( &P );
00469                 GetGlobalMouse ( &P );
00470                 P.h -= controlBounds.left + globalBounds.left;
00471                 P.v -= controlBounds.top + globalBounds.top;
00472                 
00473                 
00474                 // prevent unnecessary draws
00475                 
00476                 theChief->setRelaxed ( true );
00477                 
00478                 
00479                 theCtrl->mouseDown( &P, with_option, with_shift );
00480                 
00481                 if ( theCtrl->wantsTracking() )
00482                 {
00483                     MouseTrackingResult outResult;
00484                         
00485                     while ( doDaTrack )
00486                     {
00487                         TrackMouseLocation (NULL, &P, &outResult);
00488                         
00489                         GetGlobalMouse ( &P );
00490                         P.h -= controlBounds.left + globalBounds.left;
00491                         P.v -= controlBounds.top + globalBounds.top;
00492     
00493                         if ( outResult == kMouseTrackingMouseUp )
00494                         {
00495                             theCtrl->mouseUp( &P, with_option, with_shift );
00496                             doDaTrack = 0;
00497                         }
00498                         else
00499                             theCtrl->mouseTrack( &P, with_option, with_shift );
00500                         
00501                     }
00502                 }
00503                 
00504                 theChief->setRelaxed ( false );
00505                 
00506                 result = noErr;
00507                 break;
00508         }
00509     }
00510     
00511     return ( result );
00512 }
00513 
00514 
00515 
00516 
00517 
00518 

(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