1 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 28 Object [] clipboardData; 29 Transfer[] clipboardDataTypes; 30 Object [] 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 ID = "CLIPBOARD PROXY OBJECT"; 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); if (getFunc.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS); 61 clearFunc = new Callback( this, "clearFunc", 2); 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 clearFunc(int clipboard,int 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 107 int getFunc( int clipboard, int selection_data, int info, int 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 [] 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 [] data, Transfer[] dataTypes, int clipboards) { 133 GtkTargetEntry[] entries = new GtkTargetEntry [0]; 134 int 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 [] 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 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 getFuncProc = getFunc.getAddress(); 165 int 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 getFuncProc = getFunc.getAddress(); 176 int 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 |