File: bltBusy.c

package info (click to toggle)
blt 2.4m-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,316 kB
  • ctags: 7,778
  • sloc: ansic: 68,187; tcl: 12,491; sh: 1,918; makefile: 709; csh: 25
file content (1192 lines) | stat: -rw-r--r-- 35,029 bytes parent folder | download
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
/*
 * bltBusy.c --
 *
 *	This module implements busy windows for the BLT toolkit.
 *
 * Copyright 1993-1998 Lucent Technologies, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and warranty
 * disclaimer appear in supporting documentation, and that the names
 * of Lucent Technologies any of their entities not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 *
 * Lucent Technologies disclaims all warranties with regard to this
 * software, including all implied warranties of merchantability and
 * fitness.  In no event shall Lucent Technologies be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether in
 * an action of contract, negligence or other tortuous action, arising
 * out of or in connection with the use or performance of this
 * software.
 *
 *	The "busy" command was created by George Howlett.
 */

#include "bltInt.h"

#ifndef NO_BUSY
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#define BUSYDEBUG 0

#ifndef TK_REPARENTED
#define TK_REPARENTED 0
#endif

#define BUSY_THREAD_KEY	"BLT Busy Data"

typedef struct {
    Display *display;		/* Display of busy window */
    Tcl_Interp *interp;		/* Interpreter where "busy" command was
				 * created. It's used to key the
				 * searches in the window hierarchy. See the
				 * "windows" command. */

    Tk_Window busyTkwin;	/* Busy window: Transparent window used
				 * to block delivery of events to windows
				 * underneath it. */

    Tk_Window parent;		/* Parent window of the busy
				 * window. It may be the reference
				 * window (if the reference is a
				 * toplevel) or a mutual ancestor of
				 * the reference window */

    Tk_Window client;		/* Reference window of the busy window.
				 * It is used to manage the size and
				 * position of the busy window. */


    int x, y;			/* Position of the reference window */

    int width, height;		/* Size of the reference window. Retained to
				 * know if the reference window has been
				 * reconfigured to a new size. */

    int isBusy;			/* Indicates whether the transparent
				 * window should be displayed. This
				 * can be different from what
				 * Tk_IsMapped says because the a
				 * sibling reference window may be
				 * unmapped, forcing the busy window
				 * to be also hidden. */

    int menuBar;		/* Menu bar flag. */
    Tk_Cursor cursor;		/* Cursor for the busy window. */

    Tcl_HashEntry *hashPtr;	/* Used the delete the busy window entry 
				 * out of the global hash table. */
} Busy;

#ifdef WIN32
#define DEF_BUSY_CURSOR "wait"
#else
#define DEF_BUSY_CURSOR "watch"
#endif

static Tk_ConfigSpec configSpecs[] =
{
    {TK_CONFIG_CURSOR, "-cursor", "busyCursor", "BusyCursor",
	DEF_BUSY_CURSOR, Tk_Offset(Busy, cursor), TK_CONFIG_NULL_OK},
    {TK_CONFIG_END, NULL, NULL, NULL, NULL, 0, 0}
};

typedef struct ThreadData {
    Tcl_HashTable busyTable;	/* Hash table of busy window
				 * structures keyed by the address of
				 * the reference Tk window */
} ThreadData;

static void BusyGeometryProc _ANSI_ARGS_((ClientData clientData,
	Tk_Window tkwin));
static void BusyCustodyProc _ANSI_ARGS_((ClientData clientData, Tk_Window tkwin));

static Tk_GeomMgr busyMgrInfo =
{
    "busy",			/* Name of geometry manager used by winfo */
    BusyGeometryProc,		/* Procedure to for new geometry requests */
    BusyCustodyProc,		/* Procedure when window is taken away */
};

/* Forward declarations */
static void DestroyBusy _ANSI_ARGS_((DestroyData dataPtr));
static void BusyEventProc _ANSI_ARGS_((ClientData clientData,
	XEvent *eventPtr));

#ifdef __STDC__
static Tk_EventProc BusyEventProc;
static Tk_EventProc RefWinEventProc;
static Tcl_CmdProc BusyCmd;
static Tcl_InterpDeleteProc BusyInterpDeleteProc;
#endif

