1 // Written by Christopher E. Miller 2 // See the included license.txt for copyright and license details. 3 4 5 /// Interfacing with the system clipboard for copy and paste operations. 6 module dfl.clipboard; 7 8 private import dfl.base, dfl.internal.winapi, dfl.data, dfl.internal.wincom, 9 dfl.internal.dlib; 10 11 12 /// 13 class Clipboard // docmain 14 { 15 private this() {} 16 17 18 static: 19 20 /// 21 dfl.data.IDataObject getDataObject() 22 { 23 dfl.internal.wincom.IDataObject comdobj; 24 if(S_OK != OleGetClipboard(&comdobj)) 25 throw new DflException("Unable to obtain clipboard data object"); 26 if(comdobj is comd) 27 return dd; 28 //delete dd; 29 comd = comdobj; 30 return dd = new ComToDdataObject(comdobj); 31 } 32 33 /// ditto 34 void setDataObject(Data obj, bool persist = false) 35 { 36 comd = null; 37 /+ 38 Object ddd; 39 ddd = cast(Object)dd; 40 delete ddd; 41 +/ 42 dd = null; 43 objref = null; 44 45 if(obj.info) 46 { 47 if(cast(TypeInfo_Class)obj.info) 48 { 49 Object foo; 50 foo = obj.getObject(); 51 52 /+ 53 if(cast(Bitmap)foo) 54 { 55 // ... 56 } 57 else +/ if(cast(dfl.data.IDataObject)foo) 58 { 59 dd = cast(dfl.data.IDataObject)foo; 60 objref = foo; 61 } 62 else 63 { 64 // Can't set any old class object. 65 throw new DflException("Unknown data object"); 66 } 67 } 68 else if(obj.info == typeid(dfl.data.IDataObject)) 69 { 70 dd = obj.getValue!(dfl.data.IDataObject)(); 71 objref = cast(Object)dd; 72 } 73 else if(cast(TypeInfo_Interface)obj.info) 74 { 75 // Can't set any old interface. 76 throw new DflException("Unknown data object"); 77 } 78 else 79 { 80 DataObject foo = new DataObject; 81 dd = foo; 82 objref = foo; 83 dd.setData(obj); 84 } 85 86 assert(!(dd is null)); 87 comd = new DtoComDataObject(dd); 88 if(S_OK != OleSetClipboard(comd)) 89 { 90 comd = null; 91 //delete dd; 92 dd = null; 93 goto err_set; 94 } 95 96 if(persist) 97 OleFlushClipboard(); 98 } 99 else 100 { 101 dd = null; 102 if(S_OK != OleSetClipboard(null)) 103 goto err_set; 104 } 105 106 return; 107 err_set: 108 throw new DflException("Unable to set clipboard data"); 109 } 110 111 /// ditto 112 void setDataObject(dfl.data.IDataObject obj, bool persist = false) 113 { 114 setDataObject(Data(obj), persist); 115 } 116 117 118 /// 119 void setString(Dstring str, bool persist = false) 120 { 121 setDataObject(Data(str), persist); 122 } 123 124 /// ditto 125 Dstring getString() 126 { 127 dfl.data.IDataObject ido; 128 ido = getDataObject(); 129 if(ido.getDataPresent(DataFormats.utf8)) 130 return ido.getData(DataFormats.utf8).getString(); 131 return null; // ? 132 } 133 134 135 /// 136 // ANSI text. 137 void setText(ubyte[] ansiText, bool persist = false) 138 { 139 setDataObject(Data(ansiText), persist); 140 } 141 142 /// ditto 143 ubyte[] getText() 144 { 145 dfl.data.IDataObject ido; 146 ido = getDataObject(); 147 if(ido.getDataPresent(DataFormats.text)) 148 return ido.getData(DataFormats.text).getText(); 149 return null; // ? 150 } 151 152 153 private: 154 dfl.internal.wincom.IDataObject comd; 155 dfl.data.IDataObject dd; 156 Object objref; // Prevent dd from being garbage collected! 157 158 159 /+ 160 static ~this() 161 { 162 Object ddd; 163 ddd = cast(Object)dd; 164 delete ddd; 165 } 166 +/ 167 } 168