summaryrefslogtreecommitdiff
path: root/plugins/TooltipNotify/src/Tooltip.cpp
blob: 014802da075625e3f8afd68fb89dc685e355043d (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
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
// Tooltip.cpp: implementation of the CTooltip class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

/*static*/ const wchar_t *CTooltip::s_szTooltipClass = L"MimTooltipNotify";

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTooltip::CTooltip(CTooltipNotify *pTooltipNotify)
{
	m_pTooltipNotify = pTooltipNotify;
	m_bLDblClick = DEF_SETTING_LDBLCLICK;
	m_hFont = nullptr;
	m_hWnd = nullptr;
	m_szText = nullptr;

	m_hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, s_szTooltipClass, nullptr, 
							WS_POPUP|WS_BORDER, 100, 100, 50, 50, nullptr, nullptr, 
							g_plugin.getInst(), nullptr);

	SetWindowLongPtr(m_hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
}


CTooltip::~CTooltip()
{
	if (m_hWnd) DestroyWindow(m_hWnd);
	if (m_hFont) DeleteObject(m_hFont);

	if (m_szText) free(m_szText);
}

/*static*/ void CTooltip::Initialize()
{
	WNDCLASSEX wcexWndClass = { 0 };
	wcexWndClass.cbSize = sizeof(WNDCLASSEX); 
	wcexWndClass.style = CS_SAVEBITS;
	wcexWndClass.lpfnWndProc = (WNDPROC)CTooltip::WindowProcWrapper;
	wcexWndClass.hInstance = g_plugin.getInst();
	wcexWndClass.lpszClassName	= s_szTooltipClass;
	RegisterClassEx(&wcexWndClass);
}

/*static*/ void CTooltip::Deinitialize()
{
	UnregisterClass(s_szTooltipClass, g_plugin.getInst());
}

LRESULT CALLBACK CTooltip::WindowProcWrapper(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	CTooltip* pThis = reinterpret_cast<CTooltip *>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
	return pThis->WindowProc(hWnd, message, wParam, lParam);
}



LRESULT CALLBACK CTooltip::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_CREATE:
			break;
		
		case WM_COMMAND:
			break;
		
		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hDC = BeginPaint(hWnd, &ps);

			RECT rect;
			GetClientRect(hWnd, &rect);
			
			rect.top += 1;
			rect.left += 1;
			rect.right -= 1;
			rect.bottom -= 1;

			SetBkMode(hDC, TRANSPARENT);
			SetTextColor(hDC, m_dwTextColor);
			SelectObject(hDC, m_hFont);
			DrawText(hDC, m_szText, -1, &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);

			EndPaint(hWnd, &ps);

			break;
		}

		case WM_ERASEBKGND:
		{
			RECT rect;
			GetClientRect(hWnd, &rect);
			HBRUSH hBgBrush = CreateSolidBrush(m_dwBgColor);
			FillRect((HDC)wParam, &rect, hBgBrush);
			DeleteObject(hBgBrush);

			return TRUE;
		}

		case WM_NCHITTEST:
		{
			UINT uHitTest = DefWindowProc(hWnd, message, wParam, lParam);
			if(uHitTest == HTCLIENT)
				return HTCAPTION;
			else
				return uHitTest;
		}

		case WM_NCLBUTTONDBLCLK:
		{
			m_pTooltipNotify->OnTooltipDblClicked(this);
			break;
		}

		case WM_SYSCOMMAND:
		{
			if (!m_pTooltipNotify->OnTooltipBeginMove(this))
				break;

			// doesn't return until the moving is complete
			DefWindowProc(hWnd, message, wParam, lParam);
			// DefWindowProc returned
			m_pTooltipNotify->OnTooltipEndMove(this);

			break;
		}

		case WM_DESTROY:
			break;
			
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;

}


void CTooltip::Validate()
{
	m_hFont = CreateFontIndirect(&m_lfFont);
	SIZE Size;
	HDC hDC = GetDC(m_hWnd);
	SelectObject(hDC, m_hFont);
	GetTextExtentPoint32(hDC, m_szText, (int)mir_wstrlen(m_szText), &Size);
	SetWindowPos(m_hWnd, nullptr, 0, 0, Size.cx+6, Size.cy+4, 
		SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOREDRAW);
	ReleaseDC(m_hWnd, hDC);
}


void CTooltip::Show()
{
	ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
}


void CTooltip::Hide()
{
	ShowWindow(m_hWnd, SW_HIDE);
}



void CTooltip::set_Translucency(BYTE bAlpha)
{
	if (SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, GetWindowLongPtr(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED) != 0)
		SetLayeredWindowAttributes(m_hWnd, RGB(0,0,0), bAlpha, LWA_ALPHA);
}

void CTooltip::set_TransparentInput(BOOL bOnOff)
{
	if (bOnOff)
		SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, GetWindowLongPtr(m_hWnd, GWL_EXSTYLE) | WS_EX_TRANSPARENT);
	else
		SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, GetWindowLongPtr(m_hWnd, GWL_EXSTYLE) & ~WS_EX_TRANSPARENT);		
}


void CTooltip::get_Rect(RECT *Rect) const
{
	GetWindowRect(m_hWnd, Rect);
}

void CTooltip::set_Position(INT x, INT y)
{
	SetWindowPos(m_hWnd, nullptr, x, y, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
}

void CTooltip::set_Text(const wchar_t* szText)
{
	if (m_szText) free(m_szText);
	m_szText = wcsdup(szText);
}