home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / µSim 1.1 / source / TrackThumb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-25  |  1.9 KB  |  78 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993-1997 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. #include    <Limits.h>
  12.  
  13. #include    "UtilsSys7.h"
  14.  
  15. #include    "TrackThumb.h"
  16.  
  17. //#pragma segment Main
  18.  
  19. short TrackThumb(ControlHandle control, Point start, void (*action)(ControlHandle, short))
  20. {
  21. enum {
  22. kTooDistant = 23
  23. };
  24.  
  25. EventRecord    dummyEventRec;
  26. Rect    rct;
  27. GrafPtr    savePort;
  28. Point    pos;
  29. long    newval;
  30. short    val, old;
  31. short    min, max;
  32. short    widthOrheight;
  33.  
  34. GetPort(&savePort);
  35. SetPort((*control)->contrlOwner);
  36. min    = GetControlMinimum(control);    // min value
  37. max    = GetControlMaximum(control);    // max value
  38. val    = GetControlValue(control);        // cur value
  39. rct    = (*control)->contrlRect;        // control's rectangle
  40.  
  41. widthOrheight = rct.right - rct.left;
  42. old = SHRT_MIN;
  43. do {
  44.     GetMouse(&pos);
  45.  
  46.     if (pos.v < rct.top + widthOrheight)
  47.         pos.v = rct.top + widthOrheight;
  48.     else if (pos.v > rct.bottom - widthOrheight)
  49.         pos.v = rct.bottom - widthOrheight;
  50.  
  51.     if ((pos.h - rct.right) > kTooDistant || (rct.left - pos.h) > kTooDistant)
  52.         newval = val;
  53.     else {
  54. //        newval = val + (max - min) * (pos.v - start.v) /
  55. //                        (rct.bottom - rct.top - widthOrheight - widthOrheight - widthOrheight);
  56.         newval = val + AMultBDivC((max - min), (pos.v - start.v),
  57.                         (rct.bottom - rct.top - widthOrheight - widthOrheight - widthOrheight));
  58.         if (newval < min)
  59.             newval = min;
  60.         else if (max < newval)
  61.             newval = max;
  62.         }
  63.  
  64.     if (newval != old) {
  65.         SetControlValue(control, newval);
  66.         action(control, old = GetControlValue(control));
  67.         }
  68.     else {    /* give some time to other applications */
  69.         SystemTask();
  70.         (void)EventAvail(everyEvent, &dummyEventRec);
  71.         }
  72.     }
  73. while (StillDown());
  74. SetPort(savePort);
  75. return kControlIndicatorPart;
  76. }
  77.  
  78.