OSDN Git Service

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