summaryrefslogtreecommitdiff
path: root/app/wlib/mswlib/mswchoic.c
blob: 2ac391a4719298c5613b8bca6793db17535c95a2 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <windows.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <commdlg.h>
#include <math.h>
#include "i18n.h"
#include "mswint.h"

/*
 *****************************************************************************
 *
 * Choice Boxes
 *
 *****************************************************************************
 */

int CHOICE_HEIGHT=(17);
int CHOICE_MIN_WIDTH=25;

static XWNDPROC oldChoiceItemProc = NULL;
static XWNDPROC newChoiceItemProc;

typedef struct {
		WOBJ_COMMON
		wChoice_p owner;
		} wChoiceItem_t, * wChoiceItem_p;

struct wChoice_t {
		WOBJ_COMMON
		const char * * labels;
		wChoiceItem_p *buttList;
		long *valueP;
		long oldVal;
		wChoiceCallBack_p action;
		HWND hBorder;
		};

static FARPROC oldChoiceProc;

void wRadioSetValue(
		wChoice_p bc,
		long val )
{
	const char ** labels;
	long cnt;
	wChoiceItem_p * butts;

	butts = (wChoiceItem_p*)bc->buttList;
	for (labels = bc->labels, cnt=0; *labels; labels++, cnt++, butts++ )
		SendMessage( (*butts)->hWnd, BM_SETCHECK,
				(val==cnt)?1:0, 0L );
	bc->oldVal = val;
	if (bc->valueP)
		*bc->valueP = val;
}

long wRadioGetValue(
		wChoice_p bc )
{
	return bc->oldVal;
}



void wToggleSetValue(
		wChoice_p bc,
		long val )
{
	const char ** labels;
	long cnt;
	wChoiceItem_p * butts;

	butts = (wChoiceItem_p*)bc->buttList;
	for (labels = bc->labels, cnt=0; *labels; labels++, cnt++, butts++ )
		SendMessage( (*butts)->hWnd, BM_SETCHECK,
				(val & (1L<<cnt)) != 0, 0L );
	bc->oldVal = val;
	if (bc->valueP)
		*bc->valueP = val;
}


long wToggleGetValue(
		wChoice_p bc )
{
	return bc->oldVal;
}


static void choiceSetBusy(
		wControl_p b,
		BOOL_T busy)
{
	wChoiceItem_p * butts;
	wChoice_p bc = (wChoice_p)b;

	for (butts = (wChoiceItem_p*)bc->buttList; *butts; butts++ )
		EnableWindow( (*butts)->hWnd, !(BOOL)busy );
}

static void choiceShow(
		wControl_p b,
		BOOL_T show)
{
	wChoice_p bc = (wChoice_p)b;
	wChoiceItem_p * butts;

	if ((bc->option & BC_NOBORDER)==0)
		ShowWindow( bc->hBorder, show?SW_SHOW:SW_HIDE );

	for (butts = (wChoiceItem_p*)bc->buttList; *butts; butts++ )
		ShowWindow( (*butts)->hWnd, show?SW_SHOW:SW_HIDE );
}

static void choiceSetPos(
		wControl_p b,
		wPos_t x,
		wPos_t y )
{
	wChoice_p bc = (wChoice_p)b;
	wChoiceItem_p * butts;
	wPos_t dx, dy;

	dx = x - bc->x;
	dy = y - bc->y;
	if ((bc->option & BC_NOBORDER)==0)
		SetWindowPos( bc->hBorder, HWND_TOP, x, y, CW_USEDEFAULT, CW_USEDEFAULT,
				SWP_NOSIZE|SWP_NOZORDER );

	for (butts = (wChoiceItem_p*)bc->buttList; *butts; butts++ ) {
		SetWindowPos( (*butts)->hWnd, HWND_TOP,
						(*butts)->x+=dx, (*butts)->y+=dy,
						CW_USEDEFAULT, CW_USEDEFAULT,
						SWP_NOSIZE|SWP_NOZORDER );
	}
	bc->x = x;
	bc->y = y;
}

