home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / ListInDialog / ListInDialog.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.9 KB  |  240 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ListInDialog.c
  3.  
  4.     Contains:    A snippet that shows how to (uuuhhhhh) put a list in a dialog.
  5.  
  6.                 I thought we had this one, but I guess not. It's easy, just create the list
  7.                 right after you create the dialog, then call LUpdate and LClick in a 
  8.                 dialog filter to respond to user events.
  9.  
  10.                 Please see the snippet DialogBits for a more comprehensive treatment of
  11.                 everything you can do in a dialog (well, almost).
  12.  
  13.     Written by: C.K. Haun    
  14.  
  15.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  16.  
  17.                 You may incorporate this Apple sample source code into your program(s) without
  18.                 restriction. This Apple sample source code has been provided "AS IS" and the
  19.                 responsibility for its operation is yours. You are not permitted to redistribute
  20.                 this Apple sample source code as "Apple sample source code" after having made
  21.                 changes. If you're going to re-distribute the source, we require that you make
  22.                 it clear in the source that the code was descended from Apple sample source
  23.                 code, but that you've made changes.
  24.  
  25.     Change History (most recent first):
  26.                 8/6/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  27.                 
  28.  
  29. */
  30.  
  31. #include <Fonts.h>
  32. #include <resources.h>
  33. #include <Sound.h>
  34. #include <Lists.h>
  35. #include <TextUtils.h>
  36.  
  37. enum  {
  38.     kSampleDialog = 128
  39. };
  40. enum  {
  41.     kListItem = 3
  42. };
  43.  
  44. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  45.  
  46. void SetUpList(ListHandle theList);
  47.  
  48. /* making the list handle a global */
  49. ListHandle gAList;
  50.  
  51. void main(void)
  52. {
  53.     
  54.     // this "rect" defines the data bounds of the list. In this case, a
  55.     // one column list
  56.     Rect listRect2 =  {
  57.         0, 0, 0, 1
  58.     };
  59.     // cell to initialize with
  60.     Cell cp =  {
  61.         0, 0
  62.     };
  63.     
  64.     // the dialog we're using
  65.     DialogPtr myDialog = nil;
  66.     
  67.     // hitItem for ModalDialog call
  68.     short hitItem = 0;
  69.     
  70.     // these three are here for all the GetDItem/SetDItem calls
  71.     Rect tempRect;
  72.     short tempItem;
  73.     Handle tempHandle;
  74.     
  75.     static ModalFilterUPP modalFilterUPP;
  76.     
  77.     modalFilterUPP = NewModalFilterProc(theListFilter);
  78.    
  79.     /* start up managers */
  80.     InitGraf((Ptr)&qd.thePort);
  81.     InitFonts();
  82.     InitWindows();
  83.     InitMenus();
  84.     TEInit();
  85.     InitDialogs(nil);
  86.     InitCursor();
  87.    
  88.     
  89.     /* get our dialog. It is created HIDDEN, and shown after I set the */
  90.     /* user item that will hold the list */
  91.  
  92.     myDialog = GetNewDialog(kSampleDialog, nil, (WindowPtr)-1);
  93.     
  94.     // setting it up in the Dialog manager's records, 
  95.     GetDialogItem(myDialog, kListItem, &tempItem, &tempHandle, &tempRect);
  96.     
  97.     /* inset the rect by 16, which is the width of the scroll bar that will be attached */
  98.     /* to this list */
  99.     tempRect.right -= 16;
  100.     
  101.     /* set the current port to this dialog */
  102.     SetPort(myDialog);
  103.     
  104.     /* create the list */
  105.     
  106.     gAList = LNew(&tempRect,    // in the tempRect bounds
  107.         &listRect2,                // with a sinngle column
  108.         cp,                        // default cell size (a cell of 0,0 says that)
  109.         nil,                    // no special LDEF, use the standard one
  110.         myDialog,                // put it in this port
  111.         false,                    // do NOT draw initially
  112.         false,                    // does NOT have a grow box space
  113.         false,                    // does NOT scroll horizontally
  114.         true);                    // DOES have a verticle scroll bar
  115.     
  116.     // call another funtion to put in the strings
  117.     SetUpList(gAList);
  118.     
  119.     // turn drawing on after the list has been filled
  120.     LSetDrawingMode(true, gAList);
  121.     
  122.     // show the dialog
  123.     ShowWindow((WindowPtr)myDialog);
  124.     
  125.     /* draw it once */
  126.     DrawDialog(myDialog);
  127.     
  128.     // loop until ModalDialog is done
  129.     do {
  130.         // we have to use a ModalDialog filter, since ModalDialog doesn't
  131.         // automatically handle lists
  132.         ModalDialog(modalFilterUPP, &hitItem);
  133.         
  134.         // switch off what item was hit
  135.         switch (hitItem) {
  136.             /* I don't care about any of the hits here */
  137.             
  138.         }
  139.         // wait for an OK or Cancel
  140.     }
  141.             while (hitItem != ok && hitItem != cancel);
  142.     
  143.     // don't do anything in this sample
  144.     DisposeDialog(myDialog);
  145. }
  146.  
  147.  
  148. /* Here is the filter that handles any List activity */
  149. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  150. {
  151.     
  152.     Rect tempR;
  153.     Boolean returnValue = false;    // defaults to me not saying I handled anything
  154.     Boolean theBoolean;
  155.     WindowPtr oldP;
  156.  
  157.     // get the current port, set the port to this dialog
  158.     GetPort(&oldP);
  159.     SetPort(theDialog);
  160.     
  161.     // was this an update event for this dialog?
  162.     if ((theEvent->what == updateEvt) && (theEvent->message == (UInt32)theDialog)) {
  163.         
  164.         
  165.         // Update the list.
  166.         LUpdate(theDialog->visRgn, gAList);
  167.         
  168.         // get the list rectangle
  169.         tempR = (*gAList)->rView;
  170.         
  171.         // push it outwards one pixel and frame the list
  172.         InsetRect(&tempR, -1, -1);
  173.         FrameRect(&tempR);
  174.         
  175.         // NOTE: Do !NOT! return 'true' if you did SOME drawing in response to a dialog update
  176.         // event in your filter!
  177.         // ONLY if you did EVERYTHING should you return 'true', or else you can cause other updating
  178.         // not to occur, which would be Bad
  179.         
  180.     } else {
  181.         // see if this was a mouseDown event
  182.         if (theEvent->what == mouseDown) {
  183.             Point theP;
  184.  
  185.             // we set the port to the dialog on entry to the filter, so a GetMouse will work
  186.             GetMouse(&theP);
  187.             
  188.             // get the list rectangle
  189.             tempR = (*gAList)->rView;
  190.             
  191.             // add the scroll bar back in for hit testing (remember we took it out earlier)
  192.             tempR.right += 16;
  193.             
  194.             // See if they clicked in our list!
  195.             if (PtInRect(theP, &tempR)) {
  196.                 theBoolean = LClick(theP, nil, gAList);
  197.                 // if they double-clicked the list, return 1, as if the OK button had been pressed
  198.                 if (theBoolean)
  199.                     *itemHit = 1;
  200.                 else
  201.                     *itemHit = kListItem;
  202.                 
  203.                 // tell the Dialog Manager that we handled this click, it can stop searching for a click-owner
  204.                 returnValue = true;
  205.             }
  206.         }
  207.     }
  208.     
  209.     // reset the original port
  210.     
  211.     SetPort(oldP);
  212.     
  213.     return(returnValue);
  214. }
  215.  
  216.  
  217. /* simple function to put a list of strings into my list */
  218. void SetUpList(ListHandle theList)
  219. {
  220.     register qq;
  221.     Cell cp;
  222.     Str255 theString;
  223.     short listCount = 0;
  224.     Boolean Changed = false;
  225.     short theCount;
  226.     Handle tempHandle = GetResource('STR#', 128);
  227.     if(tempHandle){
  228.     theCount = *((short *)(*tempHandle));
  229.         if (theCount) {
  230.             for (qq = 0; qq < theCount; qq++) {
  231.                 GetIndString(theString, 128, qq + 1);
  232.                 listCount = LAddRow(1, listCount + 1, theList);
  233.                 cp.h = 0;
  234.                 cp.v = listCount;
  235.                 LAddToCell(&theString[1], theString[0], cp, theList);
  236.             }
  237.         }
  238.     }
  239. }
  240.