static void
ShowBusyWindow(busyPtr) 
    Busy *busyPtr;
{
    if (busyPtr->busyTkwin != NULL) {
	Tk_MapWindow(busyPtr->busyTkwin);
	/* 
	 * Always restack the busy window, just in case new sibling
	 * windows have been created. Can't use Tk_RestackWindow 
	 * because it doesn't work under Win32.
	 */
	XRaiseWindow(Tk_Display(busyPtr->busyTkwin), 
	     Tk_WindowId(busyPtr->busyTkwin));
    }
#ifdef WIN32
    {
	POINT point;
	/*
	 * Under Win32, cursors aren't associated with windows.  Tk
	 * fakes this by watching Motion events on its windows.  So Tk
	 * will automatically change the cursor when the pointer
	 * enters the Busy window.  But Windows doesn't immediately
	 * change the cursor; it waits for the cursor position to
	 * change or a system call.  We need to change the cursor
 	 * before the application starts processing, so set the cursor
	 * position redundantly back to the current position.
	 */
	GetCursorPos(&point);
	SetCursorPos(point.x, point.y);
    }
#endif /* WIN32 */
}

static void
HideBusyWindow(busyPtr)
    Busy *busyPtr;
{
    if (busyPtr->busyTkwin != NULL) {
	Tk_UnmapWindow(busyPtr->busyTkwin); 
    }
#ifdef WIN32
    {
	POINT point;
	/*
	 * Under Win32, cursors aren't associated with windows.  Tk
	 * fakes this by watching Motion events on its windows.  So Tk
	 * will automatically change the cursor when the pointer
	 * enters the Busy window.  But Windows doesn't immediately
	 * change the cursor; it waits for the cursor position to
	 * change or a system call.  We need to change the cursor
	 * before the application starts processing, so set the cursor
	 * position redundantly back to the current position.
	 */
	GetCursorPos(&point);
	SetCursorPos(point.x, point.y);
    }
#endif /* WIN32 */
}

/*
 *----------------------------------------------------------------------
 *
 * BusyEventProc --
 *
 *	This procedure is invoked by the Tk dispatcher for events on
 *	the busy window itself.  We're only concerned with destroy
 *	events.
 *
 *	It might be necessary (someday) to watch resize events.  Right
 *	now, I don't think there's any point in it.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	When a busy window is destroyed, all internal structures
 * 	associated with it released at the next idle point.
 *
 *----------------------------------------------------------------------
 */
static void
BusyEventProc(clientData, eventPtr)
    ClientData clientData;	/* Busy window record */
    XEvent *eventPtr;		/* Event which triggered call to routine */
{
    Busy *busyPtr = (Busy *)clientData;

    if (eventPtr->type == DestroyNotify) {
	busyPtr->busyTkwin = NULL;
	Tcl_EventuallyFree((ClientData)busyPtr, DestroyBusy);
    }
}


/*
 * ----------------------------------------------------------------------------
 *
 * BusyCustodyProc --
 *
 *	This procedure is invoked when the busy window has been stolen
 *	by another geometry manager.  The information and memory
 *	associated with the busy window is released. I don't know why
 *	anyone would try to pack a busy window, but this should keep
 *	everything sane, if it is.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The Busy structure is freed at the next idle point.
 *
 * ----------------------------------------------------------------------------
 */
/* ARGSUSED */
static void
BusyCustodyProc(clientData, tkwin)
    ClientData clientData;	/* Information about the busy window. */
    Tk_Window tkwin;		/* Not used. */
{
    Busy *busyPtr = (Busy *)clientData;

    Tk_DeleteEventHandler(busyPtr->busyTkwin, StructureNotifyMask, 
	BusyEventProc, (ClientData)busyPtr);
    HideBusyWindow(busyPtr);
    busyPtr->busyTkwin = NULL;
    Tcl_EventuallyFree((ClientData)busyPtr, DestroyBusy);
}

/*
 * ----------------------------------------------------------------------------
 *
 * BusyGeometryProc --
 *
 *	This procedure is invoked by Tk_GeometryRequest for busy
 *	windows.  Busy windows never request geometry, so it's
 *	unlikely that this routine will ever be called.  The routine
 *	exists simply as a place holder for the GeomProc in the
 *	Geometry Manager structure.
 *
 * Results:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */
/* ARGSUSED */
static void
BusyGeometryProc(clientData, tkwin)
    ClientData clientData;	/* Information about window that got new
				 * preferred geometry.  */
    Tk_Window tkwin;		/* Other Tk-related information about the
			         * window. */
{
    /* Should never get here */
}

