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