John Sullivan's sizetip patch - experimental
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 3 Nov 1999 14:08:26 +0000 (14:08 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Wed, 3 Nov 1999 14:08:26 +0000 (14:08 +0000)
git-svn-id: svn://svn.tartarus.org/sgt/putty@269 cda61777-01e9-0310-a592-d414129be87e

putty.h
sizetip.c [new file with mode: 0644]
sizetip.h [new file with mode: 0644]
window.c

diff --git a/putty.h b/putty.h
index 24e64d4..6999d3e 100644 (file)
--- a/putty.h
+++ b/putty.h
@@ -14,6 +14,8 @@
 #define GLOBAL extern
 #endif
 
+GLOBAL HINSTANCE putty_inst;
+
 #define ATTR_ACTCURS 0x80000000UL      /* active cursor (block) */
 #define ATTR_PASCURS 0x40000000UL      /* passive cursor (box) */
 #define ATTR_INVALID 0x20000000UL
diff --git a/sizetip.c b/sizetip.c
new file mode 100644 (file)
index 0000000..20ab423
--- /dev/null
+++ b/sizetip.c
@@ -0,0 +1,185 @@
+
+#include <windows.h>
+#include <winreg.h>
+#include <tchar.h>
+#include <stdio.h>
+
+#include "putty.h"
+
+ATOM tip_class = 0;
+
+HFONT tip_font;
+COLORREF tip_bg;
+COLORREF tip_text;
+
+LRESULT CALLBACK SizeTipWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
+{
+
+    switch (nMsg) {
+    case WM_ERASEBKGND:
+        return TRUE;
+
+    case WM_PAINT:
+        {
+            HBRUSH hbr;
+            HGDIOBJ holdbr;
+            RECT cr;
+            int wtlen;
+            LPTSTR wt;
+            HDC hdc;
+
+            PAINTSTRUCT ps;
+            hdc = BeginPaint(hWnd, &ps);
+
+            SelectObject(hdc, tip_font);
+            SelectObject(hdc, GetStockObject(BLACK_PEN));
+
+            hbr = CreateSolidBrush(tip_bg);
+            holdbr = SelectObject(hdc, hbr);
+
+            GetClientRect(hWnd, &cr);
+            Rectangle(hdc, cr.left, cr.top, cr.right, cr.bottom);
+
+            wtlen = GetWindowTextLength(hWnd);
+            wt = (LPTSTR)malloc((wtlen+1)*sizeof(TCHAR));
+            GetWindowText(hWnd, wt, wtlen+1);
+
+            SetTextColor(hdc, tip_text);
+            SetBkColor(hdc, tip_bg);
+
+            TextOut(hdc, cr.left+3, cr.top+3, wt, wtlen);
+
+            free(wt);
+
+            SelectObject(hdc, holdbr);
+            DeleteObject(hbr);
+
+            EndPaint(hWnd, &ps);
+        }
+        return 0;
+
+    case WM_NCHITTEST:
+        return HTTRANSPARENT;
+
+    case WM_DESTROY:
+        DeleteObject(tip_font);
+        tip_font = NULL;
+        break;
+
+    case WM_SETTEXT:
+        {
+            LPCTSTR str = (LPCTSTR)lParam;
+            SIZE sz;
+            HDC hdc = CreateCompatibleDC(NULL);
+
+            SelectObject(hdc, tip_font);
+            GetTextExtentPoint32(hdc, str, _tcslen(str), &sz);
+
+            SetWindowPos(hWnd, NULL, 0, 0, sz.cx+6, sz.cy+6, SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE);
+            InvalidateRect(hWnd, NULL, FALSE);
+
+            DeleteDC(hdc);
+        }
+        break;
+    }
+
+    return DefWindowProc(hWnd, nMsg, wParam, lParam);
+}
+
+HWND tip_wnd = NULL;
+int tip_enabled = 0;
+
+void UpdateSizeTip(HWND src, int cx, int cy)
+{
+    TCHAR str[16];
+
+    if (!tip_enabled) return;
+
+    if (!tip_wnd) {
+        NONCLIENTMETRICS nci;
+
+        // First make sure the window class is registered
+
+        if (!tip_class) {
+            WNDCLASS wc;
+            wc.style = CS_HREDRAW|CS_VREDRAW;
+            wc.lpfnWndProc = SizeTipWndProc;
+            wc.cbClsExtra = 0;
+            wc.cbWndExtra = 0;
+            wc.hInstance = putty_inst;
+            wc.hIcon = NULL;
+            wc.hCursor = NULL;
+            wc.hbrBackground = NULL;
+            wc.lpszMenuName = NULL;
+            wc.lpszClassName = "SizeTipClass";
+
+            tip_class = RegisterClass(&wc);
+        }
+
+//        // Default values based on Windows Standard color scheme
+//
+//        tip_font = GetStockObject(SYSTEM_FONT);
+//        tip_bg = RGB(255, 255, 225);
+//        tip_text = RGB(0, 0, 0);
+
+        // Prepare other GDI objects and drawing info
+
+        tip_bg = GetSysColor(COLOR_INFOBK);
+        tip_text = GetSysColor(COLOR_INFOTEXT);
+        
+        memset(&nci, 0, sizeof(NONCLIENTMETRICS));
+        nci.cbSize = sizeof(NONCLIENTMETRICS);
+        SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &nci, 0);
+        tip_font = CreateFontIndirect(&nci.lfStatusFont);
+    }
+
+    // Generate the tip text
+
+    _sntprintf(str, 16, "%dx%d", cx, cy);
+
+
+    if (!tip_wnd) {
+        HDC hdc;
+        SIZE sz;
+        RECT wr;
+        int ix, iy;
+
+        // calculate the tip's size
+
+        hdc = CreateCompatibleDC(NULL);
+        GetTextExtentPoint32(hdc, str, _tcslen(str), &sz);
+        DeleteDC(hdc);
+
+        GetWindowRect(src, &wr);
+
+        ix = wr.left;
+        if (ix<16) ix = 16;
+
+        iy = wr.top - sz.cy;
+        if (iy<16) iy = 16;
+
+        // Create the tip window
+
+        tip_wnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST, MAKEINTRESOURCE(tip_class), str, WS_POPUP,
+                                   ix, iy, sz.cx, sz.cy,
+                                   NULL, NULL, putty_inst, NULL);
+
+        ShowWindow(tip_wnd, SW_SHOWNOACTIVATE);
+
+    } else {
+
+        // Tip already exists, just set the text
+
+        SetWindowText(tip_wnd, str);
+    }
+}
+
+void EnableSizeTip(int bEnable)
+{
+    if (tip_wnd && !bEnable) {
+        DestroyWindow(tip_wnd);
+        tip_wnd = NULL;
+    }
+    
+    tip_enabled = bEnable;
+}
diff --git a/sizetip.h b/sizetip.h
new file mode 100644 (file)
index 0000000..c7ac3ae
--- /dev/null
+++ b/sizetip.h
@@ -0,0 +1,3 @@
+\r
+void UpdateSizeTip(HWND src, int cx, int cy);\r
+void EnableSizeTip(int bEnable);\r
index 6ca4b48..8c82af9 100644 (file)
--- a/window.c
+++ b/window.c
@@ -7,6 +7,7 @@
 #define PUTTY_DO_GLOBALS                      /* actually _define_ globals */
 #include "putty.h"
 #include "win_res.h"
