1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.ole.win32.OleEventTable;
14 
15 import org.eclipse.swt.ole.win32.OleListener;
16 import org.eclipse.swt.ole.win32.OleEvent;
17 
18 import java.lang.all;
19 
20 /**
21 * The OleEventTable class implements a simple
22 * look up mechanism that maps an event type
23 * to a listener.  Multiple listeners for the
24 * same event type are supported.
25 *
26 */
27 
28 class OleEventTable {
29     int [] types;
30     OleListener [] handlers;
31 void hook (int eventType, OleListener handler) {
32     if (types is null) types = new int [4];
33     if (handlers is null) handlers = new OleListener [4];
34     for (int i=0; i<types.length; i++) {
35         if (types [i] is 0) {
36             types [i] = eventType;
37             handlers [i] = handler;
38             return;
39         }
40     }
41     int size = cast(int)/*64bit*/types.length;
42     int [] newTypes = new int [size + 4];
43     OleListener [] newHandlers = new OleListener [size + 4];
44     System.arraycopy (types, 0, newTypes, 0, size);
45     SimpleType!(OleListener).arraycopy (handlers, 0, newHandlers, 0, size);
46     types = newTypes;  handlers = newHandlers;
47     types [size] = eventType;  handlers [size] = handler;
48 }
49 bool hooks (int eventType) {
50     if (handlers is null) return false;
51     for (int i=0; i<types.length; i++) {
52         if (types [i] is eventType) return true;
53     }
54     return false;
55 }
56 void sendEvent (OleEvent event) {
57     if (handlers is null) return;
58     for (int i=0; i<types.length; i++) {
59         if (types [i] is event.type) {
60             OleListener listener = handlers [i];
61             if (listener !is null) listener.handleEvent (event);
62         }
63     }
64 }
65 void unhook (int eventType, OleListener handler) {
66     if (handlers is null) return;
67     for (int i=0; i<types.length; i++) {
68         if ((types [i] is eventType) && (handlers [i] is handler)) {
69             types [i] = 0;
70             handlers [i] = null;
71             return;
72         }
73     }
74 }
75 bool hasEntries() {
76     for (int i=0; i<types.length; i++) {
77         if (types[i] !is 0) return true;
78     }
79     return false;
80 }
81 }