also scroll the selection
[reactos.git] / reactos / subsys / csrss / win32csr / guiconsole.c
index 44f82e4..06b89d4 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: guiconsole.c,v 1.25 2004/12/18 19:23:05 gvg 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>
@@ -30,8 +27,6 @@ typedef struct GUI_CONSOLE_DATA_TAG
   BOOL CursorBlinkOn;
   BOOL ForceCursorOff;
   CRITICAL_SECTION Lock;
-  HDC MemoryDC;
-  HBITMAP MemoryBitmap;
   RECT Selection;
   POINT SelectionStart;
   BOOL MouseDown;
@@ -45,7 +40,7 @@ typedef struct GUI_CONSOLE_DATA_TAG
 
 #define CURSOR_BLINK_TIME 500
 
-static BOOL Initialized = FALSE;
+static BOOL ConsInitialized = FALSE;
 static HWND NotifyWnd;
 
 /* FUNCTIONS *****************************************************************/
@@ -125,21 +120,12 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
   GuiData->CharHeight = Metrics.tmHeight + Metrics.tmExternalLeading;
   SelectObject(Dc, OldFont);
 
-  GuiData->MemoryDC = CreateCompatibleDC(Dc);
-  GuiData->MemoryBitmap = CreateCompatibleBitmap(Dc, 
-                                                 Console->Size.X * GuiData->CharWidth, 
-                                                Console->Size.Y * GuiData->CharHeight);
-  /* 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;
   SetWindowLongPtrW(hWnd, GWL_USERDATA, (DWORD_PTR) Console);
 
@@ -192,7 +178,7 @@ static VOID FASTCALL
 GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
 {
   RECT oldRect = GuiData->Selection;
-  
+
   if(rc != NULL)
   {
     RECT changeRect = *rc;
@@ -203,7 +189,7 @@ GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
     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 ||
@@ -212,12 +198,12 @@ GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
       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)))
         {
@@ -252,149 +238,187 @@ GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
 }
 
 
-VOID FASTCALL
-GuiConsoleUpdateBitmap(HWND hWnd, RECT rc)
+static VOID FASTCALL
+GuiConsolePaint(PCSRSS_CONSOLE Console,
+                PGUI_CONSOLE_DATA GuiData,
+                HDC hDC,
+                PRECT rc)
 {
-  PCSRSS_CONSOLE Console;
-  PGUI_CONSOLE_DATA GuiData;
-  PCSRSS_SCREEN_BUFFER Buff;
-  HDC Dc;
-  ULONG TopLine, BottomLine, LeftChar, RightChar;
-  ULONG Line, Char, Start;
-  PBYTE From;
-  PWCHAR To;
-  BYTE LastAttribute, Attribute;
-  ULONG CursorX, CursorY, CursorHeight;
-  HBRUSH CursorBrush, OldBrush;
+    PCSRSS_SCREEN_BUFFER Buff;
+    ULONG TopLine, BottomLine, LeftChar, RightChar;
+    ULONG Line, Char, Start;
+    PBYTE From;
+    PWCHAR To;
+    BYTE LastAttribute, Attribute;
+    ULONG CursorX, CursorY, CursorHeight;
+    HBRUSH CursorBrush, OldBrush;
+    HFONT OldFont;
 
-  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
-  if (NULL != Console && NULL != GuiData && NULL != Console->ActiveBuffer)
+    Buff = Console->ActiveBuffer;
+
+    TopLine = rc->top / GuiData->CharHeight;
+    BottomLine = (rc->bottom + (GuiData->CharHeight - 1)) / GuiData->CharHeight - 1;
+    LeftChar = rc->left / GuiData->CharWidth;
+    RightChar = (rc->right + (GuiData->CharWidth - 1)) / GuiData->CharWidth - 1;
+    LastAttribute = Buff->Buffer[(TopLine * Buff->MaxX + LeftChar) * 2 + 1];
+
+    GuiConsoleSetTextColors(hDC,
+                            LastAttribute);
+
+    EnterCriticalSection(&Buff->Header.Lock);
+
+    OldFont = SelectObject(hDC,
+                           GuiData->Font);
+
+    for (Line = TopLine; Line <= BottomLine; Line++)
     {
-      Buff = Console->ActiveBuffer;
-      EnterCriticalSection(&Buff->Header.Lock);
-      Dc = GetDC(hWnd);
-      if (rc.right <= rc.left || rc.bottom <= rc.top)
+        if (Line + Buff->ShowY < Buff->MaxY)
         {
-          ReleaseDC(hWnd, Dc);
-          LeaveCriticalSection(&Buff->Header.Lock);
-          return;
+            From = Buff->Buffer + ((Line + Buff->ShowY) * Buff->MaxX + LeftChar) * 2;
         }
+        else
+        {
+            From = Buff->Buffer +
+                   ((Line - (Buff->MaxY - Buff->ShowY)) * Buff->MaxX + LeftChar) * 2;
+        }
+        Start = LeftChar;
+        To = GuiData->LineBuffer;
 
-      EnterCriticalSection(&GuiData->Lock);
-
-      TopLine = rc.top / GuiData->CharHeight;
-      BottomLine = (rc.bottom + (GuiData->CharHeight - 1)) / GuiData->CharHeight - 1;
-      LeftChar = rc.left / GuiData->CharWidth;
-      RightChar = (rc.right + (GuiData->CharWidth - 1)) / GuiData->CharWidth - 1;
-      LastAttribute = Buff->Buffer[(TopLine * Buff->MaxX + LeftChar) * 2 + 1];
-      GuiConsoleSetTextColors(GuiData->MemoryDC, LastAttribute);
-
-      for (Line = TopLine; Line <= BottomLine; Line++)
+        for (Char = LeftChar; Char <= RightChar; Char++)
         {
-          if (Line + Buff->ShowY < Buff->MaxY)
-            {
-              From = Buff->Buffer + ((Line + Buff->ShowY) * Buff->MaxX + LeftChar) * 2;
-            }
-          else
-            {
-              From = Buff->Buffer +
-                     ((Line - (Buff->MaxY - Buff->ShowY)) * Buff->MaxX + LeftChar) * 2;
-            }
-          Start = LeftChar;
-          To = GuiData->LineBuffer;
-          for (Char = LeftChar; Char <= RightChar; Char++)
+            if (*(From + 1) != LastAttribute)
             {
-              if (*(From + 1) != LastAttribute)
+                TextOutW(hDC,
+                         Start * GuiData->CharWidth,
+                         Line * GuiData->CharHeight,
+                         GuiData->LineBuffer,
+                         Char - Start);
+                Start = Char;
+                To = GuiData->LineBuffer;
+                Attribute = *(From + 1);
+                if (Attribute != LastAttribute)
                 {
-                  TextOutW(GuiData->MemoryDC, Start * GuiData->CharWidth, Line * GuiData->CharHeight,
-                           GuiData->LineBuffer, Char - Start);
-                  Start = Char;
-                  To = GuiData->LineBuffer;
-                  Attribute = *(From + 1);
-                  if (Attribute != LastAttribute)
-                    {
-                      GuiConsoleSetTextColors(GuiData->MemoryDC, Attribute);
-                      LastAttribute = Attribute;
-                    }
-                }  
-              MultiByteToWideChar(Console->OutputCodePage, 0, From, 1, To, 1);
-              To++;
-              From += 2;
+                    GuiConsoleSetTextColors(hDC,
+                                            Attribute);
+                    LastAttribute = Attribute;
+                }
             }
-          TextOutW(GuiData->MemoryDC, Start * GuiData->CharWidth, Line * GuiData->CharHeight,
-                   GuiData->LineBuffer, RightChar - Start + 1);
+
+            MultiByteToWideChar(Console->OutputCodePage,
+                                0,
+                                (PCHAR)From,
+                                1,
+                                To,
+                                1);
+            To++;
+            From += 2;
         }
 
-      if (Buff->CursorInfo.bVisible && GuiData->CursorBlinkOn
-          &&! GuiData->ForceCursorOff)
+        TextOutW(hDC,
+                 Start * GuiData->CharWidth,
+                 Line * GuiData->CharHeight,
+                 GuiData->LineBuffer,
+                 RightChar - Start + 1);
+    }
+
+    if (Buff->CursorInfo.bVisible && GuiData->CursorBlinkOn &&
+        !GuiData->ForceCursorOff)
+    {
+        GuiConsoleGetLogicalCursorPos(Buff,
+                                      &CursorX,
+                                      &CursorY);
+        if (LeftChar <= CursorX && CursorX <= RightChar &&
+            TopLine <= CursorY && CursorY <= BottomLine)
         {
-          GuiConsoleGetLogicalCursorPos(Buff, &CursorX, &CursorY);
-          if (LeftChar <= CursorX && CursorX <= RightChar &&
-              TopLine <= CursorY && CursorY <= BottomLine)
+            CursorHeight = (GuiData->CharHeight * Buff->CursorInfo.dwSize) / 100;
+            if (CursorHeight < 1)
             {
-              CursorHeight = (GuiData->CharHeight * Buff->CursorInfo.dwSize) / 100;
-              if (CursorHeight < 1)
-                {
-                  CursorHeight = 1;
-                }
-              From = Buff->Buffer + (Buff->CurrentY * Buff->MaxX + Buff->CurrentX) * 2 + 1;
-              CursorBrush = CreateSolidBrush(GuiConsoleRGBFromAttribute(*From));
-              OldBrush = SelectObject(GuiData->MemoryDC, CursorBrush);
-              PatBlt(GuiData->MemoryDC, CursorX * GuiData->CharWidth,
-                     CursorY * GuiData->CharHeight + (GuiData->CharHeight - CursorHeight),
-                     GuiData->CharWidth, CursorHeight, PATCOPY);
-              SelectObject(GuiData->MemoryDC, OldBrush);
-              DeleteObject(CursorBrush);
+                CursorHeight = 1;
             }
+            From = Buff->Buffer + (Buff->CurrentY * Buff->MaxX + Buff->CurrentX) * 2 + 1;
+            CursorBrush = CreateSolidBrush(GuiConsoleRGBFromAttribute(*From));
+            OldBrush = SelectObject(hDC,
+                                    CursorBrush);
+            PatBlt(hDC,
+                   CursorX * GuiData->CharWidth,
+                   CursorY * GuiData->CharHeight + (GuiData->CharHeight - CursorHeight),
+                   GuiData->CharWidth,
+                   CursorHeight,
+                   PATCOPY);
+            SelectObject(hDC,
+                         OldBrush);
+            DeleteObject(CursorBrush);
         }
+    }
 
-      LeaveCriticalSection(&GuiData->Lock);
-      ReleaseDC(hWnd, Dc);
-      LeaveCriticalSection(&Buff->Header.Lock);
-      InvalidateRect(hWnd, &rc, FALSE);
-   }
+    LeaveCriticalSection(&Buff->Header.Lock);
+
+    SelectObject(hDC,
+                 OldFont);
 }
 
-VOID FASTCALL
-GuiConsoleHandlePaint(HWND hWnd)
+static VOID FASTCALL
+GuiConsoleHandlePaint(HWND hWnd, HDC hDCPaint)
 {
-  PAINTSTRUCT Ps;
-  HDC Dc;
-  PCSRSS_CONSOLE Console;
-  PGUI_CONSOLE_DATA GuiData;
-
-  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
-  if (NULL != Console && NULL != GuiData)
-    {
-      EnterCriticalSection(&GuiData->Lock);
-      Dc = BeginPaint (hWnd, &Ps);
-      BitBlt(Dc, Ps.rcPaint.left, Ps.rcPaint.top,
-             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);
-    }
-  else
+    RECT rcUpdate;
+    HDC hDC;
+    PAINTSTRUCT ps;
+    PCSRSS_CONSOLE Console;
+    PGUI_CONSOLE_DATA GuiData;
+
+    if (GetUpdateRect(hWnd,
+                      &rcUpdate,
+                      FALSE))
     {
-      Dc = BeginPaint (hWnd, &Ps);
-      EndPaint (hWnd, &Ps);
+        hDC = (hDCPaint != NULL ? hDCPaint : BeginPaint(hWnd,
+                                                        &ps));
+        if (hDC != NULL)
+        {
+            GuiConsoleGetDataPointers(hWnd,
+                                      &Console,
+                                      &GuiData);
+            if (Console != NULL && GuiData != NULL &&
+                Console->ActiveBuffer != NULL)
+            {
+                EnterCriticalSection(&GuiData->Lock);
+
+                GuiConsolePaint(Console,
+                                GuiData,
+                                hDC,
+                                &rcUpdate);
+
+                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;
+
+                    /* invert the selection */
+                    if (IntersectRect(&rc,
+                                      &rcUpdate,
+                                      &rc))
+                    {
+                        PatBlt(hDC,
+                               rc.left,
+                               rc.top,
+                               rc.right - rc.left,
+                               rc.bottom - rc.top,
+                               DSTINVERT);
+                    }
+                }
+
+                LeaveCriticalSection(&GuiData->Lock);
+            }
+
+            if (hDCPaint == NULL)
+            {
+                EndPaint(hWnd,
+                         &ps);
+            }
+        }
     }
 }
 