/*
 * ------------------------------------------------------------------
 *
 * RefWinEventProc --
 *
 *	This procedure is invoked by the Tk dispatcher for the
 *	following events on the reference window.  If the reference and
 *	parent windows are the same, only the first event is
 *	important.
 *
 *	   1) ConfigureNotify  - The reference window has been resized or
 *				 moved.  Move and resize the busy window
 *				 to be the same size and position of the
 *				 reference window.
 *
 *	   2) DestroyNotify    - The reference window was destroyed. Destroy
 *				 the busy window and the free resources
 *				 used.
 *
 *	   3) MapNotify	       - The reference window was (re)shown. Map the
 *				 busy window again.
 *
 *	   4) UnmapNotify      - The reference window was hidden. Unmap the
 *				 busy window.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	When the reference window gets deleted, internal structures get
 *	cleaned up.  When it gets resized, the busy window is resized
 *	accordingly. If it's displayed, the busy window is displayed. And
 *	when it's hidden, the busy window is unmapped.
 *
 * -------------------------------------------------------------------
 */
static void
RefWinEventProc(clientData, eventPtr)
    ClientData clientData;	/* Busy window record */
    register XEvent *eventPtr;	/* Event which triggered call to routine */
{
    register Busy *busyPtr = (Busy *)clientData;

    switch (eventPtr->type) {
    case ReparentNotify:
    case DestroyNotify:

	/*
	 * Arrange for the busy structure to be removed at a proper time.
	 */

	Tcl_EventuallyFree((ClientData)busyPtr, DestroyBusy);
	break;

    case ConfigureNotify:
	if ((busyPtr->width != Tk_Width(busyPtr->client)) ||
	    (busyPtr->height != Tk_Height(busyPtr->client)) ||
	    (busyPtr->x != Tk_X(busyPtr->client)) ||
	    (busyPtr->y != Tk_Y(busyPtr->client))) {
	    int x, y;

	    busyPtr->width = Tk_Width(busyPtr->client);
	    busyPtr->height = Tk_Height(busyPtr->client);
	    busyPtr->x = Tk_X(busyPtr->client);
	    busyPtr->y = Tk_Y(busyPtr->client);

	    x = y = 0;
	    if (busyPtr->parent != busyPtr->client) {
		Tk_Window tkwin;

		for (tkwin = busyPtr->client; (tkwin != NULL) &&
			 (!Tk_IsTopLevel(tkwin)); tkwin = Tk_Parent(tkwin)) {
		    x += Tk_X(tkwin) + Tk_Changes(tkwin)->border_width;
		    y += Tk_Y(tkwin) + Tk_Changes(tkwin)->border_width;
		    if (tkwin == busyPtr->parent) {
			break;
		    }
		}
	    }
#if BUSYDEBUG
	    PurifyPrintf("menubar2: width=%d, height=%d\n", 
		busyPtr->width, busyPtr->height);
#endif
	    if (busyPtr->busyTkwin != NULL) {
		Tk_MoveResizeWindow(busyPtr->busyTkwin, x, y, busyPtr->width,
		    busyPtr->height);
		if (busyPtr->isBusy) {
		    ShowBusyWindow(busyPtr);
		}
	    }
	}
	break;

    case MapNotify:
	if ((busyPtr->parent != busyPtr->client) && (busyPtr->isBusy)) {
	    ShowBusyWindow(busyPtr);
	}
	break;

    case UnmapNotify:
	if (busyPtr->parent != busyPtr->client) {
	    HideBusyWindow(busyPtr);
	}
	break;
    }
}

/*
 * ------------------------------------------------------------------
 *
 * ConfigureBusy --
 *
 *	This procedure is called from the Tk event dispatcher. It
 * 	releases X resources and memory used by the busy window and
 *	updates the internal hash table.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory and resources are released and the Tk event handler
 *	is removed.
 *
 * -------------------------------------------------------------------
 */
