- Don't use STATIC
[reactos.git] / reactos / subsys / csrss / win32csr / guiconsole.c
index 1d77db9..43c5a1b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: guiconsole.c,v 1.12 2004/03/14 17:53:27 weiden Exp $
+/* $Id$
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
@@ -8,10 +8,7 @@
 
 /* INCLUDES ******************************************************************/
 
-#include <windows.h>
-#include "conio.h"
-#include "guiconsole.h"
-#include "win32csr.h"
+#include "w32csr.h"
 
 #define NDEBUG
 #include <debug.h>
@@ -32,6 +29,9 @@ typedef struct GUI_CONSOLE_DATA_TAG
   CRITICAL_SECTION Lock;
   HDC MemoryDC;
   HBITMAP MemoryBitmap;
+  RECT Selection;
+  POINT SelectionStart;
+  BOOL MouseDown;
 } GUI_CONSOLE_DATA, *PGUI_CONSOLE_DATA;
 
 #ifndef WM_APP
@@ -42,7 +42,7 @@ typedef struct GUI_CONSOLE_DATA_TAG
 
 #define CURSOR_BLINK_TIME 500
 
-static BOOL Initialized = FALSE;
+static BOOL ConsInitialized = FALSE;
 static HWND NotifyWnd;
 
 /* FUNCTIONS *****************************************************************/
@@ -50,7 +50,7 @@ static HWND NotifyWnd;
 static VOID FASTCALL
 GuiConsoleGetDataPointers(HWND hWnd, PCSRSS_CONSOLE *Console, PGUI_CONSOLE_DATA *GuiData)
 {
-  *Console = (PCSRSS_CONSOLE) GetWindowLongW(hWnd, GWL_USERDATA);
+  *Console = (PCSRSS_CONSOLE) GetWindowLongPtrW(hWnd, GWL_USERDATA);
   *GuiData = (NULL == *Console ? NULL : (*Console)->PrivateData);
 }
 
@@ -63,7 +63,6 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
   HDC Dc;
   HFONT OldFont;
   TEXTMETRICW Metrics;
-  NTSTATUS Status;
 
   GuiData = HeapAlloc(Win32CsrApiHeap, HEAP_ZERO_MEMORY,
                       sizeof(GUI_CONSOLE_DATA) +
@@ -74,25 +73,19 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
       return FALSE;
     }
 
-  Status = RtlInitializeCriticalSection(&GuiData->Lock);
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("RtlInitializeCriticalSection failed, Status=%x\n", Status);
-      HeapFree(Win32CsrApiHeap, 0, GuiData);
-      return FALSE;
-    }
+  InitializeCriticalSection(&GuiData->Lock);
 
   GuiData->LineBuffer = (PWCHAR)(GuiData + 1);
 
   GuiData->Font = CreateFontW(12, 0, 0, TA_BASELINE, FW_NORMAL,
-                              FALSE, FALSE, FALSE, ANSI_CHARSET,
+                              FALSE, FALSE, FALSE, OEM_CHARSET,
                               OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
-                              DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE,
+                              NONANTIALIASED_QUALITY, FIXED_PITCH | FF_DONTCARE,
                               L"Bitstream Vera Sans Mono");
   if (NULL == GuiData->Font)
     {
       DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
-      RtlDeleteCriticalSection(&GuiData->Lock);
+      DeleteCriticalSection(&GuiData->Lock);
       HeapFree(Win32CsrApiHeap, 0, GuiData);
       return FALSE;
     }
@@ -101,7 +94,7 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
     {
       DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
       DeleteObject(GuiData->Font);
-      RtlDeleteCriticalSection(&GuiData->Lock);
+      DeleteCriticalSection(&GuiData->Lock);
       HeapFree(Win32CsrApiHeap, 0, GuiData);
       return FALSE;
     }
@@ -111,7 +104,7 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
       DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
       ReleaseDC(hWnd, Dc);
       DeleteObject(GuiData->Font);
-      RtlDeleteCriticalSection(&GuiData->Lock);
+      DeleteCriticalSection(&GuiData->Lock);
       HeapFree(Win32CsrApiHeap, 0, GuiData);
       return FALSE;
     }
