summaryrefslogtreecommitdiff
path: root/maccaml/lcontrols.c
blob: 2d0b3c2321b4dde285e728dcefc2e3b74a71bc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
    WASTE Demo Project:
    Macintosh Controls with Long Values

    Copyright © 1993-1996 Marco Piovanelli
    All Rights Reserved

    C port by John C. Daub
*/

/***************************************************************************
   This file is not subject to the O'Caml licence.
   It is a slightly modified version of "LongControls.c" from
   the WASTE Demo source (version 1.2).
 ***************************************************************************/
/* $Id$ */

#ifndef __CONTROLS__
#include <Controls.h>
#endif

#ifndef __FIXMATH__
#include <FixMath.h>
#endif

#ifndef __TOOLUTILS__
#include <ToolUtils.h>
#endif

#include "main.h"                              /* The change */
#define BSL(A, B)    (((long) (A)) << (B))      /* is here    */


// long control auxiliary record used for keeping long settings
// a handle to this record is stored in the reference field of the control record

struct LCAuxRec
{
    long    value;    // long value
    long    min;    // long min
    long    max;    // long max
};
typedef struct LCAuxRec LCAuxRec, *LCAuxPtr, **LCAuxHandle;


OSErr LCAttach( ControlRef control )
{
    Handle        aux;
    LCAuxPtr    pAux;

    /*    allocate the auxiliary record that will hold long settings */

    if ( ( aux = NewHandleClear( sizeof( LCAuxRec ) ) ) == nil )
    {
        return    MemError( );
    }

    /*    store a handle to the auxiliary record in the contrlRfCon field */

    SetControlReference( control, (long) aux );

    /*    copy current control settings into the auxiliary record */

    pAux = * (LCAuxHandle) aux;
    pAux->value = GetControlValue( control );
    pAux->min = GetControlMinimum( control );
    pAux->max = GetControlMaximum( control );

    return noErr;
}

void LCDetach( ControlRef control )
{
    Handle aux;

    if ( ( aux = (Handle) GetControlReference( control ) ) != nil )
    {
        SetControlReference( control, 0L );
        DisposeHandle( aux );
    }
}

void LCSetValue( ControlRef control, long value )
{
    LCAuxPtr pAux;
    short controlMin, controlMax, newControlValue;

    pAux = * (LCAuxHandle) GetControlReference( control );

    /*    make sure value is in the range min...max */

    if ( value < pAux->min )
    {
        value = pAux->min;
    }
    if ( value > pAux->max )
    {
        value = pAux->max;
    }

    /*    save value in auxiliary record */

    pAux->value = value;

    /*    calculate new thumb position */

    controlMin = GetControlMinimum( control );
    controlMax = GetControlMaximum( control );
    newControlValue = controlMin + FixRound( FixMul ( FixDiv( value - pAux->min,
                pAux->max - pAux->min), BSL(controlMax - controlMin, 16 )));

    /*    do nothing if the thumb position hasn't changed */

    if ( newControlValue != GetControlValue(control) )
    {
        SetControlValue( control, newControlValue );
    }
}

void LCSetMin( ControlRef control, long min )
{
    LCAuxPtr pAux;

    pAux = * (LCAuxHandle) GetControlReference( control );

    /*    make sure min is less than or equal to max */

    if ( min > pAux->max )
    {
        min = pAux->max;
    }

    /*    save min in auxiliary record */

    pAux->min = min;

    /*    set control minimum to min or SHRT_MIN, whichever is greater */

    SetControlMinimum( control, ( min >= SHRT_MIN ) ? min : SHRT_MIN );

    /*    reset value */

    LCSetValue( control, pAux->value );
}

void LCSetMax( ControlRef control, long max )
{
    LCAuxPtr pAux;

    pAux = * (LCAuxHandle) GetControlReference( control );

    /*    make sure max is greater than or equal to min */

    if ( max < pAux->min )
    {
        max = pAux->min;
    }

    /*    save max in auxiliary record */

    pAux->max = max;

    /*    set control maximum to max or SHRT_MAX, whichever is less */

    SetControlMaximum( control, ( max <= SHRT_MAX ) ? max : SHRT_MAX );

    /*    reset value */

    LCSetValue( control, pAux->value );
}

/*    In each of these LCGetXXX() functions, there are 2 ways listed to do things.  They are
    both the same thing and perform the same stuff, just one is easier to read than the
    other (IMHO).  I asked Marco about it and he gave me the shorter code (what's commented
    in each function) and gave me this explanation:

        This version [the commented code] yields smaller and faster code
        (try disassembling both versions if you wish), but some people may
        find it somewhat harder to read.

    I agree with Marco that his code is better overall, but in the interest of readabilty
    (since this demo is a learning tool), I left my code in and put Marco's in commented
    out.  Pick whichever you'd like to use.
*/

long LCGetValue( ControlRef control )
{
    LCAuxPtr    pAux;

    pAux = *((LCAuxHandle)GetControlReference( control ));

    return pAux->value;

//    this is Marco's code.  Remember, this is a little harder to read, but overall
//    yields tighter code.

//    return (* (LCAuxHandle) GetControlReference(control)) -> value;

}

long LCGetMin( ControlRef control )
{
    LCAuxPtr    pAux;

    pAux = *((LCAuxHandle)GetControlReference( control ));

    return pAux->min;

//    this is Marco's code.  Remember, this is a little harder to read, but overall
//    yields tighter code.

//    return (* (LCAuxHandle)GetControlReference(control)) -> min;

}

long LCGetMax( ControlRef control )
{
    LCAuxPtr    pAux;

    pAux = *((LCAuxHandle)GetControlReference( control ));

    return pAux->max;

//    this is Marco's code.  Remember, this is a little harder to read, but overall
//    yields tighter code.

//    return (* (LCAuxHandle)GetControlReference(control)) -> max;

}

void LCSynch( ControlRef control )
{
    LCAuxPtr pAux;
    short controlMin, controlMax, controlValue;

    controlMin = GetControlMinimum( control );
    controlMax = GetControlMaximum( control );
    controlValue = GetControlValue( control );
    pAux = * (LCAuxHandle) GetControlReference( control );

    /*    calculate new long value */

    pAux->value = pAux->min + FixMul( FixRatio ( controlValue - controlMin,
                  controlMax - controlMin), pAux->max - pAux->min );
}