KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > ClipboardProxy


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

11 package org.eclipse.swt.dnd;
12
13  
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.internal.Callback;
16 import org.eclipse.swt.internal.Converter;
17 import org.eclipse.swt.internal.gtk.GtkSelectionData;
18 import org.eclipse.swt.internal.gtk.GtkTargetEntry;
19 import org.eclipse.swt.internal.gtk.OS;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.swt.widgets.Listener;
23
24 class ClipboardProxy {
25     /* Data is not flushed to the clipboard immediately.
26      * This class will remember the data and provide it when requested.
27      */

28     Object JavaDoc[] clipboardData;
29     Transfer[] clipboardDataTypes;
30     Object JavaDoc[] primaryClipboardData;
31     Transfer[] primaryClipboardDataTypes;
32
33     Display display;
34     Clipboard activeClipboard = null;
35     Clipboard activePrimaryClipboard = null;
36     Callback getFunc;
37     Callback clearFunc;
38     
39     static String JavaDoc ID = "CLIPBOARD PROXY OBJECT"; //$NON-NLS-1$
40

41 static ClipboardProxy _getInstance(final Display display) {
42     ClipboardProxy proxy = (ClipboardProxy) display.getData(ID);
43     if (proxy != null) return proxy;
44     proxy = new ClipboardProxy(display);
45     display.setData(ID, proxy);
46     display.addListener(SWT.Dispose, new Listener() {
47         public void handleEvent(Event event) {
48             ClipboardProxy clipbordProxy = (ClipboardProxy)display.getData(ID);
49             if (clipbordProxy == null) return;
50             display.setData(ID, null);
51             clipbordProxy.dispose();
52         }
53     });
54     return proxy;
55 }
56
57 ClipboardProxy(Display display) {
58     this.display = display;
59     getFunc = new Callback( this, "getFunc", 4); //$NON-NLS-1$
60
if (getFunc.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
61     clearFunc = new Callback( this, "clearFunc", 2); //$NON-NLS-1$
62
if (clearFunc.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
63 }
64
65 void clear (Clipboard owner, int clipboards) {
66     if ((clipboards & DND.CLIPBOARD) != 0 && activeClipboard == owner) {
67         OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
68     }
69     if ((clipboards & DND.SELECTION_CLIPBOARD) != 0 && activePrimaryClipboard == owner) {
70         OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
71     }
72 }
73
74 int /*long*/ clearFunc(int /*long*/ clipboard,int /*long*/ user_data_or_owner){
75     if (clipboard == Clipboard.GTKCLIPBOARD) {
76         activeClipboard = null;
77         clipboardData = null;
78         clipboardDataTypes = null;
79     }
80     if (clipboard == Clipboard.GTKPRIMARYCLIPBOARD) {
81         activePrimaryClipboard = null;
82         primaryClipboardData = null;
83         primaryClipboardDataTypes = null;
84     }
85     return 1;
86 }
87
88 void dispose () {
89     if (display == null) return;
90     if (activeClipboard != null) OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
91     if (activePrimaryClipboard != null) OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
92     display = null;
93     if (getFunc != null ) getFunc.dispose();
94     getFunc = null;
95     if (clearFunc != null) clearFunc.dispose();
96     clearFunc = null;
97     clipboardData = null;
98     clipboardDataTypes = null;
99     primaryClipboardData = null;
100     primaryClipboardDataTypes = null;
101 }
102
103 /**
104  * This function provides the data to the clipboard on request.
105  * When this clipboard is disposed, the data will no longer be available.
106  */

107 int /*long*/ getFunc( int /*long*/ clipboard, int /*long*/ selection_data, int /*long*/ info, int /*long*/ user_data_or_owner){
108     if (selection_data == 0) return 0;
109     GtkSelectionData selectionData = new GtkSelectionData();
110     OS.memmove(selectionData, selection_data, GtkSelectionData.sizeof);
111     TransferData tdata = new TransferData();
112     tdata.type = selectionData.target;
113     Transfer[] types = (clipboard == Clipboard.GTKCLIPBOARD) ? clipboardDataTypes : primaryClipboardDataTypes;
114     int index = -1;
115     for (int i = 0; i < types.length; i++) {
116         if (types[i].isSupportedType(tdata)) {
117             index = i;
118             break;
119         }
120     }
121     if (index == -1) return 0;
122     Object JavaDoc[] data = (clipboard == Clipboard.GTKCLIPBOARD) ? clipboardData : primaryClipboardData;
123     types[index].javaToNative(data[index], tdata);
124     if (tdata.format < 8 || tdata.format % 8 != 0) {
125         return 0;
126     }
127     OS.gtk_selection_data_set(selection_data, tdata.type, tdata.format, tdata.pValue, tdata.length);
128     OS.g_free(tdata.pValue);
129     return 1;
130 }
131
132 boolean setData(Clipboard owner, Object JavaDoc[] data, Transfer[] dataTypes, int clipboards) {
133     GtkTargetEntry[] entries = new GtkTargetEntry [0];
134     int /*long*/ pTargetsList = 0;
135     try {
136         for (int i = 0; i < dataTypes.length; i++) {
137             Transfer transfer = dataTypes[i];
138             int[] typeIds = transfer.getTypeIds();
139             String JavaDoc[] typeNames = transfer.getTypeNames();
140             for (int j = 0; j < typeIds.length; j++) {
141                 GtkTargetEntry entry = new GtkTargetEntry();
142                 entry.info = typeIds[j];
143                 byte[] buffer = Converter.wcsToMbcs(null, typeNames[j], true);
144                 int /*long*/ pName = OS.g_malloc(buffer.length);
145                 OS.memmove(pName, buffer, buffer.length);
146                 entry.target = pName;
147                 GtkTargetEntry[] tmp = new GtkTargetEntry [entries.length + 1];
148                 System.arraycopy(entries, 0, tmp, 0, entries.length);
149                 tmp[entries.length] = entry;
150                 entries = tmp;
151             }
152         }
153         
154         pTargetsList = OS.g_malloc(GtkTargetEntry.sizeof * entries.length);
155         int offset = 0;
156         for (int i = 0; i < entries.length; i++) {
157             OS.memmove(pTargetsList + offset, entries[i], GtkTargetEntry.sizeof);
158             offset += GtkTargetEntry.sizeof;
159         }
160         if ((clipboards & DND.CLIPBOARD) != 0) {
161             if (activeClipboard != null) OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
162             clipboardData = data;
163             clipboardDataTypes = dataTypes;
164             int /*long*/ getFuncProc = getFunc.getAddress();
165             int /*long*/ clearFuncProc = clearFunc.getAddress();
166             if (!OS.gtk_clipboard_set_with_data(Clipboard.GTKCLIPBOARD, pTargetsList, entries.length, getFuncProc, clearFuncProc, 0)) {
167                 return false;
168             }
169             activeClipboard = owner;
170         }
171         if ((clipboards & DND.SELECTION_CLIPBOARD) != 0) {
172             if (activePrimaryClipboard != null) OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
173             primaryClipboardData = data;
174             primaryClipboardDataTypes = dataTypes;
175             int /*long*/ getFuncProc = getFunc.getAddress();
176             int /*long*/ clearFuncProc = clearFunc.getAddress();
177             if (!OS.gtk_clipboard_set_with_data(Clipboard.GTKPRIMARYCLIPBOARD, pTargetsList, entries.length, getFuncProc, clearFuncProc, 0)) {
178                 return false;
179             }
180             activePrimaryClipboard = owner;
181         }
182         return true;
183     } finally {
184         for (int i = 0; i < entries.length; i++) {
185             GtkTargetEntry entry = entries[i];
186             if( entry.target != 0) OS.g_free(entry.target);
187         }
188         if (pTargetsList != 0) OS.g_free(pTargetsList);
189     }
190 }
191 }
192
Popular Tags