@@ -410,7 +434,7 @@ GuiConsoleHandleKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
   Message.message = msg;
   Message.wParam = wParam;
   Message.lParam = lParam;
-  
+
   if(msg == WM_CHAR || msg == WM_SYSKEYDOWN)
   {
     /* clear the selection */
@@ -430,7 +454,7 @@ GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData, HWND Wnd, RECT *Region)
   RegionRect.right = (Region->right + 1) * GuiData->CharWidth;
   RegionRect.bottom = (Region->bottom + 1) * GuiData->CharHeight;
 
-  GuiConsoleUpdateBitmap(Wnd, RegionRect);
+  InvalidateRect(Wnd, &RegionRect, FALSE);
 }
 
 static VOID STDCALL
@@ -438,12 +462,10 @@ GuiDrawRegion(PCSRSS_CONSOLE Console, RECT *Region)
 {
   PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
 
-  if (NULL == Console->hWindow || NULL == GuiData)
+  if (NULL != Console->hWindow && NULL != GuiData)
     {
-      return;
+      GuiIntDrawRegion(GuiData, Console->hWindow, Region);
     }
-
-  GuiIntDrawRegion(GuiData, Console->hWindow, Region);
 }
 
 static VOID FASTCALL
@@ -460,13 +482,13 @@ 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;
   PCSRSS_SCREEN_BUFFER Buff = Console->ActiveBuffer;
   LONG CursorEndX, CursorEndY;