@@ -121,7 +114,7 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
       SelectObject(Dc, OldFont);
       ReleaseDC(hWnd, Dc);
       DeleteObject(GuiData->Font);
-      RtlDeleteCriticalSection(&GuiData->Lock);
+      DeleteCriticalSection(&GuiData->Lock);
       HeapFree(Win32CsrApiHeap, 0, GuiData);
       return FALSE;
     }
@@ -130,19 +123,22 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
   SelectObject(Dc, OldFont);
 
   GuiData->MemoryDC = CreateCompatibleDC(Dc);
-  GuiData->MemoryBitmap = CreateCompatibleBitmap(Dc, 
-                                                 Console->Size.X * GuiData->CharWidth, 
+  GuiData->MemoryBitmap = CreateCompatibleBitmap(Dc,
+                                                 Console->Size.X * GuiData->CharWidth,
                                                 Console->Size.Y * GuiData->CharHeight);
-  DeleteObject(SelectObject(GuiData->MemoryDC, GuiData->MemoryBitmap));
-  DeleteObject(SelectObject(GuiData->MemoryDC, GuiData->Font));
-
+  /* NOTE: Don't delete the "first bitmap", it's done in DeleteDC. */
+  SelectObject(GuiData->MemoryDC, GuiData->MemoryBitmap);
+  /* NOTE: Don't delete stock font. */
+  SelectObject(GuiData->MemoryDC, GuiData->Font);
 
   ReleaseDC(hWnd, Dc);
   GuiData->CursorBlinkOn = TRUE;
   GuiData->ForceCursorOff = FALSE;
 
+  GuiData->Selection.left = -1;
+
   Console->PrivateData = GuiData;
-  SetWindowLongW(hWnd, GWL_USERDATA, (LONG) Console);
+  SetWindowLongPtrW(hWnd, GWL_USERDATA, (DWORD_PTR) Console);
 
   GetWindowRect(hWnd, &Rect);
   Rect.right = Rect.left + Console->Size.X * GuiData->CharWidth +
@@ -188,6 +184,71 @@ GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff, ULONG *CursorX, ULONG *
     }
 }
 
+
+static VOID FASTCALL
+GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
+{
+  RECT oldRect = GuiData->Selection;
+
+  if(rc != NULL)
+  {
+    RECT changeRect = *rc;
+
+    GuiData->Selection = *rc;
+
+    changeRect.left *= GuiData->CharWidth;
+    changeRect.top *= GuiData->CharHeight;
+    changeRect.right *= GuiData->CharWidth;
+    changeRect.bottom *= GuiData->CharHeight;
+
+    if(rc->left != oldRect.left ||
+       rc->top != oldRect.top ||
+       rc->right != oldRect.right ||
+       rc->bottom != oldRect.bottom)
+    {
+      if(oldRect.left != -1)
+      {
+        HRGN rgn1, rgn2;
+
+        oldRect.left *= GuiData->CharWidth;
+        oldRect.top *= GuiData->CharHeight;
+        oldRect.right *= GuiData->CharWidth;
+        oldRect.bottom *= GuiData->CharHeight;
+
+        /* calculate the region that needs to be updated */
+        if((rgn1 = CreateRectRgnIndirect(&oldRect)))
+        {
+          if((rgn2 = CreateRectRgnIndirect(&changeRect)))
+          {
+            if(CombineRgn(rgn1, rgn2, rgn1, RGN_XOR) != ERROR)
+            {
+              InvalidateRgn(hWnd, rgn1, FALSE);
+            }
+
+            DeleteObject(rgn2);
+          }
+          DeleteObject(rgn1);
+        }
+      }
+      else
+      {
+        InvalidateRect(hWnd, &changeRect, FALSE);
+      }
+    }
+  }
+  else if(oldRect.left != -1)
+  {
+    /* clear the selection */
+    GuiData->Selection.left = -1;
+    oldRect.left *= GuiData->CharWidth;
+    oldRect.top *= GuiData->CharHeight;
+    oldRect.right *= GuiData->CharWidth;
+    oldRect.bottom *= GuiData->CharHeight;
+    InvalidateRect(hWnd, &oldRect, FALSE);
+  }
+}
+
+
 VOID FASTCALL
 GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
 {
@@ -236,12 +297,11 @@ GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
               From = Buff->Buffer +
                      ((Line - (Buff->MaxY - Buff->ShowY)) * Buff->MaxX + LeftChar) * 2;
             }