long FAR PASCAL _export pushChoiceItem(
		HWND hWnd,
		UINT message,
		UINT wParam,
		LONG lParam )
{
	/* Catch <Return> and cause focus to leave control */
#ifdef WIN32
	long inx = GetWindowLong( hWnd, GWL_ID );
#else
	short inx = GetWindowWord( hWnd, GWW_ID );
#endif

	wControl_p b = mswMapIndex( inx );

	switch (message) {
	case WM_CHAR:
		if ( b != NULL) {
			switch( wParam ) {
			case 0x0D:
			case 0x1B:
			case 0x09:
				SetFocus( ((wControl_p)(b->parent))->hWnd ); 
				SendMessage( ((wControl_p)(b->parent))->hWnd, WM_CHAR,
						wParam, lParam );
				/*SendMessage( ((wControl_p)(b->parent))->hWnd, WM_COMMAND,
						inx, MAKELONG( hWnd, EN_KILLFOCUS ) );*/
				return 0L;
			}
		}
		break;
	}
	return CallWindowProc( oldChoiceItemProc, hWnd, message, wParam, lParam );
}

LRESULT choiceItemProc(
		wControl_p b,
		HWND hWnd,
		UINT message,
		WPARAM wParam,
		LPARAM lParam )
{			   
	wChoiceItem_p me = (wChoiceItem_p)b, *rest;
	wChoice_p bc;
	int num;

	switch( message ) {
	
	case WM_COMMAND:
		switch (WCMD_PARAM_NOTF) {
		case BN_CLICKED:
			bc = me->owner;
			num = -1;
			for (rest = (wChoiceItem_p*)bc->buttList; *rest; rest++ ) {
				switch (bc->type) {
				case B_TOGGLE:
					num = rest-(wChoiceItem_p*)bc->buttList;
					if (*rest == me) {
						bc->oldVal ^= (1L<<num);
					}
					SendMessage( (*rest)->hWnd, BM_SETCHECK,
						(bc->oldVal & (1L<<num)) != 0, 0L );
					break;
	
				case B_RADIO:
					if (*rest != me) {
						SendMessage( (*rest)->hWnd, BM_SETCHECK, 0, 0L );
					} else {
						bc->oldVal = rest-(wChoiceItem_p*)bc->buttList;
						SendMessage( (*rest)->hWnd, BM_SETCHECK, 1, 0L );
					}
					break;
				}
			}
			if (bc->valueP)
				*bc->valueP = bc->oldVal;
			if (bc->action)
				bc->action( bc->oldVal, bc->data );
			break;

		}
		break;
	}													  
	
	return DefWindowProc( hWnd, message, wParam, lParam );
}					


static callBacks_t choiceCallBacks = {
		mswRepaintLabel,
		NULL,
		NULL,
		choiceSetBusy,
		choiceShow,
		choiceSetPos };

static callBacks_t choiceItemCallBacks = {
		NULL,
		NULL,
		choiceItemProc };

/**
 * Creates choice buttons. This function is used to create a group of 
 * radio buttons and checkboxes.
 *
 * \param type IN type of button
 * \param parent IN parent window
 * \param x, y IN position of group
 * \param helpStr IN index string to find help
 * \param labelStr IN label for group
 * \param option IN ?
 * \param labels IN labels for individual choices
 * \param valueP OUT pointer for return value
 * \param action IN ?
 * \param data IN ?
 * \return    created choice button group
 */