-  RECT Source, Dest;
+  RECT ScrollRect;
 
   if (NULL == Console->hWindow || NULL == GuiData)
     {
@@ -475,16 +497,38 @@ GuiWriteStream(PCSRSS_CONSOLE Console, RECT *Region, UINT CursorStartX, UINT Cur
 
   if (0 != ScrolledLines)
     {
-      Source.left = 0;
-      Source.top = ScrolledLines;
-      Source.right = Console->Size.X - 1;
-      Source.bottom = ScrolledLines + Region->top - 1;
-      Dest.left = 0;
-      Dest.top = 0;
-      Dest.right = Console->Size.X - 1;
-      Dest.bottom = Region->top - 1;
-
-      GuiConsoleCopyRegion(Console->hWindow, &Source, &Dest);
+      ScrollRect.left = 0;
+      ScrollRect.top = 0;
+      ScrollRect.right = Console->Size.X * GuiData->CharWidth;
+      ScrollRect.bottom = Region->top * GuiData->CharHeight;
+
+      if (GuiData->Selection.left != -1)
+      {
+          /* scroll the selection */
+          if (GuiData->Selection.top > ScrolledLines)
+          {
+              GuiData->Selection.top -= ScrolledLines;
+              GuiData->Selection.bottom -= ScrolledLines;
+          }
+          else if (GuiData->Selection.bottom < ScrolledLines)
+          {
+              GuiData->Selection.left = -1;
+          }
+          else
+          {
+              GuiData->Selection.top = 0;
+              GuiData->Selection.bottom -= ScrolledLines;
+          }
+      }
+
+      ScrollWindowEx(Console->hWindow,
+                     0,
+                     -(ScrolledLines * GuiData->CharHeight),
+                     &ScrollRect,
+                     NULL,
+                     NULL,
+                     NULL,
+                     SW_INVALIDATE);
     }
 
   GuiIntDrawRegion(GuiData, Console->hWindow, Region);
@@ -598,7 +642,6 @@ GuiConsoleHandleNcDestroy(HWND hWnd)
   GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
   KillTimer(hWnd, 1);
   Console->PrivateData = NULL;
-  DeleteDC(GuiData->MemoryDC);
   DeleteCriticalSection(&GuiData->Lock);
   HeapFree(Win32CsrApiHeap, 0, GuiData);
 }
@@ -610,7 +653,7 @@ GuiConsoleLeftMouseDown(HWND hWnd, LPARAM lParam)
   PGUI_CONSOLE_DATA GuiData;
   POINTS pt;
   RECT rc;
-  
+
   GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
   if (Console == NULL || GuiData == NULL) return;
 
@@ -620,14 +663,14 @@ GuiConsoleLeftMouseDown(HWND hWnd, LPARAM lParam)
   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);
 }
 
