2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 #include <afs/param.h>
16 #include <WINNT/subclass.h>
20 * PROTOTYPES _________________________________________________________________
24 HRESULT CALLBACK Subclass_WndProc (HWND hTarget, UINT msg, WPARAM wp, LPARAM lp);
28 * MISCELLANEOUS ______________________________________________________________
33 #define REALLOC(_a,_c,_r,_i) SubclassReallocFunction ((LPVOID*)&_a,sizeof(*_a),&_c,_r,_i)
34 BOOL SubclassReallocFunction (LPVOID *ppTarget, size_t cbElement, size_t *pcTarget, size_t cReq, size_t cInc)
39 if (cReq <= *pcTarget)
42 if ((cNew = cInc * ((cReq + cInc-1) / cInc)) <= 0)
45 if ((pNew = (LPVOID)GlobalAlloc (GMEM_FIXED, cbElement * cNew)) == NULL)
47 memset (pNew, 0x00, cbElement * cNew);
51 memcpy (pNew, *ppTarget, cbElement * (*pcTarget));
52 GlobalFree ((HGLOBAL)*ppTarget);
63 * VARIABLES __________________________________________________________________
69 HWND hTarget; // window being subclassed
70 PVOID procOrig; // original pre-SubclassProc proc
78 size_t nHooks; // number of entries in aHooks
79 size_t nHooksActive; // number of hooks in use
82 SubclassWindow *aTargets = NULL;
85 #define cREALLOC_SUBCLASS_TARGETS 8
86 #define cREALLOC_SUBCLASS_HOOKS 4
90 * ROUTINES ___________________________________________________________________
94 BOOL Subclass_AddHook (HWND hTarget, PVOID wndProc)
97 for (iTarget = 0; iTarget < nTargets; ++iTarget)
99 if (aTargets[ iTarget ].hTarget == hTarget)
102 if (iTarget >= nTargets)
104 for (iTarget = 0; iTarget < nTargets; ++iTarget)
106 if (aTargets[ iTarget ].hTarget == NULL)
110 if (iTarget >= nTargets)
112 if (!REALLOC (aTargets, nTargets, 1+iTarget, cREALLOC_SUBCLASS_TARGETS))
116 aTargets[ iTarget ].hTarget = hTarget;
119 for (iHook = 0; iHook < aTargets[iTarget].nHooks; ++iHook)
121 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc == wndProc)
124 if (iHook >= aTargets[ iTarget ].nHooks)
126 for (iHook = 0; iHook < aTargets[iTarget].nHooks; ++iHook)
128 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc == NULL)
132 if (iHook >= aTargets[ iTarget ].nHooks)
134 if (!REALLOC (aTargets[ iTarget ].aHooks, aTargets[ iTarget ].nHooks, 1+iHook, cREALLOC_SUBCLASS_HOOKS))
138 aTargets[ iTarget ].aHooks[ iHook ].wndProc = wndProc;
139 aTargets[ iTarget ].aHooks[ iHook ].nReq ++;
140 aTargets[ iTarget ].nHooksActive ++;
142 if (aTargets[ iTarget ].nHooksActive == 1)
144 aTargets[ iTarget ].procOrig = (PVOID)GetWindowLongPtr (hTarget, GWLP_WNDPROC);
145 SetWindowLongPtr (hTarget, GWLP_WNDPROC, PtrToLong(Subclass_WndProc));
152 void Subclass_RemoveHook (HWND hTarget, PVOID wndProc)
155 for (iTarget = 0; iTarget < nTargets; ++iTarget)
157 if (aTargets[ iTarget ].hTarget == hTarget)
160 if (iTarget < nTargets)
163 for (iHook = 0; iHook < aTargets[iTarget].nHooks; ++iHook)
165 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc == wndProc)
168 if (iHook < aTargets[ iTarget ].nHooks)
170 aTargets[ iTarget ].aHooks[ iHook ].nReq --;
171 if (aTargets[ iTarget ].aHooks[ iHook ].nReq == 0)
173 memset (&aTargets[ iTarget ].aHooks[ iHook ], 0x00, sizeof(aTargets[ iTarget ].aHooks[ iHook ]));
177 aTargets[ iTarget ].nHooksActive --;
178 if (aTargets[ iTarget ].nHooksActive == 0)
180 SetWindowLongPtr (aTargets[ iTarget ].hTarget, GWLP_WNDPROC, (LONG)aTargets[ iTarget ].procOrig);
181 memset (&aTargets[ iTarget ], 0x00, sizeof(aTargets[ iTarget ]));
187 PVOID Subclass_FindNextHook (HWND hTarget, PVOID wndProc)
190 for (iTarget = 0; iTarget < nTargets; ++iTarget)
192 if (aTargets[ iTarget ].hTarget == hTarget)
195 if (iTarget >= nTargets)
199 for (iHook = 0; iHook < aTargets[iTarget].nHooks; ++iHook)
201 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc == wndProc)
204 if (iHook >= aTargets[ iTarget ].nHooks)
205 return aTargets[ iTarget ].procOrig;
207 for (++iHook; iHook < aTargets[iTarget].nHooks; ++iHook)
209 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc != NULL)
210 return aTargets[ iTarget ].aHooks[ iHook ].wndProc;
213 return aTargets[ iTarget ].procOrig;
217 HRESULT CALLBACK Subclass_WndProc (HWND hTarget, UINT msg, WPARAM wp, LPARAM lp)
220 for (iTarget = 0; iTarget < nTargets; ++iTarget)
222 if (aTargets[ iTarget ].hTarget == hTarget)
225 if (iTarget >= nTargets)
226 return DefWindowProc (hTarget, msg, wp, lp);
229 for (iHook = 0; iHook < aTargets[iTarget].nHooks; ++iHook)
231 if (aTargets[ iTarget ].aHooks[ iHook ].wndProc != NULL)
235 if (iHook >= aTargets[iTarget].nHooks)
236 return CallWindowProc ((WNDPROC)aTargets[ iTarget ].procOrig, hTarget, msg, wp, lp);
238 return CallWindowProc ((WNDPROC)aTargets[ iTarget ].aHooks[ iHook ].wndProc, hTarget, msg, wp, lp);