-          Attribute = *(From + 1);
           Start = LeftChar;
           To = GuiData->LineBuffer;
           for (Char = LeftChar; Char <= RightChar; Char++)
             {
-              if (*(From + 1) != Attribute)
+              if (*(From + 1) != LastAttribute)
                 {
                   TextOutW(GuiData->MemoryDC, Start * GuiData->CharWidth, Line * GuiData->CharHeight,
                            GuiData->LineBuffer, Char - Start);
@@ -253,9 +313,8 @@ GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
                       GuiConsoleSetTextColors(GuiData->MemoryDC, Attribute);
                       LastAttribute = Attribute;
                     }
-                }  
-              *((PBYTE) To) = *From;
-              *(((PBYTE) To) + 1) = '\0';
+                }
+              MultiByteToWideChar(Console->OutputCodePage, 0, (PCHAR)From, 1, To, 1);
               To++;
               From += 2;
             }
@@ -291,7 +350,6 @@ GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
       LeaveCriticalSection(&Buff->Header.Lock);
       InvalidateRect(hWnd, &rc, FALSE);
    }
-
 }
 
 VOID FASTCALL
@@ -311,6 +369,22 @@ GuiConsoleHandlePaint(HWND hWnd)
              Ps.rcPaint.right - Ps.rcPaint.left + 1,
              Ps.rcPaint.bottom - Ps.rcPaint.top + 1, GuiData->MemoryDC,
              Ps.rcPaint.left, Ps.rcPaint.top, SRCCOPY);
+
+      if (GuiData->Selection.left != -1)
+      {
+        RECT rc = GuiData->Selection;
+
+        rc.left *= GuiData->CharWidth;
+        rc.top *= GuiData->CharHeight;
+        rc.right *= GuiData->CharWidth;
+        rc.bottom *= GuiData->CharHeight;
+
+        if (IntersectRect(&rc, &Ps.rcPaint, &rc))
+        {
+          PatBlt(Dc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, DSTINVERT);
+        }
+      }
+
       EndPaint (hWnd, &Ps);
       LeaveCriticalSection(&GuiData->Lock);
     }
@@ -334,6 +408,12 @@ GuiConsoleHandleKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
   Message.wParam = wParam;
   Message.lParam = lParam;
 
+  if(msg == WM_CHAR || msg == WM_SYSKEYDOWN)
+  {
+    /* clear the selection */
+    GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
+  }
+
   ConioProcessKey(&Message, Console, FALSE);
 }
 
@@ -377,7 +457,7 @@ GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData, HWND Wnd, UINT x, UINT y)
 }
 
 static VOID STDCALL
-GuiWriteStream(PCSRSS_CONSOLE Console, RECT *Region, UINT CursorStartX, UINT CursorStartY,
+GuiWriteStream(PCSRSS_CONSOLE Console, RECT *Region, LONG CursorStartX, LONG CursorStartY,
                UINT ScrolledLines, CHAR *Buffer, UINT Length)
 {
   PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
@@ -485,7 +565,25 @@ GuiConsoleHandleTimer(HWND hWnd)
 static VOID FASTCALL
 GuiConsoleHandleClose(HWND hWnd)
 {
-  /* FIXME for now, just ignore close requests */
+  PCSRSS_CONSOLE Console;
+  PGUI_CONSOLE_DATA GuiData;
+  PLIST_ENTRY current_entry;
+  PCSRSS_PROCESS_DATA current;
+
+  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
+
+  EnterCriticalSection(&Console->Header.Lock);
+
+  current_entry = Console->ProcessList.Flink;
+  while (current_entry != &Console->ProcessList)
+    {
+      current = CONTAINING_RECORD(current_entry, CSRSS_PROCESS_DATA, ProcessEntry);
+      current_entry = current_entry->Flink;
+
+      ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT, current);
+    }
+
+  LeaveCriticalSection(&Console->Header.Lock);
 }
 
 static VOID FASTCALL
@@ -498,14 +596,145 @@ GuiConsoleHandleNcDestroy(HWND hWnd)
   KillTimer(hWnd, 1);
   Console->PrivateData = NULL;
   DeleteDC(GuiData->MemoryDC);
-  RtlDeleteCriticalSection(&GuiData->Lock);
+  DeleteCriticalSection(&GuiData->Lock);
   HeapFree(Win32CsrApiHeap, 0, GuiData);
 }
 