static int
ConfigureBusy(interp, busyPtr, argc, argv)
    Tcl_Interp *interp;
    Busy *busyPtr;
    int argc;
    char **argv;
{
    Tk_Cursor oldCursor;

    oldCursor = busyPtr->cursor;
    if (Tk_ConfigureWidget(interp, busyPtr->client, configSpecs, argc, argv,
	    (char *)busyPtr, 0) != TCL_OK) {
	return TCL_ERROR;
    }
    if (busyPtr->cursor != oldCursor) {
	if (busyPtr->cursor == None) {
	    Tk_UndefineCursor(busyPtr->busyTkwin);
	} else {
	    Tk_DefineCursor(busyPtr->busyTkwin, busyPtr->cursor);
	}
    }
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * CreateBusy --
 *
 *	Creates a child transparent window that obscures its parent
 *	window thereby effectively blocking device events.  The size
 *	and position of the busy window is exactly that of the reference
 *	window.
 *
 *	We want to create sibling to the window to be blocked.  If the
 *	busy window is a child of the window to be blocked, Enter/Leave
 *	events can sneak through.  Futhermore under WIN32, messages of
 *	transparent windows are sent directly to the parent.  The only
 *	exception to this are toplevels, since we can't make a sibling.
 *	Fortunately, toplevel windows rarely receive events that need
 *	blocking.
 *
 * Results:
 *	Returns a pointer to the new busy window structure.
 *
 * Side effects:
 *	When the busy window is eventually displayed, it will screen
 *	device events (in the area of the reference window) from reaching
 *	its parent window and its children.  User feed back can be
 *	achieved by changing the cursor.
 *
 * -------------------------------------------------------------------
 */
static Busy *
CreateBusy(interp, client)
    Tcl_Interp *interp;		/* Interpreter to report error to */
    Tk_Window client;		/* Window hosting the busy window */
{
    Busy *busyPtr;
    int length;
    char *fmt, *name;
    Tk_Window busyTkwin;
    Window parentWindow;
    Tk_Window child, parent;
    Tk_FakeWin *winPtr;
    int x, y;

    busyPtr = (Busy *)calloc(1, sizeof(Busy));
    assert(busyPtr);
    x = y = 0;
    length = strlen(Tk_Name(client));
    name = (char *)malloc(length + 6);
    if (Tk_IsTopLevel(client)) {
	fmt = "_Busy";		/* Child */
	parent = client;
    } else {
	Tk_Window tkwin;

	fmt = "%s_Busy";	/* Sibling */
	parent = Tk_Parent(client);
	for (tkwin = client; (tkwin != NULL) && (!Tk_IsTopLevel(tkwin)); 
	     tkwin = Tk_Parent(tkwin)) {
	    x += Tk_X(tkwin) + Tk_Changes(tkwin)->border_width;
	    y += Tk_Y(tkwin) + Tk_Changes(tkwin)->border_width;
	    if (tkwin == parent) {
		break;
	    }
	}
    }
    for (child = Blt_FirstChild(parent); child != NULL; 
	 child = Blt_NextChild(child)) {
	Tk_MakeWindowExist(child);
    }
    sprintf(name, fmt, Tk_Name(client));
    busyTkwin = Tk_CreateWindow(interp, parent, name, (char *)NULL);
    free((char *)name);

    if (busyTkwin == NULL) {
	return NULL;
    }
    Tk_MakeWindowExist(client);
    busyPtr->display = Tk_Display(client);
    busyPtr->client = client;
    busyPtr->parent = parent;
    busyPtr->interp = interp;
    busyPtr->width = Tk_Width(client);
    busyPtr->height = Tk_Height(client);
    busyPtr->x = Tk_X(client);
    busyPtr->y = Tk_Y(client);
    busyPtr->cursor = None;
    busyPtr->busyTkwin = busyTkwin;
    busyPtr->isBusy = FALSE;
    Tk_SetClass(busyTkwin, "Busy");
#if (TK_MAJOR_VERSION > 4)
    Blt_SetWindowInstanceData(busyTkwin, (ClientData)busyPtr);
#endif
    winPtr = (Tk_FakeWin *) client;
    if (winPtr->flags & TK_REPARENTED) {
	/*
	 * This works around a bug in the implementation of menubars
	 * for non-MacIntosh window systems (Win32 and X11).  Tk
	 * doesn't reset the pointers to the parent window when the
	 * menu is reparented (winPtr->parentPtr points to the
	 * wrong window). We get around this by determining the parent
	 * via the native API calls.
	 */
#ifdef WIN32
	{
	    HWND hWnd;
	    RECT region;

	    hWnd = GetParent(Tk_GetHWND(Tk_WindowId(client)));
	    parentWindow = (Window) hWnd;
	    if (GetWindowRect(hWnd, &region)) {
		busyPtr->width = region.right - region.left;
		busyPtr->height = region.bottom - region.top;
#if BUSYDEBUG
		PurifyPrintf("menubar: width=%d, height=%d\n",
		    busyPtr->width, busyPtr->height);
#endif
	    }
	}
#else
	parentWindow = Blt_GetParent(Tk_Display(client), Tk_WindowId(client));
#endif
    } else {
	parentWindow = Tk_WindowId(parent);
#ifdef WIN32
	parentWindow = (Window) Tk_GetHWND(parentWindow);
#endif
    }
    Blt_MakeTransparentWindowExist(busyTkwin, parentWindow, TRUE);

#if BUSYDEBUG
    PurifyPrintf("menubar1: width=%d, height=%d\n", busyPtr->width, 
	busyPtr->height);
#endif
    Tk_MoveResizeWindow(busyTkwin, x, y, busyPtr->width, busyPtr->height);

    /* Only worry if the busy window is destroyed.  */
    Tk_CreateEventHandler(busyTkwin, StructureNotifyMask, BusyEventProc,
	(ClientData)busyPtr);
    /*
     * Indicate that the busy window's geometry is being managed.
     * This will also notify us if the busy window is ever packed.
     */
    Tk_ManageGeometry(busyTkwin, &busyMgrInfo, (ClientData)busyPtr);
    if (busyPtr->cursor != None) {
	Tk_DefineCursor(busyTkwin, busyPtr->cursor);
    }
    /* Track the reference window to see if it is resized or destroyed.  */
    Tk_CreateEventHandler(client, StructureNotifyMask, RefWinEventProc,
	(ClientData)busyPtr);
    return busyPtr;
}

/*
 * ------------------------------------------------------------------
 *
 * DestroyBusy --
 *
 *	This procedure is called from the Tk event dispatcher. It
 *	releases X resources and memory used by the busy window and
 *	updates the internal hash table.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory and resources are released and the Tk event handler
 *	is removed.
 *
 * -------------------------------------------------------------------
 */
static void
DestroyBusy(dataPtr)
    DestroyData dataPtr;	/* Busy window structure record */
{
    Busy *busyPtr = (Busy *)dataPtr;

    Tk_FreeOptions(configSpecs, (char *)busyPtr, busyPtr->display, 0);
    if (busyPtr->hashPtr != NULL) {
	Tcl_DeleteHashEntry(busyPtr->hashPtr);
    }
    Tk_DeleteEventHandler(busyPtr->client, StructureNotifyMask,
	RefWinEventProc, (ClientData)busyPtr);
    if (busyPtr->busyTkwin != NULL) {
	Tk_DeleteEventHandler(busyPtr->busyTkwin, StructureNotifyMask,
	    BusyEventProc, (ClientData)busyPtr);
	Tk_ManageGeometry(busyPtr->busyTkwin, (Tk_GeomMgr *) NULL,
	    (ClientData)busyPtr);
	Tk_DestroyWindow(busyPtr->busyTkwin);
    }
    free((char *)busyPtr);
}

/*
 * ------------------------------------------------------------------
 *
 * GetBusy --
 *
 *	Returns the busy window structure associated with the reference
 *	window, keyed by its path name.  The clientData argument is
 *	the main window of the interpreter, used to search for the
 *	reference window in its own window hierarchy.
 *
 * Results:
 *	If path name represents a reference window with a busy window, a
 *	pointer to the busy window structure is returned. Otherwise,
 *	NULL is returned and an error message is left in
 *	interp->result.
 *
 * -------------------------------------------------------------------
 */
static int
GetBusy(dataPtr, interp, pathName, busyPtrPtr)
    ThreadData *dataPtr;		/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    char *pathName;		/* Path name of parent window */
    Busy **busyPtrPtr;		/* Will contain address of busy window if
				 * found. */
{
    Tcl_HashEntry *hPtr;
    Tk_Window tkwin;

    tkwin = Tk_NameToWindow(interp, pathName, Tk_MainWindow(interp));
    if (tkwin == NULL) {
	return TCL_ERROR;
    }
    hPtr = Tcl_FindHashEntry(&(dataPtr->busyTable), (char *)tkwin);
    if (hPtr == NULL) {
	Tcl_AppendResult(interp, "can't find busy window \"", pathName, "\"",
	    (char *)NULL);
	return TCL_ERROR;
    }
    *busyPtrPtr = ((Busy *)Tcl_GetHashValue(hPtr));
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * HoldBusy --
 *
 *	Creates (if necessary) and maps a busy window, thereby
 *	preventing device events from being be received by the parent
 *	window and its children.
 *
 * Results:
 *	Returns a standard TCL result. If path name represents a busy
 *	window, it is unmapped and TCL_OK is returned. Otherwise,
 *	TCL_ERROR is returned and an error message is left in
 *	interp->result.
 *
 * Side effects:
 *	The busy window is created and displayed, blocking events from
 *	the parent window and its children.
 *
 * -------------------------------------------------------------------
 */
static int
HoldBusy(dataPtr, interp, argc, argv)
    ThreadData *dataPtr;		/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;		/* Window name and option pairs */
{
    Tk_Window tkwin;
    Tcl_HashEntry *hPtr;
    Busy *busyPtr;
    int isNew;
    int result;

    tkwin = Tk_NameToWindow(interp, argv[0], Tk_MainWindow(interp));
    if (tkwin == NULL) {
	return TCL_ERROR;
    }
    hPtr = Tcl_CreateHashEntry(&(dataPtr->busyTable), (char *)tkwin, &isNew);
    if (isNew) {
	busyPtr = (Busy *)CreateBusy(interp, tkwin);
	if (busyPtr == NULL) {
	    return TCL_ERROR;
	}
	Tcl_SetHashValue(hPtr, (char *)busyPtr);
	busyPtr->hashPtr = hPtr;
    } else {
	busyPtr = (Busy *)Tcl_GetHashValue(hPtr);
    }
    result = ConfigureBusy(interp, busyPtr, argc - 1, argv + 1);
    /* Don't map the busy window unless the reference window is also
     * displayed */
    if (Tk_IsMapped(busyPtr->client)) {
	ShowBusyWindow(busyPtr);
    } else {
	HideBusyWindow(busyPtr);
    }
    busyPtr->isBusy = TRUE;
    return result;
}

/*
 * ------------------------------------------------------------------
 *
 * StatusOp --
 *
 *	Returns the status of the busy window; whether it's blocking
 *	events or not.
 *
 * Results:
 *	Returns a standard TCL result. If path name represents a busy
 *	window, the status is returned via interp->result and TCL_OK
 *	is returned. Otherwise, TCL_ERROR is returned and an error
 *	message is left in interp->result.
 *
 * -------------------------------------------------------------------
 */
/*ARGSUSED*/
static int
StatusOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report error to */
    int argc;			/* Not used. */
    char **argv;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Busy *busyPtr;

    if (GetBusy(dataPtr, interp, argv[2], &busyPtr) != TCL_OK) {
	return TCL_ERROR;
    }
    Tcl_Preserve((ClientData)busyPtr);
    Tcl_SetResult(interp, busyPtr->isBusy ? "1" : "0", TCL_STATIC);
    Tcl_Release((ClientData)busyPtr);
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * ForgetOp --
 *
 *	Destroys the busy window associated with the reference window and
 *	arranges for internal resources to the released when they're
 *	not being used anymore.
 *
 * Results:
 *	Returns a standard TCL result. If path name represents a busy
 *	window, it is destroyed and TCL_OK is returned. Otherwise,
 *	TCL_ERROR is returned and an error message is left in
 *	interp->result.
 *
 * Side effects:
 *	The busy window is removed.  Other related memory and resources
 *	are eventually released by the Tk dispatcher.
 *
 * -------------------------------------------------------------------
 */
static int
ForgetOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Busy *busyPtr;
    register int i;

    for (i = 2; i < argc; i++) {
	if (GetBusy(dataPtr, interp, argv[i], &busyPtr) != TCL_OK) {
	    return TCL_ERROR;
	}
	/* Unmap the window even though it will be soon destroyed */
	HideBusyWindow(busyPtr);
	Tcl_EventuallyFree((ClientData)busyPtr, DestroyBusy);
    }
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * ReleaseOp --
 *
 *	Unmaps the busy window, thereby permitting device events
 *	to be received by the parent window and its children.
 *
 * Results:
 *	Returns a standard TCL result. If path name represents a busy
 *	window, it is unmapped and TCL_OK is returned. Otherwise,
 *	TCL_ERROR is returned and an error message is left in
 *	interp->result.
 *
 * Side effects:
 *	The busy window is hidden, allowing the parent window and
 *	its children to receive events again.
 *
 * -------------------------------------------------------------------
 */
static int
ReleaseOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Busy *busyPtr;
    int i;

    for (i = 2; i < argc; i++) {
	if (GetBusy(dataPtr, interp, argv[i], &busyPtr) != TCL_OK) {
	    return TCL_ERROR;
	}
	HideBusyWindow(busyPtr);
	busyPtr->isBusy = FALSE;
    }
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * WindowsOp --
 *
 *	Reports the names of all widgets with busy windows attached to
 *	them, matching a given pattern.  If no pattern is given, all
 *	busy widgets are listed.
 *
 * Results:
 *	Returns a TCL list of the names of the widget with busy windows
 *	attached to them, regardless if the widget is currently busy
 *	or not.
 *
 * -------------------------------------------------------------------
 */
static int
WindowsOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch cursor;
    Busy *busyPtr;

    for (hPtr = Tcl_FirstHashEntry(&(dataPtr->busyTable), &cursor);
	hPtr != NULL; hPtr = Tcl_NextHashEntry(&cursor)) {
	busyPtr = (Busy *)Tcl_GetHashValue(hPtr);
	if (busyPtr->interp == interp) {
	    if ((argc != 3) ||
		(Tcl_StringMatch(Tk_PathName(busyPtr->client), argv[2]))) {
		Tcl_AppendElement(interp, Tk_PathName(busyPtr->client));
	    }
	}
    }
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * IsBusyOp --
 *
 *	Reports the names of all widgets with busy windows attached to
 *	them, matching a given pattern.  If no pattern is given, all
 *	busy widgets are listed.
 *
 * Results:
 *	Returns a TCL list of the names of the widget with busy windows
 *	attached to them, regardless if the widget is currently busy
 *	or not.
 *
 * -------------------------------------------------------------------
 */
static int
IsBusyOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch cursor;
    Busy *busyPtr;

    for (hPtr = Tcl_FirstHashEntry(&(dataPtr->busyTable), &cursor);
	hPtr != NULL; hPtr = Tcl_NextHashEntry(&cursor)) {
	busyPtr = (Busy *)Tcl_GetHashValue(hPtr);
	if (busyPtr->interp == interp) {
	    if ((busyPtr->isBusy) && ((argc != 3) ||
		    (Tcl_StringMatch(Tk_PathName(busyPtr->client), argv[2])))) {
		Tcl_AppendElement(interp, Tk_PathName(busyPtr->client));
	    }
	}
    }
    return TCL_OK;
}

/*
 * ------------------------------------------------------------------
 *
 * HoldOp --
 *
 *	Creates (if necessary) and maps a busy window, thereby
 *	preventing device events from being be received by the parent
 *      window and its children. The argument vector may contain
 *	option-value pairs of configuration options to be set.
 *
 * Results:
 *	Returns a standard TCL result.
 *
 * Side effects:
 *	The busy window is created and displayed, blocking events from the
 *	parent window and its children.
 *
 * -------------------------------------------------------------------
 */
static int
HoldOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;		/* Window name and option pairs */
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    register int i, count;

    if ((argv[1][0] == 'h') && (strcmp(argv[1], "hold") == 0)) {
	argc--, argv++;		/* Command used "hold" keyword */
    }
    for (i = 1; i < argc; i++) {
	/*
	 * Find the end of the option-value pairs for this window.
	 */
	for (count = i + 1; count < argc; count += 2) {
	    if (argv[count][0] != '-') {
		break;
	    }
	}
	if (count > argc) {
	    count = argc;
	}
	if (HoldBusy(dataPtr, interp, count - i, argv + i) != TCL_OK) {
	    return TCL_ERROR;
	}
	i = count;
    }
    return TCL_OK;
}

