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