summaryrefslogtreecommitdiffstats
path: root/src/windows/lib/vardlg.c
blob: 91a6bf4b550a6a3f72368c84652d4861b61fc4a8 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (C) 1997 Cygnus Solutions.
 *
 * Author:  Michael Graff
 */
/*
 * Dialog box building for various numbers of (label, entry) fields.
 *
 * This code is somewhat hardcoded to build boxes for the krb5_get_init_creds()
 * function.
 */

#include <windows.h>
#include <windowsx.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "krb5.h"
#include "vardlg.h"

/*
 * a hack, I know...  No error checking below, either.
 */
static unsigned char dlg[DLG_BUF];

/*
 * Add a WORD (16-bit int) to the buffer.  Return the number of characters
 * added.
 */
static int
ADD_WORD(unsigned char *p, WORD w)
{
	*((WORD *)p) = w;

	return 2;
}

static int
ADD_DWORD(unsigned char *p, DWORD dw)
{
	*((DWORD *)p) = dw;

	return 4;
}

static size_t
ADD_UNICODE_STRING(unsigned char *p, const char *s)
{
	WORD *w;
	size_t i;
	size_t len;

	w = (WORD *)p;

	len = strlen(s) + 1; /* copy the null, too */

	for (i = 0 ; i < len ; i++)
		*w++ = *s++;

	return (len * 2);
}

#define DWORD_ALIGN(p) { while ((DWORD)p % 4) *p++ = 0x00; }

static size_t
ADD_DLGTEMPLATE(unsigned char *dlg, short x, short y, short cx, short cy,
		const char *caption, const char *fontname, WORD fontsize,
		WORD n)
{
	unsigned char    *p;
	DLGTEMPLATE       dlt;

	p = dlg;

	dlt.style = (DS_MODALFRAME | WS_POPUP);
	if (caption != NULL)
		dlt.style |= WS_CAPTION;
	if (fontname != NULL)
		dlt.style |= DS_SETFONT;
	dlt.dwExtendedStyle = 0;
	dlt.cdit = n;
	dlt.x = x;
	dlt.y = y;
	dlt.cx = cx;
	dlt.cy = cy;
	memcpy(p, &dlt, sizeof(dlt));
	p += sizeof(dlt);

	p += ADD_WORD(p, 0x0000);  /* menu == none */

	p += ADD_WORD(p, 0x0000);  /* class == default? */

	if (caption != NULL)
		p += ADD_UNICODE_STRING(p, caption);
	else
		p += ADD_WORD(p, 0x0000);

	if (fontname != NULL) {
		p += ADD_WORD(p, fontsize);
		p += ADD_UNICODE_STRING(p, fontname);
	}

	DWORD_ALIGN(p);

	return (p - dlg);
}

static size_t
ADD_DLGITEM(unsigned char *dlg, short x, short y, short cx, short cy,
	    const char *label, WORD id, WORD type, DWORD style)
{
	unsigned char    *p;
	DLGITEMTEMPLATE   dit;

	p = dlg;

	dit.style = style;
	dit.dwExtendedStyle = 0;
	dit.x = x;
	dit.y = y;
	dit.cx = cx;
	dit.cy = cy;
	dit.id = id;
	memcpy(p, &dit, sizeof(dit));
	p += sizeof(dit);

	p += ADD_WORD(p, 0xffff);
	p += ADD_WORD(p, type);

	p += ADD_UNICODE_STRING(p, label);

	/*
	 * creation data? For now, just make this empty, like the resource
	 * compiler does.
	 */
	p += ADD_WORD(p, 0x0000);

	DWORD_ALIGN(p);

	return (p - dlg);
}

#define ADD_DLGITEM_defpushbutton(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0080, 0x50010001);

#define ADD_DLGITEM_pushbutton(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0080, 0x50010000);

#define ADD_DLGITEM_left_static(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0082, 0x50020000);

#define ADD_DLGITEM_centered_static(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0082, 0x50020001);

#define ADD_DLGITEM_right_static(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0082, 0x50020002);

#define ADD_DLGITEM_entry(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0081, 0x50810080);

#define ADD_DLGITEM_hidden_entry(a, b, c, d, e, f, g) \
	ADD_DLGITEM((a), (b), (c), (d), (e), (f), (g), 0x0081, 0x508100a0);


/*
 * "build" the dialog box.  In this bit of code, we create the dialog box,
 * create the OK button, and a static label for the banner text.
 *
 * If there are items, we also create a Cancel button and one (label, entry)
 * fields for each item.
 */