/* ARGSUSED*/
static int
CgetOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;		/* Widget pathname and option switch */
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Busy *busyPtr;
    int result;

    if (GetBusy(dataPtr, interp, argv[2], &busyPtr) != TCL_OK) {
	return TCL_ERROR;
    }
    Tcl_Preserve((ClientData)busyPtr);
    result = Tk_ConfigureValue(interp, busyPtr->client, configSpecs,
	(char *)busyPtr, argv[3], 0);
    Tcl_Release((ClientData)busyPtr);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * ConfigureOp --
 *
 *	This procedure is called to process an argv/argc list in order
 *	to configure (or reconfigure) a busy window.
 *
 * Results:
 *	The return value is a standard Tcl result.  If TCL_ERROR is
 *	returned, then interp->result contains an error message.
 *
 * Side effects:
 *	Configuration information get set for busyPtr; old resources
 *	get freed, if there were any.  The busy window destroyed and
 *	recreated in a new parent window.
 *
 *----------------------------------------------------------------------
 */
static int
ConfigureOp(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter to report errors to */
    int argc;
    char **argv;		/* Reference window path name and options */
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Busy *busyPtr;
    int result;

    if (GetBusy(dataPtr, interp, argv[2], &busyPtr) != TCL_OK) {
	return TCL_ERROR;
    }
    Tcl_Preserve((ClientData)busyPtr);
    if (argc == 3) {
	result = Tk_ConfigureInfo(interp, busyPtr->client, configSpecs,
	    (char *)busyPtr, (char *)NULL, 0);
    } else if (argc == 4) {
	result = Tk_ConfigureInfo(interp, busyPtr->client, configSpecs,
	    (char *)busyPtr, argv[3], 0);
    } else {
	result = ConfigureBusy(interp, busyPtr, argc - 3, argv + 3);
    }
    Tcl_Release((ClientData)busyPtr);
    return result;
}

