1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251:
| unit DirectRTF;
interface
uses Windows, SysUtils, Types, Classes, RichEdit, RxRichEd;
type TEditStreamCallBack = function(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall;
TEditStream = record dwCookie: Longint; dwError: Longint; pfnCallback: TEditStreamCallBack; end;
const EditStreamCookieDoOut = 0; EditStreamCookieDoIn = 1;
type TMyRichEdit = TRxRichEdit;
var EditStreamCallBackData: PChar; EditStreamCallBackPos: Longint;
procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream); function StreamOutRtf(const RichEdit: HWND; out Stream: PChar; const Sel: boolean): Cardinal; function StreamInRtf(const RichEdit: HWND; const Stream: PChar): Cardinal;
implementation
function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall; var theStream: TStream; dataAvail: LongInt; begin theStream := TStream(dwCookie); with theStream do begin dataAvail := Size - Position; Result := 0; if dataAvail <= cb then begin pcb := read(pbBuff^, dataAvail); if pcb <> dataAvail then Result := UINT(E_FAIL); end else begin pcb := read(pbBuff^, cb); if pcb <> cb then Result := UINT(E_FAIL); end; end; end;
function EditStreamCallBack(dwCookie: Longint; pbBuff: PByte; cb: Longint; var pcb: Longint): DWORD; stdcall; var Size: Integer; Data: PChar; begin case dwCookie of EditStreamCookieDoOut: begin if EditStreamCallBackData = nil then begin Data := GetMemory(cb); if Data <> nil then try CopyMemory(Data, pbBuff, cb); EditStreamCallBackData := Data; EditStreamCallBackPos := cb; pcb := cb; Result := ERROR_SUCCESS; except Result := ERROR_CANNOT_COPY; end else begin Result := ERROR_NOT_ENOUGH_MEMORY; end; end else begin Data := GetMemory(EditStreamCallBackPos + cb); if Data <> nil then try CopyMemory(Data, EditStreamCallBackData, EditStreamCallBackPos); CopyMemory(@Data[EditStreamCallBackPos], pbBuff, cb); FreeMemory(EditStreamCallBackData); EditStreamCallBackData := Data; EditStreamCallBackPos := EditStreamCallBackPos + cb; pcb := cb; Result := ERROR_SUCCESS; except Result := ERROR_CANNOT_COPY; end else begin Result := ERROR_NOT_ENOUGH_MEMORY; end; end; end; EditStreamCookieDoIn: begin if EditStreamCallBackData <> nil then begin Size := lstrlen(EditStreamCallBackData) + 1 - EditStreamCallBackPos; if Size > 0 then begin if cb < Size then pcb := cb else pcb := Size; try CopyMemory(pbBuff, @EditStreamCallBackData[EditStreamCallBackPos], pcb); EditStreamCallBackPos := EditStreamCallBackPos + pcb; Result := ERROR_SUCCESS; except Result := ERROR_CANNOT_COPY; end; end else begin Result := ERROR_INSUFFICIENT_BUFFER; end; end else begin Result := ERROR_NO_DATA; end; end; else Result := ERROR_INVALID_PARAMETER; end; end;
procedure PutRTFSelection(RichEdit: TMyRichEdit; SourceStream: TStream); var EditStream: TEditStream; begin with EditStream do begin dwCookie := Longint(SourceStream); dwError := 0; pfnCallback := EditStreamInCallBack; end; RichEdit.Perform(EM_STREAMIN, SF_RTF or SFF_SELECTION, Longint(@EditStream)); end;
function StreamOutRtf(const RichEdit: HWND; out Stream: PChar; const Sel: boolean): Cardinal; var EditStream: TEditStream; begin if (RichEdit <> 0) and IsWindow(RichEdit) then begin if (EditStreamCallBackData = nil) then try EditStream.dwCookie := EditStreamCookieDoOut; EditStream.dwError := ERROR_NO_DATA; EditStream.pfnCallback := EditStreamCallBack; if Sel then SendMessage(RichEdit, EM_STREAMOUT, SF_RTF or SFF_SELECTION, lParam(@EditStream)) else SendMessage(RichEdit, EM_STREAMOUT, SF_RTF, lParam(@EditStream)); Result := EditStream.dwError; if Result <> ERROR_SUCCESS then begin if EditStreamCallBackData <> nil then FreeMemory(EditStreamCallBackData); end else begin Stream := GetMemory(EditStreamCallBackPos + 1); if Stream <> nil then try ZeroMemory(Stream, EditStreamCallBackPos + 1); CopyMemory(Stream, EditStreamCallBackData, EditStreamCallBackPos); except FreeMemory(Stream); Stream := nil; Result := ERROR_CANNOT_COPY; end else begin Result := ERROR_NOT_ENOUGH_MEMORY; end; if Result <> ERROR_SUCCESS then FreeMemory(EditStreamCallBackData); end; finally EditStreamCallBackData := nil; EditStreamCallBackPos := 0; end else begin Result := ERROR_NOT_READY; end; end else begin Result := ERROR_INVALID_PARAMETER; end; end;
function StreamInRtf(const RichEdit: HWND; const Stream: PChar): Cardinal; var EditStream: TEditStream; begin if (RichEdit <> 0) and IsWindow(RichEdit) and (Stream <> nil) then begin if (EditStreamCallBackData = nil) then try EditStreamCallBackData := Stream; EditStreamCallBackPos := 0; EditStream.dwCookie := EditStreamCookieDoIn; EditStream.dwError := ERROR_NO_DATA; EditStream.pfnCallback := EditStreamCallBack; SendMessage(RichEdit, EM_STREAMIN, SF_RTF, lParam(@EditStream)); Result := EditStream.dwError; finally EditStreamCallBackData := nil; EditStreamCallBackPos := 0; end else begin Result := ERROR_NOT_READY; end; end else begin Result := ERROR_INVALID_PARAMETER; end; end;
end. |