@@ -638,13 +681,13 @@ GuiConsoleLeftMouseUp(HWND hWnd, LPARAM lParam)
   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);
@@ -665,11 +708,11 @@ GuiConsoleLeftMouseUp(HWND hWnd, LPARAM lParam)
     rc.top = max(rc.bottom - 1, 0);
     rc.bottom = tmp + 1;
   }
-  
+
   GuiData->MouseDown = FALSE;
 
   GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
-  
+
   ReleaseCapture();
 }
 
@@ -680,9 +723,9 @@ GuiConsoleMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
   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;
 
@@ -710,14 +753,14 @@ GuiConsoleMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
   }
 
   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;
 
@@ -728,16 +771,16 @@ GuiConsoleRightMouseDown(HWND hWnd)
   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)
     {
@@ -745,8 +788,7 @@ GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
         Result = (LRESULT) GuiConsoleHandleNcCreate(hWnd, (CREATESTRUCTW *) lParam);
         break;
       case WM_PAINT:
-        GuiConsoleHandlePaint(hWnd);
-        Result = 0;
+        GuiConsoleHandlePaint(hWnd, (HDC)wParam);
         break;
       case WM_KEYDOWN:
       case WM_KEYUP:
@@ -754,19 +796,15 @@ 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);
@@ -794,6 +832,7 @@ 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)
@@ -802,8 +841,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,
@@ -813,6 +864,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)
           {
@@ -911,7 +966,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);
@@ -939,15 +994,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);
@@ -980,12 +1053,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;
         }
     }
@@ -1031,30 +1104,4 @@ GuiInitConsole(PCSRSS_CONSOLE Console)
   return STATUS_SUCCESS;
 }
 
-VOID STDCALL
-GuiConsoleCopyRegion(HWND hWnd,
-                     RECT *Source,
-                     RECT *Dest)
-{
-  RECT ScrollRect;
-  PGUI_CONSOLE_DATA GuiData;
-  PCSRSS_CONSOLE Console;
-
-
-  GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
-
-  ScrollRect.left = Dest->left * GuiData->CharWidth;
-  ScrollRect.right = (Dest->right + 1) * GuiData->CharWidth;
-  ScrollRect.top = Dest->top * GuiData->CharHeight;
-  ScrollRect.bottom = (Dest->bottom + 1) * GuiData->CharHeight;
-  EnterCriticalSection(&GuiData->Lock);
-  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);
-
-  LeaveCriticalSection(&GuiData->Lock);
-
-  InvalidateRect(hWnd, &ScrollRect, FALSE);
-}
-
 /* EOF */