/*
 * -----------------------------------------------------------------------
 *
 * BusyInterpDeleteProc --
 *
 *	This is called when the interpreter hosting the "busy" command 
 *	is destroyed.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Destroys all the hash table managing the busy windows.
 *
 * ------------------------------------------------------------------------
 */
/* ARGSUSED */
static void
BusyInterpDeleteProc(clientData, interp)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;
{
    ThreadData *dataPtr = (ThreadData *)clientData;
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch cursor;
    Busy *busyPtr;

    for (hPtr = Tcl_FirstHashEntry(&(dataPtr->busyTable), &cursor);
	 hPtr != NULL; hPtr = Tcl_NextHashEntry(&cursor)) {
	busyPtr = (Busy *)Tcl_GetHashValue(hPtr);
	busyPtr->hashPtr = NULL;
	DestroyBusy((DestroyData)busyPtr);
    }
    Tcl_DeleteHashTable(&(dataPtr->busyTable));
    Tcl_DeleteAssocData(interp, BUSY_THREAD_KEY);
    free((char *)dataPtr);
}

/*
 *--------------------------------------------------------------
 *
 * Busy Sub-command specification:
 *
 *	- Name of the sub-command.
 *	- Minimum number of characters needed to unambiguously
 *        recognize the sub-command.
 *	- Pointer to the function to be called for the sub-command.
 *	- Minimum number of arguments accepted.
 *	- Maximum number of arguments accepted.
 *	- String to be displayed for usage (arguments only).
 *
 *--------------------------------------------------------------
 */