void *
vardlg_build(WORD cx, const char *name, const char *banner,
	     WORD n, krb5_prompt prompts[], WORD id)
{
	unsigned char *p;
	WORD i;

	p = dlg;  /* global */

	if (cx < MIN_WIDTH)
		cx = MIN_WIDTH;
	if (cx > MAX_WIDTH)
		cx = MAX_WIDTH;

	/*
	 * Store the dialog template
	 */
	p += ADD_DLGTEMPLATE(p, 0, 0, cx, 0, name ?
			     strlen(name) < 30 ? name : "Kerberos V5" :
			     "Kerberos V5",
			     "MS Sans Serif", 8,
			     (WORD)(n * 2 + 3));

	/*
	 * Create a label for the banner.  This will be ID (id).
	 */
	p += ADD_DLGITEM_left_static(p, 0, 0, 0, 0, "", id++);

	/*
	 * Each label field is ID (id + 1) + (item * 2), and each entry field
	 * is (id + 2) + (item * 2)
	 */
	for (i = 0 ; i < n ; i++) {
		p += ADD_DLGITEM_right_static(p, 0, 0, 0, 0, "", id++);
		if (prompts[i].hidden) {
			p += ADD_DLGITEM_hidden_entry(p, 0, 0, 0, 0, "", id++);
		} else {
			p += ADD_DLGITEM_entry(p, 0, 0, 0, 0, "", id++);
		}
	}

	/*
	 * Create the OK and Cancel buttons.
	 */
	p += ADD_DLGITEM_defpushbutton(p, 0, 0, 0, 0,
				       "OK", IDOK);
	if (n != 0)
		p += ADD_DLGITEM_pushbutton(p, 0, 0, 0, 0,
					    "Cancel", IDCANCEL);

	return dlg;
}

#define SPACE_Y     4  /* logical units */
#define SPACE_X     4  /* logical units */
#define ENTRY_PX  120  /* pixels */
#define BUTTON_PX  70  /* pixels */
#define BUTTON_PY  30  /* pixels */

