summaryrefslogtreecommitdiff
path: root/plugins/!Deprecated/W7UI/src/subclassmgr.h
blob: 1cf2433254afeb4d7fda2d42720784e00f532c78 (plain)
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
#ifndef subclassmgr_h__
#define subclassmgr_h__

struct TSubclassData
{
	WNDPROC oldWndProc;
	LPARAM lParam;
};

typedef LRESULT (*TSubclassProc)(MSG *msg, TSubclassData *data);

class CSubclassMgr
{
public:
	static void Subclass(HWND hwnd, TSubclassProc newWndProc, LPARAM lParam)
	{
		TWindowInfo *wi = new TWindowInfo;
		wi->hwnd = hwnd;
		wi->newWndProc = newWndProc;
		wi->lParam = lParam;
		Instance().m_windows.insert(wi);
		wi->oldWndProc = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)GlobalSubclassProc);
	}

private:
	CSubclassMgr(): m_windows(5, TWindowInfo::Compare) {}
	CSubclassMgr(const CSubclassMgr &);
	CSubclassMgr &operator=(const CSubclassMgr &);

	static CSubclassMgr &Instance()
	{
		static CSubclassMgr theInstance;
		return theInstance;
	}

	struct TWindowInfo
	{
		HWND hwnd;
		WNDPROC oldWndProc;
		TSubclassProc newWndProc;
		LPARAM lParam;

		static int Compare(const TWindowInfo *p1, const TWindowInfo *p2)
		{
			return (int)p1->hwnd - (int)p2->hwnd;
		}
	};

	OBJLIST<TWindowInfo> m_windows;

	static LRESULT CALLBACK GlobalSubclassProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		TWindowInfo search = { hwnd };
		TWindowInfo *wnd = Instance().m_windows.find(&search);
		if (!wnd) return DefWindowProc(hwnd, message, wParam, lParam);

		MSG msg = { hwnd, message, wParam, lParam };
		TSubclassData data = { wnd->oldWndProc, wnd->lParam };
		LRESULT result = wnd->newWndProc(&msg, &data);

		if (message == WM_DESTROY)
		{
			SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)wnd->oldWndProc);
			Instance().m_windows.remove(wnd);
		}

		return result;
	}
};

#endif // subclassmgr_h__