Miscellaneous cleanups and reorgs in preparation for building
[u/mdw/putty] / sizetip.c
CommitLineData
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"
8c3cd914 15#include "winstuff.h"
73251d5d 16
75cab814 17static ATOM tip_class = 0;
73251d5d 18
75cab814 19static HFONT tip_font;
20static COLORREF tip_bg;
21static COLORREF tip_text;
73251d5d 22
75cab814 23static LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg,
24 WPARAM wParam, LPARAM lParam)
73251d5d 25{
26
27 switch (nMsg) {
b73fd1b1 28 case WM_ERASEBKGND:
73251d5d 29 return TRUE;
30
b73fd1b1 31 case WM_PAINT:
73251d5d 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
b73fd1b1 70 case WM_NCHITTEST:
73251d5d 71 return HTTRANSPARENT;
72
b73fd1b1 73 case WM_DESTROY:
73251d5d 74 DeleteObject(tip_font);
75 tip_font = NULL;
76 break;
77
b73fd1b1 78 case WM_SETTEXT:
73251d5d 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
75cab814 98static HWND tip_wnd = NULL;
99static int tip_enabled = 0;
73251d5d 100
101void UpdateSizeTip(HWND src, int cx, int cy)
102{
cfb1e4b1 103 TCHAR str[32];
73251d5d 104
105 if (!tip_enabled) return;
106
107 if (!tip_wnd) {
108 NONCLIENTMETRICS nci;
109
b73fd1b1 110 /* First make sure the window class is registered */
73251d5d 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;
8c3cd914 118 wc.hInstance = hinst;
73251d5d 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
b73fd1b1 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
73251d5d 135
b73fd1b1 136 /* Prepare other GDI objects and drawing info */
73251d5d 137
138 tip_bg = GetSysColor(COLOR_INFOBK);
139 tip_text = GetSysColor(COLOR_INFOTEXT);
b73fd1b1 140
73251d5d 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
b73fd1b1 147 /* Generate the tip text */
73251d5d 148
cfb1e4b1 149 sprintf(str, "%dx%d", cx, cy);
73251d5d 150
151 if (!tip_wnd) {
152 HDC hdc;
153 SIZE sz;
154 RECT wr;
155 int ix, iy;
156
b73fd1b1 157 /* calculate the tip's size */
73251d5d 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
b73fd1b1 171 /* Create the tip window */
73251d5d 172
173 tip_wnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST, MAKEINTRESOURCE(tip_class), str, WS_POPUP,
b73fd1b1 174 ix, iy, sz.cx, sz.cy,
8c3cd914 175 NULL, NULL, hinst, NULL);
73251d5d 176
177 ShowWindow(tip_wnd, SW_SHOWNOACTIVATE);
178
179 } else {
180
b73fd1b1 181 /* Tip already exists, just set the text */
73251d5d 182
183 SetWindowText(tip_wnd, str);
184 }
185}
186
187void EnableSizeTip(int bEnable)
188{
189 if (tip_wnd && !bEnable) {
190 DestroyWindow(tip_wnd);
191 tip_wnd = NULL;
192 }
b73fd1b1 193
73251d5d 194 tip_enabled = bEnable;
195}