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 ******************************************************************/
16 /* Not defined in any header file */
17 extern VOID STDCALL
PrivateCsrssManualGuiCheck(LONG Check
);
19 /* GLOBALS *******************************************************************/
21 typedef struct GUI_CONSOLE_DATA_TAG
29 CRITICAL_SECTION Lock
;
33 HMODULE ConsoleLibrary
;
37 WCHAR FontName
[LF_FACESIZE
];
44 DWORD NumberOfHistoryBuffers
;
45 DWORD HistoryBufferSize
;
47 DWORD ScreenBufferSize
;
50 COLORREF ScreenBackground
;
51 COLORREF PopupBackground
;
54 WCHAR szProcessName
[MAX_PATH
];
55 } GUI_CONSOLE_DATA
, *PGUI_CONSOLE_DATA
;
60 #define PM_CREATE_CONSOLE (WM_APP + 1)
61 #define PM_DESTROY_CONSOLE (WM_APP + 2)
63 #define CURSOR_BLINK_TIME 500
64 #define DEFAULT_ATTRIB (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY)
66 static BOOL ConsInitialized
= FALSE
;
67 static HWND NotifyWnd
;
69 typedef struct _GUICONSOLE_MENUITEM
72 const struct _GUICONSOLE_MENUITEM
*SubMenu
;
74 } GUICONSOLE_MENUITEM
, *PGUICONSOLE_MENUITEM
;
76 static const GUICONSOLE_MENUITEM GuiConsoleEditMenuItems
[] =
78 { IDS_MARK
, NULL
, ID_SYSTEM_EDIT_MARK
},
79 { IDS_COPY
, NULL
, ID_SYSTEM_EDIT_COPY
},
80 { IDS_PASTE
, NULL
, ID_SYSTEM_EDIT_PASTE
},
81 { IDS_SELECTALL
, NULL
, ID_SYSTEM_EDIT_SELECTALL
},
82 { IDS_SCROLL
, NULL
, ID_SYSTEM_EDIT_SCROLL
},
83 { IDS_FIND
, NULL
, ID_SYSTEM_EDIT_FIND
},
85 { 0, NULL
, 0 } /* End of list */
88 static const GUICONSOLE_MENUITEM GuiConsoleMainMenuItems
[] =
90 { (UINT
)-1, NULL
, 0 }, /* Separator */
91 { IDS_EDIT
, GuiConsoleEditMenuItems
, 0 },
92 { IDS_DEFAULTS
, NULL
, ID_SYSTEM_DEFAULTS
},
93 { IDS_PROPERTIES
, NULL
, ID_SYSTEM_PROPERTIES
},
95 { 0, NULL
, 0 } /* End of list */
98 static const COLORREF s_Colors
[] =
118 /* FUNCTIONS *****************************************************************/
121 GuiConsoleAppendMenuItems(HMENU hMenu
,
122 const GUICONSOLE_MENUITEM
*Items
)
125 WCHAR szMenuString
[255];
128 for (i
= 0; Items
[i
].uID
!= 0; i
++)
130 if (Items
[i
].uID
!= (UINT
)-1)
132 if (LoadStringW(Win32CsrDllHandle
,
135 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
137 if (Items
[i
].SubMenu
!= NULL
)
139 hSubMenu
= CreatePopupMenu();
140 if (hSubMenu
!= NULL
)
142 GuiConsoleAppendMenuItems(hSubMenu
,
145 if (!AppendMenuW(hMenu
,
146 MF_STRING
| MF_POPUP
,
150 DestroyMenu(hSubMenu
);
174 GuiConsoleCreateSysMenu(PCSRSS_CONSOLE Console
)
178 hMenu
= GetSystemMenu(Console
->hWindow
,
182 GuiConsoleAppendMenuItems(hMenu
,
183 GuiConsoleMainMenuItems
);
188 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
190 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
191 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
195 GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId
, PHANDLE hProcHandle
, PHKEY hResult
, REGSAM samDesired
)
197 HANDLE hProcessToken
= NULL
;
202 UNICODE_STRING SidName
;
206 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| READ_CONTROL
, FALSE
, ProcessId
);
209 DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
213 if (!OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
215 DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
216 CloseHandle(hProcess
);
220 if (!GetTokenInformation(hProcessToken
, TokenUser
, (PVOID
)Buffer
, sizeof(Buffer
), &Length
))
222 DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
223 CloseHandle(hProcess
);
224 CloseHandle(hProcessToken
);
228 TokUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
229 if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName
, TokUser
, TRUE
)))
231 DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
235 res
= RegOpenKeyExW(HKEY_USERS
, SidName
.Buffer
, 0, samDesired
, hResult
);
236 RtlFreeUnicodeString(&SidName
);
238 CloseHandle(hProcessToken
);
240 *hProcHandle
= hProcess
;
242 CloseHandle(hProcess
);
244 if (res
!= ERROR_SUCCESS
)
251 GuiConsoleOpenUserSettings(PGUI_CONSOLE_DATA GuiData
, DWORD ProcessId
, PHKEY hSubKey
, REGSAM samDesired
, BOOL bCreate
)
253 WCHAR szProcessName
[MAX_PATH
];
254 WCHAR szBuffer
[MAX_PATH
];
255 UINT fLength
, wLength
;
256 DWORD dwBitmask
, dwLength
;
257 WCHAR CurDrive
[] = { 'A',':', 0 };
263 * console properties are stored under
266 * There are 3 ways to store console properties
268 * 1. use console title as subkey name
271 * 2. use application name as subkey name
273 * 3. use unexpanded path to console application.
274 * i.e. %SystemRoot%_system32_cmd.exe
277 DPRINT("GuiConsoleOpenUserSettings entered\n");
279 if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId
, &hProcess
, &hKey
, samDesired
))
281 DPRINT("GuiConsoleOpenUserRegistryPathPerProcessId failed\n");
285 fLength
= GetProcessImageFileNameW(hProcess
, szProcessName
, sizeof(GuiData
->szProcessName
) / sizeof(WCHAR
));
286 CloseHandle(hProcess
);
290 DPRINT("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess
);
294 * try the process name as path
297 ptr
= wcsrchr(szProcessName
, L
'\\');
298 wcscpy(GuiData
->szProcessName
, ptr
);
300 swprintf(szBuffer
, L
"Console%s",ptr
);
301 DPRINT("#1 Path : %S\n", szBuffer
);
305 if (RegCreateKeyW(hKey
, szBuffer
, hSubKey
) == ERROR_SUCCESS
)
314 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
321 * try the "Shortcut to processname" as path
322 * FIXME: detect wheter the process was started as a shortcut
325 swprintf(szBuffer
, L
"Console\\Shortcut to %S", ptr
);
326 DPRINT("#2 Path : %S\n", szBuffer
);
327 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
329 swprintf(GuiData
->szProcessName
, L
"Shortcut to %S", ptr
);
335 * if the path contains \\Device\\HarddiskVolume1\... remove it
338 if (szProcessName
[0] == L
'\\')
340 dwBitmask
= GetLogicalDrives();
345 dwLength
= QueryDosDeviceW(CurDrive
, szBuffer
, MAX_PATH
);
348 if (!memcmp(szBuffer
, szProcessName
, (dwLength
-2)*sizeof(WCHAR
)))
350 wcscpy(szProcessName
, CurDrive
);
351 RtlMoveMemory(&szProcessName
[2], &szProcessName
[dwLength
-1], fLength
- dwLength
-1);
356 dwBitmask
= (dwBitmask
>> 1);
362 * last attempt: check whether the file is under %SystemRoot%
363 * and use path like Console\%SystemRoot%_dir_dir2_file.exe
366 wLength
= GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
369 if (!wcsncmp(szProcessName
, szBuffer
, wLength
))
371 /* replace slashes by underscores */
372 while((ptr
= wcschr(szProcessName
, L
'\\')))
375 swprintf(szBuffer
, L
"Console\\\%SystemRoot\%%S", &szProcessName
[wLength
]);
376 DPRINT("#3 Path : %S\n", szBuffer
);
377 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
379 swprintf(GuiData
->szProcessName
, L
"\%SystemRoot\%%S", &szProcessName
[wLength
]);
390 GuiConsoleWriteUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
)
393 PCSRSS_PROCESS_DATA ProcessData
;
395 if (Console
->ProcessList
.Flink
== &Console
->ProcessList
)
397 DPRINT("GuiConsoleWriteUserSettings: No Process!!!\n");
400 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
401 if (!GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
| KEY_WRITE
, TRUE
))
406 if (Console
->ActiveBuffer
->CursorInfo
.dwSize
<= 1)
408 RegDeleteKeyW(hKey
, L
"CursorSize");
412 RegSetValueExW(hKey
, L
"CursorSize", 0, REG_DWORD
, (const BYTE
*)&Console
->ActiveBuffer
->CursorInfo
.dwSize
, sizeof(DWORD
));
415 if (GuiData
->NumberOfHistoryBuffers
== 5)
417 RegDeleteKeyW(hKey
, L
"NumberOfHistoryBuffers");
421 RegSetValueExW(hKey
, L
"NumberOfHistoryBuffers", 0, REG_DWORD
, (const BYTE
*)&GuiData
->NumberOfHistoryBuffers
, sizeof(DWORD
));
424 if (GuiData
->HistoryBufferSize
== 50)
426 RegDeleteKeyW(hKey
, L
"HistoryBufferSize");
430 RegSetValueExW(hKey
, L
"HistoryBufferSize", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryBufferSize
, sizeof(DWORD
));
433 if (GuiData
->FullScreen
== FALSE
)
435 RegDeleteKeyW(hKey
, L
"FullScreen");
439 RegSetValueExW(hKey
, L
"FullScreen", 0, REG_DWORD
, (const BYTE
*)&GuiData
->FullScreen
, sizeof(DWORD
));
442 if ( GuiData
->QuickEdit
== FALSE
)
444 RegDeleteKeyW(hKey
, L
"QuickEdit");
448 RegSetValueExW(hKey
, L
"QuickEdit", 0, REG_DWORD
, (const BYTE
*)&GuiData
->QuickEdit
, sizeof(DWORD
));
451 if (GuiData
->InsertMode
== TRUE
)
453 RegDeleteKeyW(hKey
, L
"InsertMode");
457 RegSetValueExW(hKey
, L
"InsertMode", 0, REG_DWORD
, (const BYTE
*)&GuiData
->InsertMode
, sizeof(DWORD
));
460 if (GuiData
->HistoryNoDup
== FALSE
)
462 RegDeleteKeyW(hKey
, L
"HistoryNoDup");
466 RegSetValueExW(hKey
, L
"HistoryNoDup", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryNoDup
, sizeof(DWORD
));
469 if (GuiData
->ScreenText
== RGB(192, 192, 192))
472 * MS uses console attributes instead of real color
474 RegDeleteKeyW(hKey
, L
"ScreenText");
478 RegSetValueExW(hKey
, L
"ScreenText", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenText
, sizeof(COLORREF
));
481 if (GuiData
->ScreenBackground
== RGB(0, 0, 0))
483 RegDeleteKeyW(hKey
, L
"ScreenBackground");
487 RegSetValueExW(hKey
, L
"ScreenBackground", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenBackground
, sizeof(COLORREF
));
494 GuiConsoleReadUserSettings(HKEY hKey
, PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
496 DWORD dwNumSubKeys
= 0;
501 WCHAR szValueName
[MAX_PATH
];
502 WCHAR szValue
[MAX_PATH
];
505 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
507 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
511 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
513 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
515 dwValue
= sizeof(Value
);
516 dwValueName
= MAX_PATH
;
518 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
520 if (dwType
== REG_SZ
)
523 * retry in case of string value
525 dwValue
= sizeof(szValue
);
526 dwValueName
= MAX_PATH
;
527 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
533 if (!wcscmp(szValueName
, L
"CursorSize"))
537 Buffer
->CursorInfo
.dwSize
= Value
;
539 else if (Value
== 0x64)
541 Buffer
->CursorInfo
.dwSize
= Value
;
544 else if (!wcscmp(szValueName
, L
"ScreenText"))
546 GuiData
->ScreenText
= Value
;
548 else if (!wcscmp(szValueName
, L
"ScreenBackground"))
550 GuiData
->ScreenBackground
= Value
;
552 else if (!wcscmp(szValueName
, L
"FaceName"))
554 wcscpy(GuiData
->FontName
, szValue
);
556 else if (!wcscmp(szValueName
, L
"FontSize"))
558 GuiData
->FontSize
= Value
;
560 else if (!wcscmp(szValueName
, L
"FontWeight"))
562 GuiData
->FontWeight
= Value
;
564 else if (!wcscmp(szValueName
, L
"HistoryNoDup"))
566 GuiData
->HistoryNoDup
= Value
;
568 else if (!wcscmp(szValueName
, L
"WindowSize"))
570 Console
->Size
.X
= LOWORD(Value
);
571 Console
->Size
.Y
= HIWORD(Value
);
573 else if (!wcscmp(szValueName
, L
"ScreenBufferSize"))
577 Buffer
->MaxX
= LOWORD(Value
);
578 Buffer
->MaxY
= HIWORD(Value
);
581 else if (!wcscmp(szValueName
, L
"FullScreen"))
583 GuiData
->FullScreen
= Value
;
585 else if (!wcscmp(szValueName
, L
"QuickEdit"))
587 GuiData
->QuickEdit
= Value
;
589 else if (!wcscmp(szValueName
, L
"InsertMode"))
591 GuiData
->InsertMode
= Value
;
596 GuiConsoleUseDefaults(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
599 * init guidata with default properties
602 wcscpy(GuiData
->FontName
, L
"Bitstream Vera Sans Mono");
603 GuiData
->FontSize
= 0x0008000C; // font is 8x12
604 GuiData
->FontWeight
= FW_NORMAL
;
605 GuiData
->HistoryNoDup
= FALSE
;
606 GuiData
->FullScreen
= FALSE
;
607 GuiData
->QuickEdit
= FALSE
;
608 GuiData
->InsertMode
= TRUE
;
609 GuiData
->HistoryBufferSize
= 50;
610 GuiData
->NumberOfHistoryBuffers
= 5;
611 GuiData
->ScreenText
= RGB(192, 192, 192);
612 GuiData
->ScreenBackground
= RGB(0, 0, 0);
613 GuiData
->PopupText
= RGB(128, 0, 128);
614 GuiData
->PopupBackground
= RGB(255, 255, 255);
615 GuiData
->WindowPosition
= UINT_MAX
;
616 GuiData
->ScreenBufferSize
= MAKELONG(80, 300); //FIXME
617 GuiData
->UseRasterFonts
= TRUE
;
618 memcpy(GuiData
->Colors
, s_Colors
, sizeof(s_Colors
));
620 Console
->Size
.X
= 80;
621 Console
->Size
.Y
= 25;
627 Buffer
->CursorInfo
.bVisible
= TRUE
;
628 Buffer
->CursorInfo
.dwSize
= 5;
635 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
638 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
639 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)Console
->PrivateData
;
643 PCSRSS_PROCESS_DATA ProcessData
;
646 Console
->hWindow
= hWnd
;
650 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
654 GuiConsoleUseDefaults(Console
, GuiData
, Console
->ActiveBuffer
);
655 if (Console
->ProcessList
.Flink
!= &Console
->ProcessList
)
657 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
658 if (GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
, FALSE
))
660 GuiConsoleReadUserSettings(hKey
, Console
, GuiData
, Console
->ActiveBuffer
);
665 InitializeCriticalSection(&GuiData
->Lock
);
667 GuiData
->LineBuffer
= (PWCHAR
)HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
668 Console
->Size
.X
* sizeof(WCHAR
));
670 GuiData
->Font
= CreateFontW(LOWORD(GuiData
->FontSize
),
671 0, //HIWORD(GuiData->FontSize),
679 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
680 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
682 if (NULL
== GuiData
->Font
)
684 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
685 DeleteCriticalSection(&GuiData
->Lock
);
686 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
692 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
693 DeleteObject(GuiData
->Font
);
694 DeleteCriticalSection(&GuiData
->Lock
);
695 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
698 OldFont
= SelectObject(Dc
, GuiData
->Font
);
701 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
703 DeleteObject(GuiData
->Font
);
704 DeleteCriticalSection(&GuiData
->Lock
);
705 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
708 if (! GetTextMetricsW(Dc
, &Metrics
))
710 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
711 SelectObject(Dc
, OldFont
);
713 DeleteObject(GuiData
->Font
);
714 DeleteCriticalSection(&GuiData
->Lock
);
715 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
718 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
719 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
720 SelectObject(Dc
, OldFont
);
723 GuiData
->CursorBlinkOn
= TRUE
;
724 GuiData
->ForceCursorOff
= FALSE
;
726 GuiData
->Selection
.left
= -1;
727 DPRINT("Console %p GuiData %p\n", Console
, GuiData
);
728 Console
->PrivateData
= GuiData
;
729 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
731 GetWindowRect(hWnd
, &Rect
);
732 Rect
.right
= Rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+
733 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
734 Rect
.bottom
= Rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+
735 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
736 MoveWindow(hWnd
, Rect
.left
, Rect
.top
, Rect
.right
- Rect
.left
,
737 Rect
.bottom
- Rect
.top
, FALSE
);
739 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
740 GuiConsoleCreateSysMenu(Console
);
741 SetEvent(GuiData
->hGuiInitEvent
);
743 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
746 static COLORREF FASTCALL
747 GuiConsoleRGBFromAttribute(BYTE Attribute
)
749 int Red
= (Attribute
& 0x04 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
750 int Green
= (Attribute
& 0x02 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
751 int Blue
= (Attribute
& 0x01 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
753 return RGB(Red
, Green
, Blue
);
757 GuiConsoleSetTextColors(HDC Dc
, BYTE Attribute
, PCSRSS_SCREEN_BUFFER Buff
, COLORREF TextColor
, COLORREF BkColor
)
759 if (Attribute
!= Buff
->DefaultAttrib
)
761 SetTextColor(Dc
, GuiConsoleRGBFromAttribute(Attribute
& 0x0f));
762 SetBkColor(Dc
, GuiConsoleRGBFromAttribute((Attribute
& 0xf0) >> 4));
766 SetTextColor(Dc
, TextColor
);
767 SetBkColor(Dc
, BkColor
);
772 GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff
, ULONG
*CursorX
, ULONG
*CursorY
)
774 *CursorX
= Buff
->CurrentX
;
775 if (Buff
->CurrentY
< Buff
->ShowY
)
777 *CursorY
= Buff
->MaxY
- Buff
->ShowY
+ Buff
->CurrentY
;
781 *CursorY
= Buff
->CurrentY
- Buff
->ShowY
;
787 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
789 RECT oldRect
= GuiData
->Selection
;
793 RECT changeRect
= *rc
;
795 GuiData
->Selection
= *rc
;
797 changeRect
.left
*= GuiData
->CharWidth
;
798 changeRect
.top
*= GuiData
->CharHeight
;
799 changeRect
.right
*= GuiData
->CharWidth
;
800 changeRect
.bottom
*= GuiData
->CharHeight
;
802 if(rc
->left
!= oldRect
.left
||
803 rc
->top
!= oldRect
.top
||
804 rc
->right
!= oldRect
.right
||
805 rc
->bottom
!= oldRect
.bottom
)
807 if(oldRect
.left
!= -1)
811 oldRect
.left
*= GuiData
->CharWidth
;
812 oldRect
.top
*= GuiData
->CharHeight
;
813 oldRect
.right
*= GuiData
->CharWidth
;
814 oldRect
.bottom
*= GuiData
->CharHeight
;
816 /* calculate the region that needs to be updated */
817 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
819 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
821 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
823 InvalidateRgn(hWnd
, rgn1
, FALSE
);
833 InvalidateRect(hWnd
, &changeRect
, FALSE
);
837 else if(oldRect
.left
!= -1)
839 /* clear the selection */
840 GuiData
->Selection
.left
= -1;
841 oldRect
.left
*= GuiData
->CharWidth
;
842 oldRect
.top
*= GuiData
->CharHeight
;
843 oldRect
.right
*= GuiData
->CharWidth
;
844 oldRect
.bottom
*= GuiData
->CharHeight
;
845 InvalidateRect(hWnd
, &oldRect
, FALSE
);
851 GuiConsolePaint(PCSRSS_CONSOLE Console
,
852 PGUI_CONSOLE_DATA GuiData
,
856 PCSRSS_SCREEN_BUFFER Buff
;
857 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
858 ULONG Line
, Char
, Start
;
861 BYTE LastAttribute
, Attribute
;
862 ULONG CursorX
, CursorY
, CursorHeight
;
863 HBRUSH CursorBrush
, OldBrush
;
866 Buff
= Console
->ActiveBuffer
;
868 TopLine
= rc
->top
/ GuiData
->CharHeight
;
869 BottomLine
= (rc
->bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1;
870 LeftChar
= rc
->left
/ GuiData
->CharWidth
;
871 RightChar
= (rc
->right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1;
872 LastAttribute
= Buff
->Buffer
[(TopLine
* Buff
->MaxX
+ LeftChar
) * 2 + 1];
874 GuiConsoleSetTextColors(hDC
,
878 GuiData
->ScreenBackground
);
880 EnterCriticalSection(&Buff
->Header
.Lock
);
882 OldFont
= SelectObject(hDC
,
885 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
887 if (Line
+ Buff
->ShowY
< Buff
->MaxY
)
889 From
= Buff
->Buffer
+ ((Line
+ Buff
->ShowY
) * Buff
->MaxX
+ LeftChar
) * 2;
893 From
= Buff
->Buffer
+
894 ((Line
- (Buff
->MaxY
- Buff
->ShowY
)) * Buff
->MaxX
+ LeftChar
) * 2;
897 To
= GuiData
->LineBuffer
;
899 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
901 if (*(From
+ 1) != LastAttribute
)
904 Start
* GuiData
->CharWidth
,
905 Line
* GuiData
->CharHeight
,
909 To
= GuiData
->LineBuffer
;
910 Attribute
= *(From
+ 1);
911 if (Attribute
!= LastAttribute
)
913 GuiConsoleSetTextColors(hDC
,
917 GuiData
->ScreenBackground
);
918 LastAttribute
= Attribute
;
922 MultiByteToWideChar(Console
->OutputCodePage
,
933 Start
* GuiData
->CharWidth
,
934 Line
* GuiData
->CharHeight
,
936 RightChar
- Start
+ 1);
939 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
940 !GuiData
->ForceCursorOff
)
942 GuiConsoleGetLogicalCursorPos(Buff
,
945 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
946 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
948 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
949 if (CursorHeight
< 1)
953 From
= Buff
->Buffer
+ (Buff
->CurrentY
* Buff
->MaxX
+ Buff
->CurrentX
) * 2 + 1;
955 if (*From
!= DEFAULT_ATTRIB
)
957 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(*From
));
961 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
964 OldBrush
= SelectObject(hDC
,
967 CursorX
* GuiData
->CharWidth
,
968 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
974 DeleteObject(CursorBrush
);
978 LeaveCriticalSection(&Buff
->Header
.Lock
);
985 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
989 PCSRSS_CONSOLE Console
;
990 PGUI_CONSOLE_DATA GuiData
;
992 hDC
= BeginPaint(hWnd
, &ps
);
994 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
995 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
997 GuiConsoleGetDataPointers(hWnd
,
1000 if (Console
!= NULL
&& GuiData
!= NULL
&&
1001 Console
->ActiveBuffer
!= NULL
)
1003 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
1005 EnterCriticalSection(&GuiData
->Lock
);
1007 GuiConsolePaint(Console
,
1012 if (GuiData
->Selection
.left
!= -1)
1014 RECT rc
= GuiData
->Selection
;
1016 rc
.left
*= GuiData
->CharWidth
;
1017 rc
.top
*= GuiData
->CharHeight
;
1018 rc
.right
*= GuiData
->CharWidth
;
1019 rc
.bottom
*= GuiData
->CharHeight
;
1021 /* invert the selection */
1022 if (IntersectRect(&rc
,
1035 LeaveCriticalSection(&GuiData
->Lock
);
1039 EndPaint(hWnd
, &ps
);
1043 static VOID FASTCALL
1044 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1046 PCSRSS_CONSOLE Console
;
1047 PGUI_CONSOLE_DATA GuiData
;
1050 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1051 Message
.hwnd
= hWnd
;
1052 Message
.message
= msg
;
1053 Message
.wParam
= wParam
;
1054 Message
.lParam
= lParam
;
1056 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1058 /* clear the selection */
1059 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1062 ConioProcessKey(&Message
, Console
, FALSE
);
1065 static VOID FASTCALL
1066 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1070 RegionRect
.left
= Region
->left
* GuiData
->CharWidth
;
1071 RegionRect
.top
= Region
->top
* GuiData
->CharHeight
;
1072 RegionRect
.right
= (Region
->right
+ 1) * GuiData
->CharWidth
;
1073 RegionRect
.bottom
= (Region
->bottom
+ 1) * GuiData
->CharHeight
;
1075 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1079 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1081 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1083 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1085 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1089 static VOID FASTCALL
1090 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1097 CellRect
.bottom
= y
;
1099 GuiIntDrawRegion(GuiData
, Wnd
, &CellRect
);
1103 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1104 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1106 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1107 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1108 LONG CursorEndX
, CursorEndY
;
1111 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1116 if (0 != ScrolledLines
)
1118 ScrollRect
.left
= 0;
1120 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1121 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1123 if (GuiData
->Selection
.left
!= -1)
1125 /* scroll the selection */
1126 if (GuiData
->Selection
.top
> ScrolledLines
)
1128 GuiData
->Selection
.top
-= ScrolledLines
;
1129 GuiData
->Selection
.bottom
-= ScrolledLines
;
1131 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1133 GuiData
->Selection
.left
= -1;
1137 GuiData
->Selection
.top
= 0;
1138 GuiData
->Selection
.bottom
-= ScrolledLines
;
1142 ScrollWindowEx(Console
->hWindow
,
1144 -(ScrolledLines
* GuiData
->CharHeight
),
1152 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1154 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1155 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1157 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1160 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1161 &CursorEndX
, &CursorEndY
);
1162 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1163 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1164 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1166 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1171 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1175 if (Console
->ActiveBuffer
== Buff
)
1177 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1178 &UpdateRect
.left
, &UpdateRect
.top
);
1179 UpdateRect
.right
= UpdateRect
.left
;
1180 UpdateRect
.bottom
= UpdateRect
.top
;
1181 ConioDrawRegion(Console
, &UpdateRect
);
1188 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
1192 if (Console
->ActiveBuffer
== Buff
)
1194 /* Redraw char at old position (removes cursor) */
1195 UpdateRect
.left
= OldCursorX
;
1196 UpdateRect
.top
= OldCursorY
;
1197 UpdateRect
.right
= OldCursorX
;
1198 UpdateRect
.bottom
= OldCursorY
;
1199 ConioDrawRegion(Console
, &UpdateRect
);
1200 /* Redraw char at new position (shows cursor) */
1201 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1202 &(UpdateRect
.left
), &(UpdateRect
.top
));
1203 UpdateRect
.right
= UpdateRect
.left
;
1204 UpdateRect
.bottom
= UpdateRect
.top
;
1205 ConioDrawRegion(Console
, &UpdateRect
);
1211 static VOID FASTCALL
1212 GuiConsoleHandleTimer(HWND hWnd
)
1214 PCSRSS_CONSOLE Console
;
1215 PGUI_CONSOLE_DATA GuiData
;
1217 ULONG CursorX
, CursorY
;
1219 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1220 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1222 GuiConsoleGetLogicalCursorPos(Console
->ActiveBuffer
, &CursorX
, &CursorY
);
1223 CursorRect
.left
= CursorX
;
1224 CursorRect
.top
= CursorY
;
1225 CursorRect
.right
= CursorX
;
1226 CursorRect
.bottom
= CursorY
;
1227 GuiDrawRegion(Console
, &CursorRect
);
1230 static VOID FASTCALL
1231 GuiConsoleHandleClose(HWND hWnd
)
1233 PCSRSS_CONSOLE Console
;
1234 PGUI_CONSOLE_DATA GuiData
;
1235 PLIST_ENTRY current_entry
;
1236 PCSRSS_PROCESS_DATA current
;
1238 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1240 EnterCriticalSection(&Console
->Header
.Lock
);
1242 current_entry
= Console
->ProcessList
.Flink
;
1243 while (current_entry
!= &Console
->ProcessList
)
1245 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1246 current_entry
= current_entry
->Flink
;
1248 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1251 LeaveCriticalSection(&Console
->Header
.Lock
);
1254 static VOID FASTCALL
1255 GuiConsoleHandleNcDestroy(HWND hWnd
)
1257 PCSRSS_CONSOLE Console
;
1258 PGUI_CONSOLE_DATA GuiData
;
1261 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1263 Console
->PrivateData
= NULL
;
1264 DeleteCriticalSection(&GuiData
->Lock
);
1265 GetSystemMenu(hWnd
, TRUE
);
1266 if (GuiData
->ConsoleLibrary
)
1267 FreeLibrary(GuiData
->ConsoleLibrary
);
1269 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1272 static VOID FASTCALL
1273 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1275 PCSRSS_CONSOLE Console
;
1276 PGUI_CONSOLE_DATA GuiData
;
1280 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1281 if (Console
== NULL
|| GuiData
== NULL
) return;
1283 pt
= MAKEPOINTS(lParam
);
1285 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1286 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1287 rc
.right
= rc
.left
+ 1;
1288 rc
.bottom
= rc
.top
+ 1;
1290 GuiData
->SelectionStart
.x
= rc
.left
;
1291 GuiData
->SelectionStart
.y
= rc
.top
;
1295 GuiData
->MouseDown
= TRUE
;
1297 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1300 static VOID FASTCALL
1301 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1303 PCSRSS_CONSOLE Console
;
1304 PGUI_CONSOLE_DATA GuiData
;
1308 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1309 if (Console
== NULL
|| GuiData
== NULL
) return;
1310 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1312 pt
= MAKEPOINTS(lParam
);
1314 rc
.left
= GuiData
->SelectionStart
.x
;
1315 rc
.top
= GuiData
->SelectionStart
.y
;
1316 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1317 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1319 /* exchange left/top with right/bottom if required */
1320 if(rc
.left
>= rc
.right
)
1324 rc
.left
= max(rc
.right
- 1, 0);
1327 if(rc
.top
>= rc
.bottom
)
1331 rc
.top
= max(rc
.bottom
- 1, 0);
1332 rc
.bottom
= tmp
+ 1;
1335 GuiData
->MouseDown
= FALSE
;
1337 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1342 static VOID FASTCALL
1343 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1345 PCSRSS_CONSOLE Console
;
1346 PGUI_CONSOLE_DATA GuiData
;
1350 if (!(wParam
& MK_LBUTTON
)) return;
1352 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1353 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1355 pt
= MAKEPOINTS(lParam
);
1357 rc
.left
= GuiData
->SelectionStart
.x
;
1358 rc
.top
= GuiData
->SelectionStart
.y
;
1359 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1360 if (Console
->Size
.X
< rc
.right
)
1362 rc
.right
= Console
->Size
.X
;
1364 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1365 if (Console
->Size
.Y
< rc
.bottom
)
1367 rc
.bottom
= Console
->Size
.Y
;
1370 /* exchange left/top with right/bottom if required */
1371 if(rc
.left
>= rc
.right
)
1375 rc
.left
= max(rc
.right
- 1, 0);
1378 if(rc
.top
>= rc
.bottom
)
1382 rc
.top
= max(rc
.bottom
- 1, 0);
1383 rc
.bottom
= tmp
+ 1;
1386 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1389 static VOID FASTCALL
1390 GuiConsoleRightMouseDown(HWND hWnd
)
1392 PCSRSS_CONSOLE Console
;
1393 PGUI_CONSOLE_DATA GuiData
;
1395 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1396 if (Console
== NULL
|| GuiData
== NULL
) return;
1398 if (GuiData
->Selection
.left
== -1)
1400 /* FIXME - paste text from clipboard */
1404 /* FIXME - copy selection to clipboard */
1406 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1413 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1415 PCSRSS_CONSOLE Console
;
1416 APPLET_PROC CPLFunc
;
1417 TCHAR szBuffer
[MAX_PATH
];
1418 ConsoleInfo SharedInfo
;
1420 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1422 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1424 if (GuiData
== NULL
)
1426 DPRINT("GuiConsoleGetDataPointers failed\n");
1430 if (GuiData
->ConsoleLibrary
== NULL
)
1432 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1433 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1434 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1436 if (GuiData
->ConsoleLibrary
== NULL
)
1438 DPRINT1("failed to load console.dll");
1443 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1446 DPRINT("Error: Console.dll misses CPlApplet export\n");
1451 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1452 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1453 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1454 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1455 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1456 SharedInfo
.PopupText
= GuiData
->PopupText
;
1457 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1458 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1459 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1460 SharedInfo
.ScreenBuffer
= GuiData
->ScreenBufferSize
;
1461 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1462 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1463 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1464 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1465 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1466 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1467 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1468 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1470 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1472 DPRINT("Error: failed to initialize console.dll\n");
1476 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1478 DPRINT("Error: console.dll returned unexpected CPL count\n");
1482 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1484 static BOOL FASTCALL
1485 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, PGUI_CONSOLE_DATA GuiData
)
1491 case ID_SYSTEM_EDIT_MARK
:
1492 case ID_SYSTEM_EDIT_COPY
:
1493 case ID_SYSTEM_EDIT_PASTE
:
1494 case ID_SYSTEM_EDIT_SELECTALL
:
1495 case ID_SYSTEM_EDIT_SCROLL
:
1496 case ID_SYSTEM_EDIT_FIND
:
1499 case ID_SYSTEM_DEFAULTS
:
1500 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1503 case ID_SYSTEM_PROPERTIES
:
1504 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1515 static VOID FASTCALL
1516 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1518 PCSRSS_CONSOLE Console
;
1519 PGUI_CONSOLE_DATA GuiData
;
1521 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1522 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
)
1524 DPRINT1("GuiConsoleResize X %d Y %d\n", LOWORD(lParam
), HIWORD(lParam
));
1529 GuiConsoleCreateScrollBar(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, HWND NewWindow
)
1536 hMenu
= CreatePopupMenu();
1539 DPRINT("CreatePopupMenu failed\n");
1543 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1544 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1545 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1546 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1547 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1548 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1549 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1550 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1551 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1552 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1554 hVScrollBar
= CreateWindowExW(0L,
1557 WS_CHILD
| WS_VSCROLL
,
1564 GetModuleHandleW(NULL
),
1570 /* set scrollbar sizes */
1571 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1572 sInfo
.fMask
= SIF_RANGE
| SIF_POS
;
1574 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
;
1576 SetScrollInfo(hVScrollBar
, SB_CTL
, &sInfo
, TRUE
);
1577 ShowScrollBar(NewWindow
, SB_CTL
, TRUE
);
1578 GuiData
->hVScrollBar
= hVScrollBar
;
1581 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
1583 hHScrollBar
= CreateWindowExW(0L,
1586 WS_CHILD
| WS_HSCROLL
,
1593 GetModuleHandleW(NULL
),
1597 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
;
1598 SetScrollInfo(hHScrollBar
, SB_CTL
, &sInfo
, TRUE
);
1599 GuiData
->hHScrollBar
= hHScrollBar
;
1604 static VOID FASTCALL
1605 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1610 /* apply text / background color */
1611 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1612 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1614 /* apply cursor size */
1615 Console
->ActiveBuffer
->CursorInfo
.dwSize
= max(min(pConInfo
->CursorSize
, 1), 100);
1617 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1618 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1620 if (windx
!= Console
->ActiveBuffer
->MaxX
|| windy
!= Console
->ActiveBuffer
->MaxY
)
1624 // resize screen buffer
1627 // Console->ActiveBuffer->MaxX = windx;
1628 // Console->ActiveBuffer->MaxY = windy;
1631 windx
= LOWORD(pConInfo
->WindowSize
);
1632 windy
= HIWORD(pConInfo
->WindowSize
);
1634 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1637 Console
->Size
.X
= windx
;
1638 Console
->Size
.Y
= windy
;
1640 GetWindowRect(pConInfo
->hConsoleWindow
, &rect
);
1642 rect
.right
= rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+ 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
1643 rect
.bottom
= rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
1645 MoveWindow(pConInfo
->hConsoleWindow
, rect
.left
, rect
.top
, rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, FALSE
);
1647 if (Console
->Size
.X
< Console
->ActiveBuffer
->MaxX
)
1649 /* show scrollbar when window becomes smaller than active screen buffer */
1650 //ShowScrollBar(GuiData->hHScrollBar, SB_CTL, TRUE);
1654 /* hide scrollbar */
1655 //ShowScrollBar(GuiData->hHScrollBar, SB_CTL, FALSE);
1658 /* repaint window */
1659 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1662 static LRESULT CALLBACK
1663 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1666 PGUI_CONSOLE_DATA GuiData
= NULL
;
1667 PCSRSS_CONSOLE Console
= NULL
;
1669 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1674 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
1677 GuiConsoleHandlePaint(hWnd
, (HDC
)wParam
);
1684 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
1687 GuiConsoleHandleTimer(hWnd
);
1690 GuiConsoleHandleClose(hWnd
);
1693 GuiConsoleHandleNcDestroy(hWnd
);
1695 case WM_LBUTTONDOWN
:
1696 GuiConsoleLeftMouseDown(hWnd
, lParam
);
1699 GuiConsoleLeftMouseUp(hWnd
, lParam
);
1701 case WM_RBUTTONDOWN
:
1702 GuiConsoleRightMouseDown(hWnd
);
1705 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
1708 if (!GuiConsoleHandleSysMenuCommand(hWnd
, wParam
, GuiData
))
1709 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1712 GuiConsoleResize(hWnd
, wParam
, lParam
);
1714 case PM_APPLY_CONSOLE_INFO
:
1715 GuiApplyUserSettings(Console
, GuiData
, (PConsoleInfo
)wParam
);
1718 GuiConsoleWriteUserSettings(Console
, GuiData
);
1722 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1729 static LRESULT CALLBACK
1730 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1735 PWCHAR Buffer
, Title
;
1736 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
1743 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
1745 case PM_CREATE_CONSOLE
:
1746 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1747 Console
->Title
.Length
+ sizeof(WCHAR
));
1750 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1751 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1758 NewWindow
= CreateWindowW(L
"ConsoleWindowClass",
1760 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
, //WS_OVERLAPPEDWINDOW
1767 (HINSTANCE
) GetModuleHandleW(NULL
),
1771 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1773 if (NULL
!= NewWindow
)
1775 // scrollbar support
1776 //GuiConsoleCreateScrollBar(Console, (PGUI_CONSOLE_DATA)Console->PrivateData, NewWindow);
1777 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
1778 ShowWindow(NewWindow
, SW_SHOW
);
1780 return (LRESULT
) NewWindow
;
1781 case PM_DESTROY_CONSOLE
:
1782 /* Window creation is done using a PostMessage(), so it's possible that the
1783 * window that we want to destroy doesn't exist yet. So first empty the message
1785 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
1787 TranslateMessage(&Msg
);
1788 DispatchMessageW(&Msg
);
1790 DestroyWindow(Console
->hWindow
);
1791 Console
->hWindow
= NULL
;
1792 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
1794 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
1795 if (0 == WindowCount
)
1798 DestroyWindow(hWnd
);
1799 PrivateCsrssManualGuiCheck(-1);
1804 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1808 static DWORD STDCALL
1809 GuiConsoleGuiThread(PVOID Data
)
1812 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
1814 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
1816 WS_OVERLAPPEDWINDOW
,
1823 (HINSTANCE
) GetModuleHandleW(NULL
),
1825 if (NULL
== NotifyWnd
)
1827 PrivateCsrssManualGuiCheck(-1);
1828 SetEvent(*GraphicsStartupEvent
);
1832 SetEvent(*GraphicsStartupEvent
);
1834 while(GetMessageW(&msg
, NULL
, 0, 0))
1836 TranslateMessage(&msg
);
1837 DispatchMessageW(&msg
);
1843 static BOOL FASTCALL
1848 if (NULL
== NotifyWnd
)
1850 PrivateCsrssManualGuiCheck(+1);
1853 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1854 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
1855 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
1857 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1860 wc
.hbrBackground
= NULL
;
1861 wc
.lpszMenuName
= NULL
;
1865 if (RegisterClassExW(&wc
) == 0)
1867 DPRINT1("Failed to register notify wndproc\n");
1871 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1872 wc
.lpszClassName
= L
"ConsoleWindowClass";
1873 wc
.lpfnWndProc
= GuiConsoleWndProc
;
1875 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1876 wc
.hIcon
= LoadIconW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1));
1877 wc
.hCursor
= LoadCursorW(NULL
, (LPCWSTR
) IDC_ARROW
);
1878 wc
.hbrBackground
= NULL
;
1879 wc
.lpszMenuName
= NULL
;
1882 wc
.hIconSm
= LoadImageW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1), IMAGE_ICON
,
1883 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
1885 if (RegisterClassExW(&wc
) == 0)
1887 DPRINT1("Failed to register console wndproc\n");
1895 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
1897 Buffer
->DefaultAttrib
= DEFAULT_ATTRIB
;
1901 GuiChangeTitle(PCSRSS_CONSOLE Console
)
1903 PWCHAR Buffer
, Title
;
1905 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1906 Console
->Title
.Length
+ sizeof(WCHAR
));
1909 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1910 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1917 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Title
);
1920 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1927 GuiChangeIcon(PCSRSS_CONSOLE Console
)
1929 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)Console
->hWindowIcon
);
1930 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)Console
->hWindowIcon
);
1936 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
1938 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
1941 static CSRSS_CONSOLE_VTBL GuiVtbl
=
1943 GuiInitScreenBuffer
,
1954 GuiInitConsole(PCSRSS_CONSOLE Console
)
1956 HANDLE GraphicsStartupEvent
;
1957 HANDLE ThreadHandle
;
1958 PGUI_CONSOLE_DATA GuiData
;
1960 if (! ConsInitialized
)
1962 ConsInitialized
= TRUE
;
1965 ConsInitialized
= FALSE
;
1966 return STATUS_UNSUCCESSFUL
;
1970 Console
->Vtbl
= &GuiVtbl
;
1971 if (NULL
== NotifyWnd
)
1973 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1974 if (NULL
== GraphicsStartupEvent
)
1976 return STATUS_UNSUCCESSFUL
;
1979 ThreadHandle
= CreateThread(NULL
,
1981 GuiConsoleGuiThread
,
1982 (PVOID
) &GraphicsStartupEvent
,
1985 if (NULL
== ThreadHandle
)
1987 NtClose(GraphicsStartupEvent
);
1988 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1989 return STATUS_UNSUCCESSFUL
;
1991 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
1992 CloseHandle(ThreadHandle
);
1994 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
1995 CloseHandle(GraphicsStartupEvent
);
1997 if (NULL
== NotifyWnd
)
1999 DPRINT1("Win32Csr: Failed to create notification window.\n");
2000 return STATUS_UNSUCCESSFUL
;
2003 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
2004 sizeof(GUI_CONSOLE_DATA
));
2007 DPRINT1("Win32Csr: Failed to create GUI_CONSOLE_DATA\n");
2008 return STATUS_UNSUCCESSFUL
;
2011 Console
->PrivateData
= (PVOID
) GuiData
;
2013 * we need to wait untill the GUI has been fully initialized
2014 * to retrieve custom settings i.e. WindowSize etc..
2015 * Ideally we could use SendNotifyMessage for this but its not
2019 GuiData
->hGuiInitEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2020 /* create console */
2021 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
2023 /* wait untill initialization has finished */
2024 WaitForSingleObject(GuiData
->hGuiInitEvent
, INFINITE
);
2025 DPRINT1("received event Console %p GuiData %p X %d Y %d\n", Console
, Console
->PrivateData
, Console
->Size
.X
, Console
->Size
.Y
);
2026 CloseHandle(GuiData
->hGuiInitEvent
);
2027 GuiData
->hGuiInitEvent
= NULL
;
2029 return STATUS_SUCCESS
;