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