Miscellaneous fixes to try to make other compilers happier
[u/mdw/putty] / sizetip.c
1 #include <windows.h>
2 #ifndef AUTO_WINSOCK
3 #ifdef WINSOCK_TWO
4 #include <winsock2.h>
5 #else
6 #include <winsock.h>
7 #endif
8 #endif
9 #include <winreg.h>
10 #include <tchar.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "putty.h"
15
16 static ATOM tip_class = 0;
17
18 static HFONT tip_font;
19 static COLORREF tip_bg;
20 static COLORREF tip_text;
21
22 static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
23 WPARAM wParam, LPARAM lParam)
24 {
25
26 switch (nMsg) {
27 case WM_ERASEBKGND:
28 return TRUE;
29
30 case WM_PAINT:
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
69 case WM_NCHITTEST:
70 return HTTRANSPARENT;
71
72 case WM_DESTROY:
73 DeleteObject(tip_font);
74 tip_font = NULL;
75 break;
76
77 case WM_SETTEXT:
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
97 static HWND tip_wnd = NULL;
98 static int tip_enabled = 0;
99
100 void UpdateSizeTip(HWND src, int cx, int cy)
101 {
102 TCHAR str[32];
103
104 if (!tip_enabled) return;
105
106 if (!tip_wnd) {
107 NONCLIENTMETRICS nci;
108
109 /* First make sure the window class is registered */
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
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
134
135 /* Prepare other GDI objects and drawing info */
136
137 tip_bg = GetSysColor(COLOR_INFOBK);
138 tip_text = GetSysColor(COLOR_INFOTEXT);
139
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
146 /* Generate the tip text */
147
148 sprintf(str, "%dx%d", cx, cy);
149
150 if (!tip_wnd) {
151 HDC hdc;
152 SIZE sz;
153 RECT wr;
154 int ix, iy;
155
156 /* calculate the tip's size */
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
170 /* Create the tip window */
171
172 tip_wnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST, MAKEINTRESOURCE(tip_class), str, WS_POPUP,
173 ix, iy, sz.cx, sz.cy,
174 NULL, NULL, putty_inst, NULL);
175
176 ShowWindow(tip_wnd, SW_SHOWNOACTIVATE);
177
178 } else {
179
180 /* Tip already exists, just set the text */
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 }
192
193 tip_enabled = bEnable;
194 }