static wChoice_p choiceCreate(
		wType_e type,
		wWin_p	parent,
		POS_T	x,
		POS_T	y,
		const char	* helpStr,
		const char	* labelStr,
		long	option,
		const char	**labels,
		long	*valueP,
		wChoiceCallBack_p action,
		void	*data )
{
	wChoice_p b;
	const char ** lp;
	int cnt;
	wChoiceItem_p * butts;
	int ppx, ppy;
	int bs;
	HDC hDc;
	HWND hButt;
	int lab_l;
	DWORD dw;
	int w, maxW;
	int pw, ph;
	int index;
	char * helpStrCopy;
	HFONT hFont;

	b = mswAlloc( parent, type, mswStrdup(labelStr), sizeof *b, data, &index );
	mswComputePos( (wControl_p)b, x, y );
	b->option = option;
	b->valueP = valueP;
	b->action = action;
	b->labels = labels;
	b->labelY += 6;
		
	ppx = b->x;
	ppy = b->y;

	switch (b->type) {
	case B_TOGGLE:
			bs = BS_CHECKBOX;
			break;
	case B_RADIO:
			bs = BS_RADIOBUTTON;
			break;
	}
	for (lp = b->labels,cnt=0; *lp; lp++,cnt++ );
	butts = (wChoiceItem_p*)malloc( (cnt+1) * sizeof *butts );
	b->buttList = butts;
	b->oldVal = (b->valueP?*b->valueP:0);
	ph = pw = 2;
	maxW = 0;
	if (helpStr)
		helpStrCopy = mswStrdup( helpStr );
	for (lp = b->labels, cnt=0; *lp; lp++, cnt++, butts++ ) {			
			*butts = (wChoiceItem_p)mswAlloc( parent, B_CHOICEITEM,
				mswStrdup(_((char *)*lp)), sizeof( wChoiceItem_t ), data, &index );
			(*butts)->owner = b;
			(*butts)->hWnd = hButt = CreateWindow( "BUTTON", (*butts)->labelStr,
						bs | WS_CHILD | WS_VISIBLE | mswGetBaseStyle(parent), b->x+pw, b->y+ph,
						80, CHOICE_HEIGHT,
						((wControl_p)parent)->hWnd, (HMENU)index, mswHInst, NULL );
			if ( hButt == (HWND)0 ) {
				mswFail( "choiceCreate button" );
				return b;
			}
			(*butts)->x = b->x+pw;
			(*butts)->y = b->y+ph;
			if (b->hWnd == 0)
				b->hWnd = (*butts)->hWnd;
			(*butts)->helpStr = helpStrCopy;

			hDc = GetDC( hButt );
			lab_l = strlen((*butts)->labelStr);
			
			if (!mswThickFont) {hFont = SelectObject( hDc, mswLabelFont );}
			dw = GetTextExtent( hDc, (char *)((*butts)->labelStr), lab_l );
			if (!mswThickFont) {SelectObject( hDc, hFont );}
		
			w = LOWORD(dw) + CHOICE_MIN_WIDTH; 

			if (w > maxW)
				maxW = w;
			SetBkMode( hDc, TRANSPARENT );
			ReleaseDC( hButt, hDc );
			if (b->option & BC_HORZ) {
				pw += w;
			} else {
				ph += CHOICE_HEIGHT;
			}
			if (!SetWindowPos( hButt, HWND_TOP, 0, 0,
				w, CHOICE_HEIGHT, SWP_NOMOVE|SWP_NOZORDER)) {
				mswFail("Create CHOICE: SetWindowPos");
			}
			mswChainFocus( (wControl_p)*butts );
			newChoiceItemProc = MakeProcInstance( (XWNDPROC)pushChoiceItem, mswHInst );
			oldChoiceItemProc = (XWNDPROC)GetWindowLong( (*butts)->hWnd, GWL_WNDPROC );
			SetWindowLong( (*butts)->hWnd, GWL_WNDPROC, (LONG)newChoiceItemProc );
			if ( !mswThickFont )
				SendMessage( (*butts)->hWnd, WM_SETFONT, (WPARAM)mswLabelFont, 0L ); 
	}
	*butts = NULL;
	switch (b->type) {
	case B_TOGGLE:
		wToggleSetValue( b, (b->valueP?*b->valueP:0L) );
		break;
	case B_RADIO:
		wRadioSetValue( b, (b->valueP?*b->valueP:0L) );
		break;
	}
	if (b->option & BC_HORZ) {
		ph = CHOICE_HEIGHT;
	} else {
		pw = maxW;
	}
	pw += 4; ph += 4;
	b->w = pw;
	b->h = ph;								  

#define FRAME_STYLE		SS_ETCHEDFRAME

	if ((b->option & BC_NOBORDER)==0) {
		b->hBorder = CreateWindow( "STATIC", NULL, WS_CHILD | WS_VISIBLE | FRAME_STYLE,
			b->x, b->y, pw, ph, ((wControl_p)parent)->hWnd, 0, mswHInst, NULL );
	}
	mswAddButton( (wControl_p)b, TRUE, helpStr );
	mswCallBacks[ B_CHOICEITEM ] = &choiceItemCallBacks;
	mswCallBacks[ type ] = &choiceCallBacks;
	return b;
}


wChoice_p wRadioCreate(
		wWin_p	parent,
		POS_T	x,
		POS_T	y,
		const char	* helpStr,
		const char	* labelStr,
		long	option,
		const char	**labels,
		long	*valueP,
		wChoiceCallBack_p action,
		void	*data )
{
	return choiceCreate( B_RADIO, parent, x, y, helpStr, labelStr,
		option, labels, valueP, action, data );
}

wChoice_p wToggleCreate(
		wWin_p	parent,
		POS_T	x,
		POS_T	y,
		const char	* helpStr,
		const char	* labelStr,
		long	option,
		const char	**labels,
		long	*valueP,
		wChoiceCallBack_p action,
		void	*data )
{
	return choiceCreate( B_TOGGLE, parent, x, y, helpStr, labelStr,
		option, labels, valueP, action, data );
}