+static VOID FASTCALL
+GuiConsoleLeftMouseDown(HWND hWnd, LPARAM lParam)
+{
+  PCSRSS_CONSOLE Console;
+  PGUI_CONSOLE_DATA GuiData;
+  POINTS pt;
+  RECT rc;
+
+  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
+  if (Console == NULL || GuiData == NULL) return;
+
+  pt = MAKEPOINTS(lParam);
+
+  rc.left = pt.x / GuiData->CharWidth;
+  rc.top = pt.y / GuiData->CharHeight;
+  rc.right = rc.left + 1;
+  rc.bottom = rc.top + 1;
+
+  GuiData->SelectionStart.x = rc.left;
+  GuiData->SelectionStart.y = rc.top;
+
+  SetCapture(hWnd);
+
+  GuiData->MouseDown = TRUE;
+
+  GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
+}
+
+static VOID FASTCALL
+GuiConsoleLeftMouseUp(HWND hWnd, LPARAM lParam)
+{
+  PCSRSS_CONSOLE Console;
+  PGUI_CONSOLE_DATA GuiData;
+  RECT rc;
+  POINTS pt;
+
+  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
+  if (Console == NULL || GuiData == NULL) return;
+  if (GuiData->Selection.left == -1 || !GuiData->MouseDown) return;
+
+  pt = MAKEPOINTS(lParam);
+
+  rc.left = GuiData->SelectionStart.x;
+  rc.top = GuiData->SelectionStart.y;
+  rc.right = (pt.x >= 0 ? (pt.x / GuiData->CharWidth) + 1 : 0);
+  rc.bottom = (pt.y >= 0 ? (pt.y / GuiData->CharHeight) + 1 : 0);
+
+  /* exchange left/top with right/bottom if required */
+  if(rc.left >= rc.right)
+  {
+    LONG tmp;
+    tmp = rc.left;
+    rc.left = max(rc.right - 1, 0);
+    rc.right = tmp + 1;
+  }
+  if(rc.top >= rc.bottom)
+  {
+    LONG tmp;
+    tmp = rc.top;
+    rc.top = max(rc.bottom - 1, 0);
+    rc.bottom = tmp + 1;
+  }
+
+  GuiData->MouseDown = FALSE;
+
+  GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
+
+  ReleaseCapture();
+}
+
+static VOID FASTCALL
+GuiConsoleMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
+{
+  PCSRSS_CONSOLE Console;
+  PGUI_CONSOLE_DATA GuiData;
+  RECT rc;
+  POINTS pt;
+
+  if (!(wParam & MK_LBUTTON)) return;
+
+  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
+  if (Console == NULL || GuiData == NULL || !GuiData->MouseDown) return;
+
+  pt = MAKEPOINTS(lParam);
+
+  rc.left = GuiData->SelectionStart.x;
+  rc.top = GuiData->SelectionStart.y;
+  rc.right = (pt.x >= 0 ? (pt.x / GuiData->CharWidth) + 1 : 0);
+  rc.bottom = (pt.y >= 0 ? (pt.y / GuiData->CharHeight) + 1 : 0);
+
+  /* exchange left/top with right/bottom if required */
+  if(rc.left >= rc.right)
+  {
+    LONG tmp;
+    tmp = rc.left;
+    rc.left = max(rc.right - 1, 0);
+    rc.right = tmp + 1;
+  }
+  if(rc.top >= rc.bottom)
+  {
+    LONG tmp;
+    tmp = rc.top;
+    rc.top = max(rc.bottom - 1, 0);
+    rc.bottom = tmp + 1;
+  }
+
+  GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
+}
+
+static VOID FASTCALL
+GuiConsoleRightMouseDown(HWND hWnd)
+{
+  PCSRSS_CONSOLE Console;
+  PGUI_CONSOLE_DATA GuiData;
+
+  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
+  if (Console == NULL || GuiData == NULL) return;
+
+  if (GuiData->Selection.left == -1)
+  {
+    /* FIXME - paste text from clipboard */
+  }
+  else
+  {
+    /* FIXME - copy selection to clipboard */
+
+    GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
+  }
+
+}
+
 static LRESULT CALLBACK
 GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
