73251d5d |
1 | #include <stdio.h> |
1d470ad2 |
2 | #include <stdlib.h> |
dc3c8261 |
3 | #include <tchar.h> |
73251d5d |
4 | |
5 | #include "putty.h" |
6 | |
75cab814 |
7 | static ATOM tip_class = 0; |
73251d5d |
8 | |
75cab814 |
9 | static HFONT tip_font; |
10 | static COLORREF tip_bg; |
11 | static COLORREF tip_text; |
73251d5d |
12 | |
75cab814 |
13 | static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg, |
32874aea |
14 | WPARAM wParam, LPARAM lParam) |
73251d5d |
15 | { |
16 | |
17 | switch (nMsg) { |
b73fd1b1 |
18 | case WM_ERASEBKGND: |
32874aea |
19 | return TRUE; |
73251d5d |
20 | |
b73fd1b1 |
21 | case WM_PAINT: |
32874aea |
22 | { |
23 | HBRUSH hbr; |
24 | HGDIOBJ holdbr; |
25 | RECT cr; |
26 | int wtlen; |
27 | LPTSTR wt; |
28 | HDC hdc; |
73251d5d |
29 | |
32874aea |
30 | PAINTSTRUCT ps; |
31 | hdc = BeginPaint(hWnd, &ps); |
73251d5d |
32 | |
32874aea |
33 | SelectObject(hdc, tip_font); |
34 | SelectObject(hdc, GetStockObject(BLACK_PEN)); |
73251d5d |
35 | |
32874aea |
36 | hbr = CreateSolidBrush(tip_bg); |
37 | holdbr = SelectObject(hdc, hbr); |
73251d5d |
38 | |
32874aea |
39 | GetClientRect(hWnd, &cr); |
40 | Rectangle(hdc, cr.left, cr.top, cr.right, cr.bottom); |
73251d5d |
41 | |
32874aea |
42 | wtlen = GetWindowTextLength(hWnd); |
3d88e64d |
43 | wt = (LPTSTR) snewn(wtlen + 1, TCHAR); |
32874aea |
44 | GetWindowText(hWnd, wt, wtlen + 1); |
73251d5d |
45 | |
32874aea |
46 | SetTextColor(hdc, tip_text); |
47 | SetBkColor(hdc, tip_bg); |
73251d5d |
48 | |
32874aea |
49 | TextOut(hdc, cr.left + 3, cr.top + 3, wt, wtlen); |
73251d5d |
50 | |
32874aea |
51 | sfree(wt); |
73251d5d |
52 | |
32874aea |
53 | SelectObject(hdc, holdbr); |
54 | DeleteObject(hbr); |
73251d5d |
55 | |
32874aea |
56 | EndPaint(hWnd, &ps); |
57 | } |
58 | return 0; |
73251d5d |
59 | |
b73fd1b1 |
60 | case WM_NCHITTEST: |
32874aea |
61 | return HTTRANSPARENT; |
73251d5d |
62 | |
b73fd1b1 |
63 | case WM_DESTROY: |
32874aea |
64 | DeleteObject(tip_font); |
65 | tip_font = NULL; |
66 | break; |
73251d5d |
67 | |
b73fd1b1 |
68 | case WM_SETTEXT: |
32874aea |
69 | { |
70 | LPCTSTR str = (LPCTSTR) lParam; |
71 | SIZE sz; |
72 | HDC hdc = CreateCompatibleDC(NULL); |
73251d5d |
73 | |
32874aea |
74 | SelectObject(hdc, tip_font); |
75 | GetTextExtentPoint32(hdc, str, _tcslen(str), &sz); |
73251d5d |
76 | |
32874aea |
77 | SetWindowPos(hWnd, NULL, 0, 0, sz.cx + 6, sz.cy + 6, |
78 | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE); |
79 | InvalidateRect(hWnd, NULL, FALSE); |
73251d5d |
80 | |
32874aea |
81 | DeleteDC(hdc); |
82 | } |
83 | break; |
73251d5d |
84 | } |
85 | |
86 | return DefWindowProc(hWnd, nMsg, wParam, lParam); |
87 | } |
88 | |
75cab814 |
89 | static HWND tip_wnd = NULL; |
90 | static int tip_enabled = 0; |
73251d5d |
91 | |
92 | void UpdateSizeTip(HWND src, int cx, int cy) |
93 | { |
cfb1e4b1 |
94 | TCHAR str[32]; |
73251d5d |
95 | |
32874aea |
96 | if (!tip_enabled) |
97 | return; |
73251d5d |
98 | |
99 | if (!tip_wnd) { |
32874aea |
100 | NONCLIENTMETRICS nci; |
101 | |
102 | /* First make sure the window class is registered */ |
103 | |
104 | if (!tip_class) { |
105 | WNDCLASS wc; |
106 | wc.style = CS_HREDRAW | CS_VREDRAW; |
107 | wc.lpfnWndProc = SizeTipWndProc; |
108 | wc.cbClsExtra = 0; |
109 | wc.cbWndExtra = 0; |
110 | wc.hInstance = hinst; |
111 | wc.hIcon = NULL; |
112 | wc.hCursor = NULL; |
113 | wc.hbrBackground = NULL; |
114 | wc.lpszMenuName = NULL; |
115 | wc.lpszClassName = "SizeTipClass"; |
116 | |
117 | tip_class = RegisterClass(&wc); |
118 | } |
b73fd1b1 |
119 | #if 0 |
32874aea |
120 | /* Default values based on Windows Standard color scheme */ |
b73fd1b1 |
121 | |
32874aea |
122 | tip_font = GetStockObject(SYSTEM_FONT); |
123 | tip_bg = RGB(255, 255, 225); |
124 | tip_text = RGB(0, 0, 0); |
b73fd1b1 |
125 | #endif |
73251d5d |
126 | |
32874aea |
127 | /* Prepare other GDI objects and drawing info */ |
73251d5d |
128 | |
32874aea |
129 | tip_bg = GetSysColor(COLOR_INFOBK); |
130 | tip_text = GetSysColor(COLOR_INFOTEXT); |
b73fd1b1 |
131 | |
32874aea |
132 | memset(&nci, 0, sizeof(NONCLIENTMETRICS)); |
133 | nci.cbSize = sizeof(NONCLIENTMETRICS); |
134 | SystemParametersInfo(SPI_GETNONCLIENTMETRICS, |
135 | sizeof(NONCLIENTMETRICS), &nci, 0); |
136 | tip_font = CreateFontIndirect(&nci.lfStatusFont); |
73251d5d |
137 | } |
138 | |
b73fd1b1 |
139 | /* Generate the tip text */ |
73251d5d |
140 | |
cfb1e4b1 |
141 | sprintf(str, "%dx%d", cx, cy); |
73251d5d |
142 | |
143 | if (!tip_wnd) { |
32874aea |
144 | HDC hdc; |
145 | SIZE sz; |
146 | RECT wr; |
147 | int ix, iy; |
73251d5d |
148 | |
32874aea |
149 | /* calculate the tip's size */ |
73251d5d |
150 | |
32874aea |
151 | hdc = CreateCompatibleDC(NULL); |
152 | GetTextExtentPoint32(hdc, str, _tcslen(str), &sz); |
153 | DeleteDC(hdc); |
73251d5d |
154 | |
32874aea |
155 | GetWindowRect(src, &wr); |
73251d5d |
156 | |
32874aea |
157 | ix = wr.left; |
158 | if (ix < 16) |
159 | ix = 16; |
73251d5d |
160 | |
32874aea |
161 | iy = wr.top - sz.cy; |
162 | if (iy < 16) |
163 | iy = 16; |
73251d5d |
164 | |
32874aea |
165 | /* Create the tip window */ |
73251d5d |
166 | |
32874aea |
167 | tip_wnd = |
168 | CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, |
169 | MAKEINTRESOURCE(tip_class), str, WS_POPUP, ix, |
170 | iy, sz.cx, sz.cy, NULL, NULL, hinst, NULL); |
73251d5d |
171 | |
32874aea |
172 | ShowWindow(tip_wnd, SW_SHOWNOACTIVATE); |
73251d5d |
173 | |
174 | } else { |
175 | |
32874aea |
176 | /* Tip already exists, just set the text */ |
73251d5d |
177 | |
32874aea |
178 | SetWindowText(tip_wnd, str); |
73251d5d |
179 | } |
180 | } |
181 | |
182 | void EnableSizeTip(int bEnable) |
183 | { |
184 | if (tip_wnd && !bEnable) { |
32874aea |
185 | DestroyWindow(tip_wnd); |
186 | tip_wnd = NULL; |
73251d5d |
187 | } |
b73fd1b1 |
188 | |
73251d5d |
189 | tip_enabled = bEnable; |
190 | } |