summaryrefslogtreecommitdiffstats
path: root/carbon/tkMacOSXMouseEvent.c
blob: 4ef0c829ee5422f020482f31659df5d183761086 (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
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
/*
 * tkMacOSXMouseEvent.c --
 *
 *	This file implements functions that decode & handle mouse events on
 *	MacOS X.
 *
 * Copyright 2001, Apple Computer, Inc.
 * Copyright (c) 2005-2007 Daniel A. Steffen <das@users.sourceforge.net>
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *	The following terms apply to all files originating from Apple
 *	Computer, Inc. ("Apple") and associated with the software unless
 *	explicitly disclaimed in individual files.
 *
 *	Apple hereby grants permission to use, copy, modify, distribute, and
 *	license this software and its documentation for any purpose, provided
 *	that existing copyright notices are retained in all copies and that
 *	this notice is included verbatim in any distributions. No written
 *	agreement, license, or royalty fee is required for any of the
 *	authorized uses. Modifications to this software may be copyrighted by
 *	their authors and need not follow the licensing terms described here,
 *	provided that the new terms are clearly indicated on the first page of
 *	each file where they apply.
 *
 *	IN NO EVENT SHALL APPLE, THE AUTHORS OR DISTRIBUTORS OF THE SOFTWARE
 *	BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
 *	CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS
 *	DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF APPLE OR THE
 *	AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. APPLE,
 *	THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 *	NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND
 *	APPLE,THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
 *	MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 *	GOVERNMENT USE: If you are acquiring this software on behalf of the
 *	U.S. government, the Government shall have only "Restricted Rights" in
 *	the software and related documentation as defined in the Federal
 *	Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are
 *	acquiring the software on behalf of the Department of Defense, the
 *	software shall be classified as "Commercial Computer Software" and the
 *	Government shall have only "Restricted Rights" as defined in Clause
 *	252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
 *	authors grant the U.S. Government and others acting in its behalf
 *	permission to use and distribute the software in accordance with the
 *	terms specified in this license.
 *
 * RCS: @(#) $Id: tkMacOSXMouseEvent.c,v 1.1 2009/06/26 01:42:47 das Exp $
 */

#include "tkMacOSXPrivate.h"
#include "tkMacOSXWm.h"
#include "tkMacOSXEvent.h"
#include "tkMacOSXDebug.h"

typedef struct {
    WindowRef whichWin;
    WindowRef activeNonFloating;
    WindowPartCode windowPart;
    unsigned int state;
    long delta;
    Window window;
    Point global;
    Point local;
} MouseEventData;

/*
 * Declarations of static variables used in this file.
 */

static int gEatButtonUp = 0;	/* 1 if we need to eat the next up event. */

/*
 * Declarations of functions used only in this file.
 */

static void		BringWindowForward(WindowRef wRef, int isFrontProcess,
			    int frontWindowOnly);
static int		GeneratePollingEvents(MouseEventData *medPtr);
static int		GenerateMouseWheelEvent(MouseEventData *medPtr);
static int		GenerateButtonEvent(MouseEventData *medPtr);
static int		GenerateToolbarButtonEvent(MouseEventData *medPtr);
static int		HandleWindowTitlebarMouseDown(MouseEventData *medPtr,
			    Tk_Window tkwin);
static unsigned int	ButtonModifiers2State(UInt32 buttonState,
			    UInt32 keyModifiers);
static Tk_Window	GetGrabWindowForWindow(Tk_Window tkwin);
static int		TkMacOSXGetEatButtonUp(void);
static void		TkMacOSXSetEatButtonUp(int f);

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXProcessMouseEvent --
 *
 *	This routine processes the event in eventPtr, and generates the
 *	appropriate Tk events from it.
 *
 * Results:
 *	True if event(s) are generated - false otherwise.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue.
 *
 *----------------------------------------------------------------------
 */

MODULE_SCOPE int
TkMacOSXProcessMouseEvent(
    TkMacOSXEvent *eventPtr,
    MacEventStatus *statusPtr)
{
    Tk_Window tkwin;
    Point where, where2;
    int result;
    TkDisplay *dispPtr;
    OSStatus err;
    MouseEventData mouseEventData, *medPtr = &mouseEventData;
    int isFrontProcess;

    switch (eventPtr->eKind) {
    case kEventMouseDown:
    case kEventMouseUp:
    case kEventMouseMoved:
    case kEventMouseDragged:
    case kEventMouseWheelMoved:
	break;
    default:
	return false;
    }

    err = ChkErr(GetEventParameter, eventPtr->eventRef,
	    kEventParamMouseLocation, typeQDPoint, NULL, sizeof(where), NULL,
	    &where);
    if (err != noErr) {
	GetGlobalMouse(&where);
    }

    err = ChkErr(GetEventParameter, eventPtr->eventRef, kEventParamWindowRef,
	    typeWindowRef, NULL, sizeof(WindowRef), NULL, &medPtr->whichWin);
    if (err == noErr) {
	err = ChkErr(GetEventParameter, eventPtr->eventRef,
		kEventParamWindowPartCode, typeWindowPartCode, NULL,
		sizeof(WindowPartCode), NULL, &medPtr->windowPart);
    }
    if (err != noErr) {
	medPtr->windowPart = FindWindow(where, &medPtr->whichWin);
    }
    medPtr->window = TkMacOSXGetXWindow(medPtr->whichWin);
    if (medPtr->whichWin != NULL && medPtr->window == None) {
	return false;
    }
    if (eventPtr->eKind == kEventMouseDown) {
	if (IsWindowActive(medPtr->whichWin) && IsWindowPathSelectEvent(
		medPtr->whichWin, eventPtr->eventRef)) {
	    ChkErr(WindowPathSelect, medPtr->whichWin, NULL, NULL);
	    return false;
	}
	if (medPtr->windowPart == inProxyIcon) {
	    TkMacOSXTrackingLoop(1);
	    err = ChkErr(TrackWindowProxyDrag, medPtr->whichWin, where);
	    TkMacOSXTrackingLoop(0);
	    if (err == errUserWantsToDragWindow) {
		medPtr->windowPart = inDrag;
	    } else {
		return false;
	    }
	}
    }
    isFrontProcess = Tk_MacOSXIsAppInFront();
    if (isFrontProcess) {
	medPtr->state = ButtonModifiers2State(GetCurrentEventButtonState(),
		GetCurrentEventKeyModifiers());
    } else {
	medPtr->state = ButtonModifiers2State(GetCurrentButtonState(),
		GetCurrentKeyModifiers());
    }
    medPtr->global = where;
    err = ChkErr(GetEventParameter, eventPtr->eventRef,
	    kEventParamWindowMouseLocation, typeQDPoint, NULL,
	    sizeof(Point), NULL, &medPtr->local);
    if (err == noErr) {
	if (medPtr->whichWin) {
	    Rect widths;

	    GetWindowStructureWidths(medPtr->whichWin, &widths);
	    medPtr->local.h -=	widths.left;
	    medPtr->local.v -=	widths.top;
	}
    } else {
	medPtr->local = where;
	if (medPtr->whichWin) {
	    QDGlobalToLocalPoint(GetWindowPort(medPtr->whichWin),
		    &medPtr->local);
	}
    }
    medPtr->activeNonFloating = ActiveNonFloatingWindow();
    dispPtr = TkGetDisplayList();
    tkwin = Tk_IdToWindow(dispPtr->display, medPtr->window);

    if (eventPtr->eKind != kEventMouseDown) {
	int res = false;

	switch (eventPtr->eKind) {
	case kEventMouseUp:
	    /*
	     * The window manager only needs to know about mouse down events
	     * and sometimes we need to "eat" the mouse up. Otherwise, we just
	     * pass the event to Tk.
	     */

	    if (TkMacOSXGetEatButtonUp()) {
		TkMacOSXSetEatButtonUp(false);
	    } else {
		res = GenerateButtonEvent(medPtr);
	    }
	    break;
	case kEventMouseWheelMoved:
	    err = ChkErr(GetEventParameter, eventPtr->eventRef,
		    kEventParamMouseWheelDelta, typeLongInteger, NULL,
		    sizeof(long), NULL, &medPtr->delta);
	    if (err != noErr ) {
		statusPtr->err = 1;
	    } else {
		EventMouseWheelAxis axis;

		err = ChkErr(GetEventParameter, eventPtr->eventRef,
			kEventParamMouseWheelAxis, typeMouseWheelAxis, NULL,
			sizeof(EventMouseWheelAxis), NULL, &axis);
		if (err == noErr && axis == kEventMouseWheelAxisX) {
		    medPtr->state |= ShiftMask;
		}
		res = GenerateMouseWheelEvent(medPtr);
	    }
	    break;
	case kEventMouseMoved:
	case kEventMouseDragged:
	    res = GeneratePollingEvents(medPtr);
	    break;
	default:
	    Tcl_Panic("Unknown mouse event !");
	}
	if (res) {
	    statusPtr->stopProcessing = 1;
	}
	return res;
    }

    TkMacOSXSetEatButtonUp(false);
    if (!medPtr->whichWin) {
	return false;
    }

    /*
     * We got a mouse down in a window, so see if this is the activate click.
     * This click moves the window forward. We don't want the corresponding
     * mouse-up to be reported to the application or else it will mess up some
     * Tk scripts.
     */

    if (!(TkpIsWindowFloating(medPtr->whichWin))
	    && (medPtr->whichWin != medPtr->activeNonFloating
	    || !isFrontProcess)) {
	int frontWindowOnly = 1;
	int cmdDragGrow = ((medPtr->windowPart == inDrag ||
		medPtr->windowPart == inGrow) && medPtr->state & Mod1Mask);

	if (!cmdDragGrow) {
	    Tk_Window grabWin = GetGrabWindowForWindow(tkwin);

	    frontWindowOnly = !grabWin;
	    if (grabWin && grabWin != tkwin) {
		TkMacOSXSetEatButtonUp(true);
		BringWindowForward(TkMacOSXDrawableWindow(
			((TkWindow *) grabWin)->window), isFrontProcess,
			frontWindowOnly);
		return false;
	    }
	}

	/*
	 * Clicks in the titlebar widgets are handled without bringing the
	 * window forward.
	 */

	result = HandleWindowTitlebarMouseDown(medPtr, tkwin);
	if (result != -1) {
	    statusPtr->stopProcessing = 1;
	    return result;
	}

	/*
	 * Only windows with the kWindowNoActivatesAttribute can receive mouse
	 * events in the background.
	 */

	if (!(((TkWindow *)tkwin)->wmInfoPtr->attributes &
		kWindowNoActivatesAttribute)) {
	    /*
	     * Allow background window dragging & growing with Command down.
	     */

	    if (!cmdDragGrow) {
		TkMacOSXSetEatButtonUp(true);
		BringWindowForward(medPtr->whichWin, isFrontProcess,
			frontWindowOnly);
	    }

	    /*
	     * Allow dragging & growing of windows that were/are in the
	     * background.
	     */

	    if (!(medPtr->windowPart == inDrag ||
		    medPtr->windowPart == inGrow)) {
		return false;
	    }
	}
    } else {
	result = HandleWindowTitlebarMouseDown(medPtr, tkwin);
	if (result != -1) {
	    statusPtr->stopProcessing = 1;
	    return result;
	}
    }

    switch (medPtr->windowPart) {
    case inDrag: {
	WindowAttributes attributes;

	GetWindowAttributes(medPtr->whichWin, &attributes);
	if (!(attributes & kWindowAsyncDragAttribute)) {
	    TkMacOSXTrackingLoop(1);
	    DragWindow(medPtr->whichWin, where, NULL);
	    TkMacOSXTrackingLoop(0);
	    where2.h = where2.v = 0;
	    QDLocalToGlobalPoint(GetWindowPort(medPtr->whichWin), &where2);
	    return (EqualPt(where, where2)) ? false : true;
	}
	break;
    }
    case inGrow:
	/*
	 * Generally the content region is the domain of Tk sub-windows.
	 * However, one exception is the grow region. A button down in this
	 * area will be handled by the window manager. Note: this means that
	 * Tk may not get button down events in this area!
	 */

	if (TkMacOSXGrowToplevel(medPtr->whichWin, where) == true) {
	    statusPtr->stopProcessing = 1;
	    return true;
	}
    case inContent:
	return GenerateButtonEvent(medPtr);
    }
    return false;
}

/*
 *----------------------------------------------------------------------
 *
 * HandleWindowTitlebarMouseDown --
 *
 *	Handle clicks in window titlebar.
 *
 * Results:
 *	1 if event was handled, 0 if event was not handled, -1 if MouseDown
 *	was not in window titlebar.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue.
 *
 *----------------------------------------------------------------------
 */

int
HandleWindowTitlebarMouseDown(
    MouseEventData *medPtr,
    Tk_Window tkwin)
{
    int result = INT_MAX;

    switch (medPtr->windowPart) {
    case inGoAway:
    case inCollapseBox:
    case inZoomIn:
    case inZoomOut:
    case inToolbarButton:
	if (!IsWindowActive(medPtr->whichWin)) {
	    WindowRef frontWindow = FrontNonFloatingWindow();
	    WindowModality frontWindowModality = kWindowModalityNone;

	    if (frontWindow && frontWindow != medPtr->whichWin) {
		ChkErr(GetWindowModality, frontWindow,
			&frontWindowModality, NULL);
	    }
	    if (frontWindowModality == kWindowModalityAppModal) {
		result = 0;
	    }
	}
	break;
    default:
	result = -1;
	break;
    }
    
    if (result == INT_MAX) {
	result = 0;
	TkMacOSXTrackingLoop(1);
	switch (medPtr->windowPart) {
	case inGoAway:
	    if (TrackGoAway(medPtr->whichWin, medPtr->global) && tkwin) {
		TkGenWMDestroyEvent(tkwin);
		result = 1;
	    }
	    break;
	case inCollapseBox:
	    if (TrackBox(medPtr->whichWin, medPtr->global,
		    medPtr->windowPart) && tkwin) {
		TkpWmSetState((TkWindow *) tkwin, IconicState);
		result = 1;
	    }
	    break;
	case inZoomIn:
	case inZoomOut:
	    if (TrackBox(medPtr->whichWin, medPtr->global,
		    medPtr->windowPart)) {
		result = TkMacOSXZoomToplevel(medPtr->whichWin,
			medPtr->windowPart);
	    }
	    break;
	case inToolbarButton:
	    if (TrackBox(medPtr->whichWin, medPtr->global,
		    medPtr->windowPart)) {
		result = GenerateToolbarButtonEvent(medPtr);
	    }
	    break;
	}
	TkMacOSXTrackingLoop(0);
    }

    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * GeneratePollingEvents --
 *
 *	This function polls the mouse position and generates X Motion, Enter &
 *	Leave events. The cursor is also updated at this time.
 *
 * Results:
 *	True if event(s) are generated - false otherwise.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue. The cursor may
 *	be changed.
 *
 *----------------------------------------------------------------------
 */

static int
GeneratePollingEvents(
    MouseEventData *medPtr)
{
    Tk_Window tkwin, rootwin, grabWin;
    int local_x, local_y;
    TkDisplay *dispPtr;

    grabWin = TkMacOSXGetCapture();

    if ((!TkpIsWindowFloating(medPtr->whichWin)
	    && (medPtr->activeNonFloating != medPtr->whichWin))) {
	/*
	 * If the window for this event is not floating, and is not the active
	 * non-floating window, don't generate polling events. We don't send
	 * events to backgrounded windows. So either send it to the grabWin,
	 * or NULL if there is no grabWin.
	 */

	tkwin = grabWin;
    } else {
	/*
	 * First check whether the toplevel containing this mouse event is the
	 * grab window. If not, then send the event to the grab window.
	 * Otherwise, set tkWin to the subwindow which most closely contains
	 * the mouse event.
	 */

	dispPtr = TkGetDisplayList();
	rootwin = Tk_IdToWindow(dispPtr->display, medPtr->window);
	if ((rootwin == NULL)
		|| ((grabWin != NULL) && (rootwin != grabWin))) {
	    tkwin = grabWin;
	} else {
	    tkwin = Tk_TopCoordsToWindow(rootwin, medPtr->local.h,
		    medPtr->local.v, &local_x, &local_y);
	}
    }

    /*
     * The following call will generate the appropiate X events and adjust any
     * state that Tk must remember.
     */

    Tk_UpdatePointer(tkwin, medPtr->global.h, medPtr->global.v,
	    medPtr->state);
    return true;
}

/*
 *----------------------------------------------------------------------
 *
 * BringWindowForward --
 *
 *	Bring this background window to the front.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The window is brought forward.
 *
 *----------------------------------------------------------------------
 */

static void
BringWindowForward(
    WindowRef wRef,
    int isFrontProcess,
    int frontWindowOnly)
{
    if (wRef && !TkpIsWindowFloating(wRef) && IsValidWindowPtr(wRef)) {
	WindowRef frontWindow = FrontNonFloatingWindow();
	WindowModality frontWindowModality = kWindowModalityNone;
	
	if (frontWindow && frontWindow != wRef) {
	    ChkErr(GetWindowModality, frontWindow, &frontWindowModality, NULL);
	}
	if (frontWindowModality != kWindowModalityAppModal) {
	    Window window = TkMacOSXGetXWindow(wRef);

	    if (window != None) {
		TkDisplay *dispPtr = TkGetDisplayList();
		TkWindow *winPtr = (TkWindow *)
			Tk_IdToWindow(dispPtr->display, window);

		if (winPtr && winPtr->wmInfoPtr &&
			winPtr->wmInfoPtr->master != None) {
		    TkWindow *masterWinPtr = (TkWindow *)Tk_IdToWindow(
			    dispPtr->display, winPtr->wmInfoPtr->master);

		    if (masterWinPtr && masterWinPtr->window != None &&
			    TkMacOSXHostToplevelExists(masterWinPtr)) {
			WindowRef masterMacWin =
				TkMacOSXDrawableWindow(masterWinPtr->window);

			if (masterMacWin) {
			    BringToFront(masterMacWin);
			}
		    }
		}
	    }
	    SelectWindow(wRef);
	} else {
	    frontWindowOnly = 0;
	}
    }
    if (!isFrontProcess) {
	ProcessSerialNumber ourPsn = {0, kCurrentProcess};

	ChkErr(SetFrontProcessWithOptions, &ourPsn,
		frontWindowOnly ? kSetFrontProcessFrontWindowOnly : 0);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXBringWindowForward --
 *
 *	Bring this window to the front in response to a mouse click. If a grab
 *	is in effect, bring the grab window to the front instead.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The window is brought forward.
 *
 *----------------------------------------------------------------------
 */

void
TkMacOSXBringWindowForward(
    WindowRef wRef)
{
    TkDisplay *dispPtr = TkGetDisplayList();
    Tk_Window tkwin =
	    Tk_IdToWindow(dispPtr->display, TkMacOSXGetXWindow(wRef));
    Tk_Window grabWin = GetGrabWindowForWindow(tkwin);

    if (grabWin && grabWin != tkwin) {
	wRef = TkMacOSXDrawableWindow(((TkWindow *) grabWin)->window);
    }
    TkMacOSXSetEatButtonUp(true);
    BringWindowForward(wRef, Tk_MacOSXIsAppInFront(), !grabWin);
}

/*
 *----------------------------------------------------------------------
 *
 * GetGrabWindowForWindow --
 *
 *	Get the grab window for the given window, if any.
 *
 * Results:
 *	Grab Tk_Window or None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

Tk_Window
GetGrabWindowForWindow(
    Tk_Window tkwin) 
{
    Tk_Window grabWin = TkMacOSXGetCapture();

    if (!grabWin) {
	int grabState = TkGrabState((TkWindow *) tkwin);

	if (grabState != TK_GRAB_NONE && grabState != TK_GRAB_IN_TREE) {
	    grabWin = (Tk_Window) (((TkWindow *) tkwin)->dispPtr->grabWinPtr);
	}
    }
    
    return grabWin;
}

/*
 *----------------------------------------------------------------------
 *
 * GenerateMouseWheelEvent --
 *
 *	Generates a "MouseWheel" Tk event.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Places a mousewheel event on the event queue.
 *
 *----------------------------------------------------------------------
 */

static int
GenerateMouseWheelEvent(
    MouseEventData *medPtr)
{
    Tk_Window tkwin, rootwin;
    TkDisplay *dispPtr;
    TkWindow *winPtr;
    XEvent xEvent;

    dispPtr = TkGetDisplayList();
    rootwin = Tk_IdToWindow(dispPtr->display, medPtr->window);
    if (rootwin == NULL) {
	tkwin = NULL;
    } else {
	tkwin = Tk_TopCoordsToWindow(rootwin, medPtr->local.h,
		medPtr->local.v, &xEvent.xbutton.x, &xEvent.xbutton.y);
    }

    /*
     * The following call will generate the appropiate X events and adjust any
     * state that Tk must remember.
     */

    if (!tkwin) {
	tkwin = TkMacOSXGetCapture();
    }
    if (!tkwin) {
	return false;
    }

    winPtr = (TkWindow *) tkwin;
    xEvent.type = MouseWheelEvent;
    xEvent.xkey.keycode = medPtr->delta;
    xEvent.xbutton.x_root = medPtr->global.h;
    xEvent.xbutton.y_root = medPtr->global.v;
    xEvent.xbutton.state = medPtr->state;
    xEvent.xany.serial = LastKnownRequestProcessed(winPtr->display);
    xEvent.xany.send_event = false;
    xEvent.xany.display = winPtr->display;
    xEvent.xany.window = Tk_WindowId(winPtr);
    Tk_QueueWindowEvent(&xEvent, TCL_QUEUE_TAIL);
    return true;
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXGetEatButtonUp --
 *
 * Results:
 *	Return the flag indicating if we need to eat the next mouse up event.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
TkMacOSXGetEatButtonUp(void)
{
    return gEatButtonUp;
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXSetEatButtonUp --
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Sets the flag indicating if we need to eat the next mouse up event
 *
 *----------------------------------------------------------------------
 */

void
TkMacOSXSetEatButtonUp(
    int f)
{
    gEatButtonUp = f;
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXKeyModifiers --
 *
 *	Returns the current state of the modifier keys.
 *
 * Results:
 *	An OS Modifier state.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

EventModifiers
TkMacOSXModifierState(void)
{
    UInt32 keyModifiers;
    int isFrontProcess = (GetCurrentEvent() && Tk_MacOSXIsAppInFront());

    keyModifiers = isFrontProcess ? GetCurrentEventKeyModifiers() :
	    GetCurrentKeyModifiers();

    return (EventModifiers) (keyModifiers & USHRT_MAX);
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXButtonKeyState --
 *
 *	Returns the current state of the button & modifier keys.
 *
 * Results:
 *	A bitwise inclusive OR of a subset of the following: Button1Mask,
 *	ShiftMask, LockMask, ControlMask, Mod*Mask.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

unsigned int
TkMacOSXButtonKeyState(void)
{
    UInt32 buttonState = 0, keyModifiers;
    int isFrontProcess = (GetCurrentEvent() && Tk_MacOSXIsAppInFront());

    if (!TkMacOSXGetEatButtonUp()) {
	buttonState = isFrontProcess ? GetCurrentEventButtonState() :
		GetCurrentButtonState();
    }
    keyModifiers = isFrontProcess ? GetCurrentEventKeyModifiers() :
	    GetCurrentKeyModifiers();

    return ButtonModifiers2State(buttonState, keyModifiers);
}

/*
 *----------------------------------------------------------------------
 *
 * ButtonModifiers2State --
 *
 *	Converts Carbon mouse button state and modifier values into a Tk
 *	button/modifier state.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static unsigned int
ButtonModifiers2State(
    UInt32 buttonState,
    UInt32 keyModifiers)
{
    unsigned int state;

    /*
     * Tk supports at most 5 buttons.
     */

    state = (buttonState & ((1<<5) - 1)) << 8;

    if (keyModifiers & alphaLock) {
	state |= LockMask;
    }
    if (keyModifiers & shiftKey) {
	state |= ShiftMask;
    }
    if (keyModifiers & controlKey) {
	state |= ControlMask;
    }
    if (keyModifiers & cmdKey) {
	state |= Mod1Mask;		/* command key */
    }
    if (keyModifiers & optionKey) {
	state |= Mod2Mask;		/* option key */
    }
    if (keyModifiers & kEventKeyModifierNumLockMask) {
	state |= Mod3Mask;
    }
    if (keyModifiers & kEventKeyModifierFnMask) {
	state |= Mod4Mask;
    }

    return state;
}

/*
 *----------------------------------------------------------------------
 *
 * XQueryPointer --
 *
 *	Check the current state of the mouse. This is not a complete
 *	implementation of this function. It only computes the root coordinates
 *	and the current mask.
 *
 * Results:
 *	Sets root_x_return, root_y_return, and mask_return. Returns true on
 *	success.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

Bool
XQueryPointer(
    Display *display,
    Window w,
    Window *root_return,
    Window *child_return,
    int *root_x_return,
    int *root_y_return,
    int *win_x_return,
    int *win_y_return,
    unsigned int *mask_return)
{
    int getGlobal = (root_x_return && root_y_return);
    int getLocal = (win_x_return && win_y_return);

    if (getGlobal || getLocal) {
	Point where, local;
	OSStatus err = noErr;
	int gotMouseLoc = 0;
	EventRef ev = GetCurrentEvent();

	if (ev && getLocal) {
	    err = ChkErr(GetEventParameter, ev, kEventParamWindowMouseLocation,
		    typeQDPoint, NULL, sizeof(Point), NULL, &local);
	    gotMouseLoc = (err == noErr);
	}
	if (getGlobal || !gotMouseLoc) {
	    if (ev) {
		err = ChkErr(GetEventParameter, ev, kEventParamMouseLocation,
			typeQDPoint, NULL, sizeof(Point), NULL, &where);
	    }
	    if (!ev || err != noErr) {
		GetGlobalMouse(&where);
	    }
	}
	if (getLocal) {
	    WindowRef whichWin;

	    if (ev) {
		err = ChkErr(GetEventParameter, ev, kEventParamWindowRef,
			typeWindowRef, NULL, sizeof(WindowRef), NULL,
			&whichWin);
	    }
	    if (!ev || err != noErr) {
		FindWindow(where, &whichWin);
	    }
	    if (gotMouseLoc) {
		if (whichWin) {
		    Rect widths;

		    ChkErr(GetWindowStructureWidths, whichWin, &widths);
		    local.h -= widths.left;
		    local.v -= widths.top;
		}
	    } else {
		local = where;
		if (whichWin) {
		    QDGlobalToLocalPoint(GetWindowPort(whichWin), &local);
		}
	    }
	}
	if (getGlobal) {
	    *root_x_return = where.h;
	    *root_y_return = where.v;
	}
	if (getLocal) {
	    *win_x_return = local.h;
	    *win_y_return = local.v;
	}
    }
    if (mask_return) {
	*mask_return = TkMacOSXButtonKeyState();
    }
    return True;
}

/*
 *----------------------------------------------------------------------
 *
 * TkGenerateButtonEventForXPointer --
 *
 *	This procedure generates an X button event for the current pointer
 *	state as reported by XQueryPointer().
 *
 * Results:
 *	True if event(s) are generated - false otherwise.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue. Grab state may
 *	also change.
 *
 *----------------------------------------------------------------------
 */

MODULE_SCOPE int
TkGenerateButtonEventForXPointer(
    Window window)		/* X Window containing button event. */
{
    MouseEventData med;
    int global_x, global_y, local_x, local_y;

    bzero(&med, sizeof(MouseEventData));
    XQueryPointer(NULL, None, NULL, NULL, &global_x, &global_y,
	    &local_x, &local_y, &med.state);
    med.global.h = global_x;
    med.global.v = global_y;
    med.local.h = local_x;
    med.local.v = local_y;
    med.window = window;
    med.activeNonFloating = ActiveNonFloatingWindow();

    return GenerateButtonEvent(&med);
}

/*
 *----------------------------------------------------------------------
 *
 * TkGenerateButtonEvent --
 *
 *	Given a global x & y position and the button key status this procedure
 *	generates the appropiate X button event. It also handles the state
 *	changes needed to implement implicit grabs.
 *
 * Results:
 *	True if event(s) are generated, false otherwise.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue. Grab state may
 *	also change.
 *
 *----------------------------------------------------------------------
 */

int
TkGenerateButtonEvent(
    int x,			/* X location of mouse, */
    int y,			/* Y location of mouse. */
    Window window,		/* X Window containing button event. */
    unsigned int state)		/* Button Key state suitable for X event. */
{
    MouseEventData med;

    bzero(&med, sizeof(MouseEventData));
    med.state = state;
    med.window = window;
    med.global.h = x;
    med.global.v = y;
    FindWindow(med.global, &med.whichWin);
    med.activeNonFloating = ActiveNonFloatingWindow();
    med.local = med.global;
    QDGlobalToLocalPoint(GetWindowPort(med.whichWin), &med.local);

    return GenerateButtonEvent(&med);
}

/*
 *----------------------------------------------------------------------
 *
 * GenerateButtonEvent --
 *
 *	Generate an X button event from a MouseEventData structure. Handles
 *	the state changes needed to implement implicit grabs.
 *
 * Results:
 *	True if event(s) are generated - false otherwise.
 *
 * Side effects:
 *	Additional events may be place on the Tk event queue. Grab state may
 *	also change.
 *
 *----------------------------------------------------------------------
 */

static int
GenerateButtonEvent(
    MouseEventData *medPtr)
{
    Tk_Window tkwin;
    int dummy;
    TkDisplay *dispPtr;

#if UNUSED
    /*
     * ButtonDown events will always occur in the front window. ButtonUp
     * events, however, may occur anywhere on the screen. ButtonUp events
     * should only be sent to Tk if in the front window or during an implicit
     * grab.
     */

    if ((medPtr->activeNonFloating == NULL)
	    || ((!(TkpIsWindowFloating(medPtr->whichWin))
	    && (medPtr->activeNonFloating != medPtr->whichWin))
	    && TkMacOSXGetCapture() == NULL)) {
	return false;
    }
#endif

    dispPtr = TkGetDisplayList();
    tkwin = Tk_IdToWindow(dispPtr->display, medPtr->window);

    if (tkwin != NULL) {
	tkwin = Tk_TopCoordsToWindow(tkwin, medPtr->local.h, medPtr->local.v,
		&dummy, &dummy);
    }

    Tk_UpdatePointer(tkwin, medPtr->global.h, medPtr->global.v, medPtr->state);
    return true;
}

/*
 *----------------------------------------------------------------------
 *
 * GenerateToolbarButtonEvent --
 *
 *	Generates a "ToolbarButton" virtual event. This can be used to manage
 *	disappearing toolbars.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Places a virtual event on the event queue.
 *
 *----------------------------------------------------------------------
 */

static int
GenerateToolbarButtonEvent(
    MouseEventData *medPtr)
{
    Tk_Window rootwin, tkwin = NULL;
    TkDisplay *dispPtr;
    TkWindow *winPtr;
    XVirtualEvent event;

    dispPtr = TkGetDisplayList();
    rootwin = Tk_IdToWindow(dispPtr->display, medPtr->window);
    if (rootwin) {
	tkwin = Tk_TopCoordsToWindow(rootwin,
		medPtr->local.h, medPtr->local.v, &event.x, &event.y);
    }
    if (!tkwin) {
	return true;
    }
    winPtr = (TkWindow *) tkwin;

    bzero(&event, sizeof(XVirtualEvent));
    event.type = VirtualEvent;
    event.serial = LastKnownRequestProcessed(winPtr->display);
    event.send_event = false;
    event.display = winPtr->display;
    event.event = winPtr->window;
    event.root = XRootWindow(winPtr->display, 0);
    event.subwindow = None;
    event.time = TkpGetMS();
    event.x_root = medPtr->global.h;
    event.y_root = medPtr->global.v;
    event.state = medPtr->state;
    event.same_screen = true;
    event.name = Tk_GetUid("ToolbarButton");

    Tk_QueueWindowEvent((XEvent *) &event, TCL_QUEUE_TAIL);
    return true;
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 79
 * coding: utf-8
 * End:
 */