| 1 |
// SPDX-License-Identifier: MIT |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include "util.h" |
| 5 |
#include "slider.h" |
| 6 |
|
| 7 |
|
| 8 |
extern Pattern gSliderPattern; |
| 9 |
RGBColor gLightGrey = { |
| 10 |
60000, 60000, 60000 |
| 11 |
}; |
| 12 |
|
| 13 |
#define kDragHeight 13 |
| 14 |
|
| 15 |
/** Custom slider control |
| 16 |
* |
| 17 |
* Pre-OS8 Mac OS has no builtin slider control. One exists |
| 18 |
* in Appearance Manager (OS 8), but we would like to stay |
| 19 |
* compatible with System 7. Therefore, we define a custom |
| 20 |
* control. |
| 21 |
*/ |
| 22 |
pascal long |
| 23 |
slider_proc(short /* varCode */, ControlHandle ctl, |
| 24 |
short message, long param) |
| 25 |
{ |
| 26 |
Rect ctlRect, filledRect, dragRect, indicatorRect; |
| 27 |
short ctlValue, ctlMax, variableHeight, filledHeight; |
| 28 |
short topDist, botDist, offset, newValue; |
| 29 |
Point testPt; |
| 30 |
IndicatorDragConstraint* dragConstraint; |
| 31 |
RgnHandle cntlRgn; |
| 32 |
long retval = 0; |
| 33 |
|
| 34 |
recalc: |
| 35 |
ctlRect = (**ctl).contrlRect; |
| 36 |
ctlValue = GetControlValue(ctl); |
| 37 |
ctlMax = GetControlMaximum(ctl); |
| 38 |
variableHeight = rect_height(&ctlRect) - kDragHeight; |
| 39 |
filledHeight = variableHeight * ctlValue / ctlMax; |
| 40 |
|
| 41 |
/* Set up drawing rects */ |
| 42 |
filledRect = ctlRect; |
| 43 |
filledRect.top = filledRect.bottom - kDragHeight - filledHeight; |
| 44 |
dragRect = filledRect; |
| 45 |
dragRect.bottom = dragRect.top + kDragHeight; |
| 46 |
indicatorRect = filledRect; |
| 47 |
indicatorRect.top += 5; |
| 48 |
indicatorRect.bottom = indicatorRect.top + 3; |
| 49 |
indicatorRect.left += 5; |
| 50 |
indicatorRect.right -= 5; |
| 51 |
|
| 52 |
switch (message) |
| 53 |
{ |
| 54 |
case drawCntl: |
| 55 |
EraseRoundRect(&ctlRect, 15, 15); |
| 56 |
|
| 57 |
// unfilled slider background |
| 58 |
RGBBackColor(&gLightGrey); |
| 59 |
FillRoundRect(&ctlRect, 15, 15, &gSliderPattern); |
| 60 |
BackColor(whiteColor); |
| 61 |
FrameRoundRect(&ctlRect, 13, 13); |
| 62 |
|
| 63 |
// contrast rings (white then black) |
| 64 |
ForeColor(whiteColor); |
| 65 |
rect_expand(&ctlRect, 1); |
| 66 |
FrameRoundRect(&ctlRect, 15, 15); |
| 67 |
|
| 68 |
ForeColor(blackColor); |
| 69 |
rect_expand(&ctlRect, 1); |
| 70 |
FrameRoundRect(&ctlRect, 18, 18); |
| 71 |
|
| 72 |
// filled slider part |
| 73 |
PaintRoundRect(&filledRect, 15, 15); |
| 74 |
|
| 75 |
// drag element |
| 76 |
if ((**ctl).contrlHilite == 129) |
| 77 |
{ |
| 78 |
FillRoundRect(&dragRect, 15, 15, &qd.white); |
| 79 |
FillRoundRect(&indicatorRect, 8, 8, &qd.black); |
| 80 |
} |
| 81 |
else |
| 82 |
FillRoundRect(&indicatorRect, 8, 8, &qd.white); |
| 83 |
break; |
| 84 |
case testCntl: |
| 85 |
testPt.v = HiWord(param); |
| 86 |
testPt.h = LoWord(param); |
| 87 |
if (PtInRect(testPt, &dragRect)) |
| 88 |
retval = 129; |
| 89 |
else if (PtInRect(testPt, &ctlRect)) |
| 90 |
retval = 1; |
| 91 |
break; |
| 92 |
case thumbCntl: |
| 93 |
dragConstraint = (void*)param; |
| 94 |
topDist = dragConstraint->limitRect.top - dragRect.top; |
| 95 |
botDist = dragRect.bottom - dragConstraint->limitRect.top; |
| 96 |
dragConstraint->axis = 2; // vertical movement only |
| 97 |
dragConstraint->limitRect = ctlRect; |
| 98 |
dragConstraint->limitRect.top += topDist; |
| 99 |
dragConstraint->limitRect.bottom -= botDist - 1; |
| 100 |
dragConstraint->slopRect = ctlRect; |
| 101 |
rect_expand(&dragConstraint->slopRect, 40); |
| 102 |
break; |
| 103 |
case calcCntlRgn: |
| 104 |
cntlRgn = (RgnHandle)param; |
| 105 |
OpenRgn(); |
| 106 |
FrameRoundRect(&ctlRect,15, 15); |
| 107 |
CloseRgn(cntlRgn); |
| 108 |
break; |
| 109 |
case calcThumbRgn: |
| 110 |
cntlRgn = (RgnHandle)param; |
| 111 |
OpenRgn(); |
| 112 |
FrameRoundRect(&dragRect, 15, 15); |
| 113 |
CloseRgn(cntlRgn); |
| 114 |
break; |
| 115 |
case posCntl: |
| 116 |
offset = HiWord(param); |
| 117 |
newValue = (filledHeight - offset) * ctlMax / variableHeight; |
| 118 |
SetControlValue(ctl, newValue); |
| 119 |
param = 0; |
| 120 |
message = drawCntl; |
| 121 |
goto recalc; |
| 122 |
break; |
| 123 |
default: |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
return retval; |
| 128 |
} |