25
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.dnd.ClipboardProxy;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17
|
|
18
|
|
19 import org.eclipse.swt.SWT;
|
|
20 import org.eclipse.swt.internal.gtk.OS;
|
|
21 import org.eclipse.swt.widgets.Display;
|
|
22 import org.eclipse.swt.widgets.Event;
|
|
23 import org.eclipse.swt.widgets.Listener;
|
|
24 import org.eclipse.swt.dnd.Transfer;
|
|
25 import org.eclipse.swt.dnd.Clipboard;
|
|
26 import org.eclipse.swt.dnd.DND;
|
|
27 import org.eclipse.swt.dnd.TransferData;
|
|
28
|
|
29 static import tango.stdc.string;
|
|
30
|
|
31
|
|
32 class ClipboardProxy {
|
|
33 /* Data is not flushed to the clipboard immediately.
|
|
34 * This class will remember the data and provide it when requested.
|
|
35 */
|
|
36 Object[] clipboardData;
|
|
37 Transfer[] clipboardDataTypes;
|
|
38 Object[] primaryClipboardData;
|
|
39 Transfer[] primaryClipboardDataTypes;
|
|
40
|
|
41 Display display;
|
|
42 Clipboard activeClipboard = null;
|
|
43 Clipboard activePrimaryClipboard = null;
|
|
44
|
|
45 static String ID = "CLIPBOARD PROXY OBJECT"; //$NON-NLS-1$
|
|
46
|
|
47 static ClipboardProxy _getInstance(Display display) {
|
|
48 ClipboardProxy proxy = cast(ClipboardProxy) display.getData(ID);
|
|
49 if (proxy !is null) return proxy;
|
|
50 proxy = new ClipboardProxy(display);
|
|
51 display.setData(ID, proxy);
|
|
52 display.addListener(SWT.Dispose, new class( display ) Listener {
|
|
53 Display disp;
|
|
54 this( Display disp ){ this.disp = disp; }
|
|
55 public void handleEvent(Event event) {
|
|
56 ClipboardProxy clipbordProxy = cast(ClipboardProxy)disp.getData(ID);
|
|
57 if (clipbordProxy is null) return;
|
|
58 disp.setData(ID, null);
|
|
59 clipbordProxy.dispose();
|
|
60 }
|
|
61 });
|
|
62 return proxy;
|
|
63 }
|
|
64
|
|
65 this(Display display) {
|
|
66 this.display = display;
|
|
67 }
|
|
68
|
|
69 void clear (Clipboard owner, int clipboards) {
|
|
70 if ((clipboards & DND.CLIPBOARD) !is 0 && activeClipboard is owner) {
|
|
71 OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
|
|
72 }
|
|
73 if ((clipboards & DND.SELECTION_CLIPBOARD) !is 0 && activePrimaryClipboard is owner) {
|
|
74 OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
|
|
75 }
|
|
76 }
|
|
77
|
|
78 private static extern(C) void clearFuncFunc(GtkClipboard *clipboard, void* user_data_or_owner){
|
|
79 auto obj = cast(ClipboardProxy)user_data_or_owner;
|
|
80 obj.clearFunc( clipboard );
|
|
81 }
|
|
82 void clearFunc(GtkClipboard *clipboard ){
|
|
83 if (clipboard is Clipboard.GTKCLIPBOARD) {
|
|
84 activeClipboard = null;
|
|
85 clipboardData = null;
|
|
86 clipboardDataTypes = null;
|
|
87 }
|
|
88 if (clipboard is Clipboard.GTKPRIMARYCLIPBOARD) {
|
|
89 activePrimaryClipboard = null;
|
|
90 primaryClipboardData = null;
|
|
91 primaryClipboardDataTypes = null;
|
|
92 }
|
|
93 }
|
|
94
|
|
95 void dispose () {
|
|
96 if (display is null) return;
|
|
97 if (activeClipboard !is null) OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
|
|
98 if (activePrimaryClipboard !is null) OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
|
|
99 display = null;
|
|
100 clipboardData = null;
|
|
101 clipboardDataTypes = null;
|
|
102 primaryClipboardData = null;
|
|
103 primaryClipboardDataTypes = null;
|
|
104 }
|
|
105
|
|
106 private static extern(C) void getFuncFunc(
|
|
107 GtkClipboard *clipboard,
|
|
108 GtkSelectionData *selection_data,
|
|
109 uint info,
|
|
110 void* user_data_or_owner)
|
|
111 {
|
|
112 auto obj = cast(ClipboardProxy)user_data_or_owner;
|
|
113 obj.getFunc( clipboard, selection_data, info );
|
|
114 }
|
|
115 /**
|
|
116 * This function provides the data to the clipboard on request.
|
|
117 * When this clipboard is disposed, the data will no longer be available.
|
|
118 */
|
|
119 void getFunc(
|
|
120 GtkClipboard *clipboard,
|
|
121 GtkSelectionData *selectionData,
|
|
122 uint info)
|
|
123 {
|
|
124 if (selectionData is null) return 0;
|
|
125 TransferData tdata = new TransferData();
|
|
126 tdata.type = selectionData.target;
|
|
127 Transfer[] types = (clipboard is Clipboard.GTKCLIPBOARD) ? clipboardDataTypes : primaryClipboardDataTypes;
|
|
128 int index = -1;
|
|
129 for (int i = 0; i < types.length; i++) {
|
|
130 if (types[i].isSupportedType(tdata)) {
|
|
131 index = i;
|
|
132 break;
|
|
133 }
|
|
134 }
|
|
135 if (index is -1) return 0;
|
|
136 Object[] data = (clipboard is Clipboard.GTKCLIPBOARD) ? clipboardData : primaryClipboardData;
|
|
137 types[index].javaToNative(data[index], tdata);
|
|
138 if (tdata.format < 8 || tdata.format % 8 !is 0) {
|
|
139 return 0;
|
|
140 }
|
|
141 OS.gtk_selection_data_set(selectionData, tdata.type, tdata.format, tdata.pValue, tdata.length);
|
|
142 OS.g_free(tdata.pValue);
|
|
143 return 1;
|
|
144 }
|
|
145
|
|
146 bool setData(Clipboard owner, Object[] data, Transfer[] dataTypes, int clipboards) {
|
|
147 GtkTargetEntry*[] entries;
|
|
148 GtkTargetEntry* pTargetsList;
|
|
149 try {
|
|
150 for (int i = 0; i < dataTypes.length; i++) {
|
|
151 Transfer transfer = dataTypes[i];
|
|
152 int[] typeIds = transfer.getTypeIds();
|
|
153 String[] typeNames = transfer.getTypeNames();
|
|
154 for (int j = 0; j < typeIds.length; j++) {
|
|
155 GtkTargetEntry* entry = new GtkTargetEntry();
|
|
156 entry.info = typeIds[j];
|
|
157 char* pName = cast(char*)OS.g_malloc(typeNames[j].length+1);
|
|
158 pName[ 0 .. typeNames[j].length ] = typeNames[j];
|
|
159 pName[ typeNames[j].length ] = '\0';
|
|
160 entry.target = pName;
|
|
161 GtkTargetEntry*[] tmp = new GtkTargetEntry*[entries.length + 1];
|
|
162 SimpleType!(GtkTargetEntry*).arraycopy(entries, 0, tmp, 0, entries.length);
|
|
163 tmp[entries.length] = entry;
|
|
164 entries = tmp;
|
|
165 }
|
|
166 }
|
|
167
|
|
168 pTargetsList = cast(GtkTargetEntry*)OS.g_malloc(GtkTargetEntry.sizeof * entries.length);
|
|
169 int offset = 0;
|
|
170 for (int i = 0; i < entries.length; i++) {
|
|
171 tango.stdc.string.memmove(pTargetsList + i, entries[i], GtkTargetEntry.sizeof);
|
|
172 offset += GtkTargetEntry.sizeof;
|
|
173 }
|
|
174 if ((clipboards & DND.CLIPBOARD) !is 0) {
|
|
175 if (activeClipboard !is null) OS.gtk_clipboard_clear(Clipboard.GTKCLIPBOARD);
|
|
176 clipboardData = data;
|
|
177 clipboardDataTypes = dataTypes;
|
|
178 if (!OS.gtk_clipboard_set_with_data(Clipboard.GTKCLIPBOARD, pTargetsList, entries.length, &getFuncFunc, &clearFuncFunc, cast(void*)this )) {
|
|
179 return false;
|
|
180 }
|
|
181 activeClipboard = owner;
|
|
182 }
|
|
183 if ((clipboards & DND.SELECTION_CLIPBOARD) !is 0) {
|
|
184 if (activePrimaryClipboard !is null) OS.gtk_clipboard_clear(Clipboard.GTKPRIMARYCLIPBOARD);
|
|
185 primaryClipboardData = data;
|
|
186 primaryClipboardDataTypes = dataTypes;
|
|
187 if (!OS.gtk_clipboard_set_with_data(Clipboard.GTKPRIMARYCLIPBOARD, pTargetsList, entries.length, &getFuncFunc, &clearFuncFunc, cast(void*)this )) {
|
|
188 return false;
|
|
189 }
|
|
190 activePrimaryClipboard = owner;
|
|
191 }
|
|
192 return true;
|
|
193 } finally {
|
|
194 for (int i = 0; i < entries.length; i++) {
|
|
195 GtkTargetEntry* entry = entries[i];
|
|
196 if( entry.target !is null) OS.g_free(entry.target);
|
|
197 }
|
|
198 if (pTargetsList !is null) OS.g_free(pTargetsList);
|
|
199 }
|
|
200 }
|
|
201 }
|