3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: subsys/csrss/win32csr/guiconsole.c
6 * PURPOSE: Implementation of gui-mode consoles
9 /* INCLUDES ******************************************************************/
15 /* Not defined in any header file */
16 extern VOID WINAPI
PrivateCsrssManualGuiCheck(LONG Check
);
18 /* GLOBALS *******************************************************************/
20 typedef struct GUI_CONSOLE_DATA_TAG
28 CRITICAL_SECTION Lock
;
32 HMODULE ConsoleLibrary
;
34 WCHAR FontName
[LF_FACESIZE
];
41 DWORD NumberOfHistoryBuffers
;
42 DWORD HistoryBufferSize
;
46 COLORREF ScreenBackground
;
47 COLORREF PopupBackground
;
50 WCHAR szProcessName
[MAX_PATH
];
53 } GUI_CONSOLE_DATA
, *PGUI_CONSOLE_DATA
;
58 #define PM_CREATE_CONSOLE (WM_APP + 1)
59 #define PM_DESTROY_CONSOLE (WM_APP + 2)
61 #define CURSOR_BLINK_TIME 500
62 #define DEFAULT_ATTRIB (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
64 static BOOL ConsInitialized
= FALSE
;
65 static HWND NotifyWnd
;
67 typedef struct _GUICONSOLE_MENUITEM
70 const struct _GUICONSOLE_MENUITEM
*SubMenu
;
72 } GUICONSOLE_MENUITEM
, *PGUICONSOLE_MENUITEM
;
74 static const GUICONSOLE_MENUITEM GuiConsoleEditMenuItems
[] =
76 { IDS_MARK
, NULL
, ID_SYSTEM_EDIT_MARK
},
77 { IDS_COPY
, NULL
, ID_SYSTEM_EDIT_COPY
},
78 { IDS_PASTE
, NULL
, ID_SYSTEM_EDIT_PASTE
},
79 { IDS_SELECTALL
, NULL
, ID_SYSTEM_EDIT_SELECTALL
},
80 { IDS_SCROLL
, NULL
, ID_SYSTEM_EDIT_SCROLL
},
81 { IDS_FIND
, NULL
, ID_SYSTEM_EDIT_FIND
},
83 { 0, NULL
, 0 } /* End of list */
86 static const GUICONSOLE_MENUITEM GuiConsoleMainMenuItems
[] =
88 { (UINT
)-1, NULL
, 0 }, /* Separator */
89 { IDS_EDIT
, GuiConsoleEditMenuItems
, 0 },
90 { IDS_DEFAULTS
, NULL
, ID_SYSTEM_DEFAULTS
},
91 { IDS_PROPERTIES
, NULL
, ID_SYSTEM_PROPERTIES
},
93 { 0, NULL
, 0 } /* End of list */
96 static const COLORREF s_Colors
[] =
116 #define GuiConsoleRGBFromAttribute(GuiData, Attribute) ((GuiData)->Colors[(Attribute) & 0xF])
118 /* FUNCTIONS *****************************************************************/
121 GuiConsoleAppendMenuItems(HMENU hMenu
,
122 const GUICONSOLE_MENUITEM
*Items
)
125 WCHAR szMenuString
[255];
127 HINSTANCE hInst
= GetModuleHandleW(L
"win32csr");
131 if (Items
[i
].uID
!= (UINT
)-1)
133 if (LoadStringW(hInst
,
136 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
138 if (Items
[i
].SubMenu
!= NULL
)
140 hSubMenu
= CreatePopupMenu();
141 if (hSubMenu
!= NULL
)
143 GuiConsoleAppendMenuItems(hSubMenu
,
146 if (!AppendMenuW(hMenu
,
147 MF_STRING
| MF_POPUP
,
151 DestroyMenu(hSubMenu
);
172 }while(!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
176 GuiConsoleCreateSysMenu(PCSRSS_CONSOLE Console
)
179 hMenu
= GetSystemMenu(Console
->hWindow
,
183 GuiConsoleAppendMenuItems(hMenu
,
184 GuiConsoleMainMenuItems
);
185 DrawMenuBar(Console
->hWindow
);
190 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
192 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
193 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
197 GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId
, PHANDLE hProcHandle
, PHKEY hResult
, REGSAM samDesired
)
199 HANDLE hProcessToken
= NULL
;
204 UNICODE_STRING SidName
;
208 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| READ_CONTROL
, FALSE
, ProcessId
);
211 DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
215 if (!OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
217 DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
218 CloseHandle(hProcess
);
222 if (!GetTokenInformation(hProcessToken
, TokenUser
, (PVOID
)Buffer
, sizeof(Buffer
), &Length
))
224 DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
225 CloseHandle(hProcess
);
226 CloseHandle(hProcessToken
);
230 TokUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
231 if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName
, TokUser
, TRUE
)))
233 DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
237 res
= RegOpenKeyExW(HKEY_USERS
, SidName
.Buffer
, 0, samDesired
, hResult
);
238 RtlFreeUnicodeString(&SidName
);
240 CloseHandle(hProcessToken
);
242 *hProcHandle
= hProcess
;
244 CloseHandle(hProcess
);
246 if (res
!= ERROR_SUCCESS
)
253 GuiConsoleOpenUserSettings(PGUI_CONSOLE_DATA GuiData
, DWORD ProcessId
, PHKEY hSubKey
, REGSAM samDesired
, BOOL bCreate
)
255 WCHAR szProcessName
[MAX_PATH
];
256 WCHAR szBuffer
[MAX_PATH
];
257 UINT fLength
, wLength
;
258 DWORD dwBitmask
, dwLength
;
259 WCHAR CurDrive
[] = { 'A',':', 0 };
265 * console properties are stored under
268 * There are 3 ways to store console properties
270 * 1. use console title as subkey name
273 * 2. use application name as subkey name
275 * 3. use unexpanded path to console application.
276 * i.e. %SystemRoot%_system32_cmd.exe
279 DPRINT("GuiConsoleOpenUserSettings entered\n");
281 if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId
, &hProcess
, &hKey
, samDesired
))
283 DPRINT("GuiConsoleOpenUserRegistryPathPerProcessId failed\n");
287 /* FIXME we do not getting the process name so no menu will be loading, why ?*/
288 fLength
= GetProcessImageFileNameW(hProcess
, szProcessName
, sizeof(GuiData
->szProcessName
) / sizeof(WCHAR
));
289 CloseHandle(hProcess
);
291 //DPRINT1("szProcessName3 : %S\n",szProcessName);
295 DPRINT("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess
);
299 * try the process name as path
302 ptr
= wcsrchr(szProcessName
, L
'\\');
303 wcscpy(GuiData
->szProcessName
, ptr
);
305 swprintf(szBuffer
, L
"Console%s",ptr
);
306 DPRINT("#1 Path : %S\n", szBuffer
);
310 if (RegCreateKeyW(hKey
, szBuffer
, hSubKey
) == ERROR_SUCCESS
)
319 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
326 * try the "Shortcut to processname" as path
327 * FIXME: detect wheter the process was started as a shortcut
330 swprintf(szBuffer
, L
"Console\\Shortcut to %S", ptr
);
331 DPRINT("#2 Path : %S\n", szBuffer
);
332 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
334 swprintf(GuiData
->szProcessName
, L
"Shortcut to %S", ptr
);
340 * if the path contains \\Device\\HarddiskVolume1\... remove it
343 if (szProcessName
[0] == L
'\\')
345 dwBitmask
= GetLogicalDrives();
350 dwLength
= QueryDosDeviceW(CurDrive
, szBuffer
, MAX_PATH
);
353 if (!memcmp(szBuffer
, szProcessName
, (dwLength
-2)*sizeof(WCHAR
)))
355 wcscpy(szProcessName
, CurDrive
);
356 RtlMoveMemory(&szProcessName
[2], &szProcessName
[dwLength
-1], fLength
- dwLength
-1);
361 dwBitmask
= (dwBitmask
>> 1);
367 * last attempt: check whether the file is under %SystemRoot%
368 * and use path like Console\%SystemRoot%_dir_dir2_file.exe
371 wLength
= GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
374 if (!wcsncmp(szProcessName
, szBuffer
, wLength
))
376 /* replace slashes by underscores */
377 while((ptr
= wcschr(szProcessName
, L
'\\')))
380 swprintf(szBuffer
, L
"Console\\%%SystemRoot%%%S", &szProcessName
[wLength
]);
381 DPRINT("#3 Path : %S\n", szBuffer
);
382 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
384 swprintf(GuiData
->szProcessName
, L
"%%SystemRoot%%%S", &szProcessName
[wLength
]);
395 GuiConsoleWriteUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
)
398 PCSRSS_PROCESS_DATA ProcessData
;
400 if (Console
->ProcessList
.Flink
== &Console
->ProcessList
)
402 DPRINT("GuiConsoleWriteUserSettings: No Process!!!\n");
405 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
406 if (!GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
| KEY_WRITE
, TRUE
))
411 if (Console
->ActiveBuffer
->CursorInfo
.dwSize
<= 1)
413 RegDeleteKeyW(hKey
, L
"CursorSize");
417 RegSetValueExW(hKey
, L
"CursorSize", 0, REG_DWORD
, (const BYTE
*)&Console
->ActiveBuffer
->CursorInfo
.dwSize
, sizeof(DWORD
));
420 if (GuiData
->NumberOfHistoryBuffers
== 5)
422 RegDeleteKeyW(hKey
, L
"NumberOfHistoryBuffers");
426 RegSetValueExW(hKey
, L
"NumberOfHistoryBuffers", 0, REG_DWORD
, (const BYTE
*)&GuiData
->NumberOfHistoryBuffers
, sizeof(DWORD
));
429 if (GuiData
->HistoryBufferSize
== 50)
431 RegDeleteKeyW(hKey
, L
"HistoryBufferSize");
435 RegSetValueExW(hKey
, L
"HistoryBufferSize", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryBufferSize
, sizeof(DWORD
));
438 if (GuiData
->FullScreen
== FALSE
)
440 RegDeleteKeyW(hKey
, L
"FullScreen");
444 RegSetValueExW(hKey
, L
"FullScreen", 0, REG_DWORD
, (const BYTE
*)&GuiData
->FullScreen
, sizeof(DWORD
));
447 if ( GuiData
->QuickEdit
== FALSE
)
449 RegDeleteKeyW(hKey
, L
"QuickEdit");
453 RegSetValueExW(hKey
, L
"QuickEdit", 0, REG_DWORD
, (const BYTE
*)&GuiData
->QuickEdit
, sizeof(DWORD
));
456 if (GuiData
->InsertMode
== TRUE
)
458 RegDeleteKeyW(hKey
, L
"InsertMode");
462 RegSetValueExW(hKey
, L
"InsertMode", 0, REG_DWORD
, (const BYTE
*)&GuiData
->InsertMode
, sizeof(DWORD
));
465 if (GuiData
->HistoryNoDup
== FALSE
)
467 RegDeleteKeyW(hKey
, L
"HistoryNoDup");
471 RegSetValueExW(hKey
, L
"HistoryNoDup", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryNoDup
, sizeof(DWORD
));
474 if (GuiData
->ScreenText
== RGB(192, 192, 192))
477 * MS uses console attributes instead of real color
479 RegDeleteKeyW(hKey
, L
"ScreenText");
483 RegSetValueExW(hKey
, L
"ScreenText", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenText
, sizeof(COLORREF
));
486 if (GuiData
->ScreenBackground
== RGB(0, 0, 0))
488 RegDeleteKeyW(hKey
, L
"ScreenBackground");
492 RegSetValueExW(hKey
, L
"ScreenBackground", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenBackground
, sizeof(COLORREF
));
499 GuiConsoleReadUserSettings(HKEY hKey
, PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
501 DWORD dwNumSubKeys
= 0;
506 WCHAR szValueName
[MAX_PATH
];
507 WCHAR szValue
[LF_FACESIZE
] = L
"\0";
510 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
512 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
516 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
518 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
520 dwValue
= sizeof(Value
);
521 dwValueName
= MAX_PATH
;
523 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
525 if (dwType
== REG_SZ
)
528 * retry in case of string value
530 dwValue
= sizeof(szValue
);
531 dwValueName
= LF_FACESIZE
;
532 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
538 if (!wcscmp(szValueName
, L
"CursorSize"))
542 Buffer
->CursorInfo
.dwSize
= Value
;
544 else if (Value
== 0x64)
546 Buffer
->CursorInfo
.dwSize
= Value
;
549 else if (!wcscmp(szValueName
, L
"ScreenText"))
551 GuiData
->ScreenText
= Value
;
553 else if (!wcscmp(szValueName
, L
"ScreenBackground"))
555 GuiData
->ScreenBackground
= Value
;
557 else if (!wcscmp(szValueName
, L
"FaceName"))
559 wcscpy(GuiData
->FontName
, szValue
);
561 else if (!wcscmp(szValueName
, L
"FontSize"))
563 GuiData
->FontSize
= Value
;
565 else if (!wcscmp(szValueName
, L
"FontWeight"))
567 GuiData
->FontWeight
= Value
;
569 else if (!wcscmp(szValueName
, L
"HistoryNoDup"))
571 GuiData
->HistoryNoDup
= Value
;
573 else if (!wcscmp(szValueName
, L
"WindowSize"))
575 Console
->Size
.X
= LOWORD(Value
);
576 Console
->Size
.Y
= HIWORD(Value
);
578 else if (!wcscmp(szValueName
, L
"ScreenBufferSize"))
582 Buffer
->MaxX
= LOWORD(Value
);
583 Buffer
->MaxY
= HIWORD(Value
);
586 else if (!wcscmp(szValueName
, L
"FullScreen"))
588 GuiData
->FullScreen
= Value
;
590 else if (!wcscmp(szValueName
, L
"QuickEdit"))
592 GuiData
->QuickEdit
= Value
;
594 else if (!wcscmp(szValueName
, L
"InsertMode"))
596 GuiData
->InsertMode
= Value
;
601 GuiConsoleUseDefaults(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
604 * init guidata with default properties
607 wcscpy(GuiData
->FontName
, L
"DejaVu Sans Mono");
608 GuiData
->FontSize
= 0x0008000C; // font is 8x12
609 GuiData
->FontWeight
= FW_NORMAL
;
610 GuiData
->HistoryNoDup
= FALSE
;
611 GuiData
->FullScreen
= FALSE
;
612 GuiData
->QuickEdit
= FALSE
;
613 GuiData
->InsertMode
= TRUE
;
614 GuiData
->HistoryBufferSize
= 50;
615 GuiData
->NumberOfHistoryBuffers
= 5;
616 GuiData
->ScreenText
= RGB(192, 192, 192);
617 GuiData
->ScreenBackground
= RGB(0, 0, 0);
618 GuiData
->PopupText
= RGB(128, 0, 128);
619 GuiData
->PopupBackground
= RGB(255, 255, 255);
620 GuiData
->WindowPosition
= UINT_MAX
;
621 GuiData
->UseRasterFonts
= TRUE
;
622 memcpy(GuiData
->Colors
, s_Colors
, sizeof(s_Colors
));
624 Console
->Size
.X
= 80;
625 Console
->Size
.Y
= 25;
631 Buffer
->CursorInfo
.bVisible
= TRUE
;
632 Buffer
->CursorInfo
.dwSize
= CSR_DEFAULT_CURSOR_SIZE
;
638 GuiConsoleInitScrollbar(PCSRSS_CONSOLE Console
, HWND hwnd
)
641 PGUI_CONSOLE_DATA GuiData
= Console
->PrivateData
;
643 DWORD Width
= Console
->Size
.X
* GuiData
->CharWidth
+ 2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
));
644 DWORD Height
= Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
);
646 /* set scrollbar sizes */
647 sInfo
.cbSize
= sizeof(SCROLLINFO
);
648 sInfo
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_POS
;
650 if (Console
->ActiveBuffer
->MaxY
> Console
->Size
.Y
)
652 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
- 1;
653 sInfo
.nPage
= Console
->Size
.Y
;
654 sInfo
.nPos
= Console
->ActiveBuffer
->ShowY
;
655 SetScrollInfo(hwnd
, SB_VERT
, &sInfo
, TRUE
);
656 Width
+= GetSystemMetrics(SM_CXVSCROLL
);
657 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
661 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
664 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
666 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
- 1;
667 sInfo
.nPage
= Console
->Size
.X
;
668 sInfo
.nPos
= Console
->ActiveBuffer
->ShowX
;
669 SetScrollInfo(hwnd
, SB_HORZ
, &sInfo
, TRUE
);
670 Height
+= GetSystemMetrics(SM_CYHSCROLL
);
671 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
676 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
679 SetWindowPos(hwnd
, NULL
, 0, 0, Width
, Height
,
680 SWP_NOZORDER
| SWP_NOMOVE
| SWP_NOACTIVATE
);
684 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
686 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
687 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)Console
->PrivateData
;
692 PCSRSS_PROCESS_DATA ProcessData
;
695 Console
->hWindow
= hWnd
;
699 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
703 GuiConsoleUseDefaults(Console
, GuiData
, Console
->ActiveBuffer
);
704 if (Console
->ProcessList
.Flink
!= &Console
->ProcessList
)
706 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
707 if (GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
, FALSE
))
709 GuiConsoleReadUserSettings(hKey
, Console
, GuiData
, Console
->ActiveBuffer
);
714 InitializeCriticalSection(&GuiData
->Lock
);
716 GuiData
->LineBuffer
= (PWCHAR
)HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
717 Console
->Size
.X
* sizeof(WCHAR
));
719 GuiData
->Font
= CreateFontW(LOWORD(GuiData
->FontSize
),
720 0, //HIWORD(GuiData->FontSize),
728 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
729 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
731 if (NULL
== GuiData
->Font
)
733 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
734 DeleteCriticalSection(&GuiData
->Lock
);
735 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
741 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
742 DeleteObject(GuiData
->Font
);
743 DeleteCriticalSection(&GuiData
->Lock
);
744 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
747 OldFont
= SelectObject(Dc
, GuiData
->Font
);
750 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
752 DeleteObject(GuiData
->Font
);
753 DeleteCriticalSection(&GuiData
->Lock
);
754 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
757 if (! GetTextMetricsW(Dc
, &Metrics
))
759 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
760 SelectObject(Dc
, OldFont
);
762 DeleteObject(GuiData
->Font
);
763 DeleteCriticalSection(&GuiData
->Lock
);
764 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
767 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
768 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
770 /* Measure real char width more precisely if possible. */
771 if (GetTextExtentPoint32W(Dc
, L
"R", 1, &CharSize
))
772 GuiData
->CharWidth
= CharSize
.cx
;
774 SelectObject(Dc
, OldFont
);
777 GuiData
->CursorBlinkOn
= TRUE
;
778 GuiData
->ForceCursorOff
= FALSE
;
780 GuiData
->Selection
.left
= -1;
781 DPRINT("Console %p GuiData %p\n", Console
, GuiData
);
782 Console
->PrivateData
= GuiData
;
783 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
785 SetTimer(hWnd
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
786 GuiConsoleCreateSysMenu(Console
);
788 GuiData
->WindowSizeLock
= TRUE
;
789 GuiConsoleInitScrollbar(Console
, hWnd
);
790 GuiData
->WindowSizeLock
= FALSE
;
792 SetEvent(GuiData
->hGuiInitEvent
);
794 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
798 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
800 RECT oldRect
= GuiData
->Selection
;
804 RECT changeRect
= *rc
;
806 GuiData
->Selection
= *rc
;
808 changeRect
.left
*= GuiData
->CharWidth
;
809 changeRect
.top
*= GuiData
->CharHeight
;
810 changeRect
.right
*= GuiData
->CharWidth
;
811 changeRect
.bottom
*= GuiData
->CharHeight
;
813 if(rc
->left
!= oldRect
.left
||
814 rc
->top
!= oldRect
.top
||
815 rc
->right
!= oldRect
.right
||
816 rc
->bottom
!= oldRect
.bottom
)
818 if(oldRect
.left
!= -1)
822 oldRect
.left
*= GuiData
->CharWidth
;
823 oldRect
.top
*= GuiData
->CharHeight
;
824 oldRect
.right
*= GuiData
->CharWidth
;
825 oldRect
.bottom
*= GuiData
->CharHeight
;
827 /* calculate the region that needs to be updated */
828 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
830 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
832 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
834 InvalidateRgn(hWnd
, rgn1
, FALSE
);
844 InvalidateRect(hWnd
, &changeRect
, FALSE
);
848 else if(oldRect
.left
!= -1)
850 /* clear the selection */
851 GuiData
->Selection
.left
= -1;
852 oldRect
.left
*= GuiData
->CharWidth
;
853 oldRect
.top
*= GuiData
->CharHeight
;
854 oldRect
.right
*= GuiData
->CharWidth
;
855 oldRect
.bottom
*= GuiData
->CharHeight
;
856 InvalidateRect(hWnd
, &oldRect
, FALSE
);
862 GuiConsolePaint(PCSRSS_CONSOLE Console
,
863 PGUI_CONSOLE_DATA GuiData
,
867 PCSRSS_SCREEN_BUFFER Buff
;
868 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
869 ULONG Line
, Char
, Start
;
872 BYTE LastAttribute
, Attribute
;
873 ULONG CursorX
, CursorY
, CursorHeight
;
874 HBRUSH CursorBrush
, OldBrush
;
877 Buff
= Console
->ActiveBuffer
;
879 EnterCriticalSection(&Buff
->Header
.Lock
);
881 TopLine
= rc
->top
/ GuiData
->CharHeight
+ Buff
->ShowY
;
882 BottomLine
= (rc
->bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1 + Buff
->ShowY
;
883 LeftChar
= rc
->left
/ GuiData
->CharWidth
+ Buff
->ShowX
;
884 RightChar
= (rc
->right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1 + Buff
->ShowX
;
885 LastAttribute
= ConioCoordToPointer(Buff
, LeftChar
, TopLine
)[1];
887 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
));
888 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
>> 4));
890 if (BottomLine
>= Buff
->MaxY
) BottomLine
= Buff
->MaxY
- 1;
891 if (RightChar
>= Buff
->MaxX
) RightChar
= Buff
->MaxX
- 1;
893 OldFont
= SelectObject(hDC
,
896 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
898 From
= ConioCoordToPointer(Buff
, LeftChar
, Line
);
900 To
= GuiData
->LineBuffer
;
902 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
904 if (*(From
+ 1) != LastAttribute
)
907 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
908 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
912 To
= GuiData
->LineBuffer
;
913 Attribute
= *(From
+ 1);
914 if (Attribute
!= LastAttribute
)
916 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
));
917 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
>> 4));
918 LastAttribute
= Attribute
;
922 MultiByteToWideChar(Console
->OutputCodePage
,
933 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
934 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
936 RightChar
- Start
+ 1);
939 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
940 !GuiData
->ForceCursorOff
)
942 CursorX
= Buff
->CurrentX
;
943 CursorY
= Buff
->CurrentY
;
944 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
945 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
947 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
948 if (CursorHeight
< 1)
952 From
= ConioCoordToPointer(Buff
, Buff
->CurrentX
, Buff
->CurrentY
) + 1;
954 if (*From
!= DEFAULT_ATTRIB
)
956 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(GuiData
, *From
));
960 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
963 OldBrush
= SelectObject(hDC
,
966 (CursorX
- Buff
->ShowX
) * GuiData
->CharWidth
,
967 (CursorY
- Buff
->ShowY
) * GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
973 DeleteObject(CursorBrush
);
977 LeaveCriticalSection(&Buff
->Header
.Lock
);
984 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
988 PCSRSS_CONSOLE Console
;
989 PGUI_CONSOLE_DATA GuiData
;
991 hDC
= BeginPaint(hWnd
, &ps
);
993 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
994 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
996 GuiConsoleGetDataPointers(hWnd
,
999 if (Console
!= NULL
&& GuiData
!= NULL
&&
1000 Console
->ActiveBuffer
!= NULL
)
1002 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
1004 EnterCriticalSection(&GuiData
->Lock
);
1006 GuiConsolePaint(Console
,
1011 if (GuiData
->Selection
.left
!= -1)
1013 RECT rc
= GuiData
->Selection
;
1015 rc
.left
*= GuiData
->CharWidth
;
1016 rc
.top
*= GuiData
->CharHeight
;
1017 rc
.right
*= GuiData
->CharWidth
;
1018 rc
.bottom
*= GuiData
->CharHeight
;
1020 /* invert the selection */
1021 if (IntersectRect(&rc
,
1034 LeaveCriticalSection(&GuiData
->Lock
);
1039 EndPaint(hWnd
, &ps
);
1042 static VOID FASTCALL
1043 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1045 PCSRSS_CONSOLE Console
;
1046 PGUI_CONSOLE_DATA GuiData
;
1049 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1050 Message
.hwnd
= hWnd
;
1051 Message
.message
= msg
;
1052 Message
.wParam
= wParam
;
1053 Message
.lParam
= lParam
;
1055 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1057 /* clear the selection */
1058 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1061 ConioProcessKey(&Message
, Console
, FALSE
);
1064 static VOID FASTCALL
1065 GuiIntDrawRegion(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1069 RegionRect
.left
= (Region
->left
- Buff
->ShowX
) * GuiData
->CharWidth
;
1070 RegionRect
.top
= (Region
->top
- Buff
->ShowY
) * GuiData
->CharHeight
;
1071 RegionRect
.right
= (Region
->right
+ 1 - Buff
->ShowX
) * GuiData
->CharWidth
;
1072 RegionRect
.bottom
= (Region
->bottom
+ 1 - Buff
->ShowY
) * GuiData
->CharHeight
;
1074 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1078 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1080 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1082 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1084 GuiIntDrawRegion(Console
->ActiveBuffer
, GuiData
, Console
->hWindow
, Region
);
1088 static VOID FASTCALL
1089 GuiInvalidateCell(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1096 CellRect
.bottom
= y
;
1098 GuiIntDrawRegion(Buff
, GuiData
, Wnd
, &CellRect
);
1102 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1103 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1105 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1106 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1107 LONG CursorEndX
, CursorEndY
;
1110 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1115 if (0 != ScrolledLines
)
1117 ScrollRect
.left
= 0;
1119 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1120 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1122 if (GuiData
->Selection
.left
!= -1)
1124 /* scroll the selection */
1125 if (GuiData
->Selection
.top
> ScrolledLines
)
1127 GuiData
->Selection
.top
-= ScrolledLines
;
1128 GuiData
->Selection
.bottom
-= ScrolledLines
;
1130 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1132 GuiData
->Selection
.left
= -1;
1136 GuiData
->Selection
.top
= 0;
1137 GuiData
->Selection
.bottom
-= ScrolledLines
;
1141 ScrollWindowEx(Console
->hWindow
,
1143 -(ScrolledLines
* GuiData
->CharHeight
),
1151 GuiIntDrawRegion(Buff
, GuiData
, Console
->hWindow
, Region
);
1153 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1154 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1156 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1159 CursorEndX
= Buff
->CurrentX
;
1160 CursorEndY
= Buff
->CurrentY
;
1161 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1162 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1163 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1165 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1168 // Set up the update timer (very short interval) - this is a "hack" for getting the OS to
1169 // repaint the window without having it just freeze up and stay on the screen permanently.
1170 GuiData
->CursorBlinkOn
= TRUE
;
1171 SetTimer(Console
->hWindow
, CONGUI_UPDATE_TIMER
, CONGUI_UPDATE_TIME
, NULL
);
1175 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1179 if (Console
->ActiveBuffer
== Buff
)
1181 UpdateRect
.left
= Buff
->CurrentX
;
1182 UpdateRect
.top
= Buff
->CurrentY
;
1183 UpdateRect
.right
= UpdateRect
.left
;
1184 UpdateRect
.bottom
= UpdateRect
.top
;
1185 ConioDrawRegion(Console
, &UpdateRect
);
1192 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
1196 if (Console
->ActiveBuffer
== Buff
)
1198 /* Redraw char at old position (removes cursor) */
1199 UpdateRect
.left
= OldCursorX
;
1200 UpdateRect
.top
= OldCursorY
;
1201 UpdateRect
.right
= OldCursorX
;
1202 UpdateRect
.bottom
= OldCursorY
;
1203 ConioDrawRegion(Console
, &UpdateRect
);
1204 /* Redraw char at new position (shows cursor) */
1205 UpdateRect
.left
= Buff
->CurrentX
;
1206 UpdateRect
.top
= Buff
->CurrentY
;
1207 UpdateRect
.right
= UpdateRect
.left
;
1208 UpdateRect
.bottom
= UpdateRect
.top
;
1209 ConioDrawRegion(Console
, &UpdateRect
);
1216 GuiUpdateScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1218 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1220 if (Console
->ActiveBuffer
== Buff
)
1222 GuiData
->ScreenText
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
);
1223 GuiData
->ScreenBackground
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
>> 4);
1229 static VOID FASTCALL
1230 GuiConsoleHandleTimer(HWND hWnd
)
1232 PCSRSS_CONSOLE Console
;
1233 PGUI_CONSOLE_DATA GuiData
;
1234 PCSRSS_SCREEN_BUFFER Buff
;
1237 SetTimer(hWnd
, CONGUI_UPDATE_TIMER
, CURSOR_BLINK_TIME
, NULL
);
1239 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1241 Buff
= Console
->ActiveBuffer
;
1242 CursorRect
.left
= Buff
->CurrentX
;
1243 CursorRect
.top
= Buff
->CurrentY
;
1244 CursorRect
.right
= CursorRect
.left
;
1245 CursorRect
.bottom
= CursorRect
.top
;
1246 GuiDrawRegion(Console
, &CursorRect
);
1247 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1249 if((GuiData
->OldCursor
.x
!= Buff
->CurrentX
) || (GuiData
->OldCursor
.y
!= Buff
->CurrentY
))
1252 int OldScrollX
= -1, OldScrollY
= -1;
1253 int NewScrollX
= -1, NewScrollY
= -1;
1255 xScroll
.cbSize
= sizeof(SCROLLINFO
);
1256 xScroll
.fMask
= SIF_POS
;
1257 // Capture the original position of the scroll bars and save them.
1258 if(GetScrollInfo(hWnd
, SB_HORZ
, &xScroll
))OldScrollX
= xScroll
.nPos
;
1259 if(GetScrollInfo(hWnd
, SB_VERT
, &xScroll
))OldScrollY
= xScroll
.nPos
;
1261 // If we successfully got the info for the horizontal scrollbar
1264 if((Buff
->CurrentX
< Buff
->ShowX
)||(Buff
->CurrentX
>= (Buff
->ShowX
+ Console
->Size
.X
)))
1266 // Handle the horizontal scroll bar
1267 if(Buff
->CurrentX
>= Console
->Size
.X
) NewScrollX
= Buff
->CurrentX
- Console
->Size
.X
+ 1;
1268 else NewScrollX
= 0;
1272 NewScrollX
= OldScrollX
;
1275 // If we successfully got the info for the vertical scrollbar
1278 if((Buff
->CurrentY
< Buff
->ShowY
) || (Buff
->CurrentY
>= (Buff
->ShowY
+ Console
->Size
.Y
)))
1280 // Handle the vertical scroll bar
1281 if(Buff
->CurrentY
>= Console
->Size
.Y
) NewScrollY
= Buff
->CurrentY
- Console
->Size
.Y
+ 1;
1282 else NewScrollY
= 0;
1286 NewScrollY
= OldScrollY
;
1290 // Adjust scroll bars and refresh the window if the cursor has moved outside the visible area
1291 // NOTE: OldScroll# and NewScroll# will both be -1 (initial value) if the info for the respective scrollbar
1292 // was not obtained successfully in the previous steps. This means their difference is 0 (no scrolling)
1293 // and their associated scrollbar is left alone.
1294 if((OldScrollX
!= NewScrollX
) || (OldScrollY
!= NewScrollY
))
1296 Buff
->ShowX
= NewScrollX
;
1297 Buff
->ShowY
= NewScrollY
;
1298 ScrollWindowEx(hWnd
,
1299 (OldScrollX
- NewScrollX
) * GuiData
->CharWidth
,
1300 (OldScrollY
- NewScrollY
) * GuiData
->CharHeight
,
1308 xScroll
.nPos
= NewScrollX
;
1309 SetScrollInfo(hWnd
, SB_HORZ
, &xScroll
, TRUE
);
1313 xScroll
.nPos
= NewScrollY
;
1314 SetScrollInfo(hWnd
, SB_VERT
, &xScroll
, TRUE
);
1317 GuiData
->OldCursor
.x
= Buff
->CurrentX
;
1318 GuiData
->OldCursor
.y
= Buff
->CurrentY
;
1323 static VOID FASTCALL
1324 GuiConsoleHandleClose(HWND hWnd
)
1326 PCSRSS_CONSOLE Console
;
1327 PGUI_CONSOLE_DATA GuiData
;
1328 PLIST_ENTRY current_entry
;
1329 PCSRSS_PROCESS_DATA current
;
1331 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1333 EnterCriticalSection(&Console
->Header
.Lock
);
1335 current_entry
= Console
->ProcessList
.Flink
;
1336 while (current_entry
!= &Console
->ProcessList
)
1338 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1339 current_entry
= current_entry
->Flink
;
1341 /* FIXME: Windows will wait up to 5 seconds for the thread to exit.
1342 * We shouldn't wait here, though, since the console lock is entered.
1343 * A copy of the thread list probably needs to be made. */
1344 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1347 LeaveCriticalSection(&Console
->Header
.Lock
);
1350 static VOID FASTCALL
1351 GuiConsoleHandleNcDestroy(HWND hWnd
)
1353 PCSRSS_CONSOLE Console
;
1354 PGUI_CONSOLE_DATA GuiData
;
1357 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1359 Console
->PrivateData
= NULL
;
1360 DeleteCriticalSection(&GuiData
->Lock
);
1361 GetSystemMenu(hWnd
, TRUE
);
1362 if (GuiData
->ConsoleLibrary
)
1363 FreeLibrary(GuiData
->ConsoleLibrary
);
1365 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1368 static VOID FASTCALL
1369 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1371 PCSRSS_CONSOLE Console
;
1372 PGUI_CONSOLE_DATA GuiData
;
1376 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1377 if (Console
== NULL
|| GuiData
== NULL
) return;
1379 pt
= MAKEPOINTS(lParam
);
1381 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1382 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1383 rc
.right
= rc
.left
+ 1;
1384 rc
.bottom
= rc
.top
+ 1;
1386 GuiData
->SelectionStart
.x
= rc
.left
;
1387 GuiData
->SelectionStart
.y
= rc
.top
;
1391 GuiData
->MouseDown
= TRUE
;
1393 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1396 static VOID FASTCALL
1397 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1399 PCSRSS_CONSOLE Console
;
1400 PGUI_CONSOLE_DATA GuiData
;
1404 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1405 if (Console
== NULL
|| GuiData
== NULL
) return;
1406 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1408 pt
= MAKEPOINTS(lParam
);
1410 rc
.left
= GuiData
->SelectionStart
.x
;
1411 rc
.top
= GuiData
->SelectionStart
.y
;
1412 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1413 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1415 /* exchange left/top with right/bottom if required */
1416 if(rc
.left
>= rc
.right
)
1420 rc
.left
= max(rc
.right
- 1, 0);
1423 if(rc
.top
>= rc
.bottom
)
1427 rc
.top
= max(rc
.bottom
- 1, 0);
1428 rc
.bottom
= tmp
+ 1;
1431 GuiData
->MouseDown
= FALSE
;
1433 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1438 static VOID FASTCALL
1439 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1441 PCSRSS_CONSOLE Console
;
1442 PGUI_CONSOLE_DATA GuiData
;
1446 if (!(wParam
& MK_LBUTTON
)) return;
1448 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1449 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1451 pt
= MAKEPOINTS(lParam
);
1453 rc
.left
= GuiData
->SelectionStart
.x
;
1454 rc
.top
= GuiData
->SelectionStart
.y
;
1455 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1456 if (Console
->Size
.X
< rc
.right
)
1458 rc
.right
= Console
->Size
.X
;
1460 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1461 if (Console
->Size
.Y
< rc
.bottom
)
1463 rc
.bottom
= Console
->Size
.Y
;
1466 /* exchange left/top with right/bottom if required */
1467 if(rc
.left
>= rc
.right
)
1471 rc
.left
= max(rc
.right
- 1, 0);
1474 if(rc
.top
>= rc
.bottom
)
1478 rc
.top
= max(rc
.bottom
- 1, 0);
1479 rc
.bottom
= tmp
+ 1;
1482 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1485 static VOID FASTCALL
1486 GuiConsoleRightMouseDown(HWND hWnd
)
1488 PCSRSS_CONSOLE Console
;
1489 PGUI_CONSOLE_DATA GuiData
;
1491 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1492 if (Console
== NULL
|| GuiData
== NULL
) return;
1494 if (GuiData
->Selection
.left
== -1)
1496 /* FIXME - paste text from clipboard */
1500 /* FIXME - copy selection to clipboard */
1502 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1509 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1511 PCSRSS_CONSOLE Console
;
1512 APPLET_PROC CPLFunc
;
1513 TCHAR szBuffer
[MAX_PATH
];
1514 ConsoleInfo SharedInfo
;
1516 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1518 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1520 if (GuiData
== NULL
)
1522 DPRINT("GuiConsoleGetDataPointers failed\n");
1526 if (GuiData
->ConsoleLibrary
== NULL
)
1528 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1529 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1530 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1532 if (GuiData
->ConsoleLibrary
== NULL
)
1534 DPRINT1("failed to load console.dll");
1539 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1542 DPRINT("Error: Console.dll misses CPlApplet export\n");
1547 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1548 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1549 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1550 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1551 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1552 SharedInfo
.PopupText
= GuiData
->PopupText
;
1553 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1554 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1555 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1556 SharedInfo
.ScreenBuffer
= (DWORD
)MAKELONG(Console
->ActiveBuffer
->MaxX
, Console
->ActiveBuffer
->MaxY
);
1557 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1558 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1559 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1560 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1561 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1562 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1563 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1564 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1566 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1568 DPRINT("Error: failed to initialize console.dll\n");
1572 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1574 DPRINT("Error: console.dll returned unexpected CPL count\n");
1578 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1580 static LRESULT FASTCALL
1581 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, PGUI_CONSOLE_DATA GuiData
)
1587 case ID_SYSTEM_EDIT_MARK
:
1588 case ID_SYSTEM_EDIT_COPY
:
1589 case ID_SYSTEM_EDIT_PASTE
:
1590 case ID_SYSTEM_EDIT_SELECTALL
:
1591 case ID_SYSTEM_EDIT_SCROLL
:
1592 case ID_SYSTEM_EDIT_FIND
:
1595 case ID_SYSTEM_DEFAULTS
:
1596 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1599 case ID_SYSTEM_PROPERTIES
:
1600 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1604 Ret
= DefWindowProcW(hWnd
, WM_SYSCOMMAND
, wParam
, lParam
);
1610 static VOID FASTCALL
1611 GuiConsoleGetMinMaxInfo(HWND hWnd
, PMINMAXINFO minMaxInfo
)
1613 PCSRSS_CONSOLE Console
;
1614 PGUI_CONSOLE_DATA GuiData
;
1615 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1616 if((Console
== NULL
)|| (GuiData
== NULL
)) return;
1618 DWORD windx
= CONGUI_MIN_WIDTH
* GuiData
->CharWidth
+ 2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
));
1619 DWORD windy
= CONGUI_MIN_HEIGHT
* GuiData
->CharHeight
+ 2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
);
1621 minMaxInfo
->ptMinTrackSize
.x
= windx
;
1622 minMaxInfo
->ptMinTrackSize
.y
= windy
;
1624 windx
= (Console
->ActiveBuffer
->MaxX
) * GuiData
->CharWidth
+ 2 * (GetSystemMetrics(SM_CXFRAME
) + GetSystemMetrics(SM_CXEDGE
));
1625 windy
= (Console
->ActiveBuffer
->MaxY
) * GuiData
->CharHeight
+ 2 * (GetSystemMetrics(SM_CYFRAME
) + GetSystemMetrics(SM_CYEDGE
)) + GetSystemMetrics(SM_CYCAPTION
);
1627 if(Console
->Size
.X
< Console
->ActiveBuffer
->MaxX
) windy
+= GetSystemMetrics(SM_CYHSCROLL
); // window currently has a horizontal scrollbar
1628 if(Console
->Size
.Y
< Console
->ActiveBuffer
->MaxY
) windx
+= GetSystemMetrics(SM_CXVSCROLL
); // window currently has a vertical scrollbar
1630 minMaxInfo
->ptMaxTrackSize
.x
= windx
;
1631 minMaxInfo
->ptMaxTrackSize
.y
= windy
;
1633 static VOID FASTCALL
1634 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1636 PCSRSS_CONSOLE Console
;
1637 PGUI_CONSOLE_DATA GuiData
;
1638 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1639 if((Console
== NULL
) || (GuiData
== NULL
)) return;
1641 if ((GuiData
->WindowSizeLock
== FALSE
) && (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
))
1643 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1645 GuiData
->WindowSizeLock
= TRUE
;
1647 DWORD windx
= LOWORD(lParam
);
1648 DWORD windy
= HIWORD(lParam
);
1650 // Compensate for existing scroll bars (because lParam values do not accommodate scroll bar)
1651 if(Console
->Size
.X
< Buff
->MaxX
) windy
+= GetSystemMetrics(SM_CYHSCROLL
); // window currently has a horizontal scrollbar
1652 if(Console
->Size
.Y
< Buff
->MaxY
) windx
+= GetSystemMetrics(SM_CXVSCROLL
); // window currently has a vertical scrollbar
1654 DWORD charx
= windx
/ GuiData
->CharWidth
;
1655 DWORD chary
= windy
/ GuiData
->CharHeight
;
1657 // Character alignment (round size up or down)
1658 if((windx
% GuiData
->CharWidth
) >= (GuiData
->CharWidth
/ 2)) ++charx
;
1659 if((windy
% GuiData
->CharHeight
) >= (GuiData
->CharHeight
/ 2)) ++chary
;
1661 // Compensate for added scroll bars in new window
1662 if(charx
< Buff
->MaxX
)windy
-= GetSystemMetrics(SM_CYHSCROLL
); // new window will have a horizontal scroll bar
1663 if(chary
< Buff
->MaxY
)windx
-= GetSystemMetrics(SM_CXVSCROLL
); // new window will have a vertical scroll bar
1665 charx
= windx
/ GuiData
->CharWidth
;
1666 chary
= windy
/ GuiData
->CharHeight
;
1668 // Character alignment (round size up or down)
1669 if((windx
% GuiData
->CharWidth
) >= (GuiData
->CharWidth
/ 2)) ++charx
;
1670 if((windy
% GuiData
->CharHeight
) >= (GuiData
->CharHeight
/ 2)) ++chary
;
1673 if((charx
!= Console
->Size
.X
) || (chary
!= Console
->Size
.Y
))
1675 Console
->Size
.X
= (charx
<= Buff
->MaxX
) ? charx
: Buff
->MaxX
;
1676 Console
->Size
.Y
= (chary
<= Buff
->MaxY
) ? chary
: Buff
->MaxY
;
1679 GuiConsoleInitScrollbar(Console
, hWnd
);
1681 // Adjust the start of the visible area if we are attempting to show nonexistent areas
1682 if((Buff
->MaxX
- Buff
->ShowX
) < Console
->Size
.X
) Buff
->ShowX
= Buff
->MaxX
- Console
->Size
.X
;
1683 if((Buff
->MaxY
- Buff
->ShowY
) < Console
->Size
.Y
) Buff
->ShowY
= Buff
->MaxY
- Console
->Size
.Y
;
1684 InvalidateRect(hWnd
, NULL
, TRUE
);
1686 GuiData
->WindowSizeLock
= FALSE
;
1691 GuiConsoleHandleScrollbarMenu()
1695 hMenu
= CreatePopupMenu();
1698 DPRINT("CreatePopupMenu failed\n");
1701 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1702 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1703 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1704 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1705 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1706 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1707 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1708 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1709 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1710 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1714 static VOID FASTCALL
1715 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1718 PCSRSS_SCREEN_BUFFER ActiveBuffer
= Console
->ActiveBuffer
;
1719 BOOL SizeChanged
= FALSE
;
1721 EnterCriticalSection(&ActiveBuffer
->Header
.Lock
);
1723 /* apply text / background color */
1724 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1725 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1727 /* apply cursor size */
1728 ActiveBuffer
->CursorInfo
.dwSize
= min(max(pConInfo
->CursorSize
, 1), 100);
1730 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1731 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1733 if (windx
!= ActiveBuffer
->MaxX
|| windy
!= ActiveBuffer
->MaxY
)
1735 BYTE
* Buffer
= HeapAlloc(Win32CsrApiHeap
, 0, windx
* windy
* 2);
1747 value
= MAKEWORD(' ', ActiveBuffer
->DefaultAttrib
);
1749 DPRINT("MaxX %d MaxY %d windx %d windy %d value %04x DefaultAttrib %d\n",ActiveBuffer
->MaxX
, ActiveBuffer
->MaxY
, windx
, windy
, value
, ActiveBuffer
->DefaultAttrib
);
1750 OldBuffer
= ActiveBuffer
->Buffer
;
1752 for (CurrentY
= 0; CurrentY
< min(ActiveBuffer
->MaxY
, windy
); CurrentY
++)
1754 OldPtr
= ConioCoordToPointer(ActiveBuffer
, 0, CurrentY
);
1755 if (windx
<= ActiveBuffer
->MaxX
)
1758 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, windx
* 2);
1759 Offset
+= (windx
* 2);
1764 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, ActiveBuffer
->MaxX
* 2);
1765 Offset
+= (ActiveBuffer
->MaxX
* 2);
1767 diff
= windx
- ActiveBuffer
->MaxX
;
1768 /* zero new part of it */
1770 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
);
1772 for (i
= 0; i
< diff
; i
++)
1774 Buffer
[Offset
++] = ' ';
1775 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1781 if (windy
> ActiveBuffer
->MaxY
)
1783 diff
= windy
- ActiveBuffer
->MaxY
;
1785 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
* windx
);
1787 for (i
= 0; i
< diff
* windx
; i
++)
1789 Buffer
[Offset
++] = ' ';
1790 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1795 (void)InterlockedExchangePointer((PVOID
volatile *)&ActiveBuffer
->Buffer
, Buffer
);
1796 HeapFree(Win32CsrApiHeap
, 0, OldBuffer
);
1797 ActiveBuffer
->MaxX
= windx
;
1798 ActiveBuffer
->MaxY
= windy
;
1799 ActiveBuffer
->VirtualY
= 0;
1804 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1809 windx
= LOWORD(pConInfo
->WindowSize
);
1810 windy
= HIWORD(pConInfo
->WindowSize
);
1812 if (windx
> Console
->Size
.X
)
1814 PWCHAR LineBuffer
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
, windx
* sizeof(WCHAR
));
1817 HeapFree(Win32CsrApiHeap
, 0, GuiData
->LineBuffer
);
1818 GuiData
->LineBuffer
= LineBuffer
;
1822 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1828 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1831 Console
->Size
.X
= windx
;
1832 Console
->Size
.Y
= windy
;
1838 GuiData
->WindowSizeLock
= TRUE
;
1839 GuiConsoleInitScrollbar(Console
, pConInfo
->hConsoleWindow
);
1840 GuiData
->WindowSizeLock
= FALSE
;
1843 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1844 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1849 GuiConsoleHandleScroll(HWND hwnd
, UINT uMsg
, WPARAM wParam
)
1851 PCSRSS_CONSOLE Console
;
1852 PCSRSS_SCREEN_BUFFER Buff
;
1853 PGUI_CONSOLE_DATA GuiData
;
1856 int old_pos
, Maximum
;
1859 GuiConsoleGetDataPointers(hwnd
, &Console
, &GuiData
);
1860 if (Console
== NULL
|| GuiData
== NULL
)
1862 Buff
= Console
->ActiveBuffer
;
1864 if (uMsg
== WM_HSCROLL
)
1867 Maximum
= Buff
->MaxX
- Console
->Size
.X
;
1868 pShowXY
= &Buff
->ShowX
;
1873 Maximum
= Buff
->MaxY
- Console
->Size
.Y
;
1874 pShowXY
= &Buff
->ShowY
;
1877 /* set scrollbar sizes */
1878 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1879 sInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_PAGE
| SIF_TRACKPOS
;
1881 if (!GetScrollInfo(hwnd
, fnBar
, &sInfo
))
1886 old_pos
= sInfo
.nPos
;
1888 switch(LOWORD(wParam
))
1899 sInfo
.nPos
-= sInfo
.nPage
;
1903 sInfo
.nPos
+= sInfo
.nPage
;
1907 sInfo
.nPos
= sInfo
.nTrackPos
;
1911 sInfo
.nPos
= sInfo
.nMin
;
1915 sInfo
.nPos
= sInfo
.nMax
;
1922 sInfo
.nPos
= max(sInfo
.nPos
, 0);
1923 sInfo
.nPos
= min(sInfo
.nPos
, Maximum
);
1925 if (old_pos
!= sInfo
.nPos
)
1927 USHORT OldX
= Buff
->ShowX
;
1928 USHORT OldY
= Buff
->ShowY
;
1929 *pShowXY
= sInfo
.nPos
;
1931 ScrollWindowEx(hwnd
,
1932 (OldX
- Buff
->ShowX
) * GuiData
->CharWidth
,
1933 (OldY
- Buff
->ShowY
) * GuiData
->CharHeight
,
1940 sInfo
.fMask
= SIF_POS
;
1941 SetScrollInfo(hwnd
, fnBar
, &sInfo
, TRUE
);
1948 static LRESULT CALLBACK
1949 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1952 PGUI_CONSOLE_DATA GuiData
= NULL
;
1953 PCSRSS_CONSOLE Console
= NULL
;
1955 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1960 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
1963 GuiConsoleHandlePaint(hWnd
, (HDC
)wParam
);
1970 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
1973 GuiConsoleHandleTimer(hWnd
);
1976 GuiConsoleHandleClose(hWnd
);
1979 GuiConsoleHandleNcDestroy(hWnd
);
1981 case WM_LBUTTONDOWN
:
1982 GuiConsoleLeftMouseDown(hWnd
, lParam
);
1985 GuiConsoleLeftMouseUp(hWnd
, lParam
);
1987 case WM_RBUTTONDOWN
:
1988 GuiConsoleRightMouseDown(hWnd
);
1991 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
1994 Result
= GuiConsoleHandleSysMenuCommand(hWnd
, wParam
, lParam
, GuiData
);
1998 Result
= GuiConsoleHandleScroll(hWnd
, msg
, wParam
);
2000 case WM_GETMINMAXINFO
:
2001 GuiConsoleGetMinMaxInfo(hWnd
, (PMINMAXINFO
)lParam
);
2004 GuiConsoleResize(hWnd
, wParam
, lParam
);
2006 case PM_APPLY_CONSOLE_INFO
:
2007 GuiApplyUserSettings(Console
, GuiData
, (PConsoleInfo
)wParam
);
2010 GuiConsoleWriteUserSettings(Console
, GuiData
);
2014 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2021 static LRESULT CALLBACK
2022 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2027 PWCHAR Buffer
, Title
;
2028 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
2035 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
2037 case PM_CREATE_CONSOLE
:
2038 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
2039 Console
->Title
.Length
+ sizeof(WCHAR
));
2042 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
2043 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
2050 NewWindow
= CreateWindowExW(WS_EX_CLIENTEDGE
,
2051 L
"ConsoleWindowClass",
2053 WS_OVERLAPPEDWINDOW
| WS_HSCROLL
| WS_VSCROLL
,
2060 (HINSTANCE
) GetModuleHandleW(NULL
),
2064 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
2066 if (NULL
!= NewWindow
)
2068 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
2069 ShowWindow(NewWindow
, SW_SHOW
);
2071 return (LRESULT
) NewWindow
;
2072 case PM_DESTROY_CONSOLE
:
2073 /* Window creation is done using a PostMessage(), so it's possible that the
2074 * window that we want to destroy doesn't exist yet. So first empty the message
2076 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
2078 TranslateMessage(&Msg
);
2079 DispatchMessageW(&Msg
);
2081 DestroyWindow(Console
->hWindow
);
2082 Console
->hWindow
= NULL
;
2083 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
2085 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
2086 if (0 == WindowCount
)
2089 DestroyWindow(hWnd
);
2090 PrivateCsrssManualGuiCheck(-1);
2095 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2100 GuiConsoleGuiThread(PVOID Data
)
2103 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
2105 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
2107 WS_OVERLAPPEDWINDOW
,
2114 (HINSTANCE
) GetModuleHandleW(NULL
),
2116 if (NULL
== NotifyWnd
)
2118 PrivateCsrssManualGuiCheck(-1);
2119 SetEvent(*GraphicsStartupEvent
);
2123 SetEvent(*GraphicsStartupEvent
);
2125 while(GetMessageW(&msg
, NULL
, 0, 0))
2127 TranslateMessage(&msg
);
2128 DispatchMessageW(&msg
);
2134 static BOOL FASTCALL
2139 if (NULL
== NotifyWnd
)
2141 PrivateCsrssManualGuiCheck(+1);
2144 wc
.cbSize
= sizeof(WNDCLASSEXW
);
2145 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
2146 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
2148 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
2151 wc
.hbrBackground
= NULL
;
2152 wc
.lpszMenuName
= NULL
;
2156 if (RegisterClassExW(&wc
) == 0)
2158 DPRINT1("Failed to register notify wndproc\n");
2162 wc
.cbSize
= sizeof(WNDCLASSEXW
);
2163 wc
.lpszClassName
= L
"ConsoleWindowClass";
2164 wc
.lpfnWndProc
= GuiConsoleWndProc
;
2166 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
2167 wc
.hIcon
= LoadIconW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1));
2168 wc
.hCursor
= LoadCursorW(NULL
, (LPCWSTR
) IDC_ARROW
);
2169 wc
.hbrBackground
= CreateSolidBrush(RGB(0,0,0));
2170 wc
.lpszMenuName
= NULL
;
2173 wc
.hIconSm
= LoadImageW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1), IMAGE_ICON
,
2174 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
2176 if (RegisterClassExW(&wc
) == 0)
2178 DPRINT1("Failed to register console wndproc\n");
2186 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
2188 Buffer
->DefaultAttrib
= DEFAULT_ATTRIB
;
2192 GuiChangeTitle(PCSRSS_CONSOLE Console
)
2194 PWCHAR Buffer
, Title
;
2196 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
2197 Console
->Title
.Length
+ sizeof(WCHAR
));
2200 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
2201 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
2209 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Title
);
2213 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
2220 GuiChangeIcon(PCSRSS_CONSOLE Console
, HICON hWindowIcon
)
2222 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)hWindowIcon
);
2223 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hWindowIcon
);
2229 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
2231 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
2234 static CSRSS_CONSOLE_VTBL GuiVtbl
=
2236 GuiInitScreenBuffer
,
2241 GuiUpdateScreenInfo
,
2248 GuiInitConsole(PCSRSS_CONSOLE Console
)
2250 HANDLE GraphicsStartupEvent
;
2251 HANDLE ThreadHandle
;
2252 PGUI_CONSOLE_DATA GuiData
;
2254 if (! ConsInitialized
)
2256 ConsInitialized
= TRUE
;
2259 ConsInitialized
= FALSE
;
2260 return STATUS_UNSUCCESSFUL
;
2264 Console
->Vtbl
= &GuiVtbl
;
2265 if (NULL
== NotifyWnd
)
2267 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2268 if (NULL
== GraphicsStartupEvent
)
2270 return STATUS_UNSUCCESSFUL
;
2273 ThreadHandle
= CreateThread(NULL
,
2275 GuiConsoleGuiThread
,
2276 (PVOID
) &GraphicsStartupEvent
,
2279 if (NULL
== ThreadHandle
)
2281 NtClose(GraphicsStartupEvent
);
2282 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
2283 return STATUS_UNSUCCESSFUL
;
2285 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
2286 CloseHandle(ThreadHandle
);
2288 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
2289 CloseHandle(GraphicsStartupEvent
);
2291 if (NULL
== NotifyWnd
)
2293 DPRINT1("Win32Csr: Failed to create notification window.\n");
2294 return STATUS_UNSUCCESSFUL
;
2297 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
2298 sizeof(GUI_CONSOLE_DATA
));
2301 DPRINT1("Win32Csr: Failed to create GUI_CONSOLE_DATA\n");
2302 return STATUS_UNSUCCESSFUL
;
2305 Console
->PrivateData
= (PVOID
) GuiData
;
2307 * we need to wait untill the GUI has been fully initialized
2308 * to retrieve custom settings i.e. WindowSize etc..
2309 * Ideally we could use SendNotifyMessage for this but its not
2313 GuiData
->hGuiInitEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2314 /* create console */
2315 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
2317 /* wait untill initialization has finished */
2318 WaitForSingleObject(GuiData
->hGuiInitEvent
, INFINITE
);
2319 DPRINT1("received event Console %p GuiData %p X %d Y %d\n", Console
, Console
->PrivateData
, Console
->Size
.X
, Console
->Size
.Y
);
2320 CloseHandle(GuiData
->hGuiInitEvent
);
2321 GuiData
->hGuiInitEvent
= NULL
;
2323 return STATUS_SUCCESS
;