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