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