SSH port forwarding! How cool is that?
[sgt/putty] / window.c
index 905c43e..15ad7b3 100644 (file)
--- a/window.c
+++ b/window.c
@@ -128,6 +128,8 @@ static char *window_name, *icon_name;
 
 static int compose_state = 0;
 
+static OSVERSIONINFO osVersion;
+
 /* Dummy routine, only required in plink. */
 void ldisc_update(int echo, int edit)
 {
@@ -166,6 +168,16 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
      * config box. */
     defuse_showwindow();
 
+    {
+       ZeroMemory(&osVersion, sizeof(osVersion));
+       osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+       if (!GetVersionEx ( (OSVERSIONINFO *) &osVersion)) {
+            MessageBox(NULL, "Windows refuses to report a version",
+                       "PuTTY Fatal Error", MB_OK | MB_ICONEXCLAMATION);
+           return 1;
+        }
+    }
+
     /*
      * Process the command line.
      */
@@ -574,6 +586,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
      * Finally show the window!
      */
     ShowWindow(hwnd, show);
+    SetForegroundWindow(hwnd);
 
     /*
      * Open the initial log file if there is one.
@@ -693,7 +706,7 @@ char *do_select(SOCKET skt, int startup)
     int msg, events;
     if (startup) {
        msg = WM_NETEVENT;
-       events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE;
+       events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE | FD_ACCEPT;
     } else {
        msg = events = 0;
     }
@@ -1591,11 +1604,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
        if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) {
            Mouse_Button b;
            if (wParam & MK_LBUTTON)
-               b = MBT_SELECT;
+               b = MBT_LEFT;
            else if (wParam & MK_MBUTTON)
-               b = cfg.mouse_is_xterm ? MBT_PASTE : MBT_EXTEND;
+               b = MBT_MIDDLE;
            else
-               b = cfg.mouse_is_xterm ? MBT_EXTEND : MBT_PASTE;
+               b = MBT_RIGHT;
            term_mouse(b, MA_DRAG, TO_CHR_X(X_POS(lParam)),
                       TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
                       wParam & MK_CONTROL);
@@ -1872,6 +1885,31 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
            kbd_codepage = atoi(lbuf);
        }
        break;
+      case WM_IME_COMPOSITION:
+       {
+           HIMC hIMC;
+           int n;
+           char *buff;
+   
+           if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS || 
+               osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
+
+           if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
+               break; /* fall back to DefWindowProc */
+
+           hIMC = ImmGetContext(hwnd);
+           n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
+
+           if (n > 0) {
+               buff = (char*) smalloc(n);
+               ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
+               luni_send((unsigned short *)buff, n / 2);
+               free(buff);
+           }
+           ImmReleaseContext(hwnd, hIMC);
+           return 1;
+       }
+
       case WM_IME_CHAR:
        if (wParam & 0xFF00) {
            unsigned char buf[2];
@@ -1915,8 +1953,27 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
  */
 void sys_cursor(int x, int y)
 {
-    if (has_focus)
-       SetCaretPos(x * font_width, y * font_height);
+    COMPOSITIONFORM cf;
+    HIMC hIMC;
+
+    if (!has_focus) return;
+    
+    SetCaretPos(x * font_width, y * font_height);
+
+    /* IMM calls on Win98 and beyond only */
+    if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
+    
+    if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
+           osVersion.dwMinorVersion == 0) return; /* 95 */
+
+    /* we should have the IMM functions */
+    hIMC = ImmGetContext(hwnd);
+    cf.dwStyle = CFS_POINT;
+    cf.ptCurrentPos.x = x * font_width;
+    cf.ptCurrentPos.y = y * font_height;
+    ImmSetCompositionWindow(hIMC, &cf);
+
+    ImmReleaseContext(hwnd, hIMC);
 }
 
 /*
@@ -2461,8 +2518,7 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
            return 0;
        }
        if (wParam == VK_INSERT && shift_state == 1) {
-           term_mouse(MBT_PASTE, MA_CLICK, 0, 0, 0, 0);
-           term_mouse(MBT_PASTE, MA_RELEASE, 0, 0, 0, 0);
+           term_do_paste();
            return 0;
        }
        if (left_alt && wParam == VK_F4 && cfg.alt_f4) {
@@ -2758,7 +2814,8 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
            return p - output;
        }
 
-       if (cfg.funky_type == 5 && code >= 11 && code <= 34) {
+       if (cfg.funky_type == 5 &&     /* SCO function keys */
+           code >= 11 && code <= 34) {
            char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
            int index = 0;
            switch (wParam) {
@@ -2780,6 +2837,16 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
            p += sprintf((char *) p, "\x1B[%c", codes[index]);
            return p - output;
        }
+       if (cfg.funky_type == 5 &&     /* SCO small keypad */
+           code >= 1 && code <= 6) {
+           char codes[] = "HL.FIG";
+           if (code == 3) {
+               *p++ = '\x7F';
+           } else {
+               p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
+           }
+           return p - output;
+       }
        if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
            int offt = 0;
            if (code > 15)
@@ -2882,7 +2949,7 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
 #ifdef SHOW_TOASCII_RESULT
        if (r == 1 && !key_down) {
            if (alt_sum) {
-               if (utf || dbcs_screenfont)
+               if (in_utf || dbcs_screenfont)
                    debug((", (U+%04x)", alt_sum));
                else
                    debug((", LCH(%d)", alt_sum));
@@ -2926,7 +2993,7 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
 
                if (!key_down) {
                    if (alt_sum) {
-                       if (utf || dbcs_screenfont) {
+                       if (in_utf || dbcs_screenfont) {
                            keybuf = alt_sum;
                            luni_send(&keybuf, 1);
                        } else {
@@ -2953,7 +3020,7 @@ static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
        if (!left_alt)
            keys[0] = 0;
        /* If we will be using alt_sum fix the 256s */
-       else if (keys[0] && (utf || dbcs_screenfont))
+       else if (keys[0] && (in_utf || dbcs_screenfont))
            keys[0] = 10;
     }