+#include "sizetip.h"
 
 #define IDM_SHOWLOG   0x0010
 #define IDM_NEWSESS   0x0020
@@ -82,6 +83,8 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
     MSG msg;
     int guess_width, guess_height;
 
+    putty_inst = inst;
+
     winsock_ver = MAKEWORD(1, 1);
     if (WSAStartup(winsock_ver, &wsadata)) {
        MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
@@ -871,6 +874,12 @@ static int WINAPI WndProc (HWND hwnd, UINT message,
       case WM_IGNORE_SIZE:
        ignore_size = TRUE;            /* don't panic on next WM_SIZE msg */
        break;
+      case WM_ENTERSIZEMOVE:
+          EnableSizeTip(1);
+          break;
+      case WM_EXITSIZEMOVE:
+          EnableSizeTip(0);
+          break;
       case WM_SIZING:
        {
            int width, height, w, h, ew, eh;
@@ -880,6 +889,7 @@ static int WINAPI WndProc (HWND hwnd, UINT message,
            height = r->bottom - r->top - extra_height;
            w = (width + font_width/2) / font_width; if (w < 1) w = 1;
            h = (height + font_height/2) / font_height; if (h < 1) h = 1;
+        UpdateSizeTip(hwnd, w, h);
            ew = width - w * font_width;
            eh = height - h * font_height;
            if (ew != 0) {