summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT PI/carbon/org/eclipse/swt/internal/carbon/MacUtil.java
blob: ba33b09b198e488b956c63cd081f2e08cf116d47 (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
/*
 * Copyright (c) 2002 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Andre Weinand, OTI - Initial version
 */
package org.eclipse.swt.internal.carbon;

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.Callback;

public class MacUtil {

	public final static boolean DEBUG;
	
	public final static boolean USE_MENU_ICONS;
	
	/** Prevent use of standard Mac shortcuts Cmd-Q, Cmd-H */
	public final static boolean KEEP_MAC_SHORTCUTS;
	public final static boolean FULL_KBD_NAV;
	
	/** use HIViews instead of ControlManager controls */
	public final static boolean HIVIEW;
	/** use setFrame calls instead of setBounds */
	public final static boolean USE_FRAME;
	
	static final char MNEMONIC = '&';
		
	static {
		DEBUG= false;
		USE_MENU_ICONS= true;
		KEEP_MAC_SHORTCUTS= true;
		FULL_KBD_NAV= true;
		HIVIEW= false;
		USE_FRAME= false;
	}
	
	//////////////////////////////////////////////////////////////////////
	
	private static int fViewClassID= 0;
	
	static int createCallback(String method, int argCount) {
		Callback cb= new Callback(MacUtil.class, method, argCount);
		int proc= cb.getAddress();
		return proc;
	}
	
	static int hiobProc(int a, int b, int c) {
		System.out.println("hiobProc");
		return OS.kNoErr;
	}
	
	static int createHIView() {
		int rc;
		
		if (fViewClassID == 0) {
				
			fViewClassID= OS.CFStringCreateWithCharacters("org.eclipse.swt.hiview");
			int baseClassID= OS.CFStringCreateWithCharacters("com.apple.hiview");
		
			int[] events= new int[] {
				OS.kEventClassHIObject, OS.kEventHIObjectConstruct,
				//OS.kEventClassHIObject, OS.kEventHIObjectInitialize,
				OS.kEventClassHIObject, OS.kEventHIObjectDestruct,
				
				OS.kEventClassControl,	OS.kEventControlDraw,
				OS.kEventClassControl,	OS.kEventControlAddedSubControl,
				OS.kEventClassControl,	OS.kEventControlRemovingSubControl,
			};
		
			int hiobProc= createCallback("hiobProc", 3);
		
			int[] tmp= new int[1];
			rc= OS.HIObjectRegisterSubclass(fViewClassID, baseClassID, 0, hiobProc, events, 0, tmp);
			System.out.println("HIObjectRegisterSubclass: " + rc);
		
			OS.CFRelease(baseClassID);
		}
		
		int[] oref= new int[1];
		//rc= OS.HIObjectCreate(fViewClassID, 0, oref);
		rc= OS.HIObjectCreate(OS.CFStringCreateWithCharacters("com.apple.hiview"), 0, oref);
		System.out.println("HIObjectCreate: " + rc + " " + oref[0]);
		return oref[0];
	}

	//////////////////////////////////////////////////////////////////////
		
	public static int getChild(int handle, int[] t, int n, int i) {
		int index= (n-1 - i);
		int status= OS.GetIndexedSubControl(handle, (short)(index+1), t);
		if (status != OS.kNoErr)
			System.out.println("MacUtil.getChild: error");
		return status;
	}
		
	public static int indexOf(int parentHandle, int handle) {
		int n= countSubControls(parentHandle);
		int[] outControl= new int[1];
		for (int i= 0; i < n; i++) {
			if (getChild(parentHandle, outControl, n, i) == OS.kNoErr)
				if (outControl[0] == handle)
					return i;
		}
		return -1;
	}
	
	/**
	 * Inserts the given child at position in the parent.
	 * If pos is out of range the child is added at the end (below all other).
	 */
	private static void insertControl(int controlHandle, int parentControlHandle, int pos) {
		
		
		int n= countSubControls(parentControlHandle);
		
		int should= pos;
		if (should < 0 || should > n)
			should= n;
		
		boolean add= false;
		if (getSuperControl(controlHandle) != parentControlHandle) {
			add= true;
		} else {
			/*
			String w1= getHIObjectClassID(parentControlHandle);
			String w2= getHIObjectClassID(controlHandle);
			System.out.println("MacUtil.insertControl: already there: " + w1 + " " + w2);
			*/
			if (n == 1)
				return;
		}
		
		if (n == 0) {
			OS.HIViewAddSubview(parentControlHandle, controlHandle);
			pos= 0;
		} else {
			if (pos >= 0 && pos < n) {
				int[] where= new int[1];
				getChild(parentControlHandle, where, n, pos);
				if (add)
					OS.HIViewAddSubview(parentControlHandle, controlHandle);
				OS.HIViewSetZOrder(controlHandle, OS.kHIViewZOrderAbove, where[0]);
			} else {
				if (add)
					OS.HIViewAddSubview(parentControlHandle, controlHandle);
				if (OS.HIViewSetZOrder(controlHandle, OS.kHIViewZOrderBelow, 0) != OS.kNoErr)
					System.out.println("error 2");
				pos= n;
			}
		}
	
		// verify correct position
		int i= indexOf(parentControlHandle, controlHandle);
		if (i != should)
			System.out.println("MacUtil.insertControl: is: "+i+" should: "+ should  + " n:" + n + " add: " + add);
	}
	
	/**
	 * Adds the given child at the end.
	 */
	public static void addControl(int controlHandle, int parentControlHandle) {
		insertControl(controlHandle, parentControlHandle, -1);
	}
		
	public static int getVisibleRegion(int cHandle, int result, boolean includingTop) {
		int tmpRgn= OS.NewRgn();
		
		getControlRegion(cHandle, OS.kControlEntireControl, result);

		int parent= cHandle;
		while ((parent= MacUtil.getSuperControl(parent)) != 0) {
			getControlRegion(parent, OS.kControlContentMetaPart, tmpRgn);
			OS.SectRgn(result, tmpRgn, result);
		}
		
		if (includingTop) {
			int n= countSubControls(cHandle);
			if (n > 0) {
				//System.out.println("have children on top");
				int[] outHandle= new int[1];
				for (int i= 0; i < n; i++) {
					int index= i; // was: n-1-i
					if (OS.GetIndexedSubControl(cHandle, (short)(index+1), outHandle) == 0) {	// indices are 1 based
						if (OS.IsControlVisible(outHandle[0])) {
							getControlRegion(outHandle[0], OS.kControlStructureMetaPart, tmpRgn);
							OS.DiffRgn(result, tmpRgn, result);
						}
					} else
						throw new SWTError();
				}
			}
		}
		
		OS.DisposeRgn(tmpRgn);
                
		return OS.kNoErr;
	}
	
	private static int find(int cHandle, Rectangle parentBounds, MacRect tmp, Point where) {
	
		if (! OS.IsControlVisible(cHandle))
			return 0;
		if (! OS.IsControlActive(cHandle))
			return 0;

		OS.GetControlBounds(cHandle, tmp.getData());
		Rectangle rr= tmp.toRectangle();
		if (parentBounds != null)
			rr= parentBounds.intersection(rr);

		int n= countSubControls(cHandle);
		if (n > 0) {
			int[] outHandle= new int[1];
			for (int i= 0; i < n; i++) {
				int index= (n-1-i);
				if (OS.GetIndexedSubControl(cHandle, (short)(index+1), outHandle) == 0) {	// indices are 1 based
					int result= find(outHandle[0], rr, tmp, where);
					if (result != 0)
						return result;
				}
			}
		}

		if (rr.contains(where))
			return cHandle;
		return 0;
	}

	//////////////////////////////////////////////////////////////////////
	
	public static Point toControl(int cHandle, Point point) {
		MacPoint mp= new MacPoint(point);
		
		int wHandle= OS.GetControlOwner(cHandle);
		int port= OS.GetWindowPort(wHandle);
		OS.QDGlobalToLocalPoint(port, mp.getData());
	
		MacRect bounds= new MacRect();
		OS.GetControlBounds(cHandle, bounds.getData());
		
		Point p= mp.toPoint();
		p.x-= bounds.getX();
		p.y-= bounds.getY();
		
		/*
		float[] p2= new float[2];
		p2[0]= mp.getX();
		p2[1]= mp.getY();
		OS.HIViewConvertPoint(p2, 0, cHandle);
		System.out.println("MacUtil.toControl: " + p + "  " + p2[0] + " " + p2[1]);
		*/
		
		return p;
	}
	
	public static Point toDisplay(int cHandle, Point point) {
		MacRect bounds= new MacRect();
		OS.GetControlBounds(cHandle, bounds.getData());
		MacPoint mp= new MacPoint(point.x+bounds.getX(), point.y+bounds.getY());
		
		int wHandle= OS.GetControlOwner(cHandle);
		int port= OS.GetWindowPort(wHandle);
		OS.QDLocalToGlobalPoint(port, mp.getData());
		
		return mp.toPoint();
	}
		
	private static void getControlRegion(int cHandle, short part, int rgn) {
		if (true) {
			short[] bounds= new short[4];
			OS.GetControlBounds(cHandle, bounds);
			OS.RectRgn(rgn, bounds);
		} else {
			OS.GetControlRegion(cHandle, part, rgn);
		}
	}

	// Hit detection on the Mac is reversed and doesn't consider clipping,
	// so we have to do it ourselves

	public static int findControlUnderMouse(MacPoint where, int wHandle, short[] cpart) {
		
		int root;
		if (true) {
			int[] rootHandle= new int[1];
			int rc= OS.GetRootControl(wHandle, rootHandle);
			if (rc != OS.kNoErr) {
				System.out.println("MacUtil.findControlUnderMouse: " + rc);
				return 0;
			}
			root= rootHandle[0];
		} else {
			root= OS.HIViewGetRoot(wHandle);
		}
		Point w= where.toPoint();
		int cHandle= find(root, null, new MacRect(), w);
		if (cHandle != 0 && cpart != null && cpart.length > 0) {
			cpart[0]= OS.TestControl(cHandle, where.getData());
			//System.out.println("findControlUnderMouse: " + cpart[0]);
		}
		return cHandle;
	}

	private static int countSubControls(int cHandle) {
		short[] cnt= new short[1];
		int status= OS.CountSubControls(cHandle, cnt);
		switch (status) {
		case OS.kNoErr:
			return cnt[0];			
		case OS.errControlIsNotEmbedder:
			//System.out.println("MacUtil.countSubControls: errControlIsNotEmbedder");
			break;
		case -30599: // OS.controlHandleInvalidErr
			System.out.println("MacUtil.countSubControls: controlHandleInvalidErr");
			break;
		default:
			System.out.println("MacUtil.countSubControls: " + status);
			break;
		}
		return 0;
	}
	
	public static String getStringAndRelease(int sHandle) {
		int length= OS.CFStringGetLength(sHandle);
		char[] buffer= new char[length];
		OS.CFStringGetCharacters(sHandle, 0, length, buffer);
		OS.CFRelease(sHandle);
		return new String(buffer);
	}
	
	public static byte[] Str255(String s) {
		int l= 0;
		if (s != null)
			l= s.length();
		if (l > 255) {
			throw new SWTError(SWT.ERROR_INVALID_RANGE);
			//System.out.println("MacUtil.Str255: string length > 255");
		}
		byte[] b= new byte[l+1];
		b[0]= (byte) l;
		for (int i= 0; i < l; i++)
			b[i+1]= (byte) s.charAt(i);
		return b;
	}
	
	public static String toString(byte[] str255) {
		int n= str255[0];
		char[] c= new char[n];
		for (int i= 0; i < n; i++)
			c[i]= (char) str255[i+1];
		return new String(c);
	}

	public static int OSType(String s) {
		return ((s.charAt(0) & 0xff) << 24) | ((s.charAt(1) & 0xff) << 16) | ((s.charAt(2) & 0xff) << 8) | (s.charAt(3) & 0xff);
	}

	public static String getHIObjectClassID(int handle) {
		int sh= OS.HIObjectCopyClassID(handle);
		return getStringAndRelease(sh);
	}

	/**
	 * Create a new control and embed it in the given parent control.
	 */
	public static int newControl(int parentControlHandle, short procID) {
		return newControl(parentControlHandle, -1, (short)0, (short)0, (short)0, procID);
	}
	
	/**
	 * Create a new control and embed it in the given parent control.
	 */
	public static int newControl(int parentControlHandle, short init, short min, short max, short procID) {
		return newControl(parentControlHandle, -1, init, min, max, procID);
	}
	
	/**
	 * Create a new control and embed it in the given parent control.
	 */
	public static int newControl(int parentControlHandle, int pos, short init, short min, short max, short procID) {
		int controlHandle;
		if (HIVIEW) {
			controlHandle= OS.NewControl(0, false, init, min, max, procID);
			insertControl(controlHandle, parentControlHandle, pos);
			OS.HIViewSetVisible(controlHandle, true);
			OS.HIViewSetNeedsDisplay(controlHandle, true);
		} else {
			int windowHandle= OS.GetControlOwner(parentControlHandle);
			controlHandle= OS.NewControl(windowHandle, false, init, min, max, procID);
			insertControl(controlHandle, parentControlHandle, pos);
			initLocation(controlHandle);
			OS.HIViewSetVisible(controlHandle, true);
		}

		return controlHandle;
	}
	
	public static int createDrawingArea(int parentControlHandle, int pos, boolean visible, int width, int height, int border) {
		int features= OS.kControlSupportsEmbedding | OS.kControlSupportsFocus | OS.kControlGetsFocusOnClick;
		int controlHandle;
		if (HIVIEW) {
			features |= OS.kControlHandlesTracking;
			controlHandle= OS.NewControl(0, false, (short)features, (short)0, (short)0, OS.kControlUserPaneProc);
			OS.SizeControl(controlHandle, (short)width, (short)height);
			insertControl(controlHandle, parentControlHandle, pos);
			OS.HIViewSetVisible(controlHandle, visible);
			OS.HIViewSetNeedsDisplay(controlHandle, true);
		} else {
			int windowHandle= OS.GetControlOwner(parentControlHandle);
			controlHandle= OS.NewControl(windowHandle, false, (short)features, (short)0, (short)0, OS.kControlUserPaneProc);
			OS.SizeControl(controlHandle, (short)width, (short)height);
			insertControl(controlHandle, parentControlHandle, pos);
			initLocation(controlHandle);
			OS.HIViewSetVisible(controlHandle, visible);
		}
		return controlHandle;
	}	
	
	public static void initLocation(int cHandle) {
		int parent= getSuperControl(cHandle);
		short[] bounds= new short[4];
		OS.GetControlBounds(parent, bounds);
		short x= bounds[1];
		short y= bounds[0];
		if (x > 0 || y > 0)
			OS.MoveControl(cHandle, x, y);
	}
	
	/**
	 * Returns the parent of the given control or null if the control is a root control.
	 */
	public static int getSuperControl(int cHandle) {
        
		int wHandle= OS.GetControlOwner(cHandle);
		if (wHandle == 0) {
			//System.out.println("MacUtil.getSuperControl: GetControlOwner error");
			return 0;
		}
		int[] rootHandle= new int[1];
		OS.GetRootControl(wHandle, rootHandle);
		if (cHandle == rootHandle[0])
			return 0;
                
		int[] parentHandle= new int[1];
		int rc= OS.GetSuperControl(cHandle, parentHandle);
		if (rc != OS.kNoErr)
			System.out.println("MacUtil.getSuperControl: " + rc);
		return parentHandle[0];
	}

	public static void dump(int matchHandle) {
		int wHandle= OS.GetControlOwner(matchHandle);
		int[] rootHandle= new int[1];
		OS.GetRootControl(wHandle, rootHandle);
		dump(rootHandle[0], 0, matchHandle);
		System.out.println();
	}
	
	public static void dump(int cHandle, int level, int matchHandle) {
		for (int x= 0; x < level; x++)
			System.out.print("  ");
		Widget w= WidgetTable.get(cHandle);
		if (w != null)
			System.out.print(w);
		else
			System.out.print(cHandle);
			
		MacRect bounds= new MacRect();
		OS.GetControlBounds(cHandle, bounds.getData());
		System.out.print(" " + bounds.toRectangle());
		
		if (cHandle == matchHandle)
			System.out.println(" ******************");
		else
			System.out.println();
                    
		int n= countSubControls(cHandle);
		if (n > 0) {
			int[] outHandle= new int[1];
			for (int i= 0; i < n; i++) {
				if (OS.GetIndexedSubControl(cHandle, (short)(i+1), outHandle) == 0)
					dump(outHandle[0], level+1, matchHandle);
			}
		}
	}
	
	public static Point computeSize(int handle) {
		if (OS.IsValidControlHandle(handle)) {
			MacRect rect= new MacRect();
			short[] base= new short[1];
			OS.GetBestControlRect(handle, rect.getData(), base);
			if (rect.isEmpty())
				System.out.println("MacUtil.computeSize: 0 size");
			return rect.getSize();
		}
		System.out.println("MacUtil.computeSize: unknown handle type");
		return new Point(50, 50);
	}
	
	public static int getDisplayWidth() {
		MacRect bounds= new MacRect();
		OS.GetAvailableWindowPositioningBounds(OS.GetMainDevice(), bounds.getData());
		return bounds.getWidth();
	}
	
	public static int getDisplayHeight() {
		MacRect bounds= new MacRect();
		OS.GetAvailableWindowPositioningBounds(OS.GetMainDevice(), bounds.getData());
		return bounds.getHeight();
	}
	
	public static String toString(int i) {
		StringBuffer sb= new StringBuffer();
		sb.append((char)((i & 0xff000000) >> 24));
		sb.append((char)((i & 0x00ff0000) >> 16));
		sb.append((char)((i & 0x0000ff00) >> 8));
		sb.append((char)((i & 0x000000ff) >> 0));
		return sb.toString();
	}
	
	public static String removeMnemonics(String s) {
		if (s != null) {
			int l= s.length();
			if (l > 0) {
				char[] buf= new char[l];
				int j= 0;
				for (int i= 0; i < l; i++) {
					char c= s.charAt(i);
					if (c != MNEMONIC)
						buf[j++]= c;
				}
				return new String(buf, 0, j);
			}
		}
		return s;
	}
	
	public static void RGBBackColor(int packed) {
		if ((packed & 0xff000000) == 0) {
			OS.RGBBackColor((short)(((packed >> 16) & 0xFF) * 257),
							(short)(((packed >> 8) & 0xFF) * 257),
							(short)(((packed) & 0xFF) * 257));
		} else {
			OS.RGBBackColor((short)0xFFFF, (short)0xFFFF, (short)0xFFFF);
		}
	}
	
	public static void RGBForeColor(int packed) {
		if ((packed & 0xff000000) == 0) {
			OS.RGBForeColor((short)(((packed >> 16) & 0xFF) * 257),
							(short)(((packed >> 8) & 0xFF) * 257),
							(short)(((packed) & 0xFF) * 257));
		} else {
			OS.RGBForeColor((short)0xFFFF, (short)0xFFFF, (short)0xFFFF);
		}
	}
	
	public static int[] getDataBrowserItems(int dataBrowserHandle, int containerID, int state, boolean recurse) {
		int resultHandle= 0;
		try {
			resultHandle= OS.NewHandle(0);
			if (OS.GetDataBrowserItems(dataBrowserHandle, containerID, recurse, state, resultHandle) == OS.kNoErr) {
				int itemCount= OS.GetHandleSize(resultHandle) / 4;	// sizeof(int)
				if (itemCount > 0) {	
					int resultIDs[]= new int[itemCount];
					OS.getHandleData(resultHandle, resultIDs);
					return resultIDs;
				}
			}
		} finally {
			OS.DisposeHandle(resultHandle);
		}
		return new int[0];
	}

	public static int[] getSelectionIDs(int dataBrowserHandle, int containerID, boolean recurse) {
		return getDataBrowserItems(dataBrowserHandle, containerID, OS.kDataBrowserItemIsSelected, recurse);
	}

}