home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Toolbox / ColoredCheckBox / main.c < prev   
Encoding:
C/C++ Source or Header  |  1994-09-09  |  3.8 KB  |  156 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    ColoredCheckBox                                            */
  4. /*                                                                            */
  5. /*    Description:    This app demonstrates how to create a check box on the    */
  6. /*                    gray window backgrounds.The key to this is making sure  */
  7. /*                    that the background color for the window is set.         */
  8. /*                    a 'wctb' was created using ResEdit which provides the     */
  9. /*                    gray content for the window. I also Derezed the app and */
  10. /*                    made a coloredCheckBox.r file just to show you what's     */
  11. /*                    going on in the 'wctb' structure.                        */
  12. /*                                                                            */
  13. /*    Files:            ColoredCheckBox.π                                        */
  14. /*                    main.c                                                    */
  15. /*                    ColoredCheckBox.π.RSRC                                    */                    
  16. /*                                                                            */
  17. /*    Programmer:        Larry Lai                                                */
  18. /*    Organization:    Apple Computer, Inc.                                    */
  19. /*    Department:        Developer Technical Support, DTS                        */
  20. /*    Language:        C (Think C version 7.0)                                    */
  21. /*    Date Created:    09-02-94                                                */
  22. /*                                                                            */
  23. /****************************************************************************/
  24.  
  25. #include <Sound.h>
  26.  
  27. #define    rColoredCheckBoxWindow         128
  28. #define    rCheckBox                    128
  29. #define    kSoundID                    128
  30.  
  31. Boolean    gQuit;
  32.  
  33. void InitToolbox(void);
  34. void MyCreateColoredCheckBoxWindow();
  35. void DoEventLoop();
  36. void DoContentClick(WindowPtr, EventRecord);
  37. void DoCheckBox(Point, ControlHandle);
  38.  
  39. void InitToolbox()
  40. {
  41.     InitGraf((Ptr) &qd.thePort);
  42.     InitFonts();
  43.     InitWindows();
  44.     InitMenus();
  45.     FlushEvents(everyEvent,0);
  46.     InitDialogs(0L);
  47.     InitCursor();
  48. }
  49.  
  50. main()
  51. {
  52.     
  53.     InitToolbox();
  54.     
  55.     MyCreateColoredCheckBoxWindow();
  56.  
  57.     DoEventLoop();
  58. }
  59.  
  60. void MyCreateColoredCheckBoxWindow()
  61. {
  62.     WindowPtr        myWindow;
  63.     ControlHandle    gMyColoredCheckBoxWindow;
  64.     
  65.     myWindow = GetNewCWindow(rColoredCheckBoxWindow, nil, (WindowPtr) -1);
  66.     if (myWindow != nil)
  67.     {
  68.         SetPort (myWindow);
  69.         GetNewControl(rCheckBox, myWindow);
  70.     }
  71.     else
  72.         SysBeep(10);
  73.         
  74. }
  75.  
  76. void DoEventLoop()
  77. {
  78.     EventRecord anEvent;
  79.     WindowPtr   evtWind;
  80.     short       clickArea;
  81.     Rect        screenRect;
  82.  
  83.     do{
  84.         if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
  85.         {
  86.             if (anEvent.what == mouseDown)
  87.             {
  88.                 clickArea = FindWindow( anEvent.where, &evtWind );
  89.                 
  90.                 if (clickArea == inDrag)
  91.                 {
  92.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  93.                     DragWindow( evtWind, anEvent.where, &screenRect );
  94.                 }
  95.                 else if (clickArea == inContent)
  96.                 {
  97.                     if (evtWind != FrontWindow())
  98.                         SelectWindow( evtWind );
  99.                     else
  100.                         DoContentClick(evtWind, anEvent);    
  101.                     }
  102.                 else if (clickArea == inGoAway)
  103.                     if (TrackGoAway( evtWind, anEvent.where ))
  104.                         gQuit = true;
  105.             }
  106.             else if (anEvent.what == updateEvt)
  107.             {
  108.                 evtWind = (WindowPtr)anEvent.message;    
  109.                 SetPort( evtWind );
  110.                 
  111.                 BeginUpdate( evtWind );
  112.                 UpdateControls(evtWind, evtWind->visRgn);
  113.                 EndUpdate (evtWind);
  114.             }
  115.         }
  116.     }while (!gQuit);
  117. }
  118.  
  119.     
  120. void DoContentClick(WindowPtr theWindow, EventRecord theEvent)
  121. {
  122.     Point            mouse;
  123.     ControlHandle    control;
  124.     short            part;
  125.     
  126.     SetPort(theWindow);
  127.     mouse = theEvent.where;    /*Get the mouse location*/
  128.     GlobalToLocal(&mouse);    /*convert to local coordinates*/
  129.     part = FindControl(mouse, theWindow, &control);
  130.  
  131.     if(part == inCheckBox)
  132.         DoCheckBox(mouse, control);
  133.  
  134. }
  135.  
  136. void DoCheckBox(Point mouse, ControlHandle control)
  137. {
  138.     short     checkbox;
  139.     Handle    theSound;
  140.  
  141.     if (TrackControl(control, mouse, nil))        /*user clicks checkbox*/
  142.     {
  143.         checkbox = GetCtlValue(control);        /*get last value of checkbox*/
  144.         checkbox = 1- checkbox;                    /*toggle value of checkbox*/
  145.         SetCtlValue(control, checkbox);            /*set checkbox to new value*/
  146.         if(checkbox == 1)                        /*express yourself next time user clicks the checkbox*/
  147.         {
  148.             theSound = GetResource('snd ', kSoundID);
  149.             if (theSound != nil) {
  150.                 SndPlay(nil, theSound, false);
  151.                 ReleaseResource(theSound);
  152.             }
  153.         }
  154.     }
  155. }
  156.