-  LRESULT Result;
+  LRESULT Result = 0;
 
   switch(msg)
     {
@@ -514,7 +743,6 @@ GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
         break;
       case WM_PAINT:
         GuiConsoleHandlePaint(hWnd);
-        Result = 0;
         break;
       case WM_KEYDOWN:
       case WM_KEYUP:
@@ -522,19 +750,27 @@ GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
       case WM_SYSKEYUP:
       case WM_CHAR:
         GuiConsoleHandleKey(hWnd, msg, wParam, lParam);
-        Result = 0;
         break;
       case WM_TIMER:
         GuiConsoleHandleTimer(hWnd);
-        Result = 0;
         break;
       case WM_CLOSE:
         GuiConsoleHandleClose(hWnd);
-        Result = 0;
         break;
       case WM_NCDESTROY:
         GuiConsoleHandleNcDestroy(hWnd);
-        Result = 0;
+        break;
+      case WM_LBUTTONDOWN:
+          GuiConsoleLeftMouseDown(hWnd, lParam);
+        break;
+      case WM_LBUTTONUP:
+          GuiConsoleLeftMouseUp(hWnd, lParam);
+        break;
+      case WM_RBUTTONDOWN:
+          GuiConsoleRightMouseDown(hWnd);
+        break;
+      case WM_MOUSEMOVE:
+          GuiConsoleMouseMove(hWnd, wParam, lParam);
         break;
       default:
         Result = DefWindowProcW(hWnd, msg, wParam, lParam);
@@ -549,6 +785,8 @@ GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
   HWND NewWindow;
   LONG WindowCount;
+  MSG Msg;
+  PWCHAR Buffer, Title;
   PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) lParam;
 
   switch(msg)
@@ -557,8 +795,20 @@ GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
         SetWindowLongW(hWnd, GWL_USERDATA, 0);
         return 0;
       case PM_CREATE_CONSOLE:
-        NewWindow = CreateWindowW(L"Win32CsrConsole",
-                                  Console->Title.Buffer,
+        Buffer = HeapAlloc(Win32CsrApiHeap, 0,
+                           Console->Title.Length + sizeof(WCHAR));
+        if (NULL != Buffer)
+          {
+            memcpy(Buffer, Console->Title.Buffer, Console->Title.Length);
+            Buffer[Console->Title.Length / sizeof(WCHAR)] = L'\0';
+            Title = Buffer;
+          }
+        else
+          {
+            Title = L"";
+          }
+        NewWindow = CreateWindowW(L"ConsoleWindowClass",
+                                  Title,
                                   WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                                   CW_USEDEFAULT,
                                   CW_USEDEFAULT,
@@ -568,6 +818,10 @@ GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                                   NULL,
                                   (HINSTANCE) GetModuleHandleW(NULL),
                                   (PVOID) Console);
+        if (NULL != Buffer)
+          {
+            HeapFree(Win32CsrApiHeap, 0, Buffer);
+          }
         Console->hWindow = NewWindow;
         if (NULL != NewWindow)
           {
@@ -576,6 +830,14 @@ GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
           }
         return (LRESULT) NewWindow;
       case PM_DESTROY_CONSOLE:
+        /* Window creation is done using a PostMessage(), so it's possible that the
+         * window that we want to destroy doesn't exist yet. So first empty the message
+         * queue */
+        while(PeekMessageW(&Msg, NULL, 0, 0, PM_REMOVE))
+          {
+            TranslateMessage(&Msg);
+            DispatchMessageW(&Msg);
+          }
         DestroyWindow(Console->hWindow);
         Console->hWindow = NULL;
         WindowCount = GetWindowLongW(hWnd, GWL_USERDATA);
@@ -632,31 +894,8 @@ GuiConsoleGuiThread(PVOID Data)
 static BOOL FASTCALL
 GuiInit(VOID)
 {
-  HDESK Desktop;
-  NTSTATUS Status;
   WNDCLASSEXW wc;
 
-  Desktop = OpenDesktopW(L"Default", 0, FALSE, GENERIC_ALL);
-  if (NULL == Desktop)
-    {
-      DPRINT1("Failed to open desktop\n");
-      return FALSE;
-    }
-  Status = NtSetInformationProcess(NtCurrentProcess(),
-                                   ProcessDesktop,
-                                   &Desktop,
-                                   sizeof(Desktop));
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Cannot set default desktop.\n");
-      return FALSE;
-    }
-  if (! SetThreadDesktop(Desktop))
-    {
-      DPRINT1("Failed to set thread desktop\n");
-      return FALSE;
-    }
-
   if (NULL == NotifyWnd)
     {
       PrivateCsrssManualGuiCheck(+1);
@@ -681,7 +920,7 @@ GuiInit(VOID)
     }
 
   wc.cbSize = sizeof(WNDCLASSEXW);
-  wc.lpszClassName = L"Win32CsrConsole";
+  wc.lpszClassName = L"ConsoleWindowClass";
   wc.lpfnWndProc = GuiConsoleWndProc;
   wc.style = 0;
   wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
@@ -709,15 +948,33 @@ GuiInitScreenBuffer(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buffer)
   Buffer->DefaultAttrib = 0x0f;
 }
 
