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