void
vardlg_config(HWND hwnd, WORD width, const char *banner, WORD num_prompts,
	      krb5_prompt *prompts, WORD id)
{
	int         n;
	WORD        cid;
	HDC         hdc;
	SIZE        csize;
	SIZE        maxsize;
	LONG        cx, cy;
	LONG        ccx, ccy;
	LONG        space_x, space_y;
	LONG        max_x, max_y;
	LONG        banner_y;
	RECT        rect;
	int         done;
	const char *p;

	/*
	 * First, set the banner's text.
	 */
	Static_SetText(GetDlgItem(hwnd, id), banner);

	/*
	 * Next, run through the items and set their static text.
	 * Also, set the corresponding edit string and set the
	 * maximum input length.
	 */
	cid = (id + 1);

	for (n = 0 ; n < num_prompts ; n++) {
		Static_SetText(GetDlgItem(hwnd, cid), prompts[n].prompt);
		cid++;
		Edit_SetText(GetDlgItem(hwnd, cid), "");
		Edit_LimitText(GetDlgItem(hwnd, cid), prompts[n].reply->length);
		cid++;
	}

	/*
	 * Now run through the entry fields and find the longest string.
	 */
	maxsize.cx = maxsize.cy = 0;
	cid = (id + 1);
	hdc = GetDC(GetDlgItem(hwnd, cid)); /* assume one label is the same as all the others */

	for (n = 0 ; n < num_prompts ; n++) {
		GetTextExtentPoint32(hdc, prompts[n].prompt, (int)strlen(prompts[n].prompt), &csize);
		if (csize.cx > maxsize.cx)
			maxsize.cx = csize.cx;
		if (csize.cy > maxsize.cy)
			maxsize.cy = csize.cy;
	}

#if 0
	/*
	 * convert the maximum values into pixels.  Ugh.
	 */
	rect.left = 0;
	rect.top = 0;
	rect.right = maxsize.cx;
	rect.bottom = maxsize.cy;
	MapDialogRect(hwnd, &rect);

	max_x = rect.right;
	max_y = rect.bottom;
#else
	max_x = maxsize.cx;
	max_y = (long)(((double)maxsize.cy) * 1.5);
#endif

	/*
	 * convert the spacing values, too.  Ugh.  Ugh.
	 */
	rect.left = 0;
	rect.top = 0;
	rect.right = SPACE_X;
	rect.bottom = SPACE_Y;
	MapDialogRect(hwnd, &rect);

	space_x = rect.right;
	space_y = rect.bottom;

	/*
	 * Now we know the maximum length of the string for the entry labels.  Guestimate
	 * that the entry fields should be ENTRY_PX pixels long and resize the dialog
	 * window to fit the longest string plus the entry fields (plus a little for the
	 * spacing between the edges of the windows and the static and edit fields, and
	 * between the static and edit fields themselves.)
	 */
	cx = max_x + ENTRY_PX + (space_x * 3);
	cy = (max_y + space_y) * num_prompts;

	/*
	 * resize the dialog box itself (take 1)
	 */
	SetWindowPos(hwnd, HWND_TOPMOST,
		0, 0,
		cx + 10, cy + 30,
		SWP_NOMOVE);

	/*
	 * position the dialog items.  First, the banner. (take 1)
	 */
	SetWindowPos(GetDlgItem(hwnd, id), HWND_BOTTOM,
		space_x, space_y,
		(cx - space_x * 2), max_y,
		0);

	/*
	 * Now that the window for the banner is in place, convert the width into logical units
	 * and find out how many lines we need to reserve room for.
	 */
	done = 0;
	p = banner;
	banner_y = 0;

	do {
		int nFit;
		int pDx[128];

		hdc = GetDC(GetDlgItem(hwnd, id));

		GetTextExtentExPoint(hdc, p, (int)strlen(p), cx, &nFit,
			pDx, &csize);

		banner_y += csize.cy;

		p += nFit;

	} while (*p != 0);

	banner_y += space_y;

	/*
	 * position the banner (take 2)
	 */
	SetWindowPos(GetDlgItem(hwnd, id), HWND_BOTTOM,
		space_x, space_y,
		(cx - space_x * 2), banner_y,
		0);

	/*
	 * Don't forget to include the banner estimate and the buttons, too.  Once again,
	 * assume the buttons are BUTTON_PY pixels high.  The extra three space_y's are
	 * for between the top of the dialog and the banner, between the banner and the
	 * first label, and between the buttons and the bottom of the screen.
	 */
	cy += banner_y + BUTTON_PY + (space_y * 3);

	/*
	 * resize the dialog box itself (Again...  ugh!)
	 */
	SetWindowPos(hwnd, HWND_TOPMOST,
		0, 0,
		cx + 10, cy + 30,
		SWP_NOMOVE);

	cid = (id + 1);
	ccy = banner_y + (space_y * 2);
	ccx = max_x + (space_x * 2);  /* where the edit fields start */

	for (n = 0 ; n < num_prompts ; n++) {
		SetWindowPos(GetDlgItem(hwnd, cid), HWND_BOTTOM,
			space_x, ccy,
			max_x, max_y, 0);
		cid++;
		SetWindowPos(GetDlgItem(hwnd, cid), HWND_BOTTOM,
			ccx, ccy,
			ENTRY_PX, max_y - 3, 0);
		cid++;
		ccy += (max_y + space_y);
	}

	/*
	 * Now the buttons.  If there are any entries we will have both an OK and a
	 * Cancel button.  If we don't have any entries, we will have only an OK.
	 */
	if (num_prompts == 0) {
		SetWindowPos(GetDlgItem(hwnd, IDOK), HWND_BOTTOM,
			(cx / 2), cy - space_y - BUTTON_PY,
			BUTTON_PX, BUTTON_PY, 0);
	} else {
		SetWindowPos(GetDlgItem(hwnd, IDOK), HWND_BOTTOM,
			space_x, cy - space_y - BUTTON_PY,
			BUTTON_PX, BUTTON_PY, 0);
		SetWindowPos(GetDlgItem(hwnd, IDCANCEL), HWND_BOTTOM,
			cx - space_x - BUTTON_PX, cy - space_y - BUTTON_PY,
			BUTTON_PX, BUTTON_PY, 0);
	}

	return;
}

/*
 * To use these functions, first create the dialog box and entries.
 * You will always get an OK button.  If there are at least one item,
 * you will also get a cancel button.  The OK button is IDOK, and the cancel
 * button is IDCANCEL, as usual.
 *
 * After calling bld_dlg, the banner will have ID "id", and the labels
 * will be "1 + id + i * 2" (i is the entry number, starting with zero) and
 * the entries will be "2 + id + i * 2".
 *
 *	unsigned char *dlg = vardlg_build(minwidth, banner, num_prompts,
 *					  krb5_prompt[], id);
 *
 * Then, "run" the dialog using:
 *
 *	rc = DialogBoxIndirect(hinstance, (LPDLGTEMPLATE)dlg,
 *			       HWND_DESKTOP, myDialogProc);
 *
 * Note that the vardlg_build function uses a static data area and so cannot
 * be used more than once before the DialogBoxIndirect() procedure is called.
 * I assume windows won't need that area after that call is complete.
 *
 * In the dialog's _initialization_ procedure, call
 *
 *	vardlg_config(hwnd, banner, num_prompts, krb5_prompt[], id);
 *
 * This function will resize the various elements of the dialog and fill in the
 * labels.
 */