static Blt_OpSpec busyOps[] =
{
    {"cget", 2, (Blt_Operation)CgetOp, 4, 4, "window option",},
    {"configure", 2, (Blt_Operation)ConfigureOp, 3, 0,
	"window ?options?...",},
    {"forget", 1, (Blt_Operation)ForgetOp, 2, 0, "?window?...",},
    {"hold", 3, (Blt_Operation)HoldOp, 3, 0,
	"window ?options?... ?window options?...",},
    {"isbusy", 1, (Blt_Operation)IsBusyOp, 2, 3, "?pattern?",},
    {"release", 1, (Blt_Operation)ReleaseOp, 2, 0, "?window?...",},
    {"status", 1, (Blt_Operation)StatusOp, 3, 3, "window",},
    {"windows", 1, (Blt_Operation)WindowsOp, 2, 3, "?pattern?",},
};
static int nBusyOps = sizeof(busyOps) / sizeof(Blt_OpSpec);

/*
 *----------------------------------------------------------------------
 *
 * BusyCmd --
 *
 *	This procedure is invoked to process the "busy" Tcl command.
 *	See the user documentation for details on what it does.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */
static int
BusyCmd(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Interpreter associated with command */
    int argc;
    char **argv;
{
    Blt_Operation proc;
    int result;
    
    if ((argc > 1) && (argv[1][0] == '.')) {
	return HoldOp(clientData, interp, argc, argv);
    }
    proc = Blt_GetOperation(interp, nBusyOps, busyOps, BLT_OPER_ARG1,
	argc, argv);
    if (proc == NULL) {
	return TCL_ERROR;
    }
    result = (*proc) (clientData, interp, argc, argv);
    return result;
}


static ThreadData *
GetBusyCmdData(interp)
     Tcl_Interp *interp;
{
    ThreadData *dataPtr;
    Tcl_InterpDeleteProc *proc;

    dataPtr = (ThreadData *)Tcl_GetAssocData(interp, BUSY_THREAD_KEY, &proc);
    if (dataPtr == NULL) {
	dataPtr = (ThreadData *)malloc(sizeof(ThreadData));
	assert(dataPtr);
	Tcl_SetAssocData(interp, BUSY_THREAD_KEY, BusyInterpDeleteProc, 
		(ClientData)dataPtr);
	Tcl_InitHashTable(&(dataPtr->busyTable), TCL_ONE_WORD_KEYS);
    }
    return dataPtr;
}


int
Blt_BusyInit(interp)
    Tcl_Interp *interp;
{
    static Blt_CmdSpec cmdSpec = {"busy", BusyCmd, };
    ThreadData *dataPtr;

    dataPtr = GetBusyCmdData(interp);
    cmdSpec.clientData = (ClientData)dataPtr;
    if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
	return TCL_ERROR;
    }
    return TCL_OK;
}

#endif /* NO_BUSY */