Skip to main content
summaryrefslogtreecommitdiffstats
blob: 20f514e91b11661e763e9d6433ae474351e3192e (plain) (blame)
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
package org.eclipse.swt.widgets;

/*
 * Copyright (c) 2000, 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
 */

import org.eclipse.swt.internal.carbon.OS;

public class WidgetTable {
	static int FreeSlot = 0;
	static int GrowSize = 1024;
	static int [] IndexTable = new int [GrowSize];
	static Widget [] WidgetTable = new Widget [GrowSize];
	static {
		for (int i=0; i<GrowSize-1; i++) IndexTable [i] = i + 1;
		IndexTable [GrowSize - 1] = -1;
	}
public static synchronized Widget get (int handle) {
	if (handle == 0) return null;
	int index = getUserData(handle) - 1;
	if (0 <= index && index < WidgetTable.length) return WidgetTable [index];
	return null;
}
public synchronized static void put (int handle, Widget widget) {
	if (handle == 0) return;
	if (FreeSlot == -1) {
		int length = (FreeSlot = IndexTable.length) + GrowSize;
		int [] newIndexTable = new int [length];
		Widget [] newWidgetTable = new Widget [length];
		System.arraycopy (IndexTable, 0, newIndexTable, 0, FreeSlot);
		System.arraycopy (WidgetTable, 0, newWidgetTable, 0, FreeSlot);
		for (int i=FreeSlot; i<length-1; i++) {
			newIndexTable [i] = i + 1;
		}
		newIndexTable [length - 1] = -1;
		IndexTable = newIndexTable;
		WidgetTable = newWidgetTable;
	}
    setUserData(handle, FreeSlot + 1);
	int oldSlot = FreeSlot;
	FreeSlot = IndexTable [oldSlot];
	IndexTable [oldSlot] = -2;
	WidgetTable [oldSlot] = widget;
}
public static synchronized Widget remove (int handle) {
	if (handle == 0) return null;
	Widget widget = null;
    int index= getUserData(handle) - 1;
	if (0 <= index && index < WidgetTable.length) {
		widget = WidgetTable [index];
		WidgetTable [index] = null;
		IndexTable [index] = FreeSlot;
		FreeSlot = index;
        setUserData(handle, 0);
	}
	return widget;
}
public static synchronized Shell [] shells () {
      int size= 0;
      for (int i= 0; i < WidgetTable.length; i++)
            if (WidgetTable[i] instanceof Shell)
				size++;

      int index= 0;
      Shell[] result= new Shell[size];
      for (int i= 0; i < WidgetTable.length; i++) {
            Widget widget= WidgetTable[i];
            if (widget instanceof Shell)
                  result [index++]= (Shell) widget;
      }
      return result;
}
public static synchronized int size () {
	int length = 0;
	for (int i=0; i<WidgetTable.length; i++) {
		if (WidgetTable [i] != null) length++;
	}
	return length;
}
/////////////////////////////////////////////////
// Mac stuff
/////////////////////////////////////////////////

	private static int getUserData(int handle) {
		if (OS.IsValidControlHandle(handle))
			return OS.GetControlReference(handle);
		if (OS.IsValidMenu(handle)) {
			int[] refCon= new int[1];
			OS.GetMenuItemRefCon(handle, (short)0, refCon);
			//System.out.println("refCon: " + refCon[0]);
			return refCon[0];
		}
		if (OS.IsValidWindowPtr(handle))
			return OS.GetWRefCon(handle);
		//System.out.println("WidgetTable.getUserData: unknown handle type");
		return -1;
	}
	
	private static void setUserData(int handle, int data) {
		if (OS.IsValidControlHandle(handle)) {
			OS.SetControlReference(handle, data);
			return;
		}
		if (OS.IsValidMenu(handle)) {
			OS.SetMenuItemRefCon(handle, (short)0, data);
			return;
		}
		if (OS.IsValidWindowPtr(handle)) {
			OS.SetWRefCon(handle, data);
			return;
		}
		System.out.println("WidgetTable.setUserData: unknown handle type: " + handle);
	}
}

Back to the top