-STATIC BOOL STDCALL
+static BOOL STDCALL
 GuiChangeTitle(PCSRSS_CONSOLE Console)
 {
-  SendMessageW(Console->hWindow, WM_SETTEXT, 0, (LPARAM) Console->Title.Buffer);
+  PWCHAR Buffer, Title;
+
+  Buffer = HeapAlloc(Win32CsrApiHeap, 0,
+                     Console->Title.Length + sizeof(WCHAR));
+  if (NULL != Buffer)
+    {
+      memcpy(Buffer, Console->Title.Buffer, Console->Title.Length);
+      Buffer[Console->Title.Length / sizeof(WCHAR)] = L'\0';
+      Title = Buffer;
+    }
+  else
+    {
+      Title = L"";
+    }
+  SendMessageW(Console->hWindow, WM_SETTEXT, 0, (LPARAM) Title);
+  if (NULL != Buffer)
+    {
+      HeapFree(Win32CsrApiHeap, 0, Buffer);
+    }
 
   return TRUE;
 }
 
-STATIC BOOL STDCALL
+static BOOL STDCALL
 GuiChangeIcon(PCSRSS_CONSOLE Console)
 {
   SendMessageW(Console->hWindow, WM_SETICON, ICON_BIG, (LPARAM)Console->hWindowIcon);
@@ -750,12 +1007,12 @@ GuiInitConsole(PCSRSS_CONSOLE Console)
   HANDLE GraphicsStartupEvent;
   HANDLE ThreadHandle;
 
-  if (! Initialized)
+  if (! ConsInitialized)
     {
-      Initialized = TRUE;
+      ConsInitialized = TRUE;
       if (! GuiInit())
         {
-          Initialized = FALSE;
+          ConsInitialized = FALSE;
           return STATUS_UNSUCCESSFUL;
         }
     }
@@ -818,7 +1075,7 @@ GuiConsoleCopyRegion(HWND hWnd,
   ScrollRect.top = Dest->top * GuiData->CharHeight;
   ScrollRect.bottom = (Dest->bottom + 1) * GuiData->CharHeight;
   EnterCriticalSection(&GuiData->Lock);
-  BitBlt(GuiData->MemoryDC, ScrollRect.left, ScrollRect.top, 
+  BitBlt(GuiData->MemoryDC, ScrollRect.left, ScrollRect.top,
          ScrollRect.right - ScrollRect.left, ScrollRect.bottom - ScrollRect.top,
         GuiData->MemoryDC, Source->left * GuiData->CharWidth, Source->top * GuiData->CharHeight, SRCCOPY);