-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathRE_SetOleCallback.ahk
175 lines (171 loc) · 9.26 KB
/
RE_SetOleCallback.ahk
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
; Title:
; Link: https://www.autohotkey.com/boards/viewtopic.php?f=5&t=38385&p=176827
; Author:
; Date:
; for: AHK_L
/*
*/
;=========================================
; Name: RichEdit OleCallback
; Namespace: RichEdit
; Authors: just me & DigiDon
; Description: IRichEditOleCallback interface AHK implementation for the RichEdit control
;=========================================
;
;=========================================
;RE_SetOleCallback
;Need to call this function just after creation of the RichEdit control
;HRE - Handle of the RichEdit Control
;ex: RE_SetOleCallback(RE2.HWND)
;Specify your contextmenu in IREOleCB_GetContextMenu() if you have one because it won't be called otherwise
;and disable existing dropfiles special GUI label for the RichEdit Control
;Then you can start dragging and dropping any document into the RichEdit field.
;=========================================
RE_SetOleCallback(HRE) {
; EM_SETOLECALLBACK = 0x0446
SendMessage, 0x0446 , 0, % IREOleCB_Create() , , ahk_id %HRE%
If (ErrorLevel = "FAIL") || (ErrorLevel = 0) {
MsgBox, 16, %A_ThisFunc%, ERROR: %ErrorLevel%!
Return False
}
Return True
}
; ================================================================================================================================
; IRichEditOleCallback -> msdn.microsoft.com/en-us/library/windows/desktop/bb774308(v=vs.85).aspx
; ================================================================================================================================
IREOleCB_Create() {
Static VTBL := [RegisterCallback("IREOleCB_QueryInterface")
, RegisterCallback("IREOleCB_AddRef")
, RegisterCallback("IREOleCB_Release")
, RegisterCallback("IREOleCB_GetNewStorage")
, RegisterCallback("IREOleCB_GetInPlaceContext")
, RegisterCallback("IREOleCB_ShowContainerUI")
, RegisterCallback("IREOleCB_QueryInsertObject")
, RegisterCallback("IREOleCB_DeleteObject")
, RegisterCallback("IREOleCB_QueryAcceptData")
, RegisterCallback("IREOleCB_ContextSensitiveHelp")
, RegisterCallback("IREOleCB_GetClipboardData")
, RegisterCallback("IREOleCB_GetDragDropEffect")
, RegisterCallback("IREOleCB_GetContextMenu")]
Static HeapSize := A_PtrSize * 20 ; VTBL pointer + 13 method pointers + 4 unused pointers + reference count + HEAP handle
Static HeapOffset := A_PtrSize * 19 ; offset to store the heap handle within the heap
Heap := DllCall("HeapCreate", "UInt", 0x05, "Ptr", 0, "Ptr", 0, "UPtr")
IREOleCB := DllCall("HeapAlloc", "Ptr", Heap, "UInt", 0x08, "Ptr", HeapSize, "UPtr")
Addr := IREOleCB
Addr := NumPut(Addr + A_PtrSize, Addr + 0, "UPtr")
For Each, CB In VTBL
Addr := NumPut(CB, Addr + 0, "UPtr")
NumPut(Heap, IREOleCB + HeapOffset, "UPtr")
Return IREOleCB
}
; --------------------------------------------------------------------------------------------------------------------------------
; IUnknown::QueryInterface
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_QueryInterface(IREOleCB, REFIID, ByRef IFPtr) {
OutputDebug, %A_ThisFunc%
Return 0 ; S_OK
}
; --------------------------------------------------------------------------------------------------------------------------------
; IUnknown::AddRef
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_AddRef(IREOleCB) {
Static RefOffset := A_PtrSize * 18
OutputDebug, %A_ThisFunc%
NumPut(RefCount := NumGet(IREOleCB + RefOffset, "UInt") + 1, IREOleCB + RefOffset, "UInt")
Return RefCount
}
; --------------------------------------------------------------------------------------------------------------------------------
; IUnknown::Release
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_Release(IREOleCB) {
Static RefOffset := A_PtrSize * 18
, HeapOffset := A_PtrSize * 19
OutputDebug, %A_ThisFunc%
NumPut(RefCount := NumGet(IREOleCB + RefOffset, "UInt") - 1, IREOleCB + RefOffset, "UInt")
If (RefCount = 0) {
Heap := NumGet(IREOleCB + HeapOffset, "UPtr")
DllCall("HeapDestroy", "Ptr", Heap)
}
Return RefCount
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::GetNewStorage
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_GetNewStorage(IREOleCB, IStoragePtr) {
OutputDebug, %A_ThisFunc%
; msdn.microsoft.com/en-us/library/windows/desktop/aa378977(v=vs.85).aspx
If !(HR := DllCall("Ole32.dll\CreateILockBytesOnHGlobal", "Ptr", 0, "Int", 1, "PtrP", ILockBytes)) {
; msdn.microsoft.com/en-us/library/windows/desktop/aa380324(v=vs.85).aspx
; STGM_READWRITE = 0x02, STGM_SHARE_EXCLUSIVE = 0x10, STGM_CREATE = 0x1000
If (HR := DllCall("Ole32.dll\StgCreateDocfileOnILockBytes", "Ptr", ILockBytes, "UInt", 0x1012, "UInt", 0, "PtrP", IStorage))
ObjRelease(ILockBytes)
Else
NumPut(IStorage, IStoragePtr + 0, "UPtr")
}
Return HR
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::GetInPlaceContext - not implemented
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_GetInPlaceContext(IREOleCB, Frame, Doc, FrameInfo) {
OutputDebug, %A_ThisFunc%
Return 0x80004001 ; E_NOTIMPL
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::ShowContainerUI - not implemented
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_ShowContainerUI(IREOleCB, Show) {
OutputDebug, %A_ThisFunc%
Return 0x80004001 ; E_NOTIMPL
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::QueryInsertObject - returns S_OK
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_QueryInsertObject(IREOleCB, CLSID, STG, CP) {
OutputDebug, %A_ThisFunc%
Return 0 ; S_OK
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::DeleteObject - returns S_OK
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_DeleteObject(IREOleCB, OleObj) {
OutputDebug, %A_ThisFunc%
Return 0 ; S_OK
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::QueryAcceptData - returns S_OK
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_QueryAcceptData(IREOleCB, DataObj, Format, Operation, Really, MetaPic) {
OutputDebug, %A_ThisFunc%
Return 0 ; S_OK
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::ContextSensitiveHelp - not implemented
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_ContextSensitiveHelp(IREOleCB, EnterMode) {
OutputDebug, %A_ThisFunc%
Return 0x80004001 ; E_NOTIMPL
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::GetClipboardData - not implemented
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_GetClipboardData(IREOleCB, CharRange, Operation, DataObj) {
OutputDebug, %A_ThisFunc%
Return 0x80004001 ; E_NOTIMPL
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::GetDragDropEffect - returns S_OK
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_GetDragDropEffect(IREOleCB, Drag, KeyState, Effect) {
OutputDebug, %A_ThisFunc%
Return 0 ; S_OK
}
; --------------------------------------------------------------------------------------------------------------------------------
; IRichEditOleCallback::GetContextMenu - not implemented
; --------------------------------------------------------------------------------------------------------------------------------
IREOleCB_GetContextMenu(IREOleCB, SelType, OleObj, CharRange, HMENU) {
;PUT YOUR CONTEXT MNU HERE
;Menu, RN_ContextMenu, Show
; OutputDebug, %A_ThisFunc%
Return 0x80004001 ; E_NOTIMPL
}