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
"DejaVu 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
, BackgroundBrush
;
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 BackgroundBrush
= CreateSolidBrush(GuiData
->ScreenBackground
);
886 FillRect(hDC
, rc
, BackgroundBrush
);
887 DeleteObject(BackgroundBrush
);
889 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
891 if (Line
+ Buff
->ShowY
< Buff
->MaxY
)
893 From
= Buff
->Buffer
+ ((Line
+ Buff
->ShowY
) * Buff
->MaxX
+ LeftChar
) * 2;
897 From
= Buff
->Buffer
+
898 ((Line
- (Buff
->MaxY
- Buff
->ShowY
)) * Buff
->MaxX
+ LeftChar
) * 2;
901 To
= GuiData
->LineBuffer
;
903 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
905 if (*(From
+ 1) != LastAttribute
)
908 Start
* GuiData
->CharWidth
,
909 Line
* GuiData
->CharHeight
,
913 To
= GuiData
->LineBuffer
;
914 Attribute
= *(From
+ 1);
915 if (Attribute
!= LastAttribute
)
917 GuiConsoleSetTextColors(hDC
,
921 GuiData
->ScreenBackground
);
922 LastAttribute
= Attribute
;
926 MultiByteToWideChar(Console
->OutputCodePage
,
937 Start
* GuiData
->CharWidth
,
938 Line
* GuiData
->CharHeight
,
940 RightChar
- Start
+ 1);
943 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
944 !GuiData
->ForceCursorOff
)
946 GuiConsoleGetLogicalCursorPos(Buff
,
949 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
950 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
952 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
953 if (CursorHeight
< 1)
957 From
= Buff
->Buffer
+ (Buff
->CurrentY
* Buff
->MaxX
+ Buff
->CurrentX
) * 2 + 1;
959 if (*From
!= DEFAULT_ATTRIB
)
961 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(*From
));
965 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
968 OldBrush
= SelectObject(hDC
,
971 CursorX
* GuiData
->CharWidth
,
972 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
978 DeleteObject(CursorBrush
);
982 LeaveCriticalSection(&Buff
->Header
.Lock
);
989 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
993 PCSRSS_CONSOLE Console
;
994 PGUI_CONSOLE_DATA GuiData
;
996 hDC
= BeginPaint(hWnd
, &ps
);
998 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
999 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
1001 GuiConsoleGetDataPointers(hWnd
,
1004 if (Console
!= NULL
&& GuiData
!= NULL
&&
1005 Console
->ActiveBuffer
!= NULL
)
1007 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
1009 EnterCriticalSection(&GuiData
->Lock
);
1011 GuiConsolePaint(Console
,
1016 if (GuiData
->Selection
.left
!= -1)
1018 RECT rc
= GuiData
->Selection
;
1020 rc
.left
*= GuiData
->CharWidth
;
1021 rc
.top
*= GuiData
->CharHeight
;
1022 rc
.right
*= GuiData
->CharWidth
;
1023 rc
.bottom
*= GuiData
->CharHeight
;
1025 /* invert the selection */
1026 if (IntersectRect(&rc
,
1039 LeaveCriticalSection(&GuiData
->Lock
);
1043 EndPaint(hWnd
, &ps
);
1047 static VOID FASTCALL
1048 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1050 PCSRSS_CONSOLE Console
;
1051 PGUI_CONSOLE_DATA GuiData
;
1054 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1055 Message
.hwnd
= hWnd
;
1056 Message
.message
= msg
;
1057 Message
.wParam
= wParam
;
1058 Message
.lParam
= lParam
;
1060 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1062 /* clear the selection */
1063 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1066 ConioProcessKey(&Message
, Console
, FALSE
);
1069 static VOID FASTCALL
1070 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1074 RegionRect
.left
= Region
->left
* GuiData
->CharWidth
;
1075 RegionRect
.top
= Region
->top
* GuiData
->CharHeight
;
1076 RegionRect
.right
= (Region
->right
+ 1) * GuiData
->CharWidth
;
1077 RegionRect
.bottom
= (Region
->bottom
+ 1) * GuiData
->CharHeight
;
1079 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1083 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1085 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1087 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1089 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1093 static VOID FASTCALL
1094 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1101 CellRect
.bottom
= y
;
1103 GuiIntDrawRegion(GuiData
, Wnd
, &CellRect
);
1107 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1108 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1110 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1111 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1112 LONG CursorEndX
, CursorEndY
;
1115 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1120 if (0 != ScrolledLines
)
1122 ScrollRect
.left
= 0;
1124 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1125 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1127 if (GuiData
->Selection
.left
!= -1)
1129 /* scroll the selection */
1130 if (GuiData
->Selection
.top
> ScrolledLines
)
1132 GuiData
->Selection
.top
-= ScrolledLines
;
1133 GuiData
->Selection
.bottom
-= ScrolledLines
;
1135 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1137 GuiData
->Selection
.left
= -1;
1141 GuiData
->Selection
.top
= 0;
1142 GuiData
->Selection
.bottom
-= ScrolledLines
;
1146 ScrollWindowEx(Console
->hWindow
,
1148 -(ScrolledLines
* GuiData
->CharHeight
),
1156 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1158 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1159 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1161 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1164 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1165 &CursorEndX
, &CursorEndY
);
1166 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1167 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1168 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1170 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1175 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1179 if (Console
->ActiveBuffer
== Buff
)
1181 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1182 &UpdateRect
.left
, &UpdateRect
.top
);
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 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
1206 &(UpdateRect
.left
), &(UpdateRect
.top
));
1207 UpdateRect
.right
= UpdateRect
.left
;
1208 UpdateRect
.bottom
= UpdateRect
.top
;
1209 ConioDrawRegion(Console
, &UpdateRect
);
1215 static VOID FASTCALL
1216 GuiConsoleHandleTimer(HWND hWnd
)
1218 PCSRSS_CONSOLE Console
;
1219 PGUI_CONSOLE_DATA GuiData
;
1221 ULONG CursorX
, CursorY
;
1223 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1224 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1226 GuiConsoleGetLogicalCursorPos(Console
->ActiveBuffer
, &CursorX
, &CursorY
);
1227 CursorRect
.left
= CursorX
;
1228 CursorRect
.top
= CursorY
;
1229 CursorRect
.right
= CursorX
;
1230 CursorRect
.bottom
= CursorY
;
1231 GuiDrawRegion(Console
, &CursorRect
);
1234 static VOID FASTCALL
1235 GuiConsoleHandleClose(HWND hWnd
)
1237 PCSRSS_CONSOLE Console
;
1238 PGUI_CONSOLE_DATA GuiData
;
1239 PLIST_ENTRY current_entry
;
1240 PCSRSS_PROCESS_DATA current
;
1242 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1244 EnterCriticalSection(&Console
->Header
.Lock
);
1246 current_entry
= Console
->ProcessList
.Flink
;
1247 while (current_entry
!= &Console
->ProcessList
)
1249 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1250 current_entry
= current_entry
->Flink
;
1252 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1255 LeaveCriticalSection(&Console
->Header
.Lock
);
1258 static VOID FASTCALL
1259 GuiConsoleHandleNcDestroy(HWND hWnd
)
1261 PCSRSS_CONSOLE Console
;
1262 PGUI_CONSOLE_DATA GuiData
;
1265 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1267 Console
->PrivateData
= NULL
;
1268 DeleteCriticalSection(&GuiData
->Lock
);
1269 GetSystemMenu(hWnd
, TRUE
);
1270 if (GuiData
->ConsoleLibrary
)
1271 FreeLibrary(GuiData
->ConsoleLibrary
);
1273 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1276 static VOID FASTCALL
1277 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1279 PCSRSS_CONSOLE Console
;
1280 PGUI_CONSOLE_DATA GuiData
;
1284 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1285 if (Console
== NULL
|| GuiData
== NULL
) return;
1287 pt
= MAKEPOINTS(lParam
);
1289 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1290 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1291 rc
.right
= rc
.left
+ 1;
1292 rc
.bottom
= rc
.top
+ 1;
1294 GuiData
->SelectionStart
.x
= rc
.left
;
1295 GuiData
->SelectionStart
.y
= rc
.top
;
1299 GuiData
->MouseDown
= TRUE
;
1301 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1304 static VOID FASTCALL
1305 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1307 PCSRSS_CONSOLE Console
;
1308 PGUI_CONSOLE_DATA GuiData
;
1312 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1313 if (Console
== NULL
|| GuiData
== NULL
) return;
1314 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1316 pt
= MAKEPOINTS(lParam
);
1318 rc
.left
= GuiData
->SelectionStart
.x
;
1319 rc
.top
= GuiData
->SelectionStart
.y
;
1320 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1321 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1323 /* exchange left/top with right/bottom if required */
1324 if(rc
.left
>= rc
.right
)
1328 rc
.left
= max(rc
.right
- 1, 0);
1331 if(rc
.top
>= rc
.bottom
)
1335 rc
.top
= max(rc
.bottom
- 1, 0);
1336 rc
.bottom
= tmp
+ 1;
1339 GuiData
->MouseDown
= FALSE
;
1341 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1346 static VOID FASTCALL
1347 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1349 PCSRSS_CONSOLE Console
;
1350 PGUI_CONSOLE_DATA GuiData
;
1354 if (!(wParam
& MK_LBUTTON
)) return;
1356 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1357 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1359 pt
= MAKEPOINTS(lParam
);
1361 rc
.left
= GuiData
->SelectionStart
.x
;
1362 rc
.top
= GuiData
->SelectionStart
.y
;
1363 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1364 if (Console
->Size
.X
< rc
.right
)
1366 rc
.right
= Console
->Size
.X
;
1368 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1369 if (Console
->Size
.Y
< rc
.bottom
)
1371 rc
.bottom
= Console
->Size
.Y
;
1374 /* exchange left/top with right/bottom if required */
1375 if(rc
.left
>= rc
.right
)
1379 rc
.left
= max(rc
.right
- 1, 0);
1382 if(rc
.top
>= rc
.bottom
)
1386 rc
.top
= max(rc
.bottom
- 1, 0);
1387 rc
.bottom
= tmp
+ 1;
1390 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1393 static VOID FASTCALL
1394 GuiConsoleRightMouseDown(HWND hWnd
)
1396 PCSRSS_CONSOLE Console
;
1397 PGUI_CONSOLE_DATA GuiData
;
1399 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1400 if (Console
== NULL
|| GuiData
== NULL
) return;
1402 if (GuiData
->Selection
.left
== -1)
1404 /* FIXME - paste text from clipboard */
1408 /* FIXME - copy selection to clipboard */
1410 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1417 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1419 PCSRSS_CONSOLE Console
;
1420 APPLET_PROC CPLFunc
;
1421 TCHAR szBuffer
[MAX_PATH
];
1422 ConsoleInfo SharedInfo
;
1424 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1426 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1428 if (GuiData
== NULL
)
1430 DPRINT("GuiConsoleGetDataPointers failed\n");
1434 if (GuiData
->ConsoleLibrary
== NULL
)
1436 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1437 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1438 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1440 if (GuiData
->ConsoleLibrary
== NULL
)
1442 DPRINT1("failed to load console.dll");
1447 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1450 DPRINT("Error: Console.dll misses CPlApplet export\n");
1455 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1456 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1457 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1458 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1459 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1460 SharedInfo
.PopupText
= GuiData
->PopupText
;
1461 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1462 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1463 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1464 SharedInfo
.ScreenBuffer
= GuiData
->ScreenBufferSize
;
1465 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1466 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1467 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1468 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1469 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1470 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1471 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1472 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1474 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1476 DPRINT("Error: failed to initialize console.dll\n");
1480 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1482 DPRINT("Error: console.dll returned unexpected CPL count\n");
1486 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1488 static LRESULT FASTCALL
1489 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, PGUI_CONSOLE_DATA GuiData
)
1495 case ID_SYSTEM_EDIT_MARK
:
1496 case ID_SYSTEM_EDIT_COPY
:
1497 case ID_SYSTEM_EDIT_PASTE
:
1498 case ID_SYSTEM_EDIT_SELECTALL
:
1499 case ID_SYSTEM_EDIT_SCROLL
:
1500 case ID_SYSTEM_EDIT_FIND
:
1503 case ID_SYSTEM_DEFAULTS
:
1504 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1507 case ID_SYSTEM_PROPERTIES
:
1508 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1512 Ret
= DefWindowProcW(hWnd
, WM_SYSCOMMAND
, wParam
, lParam
);
1518 static VOID FASTCALL
1519 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1521 PCSRSS_CONSOLE Console
;
1522 PGUI_CONSOLE_DATA GuiData
;
1524 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1525 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
)
1527 DPRINT1("GuiConsoleResize X %d Y %d\n", LOWORD(lParam
), HIWORD(lParam
));
1532 GuiConsoleCreateScrollBar(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, HWND NewWindow
)
1539 hMenu
= CreatePopupMenu();
1542 DPRINT("CreatePopupMenu failed\n");
1546 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1547 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1548 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1549 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1550 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1551 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1552 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1553 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1554 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1555 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1557 hVScrollBar
= CreateWindowExW(0L,
1560 WS_CHILD
| WS_VSCROLL
,
1567 GetModuleHandleW(NULL
),
1573 /* set scrollbar sizes */
1574 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1575 sInfo
.fMask
= SIF_RANGE
| SIF_POS
;
1577 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
;
1579 SetScrollInfo(hVScrollBar
, SB_CTL
, &sInfo
, TRUE
);
1580 ShowScrollBar(NewWindow
, SB_CTL
, TRUE
);
1581 GuiData
->hVScrollBar
= hVScrollBar
;
1584 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
1586 hHScrollBar
= CreateWindowExW(0L,
1589 WS_CHILD
| WS_HSCROLL
,
1596 GetModuleHandleW(NULL
),
1600 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
;
1601 SetScrollInfo(hHScrollBar
, SB_CTL
, &sInfo
, TRUE
);
1602 GuiData
->hHScrollBar
= hHScrollBar
;
1607 static VOID FASTCALL
1608 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1613 /* apply text / background color */
1614 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1615 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1617 /* apply cursor size */
1618 Console
->ActiveBuffer
->CursorInfo
.dwSize
= max(min(pConInfo
->CursorSize
, 1), 100);
1620 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1621 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1623 if (windx
!= Console
->ActiveBuffer
->MaxX
|| windy
!= Console
->ActiveBuffer
->MaxY
)
1627 // resize screen buffer
1630 // Console->ActiveBuffer->MaxX = windx;
1631 // Console->ActiveBuffer->MaxY = windy;
1634 windx
= LOWORD(pConInfo
->WindowSize
);
1635 windy
= HIWORD(pConInfo
->WindowSize
);
1637 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1640 Console
->Size
.X
= windx
;
1641 Console
->Size
.Y
= windy
;
1643 GetWindowRect(pConInfo
->hConsoleWindow
, &rect
);
1645 rect
.right
= rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+ 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
1646 rect
.bottom
= rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
1648 MoveWindow(pConInfo
->hConsoleWindow
, rect
.left
, rect
.top
, rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, FALSE
);
1650 if (Console
->Size
.X
< Console
->ActiveBuffer
->MaxX
)
1652 /* show scrollbar when window becomes smaller than active screen buffer */
1653 //ShowScrollBar(GuiData->hHScrollBar, SB_CTL, TRUE);
1657 /* hide scrollbar */
1658 //ShowScrollBar(GuiData->hHScrollBar, SB_CTL, FALSE);
1661 /* repaint window */
1662 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1665 static LRESULT CALLBACK
1666 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1669 PGUI_CONSOLE_DATA GuiData
= NULL
;
1670 PCSRSS_CONSOLE Console
= NULL
;
1672 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1677 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
1680 GuiConsoleHandlePaint(hWnd
, (HDC
)wParam
);
1687 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
1690 GuiConsoleHandleTimer(hWnd
);
1693 GuiConsoleHandleClose(hWnd
);
1696 GuiConsoleHandleNcDestroy(hWnd
);
1698 case WM_LBUTTONDOWN
:
1699 GuiConsoleLeftMouseDown(hWnd
, lParam
);
1702 GuiConsoleLeftMouseUp(hWnd
, lParam
);
1704 case WM_RBUTTONDOWN
:
1705 GuiConsoleRightMouseDown(hWnd
);
1708 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
1711 Result
= GuiConsoleHandleSysMenuCommand(hWnd
, wParam
, lParam
, GuiData
);
1714 GuiConsoleResize(hWnd
, wParam
, lParam
);
1716 case PM_APPLY_CONSOLE_INFO
:
1717 GuiApplyUserSettings(Console
, GuiData
, (PConsoleInfo
)wParam
);
1720 GuiConsoleWriteUserSettings(Console
, GuiData
);
1724 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1731 static LRESULT CALLBACK
1732 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1737 PWCHAR Buffer
, Title
;
1738 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
1745 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
1747 case PM_CREATE_CONSOLE
:
1748 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1749 Console
->Title
.Length
+ sizeof(WCHAR
));
1752 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1753 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1760 NewWindow
= CreateWindowW(L
"ConsoleWindowClass",
1762 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
, //WS_OVERLAPPEDWINDOW
1769 (HINSTANCE
) GetModuleHandleW(NULL
),
1773 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1775 if (NULL
!= NewWindow
)
1777 // scrollbar support
1778 //GuiConsoleCreateScrollBar(Console, (PGUI_CONSOLE_DATA)Console->PrivateData, NewWindow);
1779 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
1780 ShowWindow(NewWindow
, SW_SHOW
);
1782 return (LRESULT
) NewWindow
;
1783 case PM_DESTROY_CONSOLE
:
1784 /* Window creation is done using a PostMessage(), so it's possible that the
1785 * window that we want to destroy doesn't exist yet. So first empty the message
1787 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
1789 TranslateMessage(&Msg
);
1790 DispatchMessageW(&Msg
);
1792 DestroyWindow(Console
->hWindow
);
1793 Console
->hWindow
= NULL
;
1794 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
1796 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
1797 if (0 == WindowCount
)
1800 DestroyWindow(hWnd
);
1801 PrivateCsrssManualGuiCheck(-1);
1806 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1810 static DWORD STDCALL
1811 GuiConsoleGuiThread(PVOID Data
)
1814 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
1816 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
1818 WS_OVERLAPPEDWINDOW
,
1825 (HINSTANCE
) GetModuleHandleW(NULL
),
1827 if (NULL
== NotifyWnd
)
1829 PrivateCsrssManualGuiCheck(-1);
1830 SetEvent(*GraphicsStartupEvent
);
1834 SetEvent(*GraphicsStartupEvent
);
1836 while(GetMessageW(&msg
, NULL
, 0, 0))
1838 TranslateMessage(&msg
);
1839 DispatchMessageW(&msg
);
1845 static BOOL FASTCALL
1850 if (NULL
== NotifyWnd
)
1852 PrivateCsrssManualGuiCheck(+1);
1855 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1856 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
1857 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
1859 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1862 wc
.hbrBackground
= NULL
;
1863 wc
.lpszMenuName
= NULL
;
1867 if (RegisterClassExW(&wc
) == 0)
1869 DPRINT1("Failed to register notify wndproc\n");
1873 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1874 wc
.lpszClassName
= L
"ConsoleWindowClass";
1875 wc
.lpfnWndProc
= GuiConsoleWndProc
;
1877 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1878 wc
.hIcon
= LoadIconW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1));
1879 wc
.hCursor
= LoadCursorW(NULL
, (LPCWSTR
) IDC_ARROW
);
1880 wc
.hbrBackground
= NULL
;
1881 wc
.lpszMenuName
= NULL
;
1884 wc
.hIconSm
= LoadImageW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1), IMAGE_ICON
,
1885 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
1887 if (RegisterClassExW(&wc
) == 0)
1889 DPRINT1("Failed to register console wndproc\n");
1897 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
1899 Buffer
->DefaultAttrib
= DEFAULT_ATTRIB
;
1903 GuiChangeTitle(PCSRSS_CONSOLE Console
)
1905 PWCHAR Buffer
, Title
;
1907 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1908 Console
->Title
.Length
+ sizeof(WCHAR
));
1911 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1912 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1919 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Title
);
1922 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1929 GuiChangeIcon(PCSRSS_CONSOLE Console
)
1931 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)Console
->hWindowIcon
);
1932 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)Console
->hWindowIcon
);
1938 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
1940 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
1943 static CSRSS_CONSOLE_VTBL GuiVtbl
=
1945 GuiInitScreenBuffer
,
1956 GuiInitConsole(PCSRSS_CONSOLE Console
)
1958 HANDLE GraphicsStartupEvent
;
1959 HANDLE ThreadHandle
;
1960 PGUI_CONSOLE_DATA GuiData
;
1962 if (! ConsInitialized
)
1964 ConsInitialized
= TRUE
;
1967 ConsInitialized
= FALSE
;
1968 return STATUS_UNSUCCESSFUL
;
1972 Console
->Vtbl
= &GuiVtbl
;
1973 if (NULL
== NotifyWnd
)
1975 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1976 if (NULL
== GraphicsStartupEvent
)
1978 return STATUS_UNSUCCESSFUL
;
1981 ThreadHandle
= CreateThread(NULL
,
1983 GuiConsoleGuiThread
,
1984 (PVOID
) &GraphicsStartupEvent
,
1987 if (NULL
== ThreadHandle
)
1989 NtClose(GraphicsStartupEvent
);
1990 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1991 return STATUS_UNSUCCESSFUL
;
1993 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
1994 CloseHandle(ThreadHandle
);
1996 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
1997 CloseHandle(GraphicsStartupEvent
);
1999 if (NULL
== NotifyWnd
)
2001 DPRINT1("Win32Csr: Failed to create notification window.\n");
2002 return STATUS_UNSUCCESSFUL
;
2005 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
2006 sizeof(GUI_CONSOLE_DATA
));
2009 DPRINT1("Win32Csr: Failed to create GUI_CONSOLE_DATA\n");
2010 return STATUS_UNSUCCESSFUL
;
2013 Console
->PrivateData
= (PVOID
) GuiData
;
2015 * we need to wait untill the GUI has been fully initialized
2016 * to retrieve custom settings i.e. WindowSize etc..
2017 * Ideally we could use SendNotifyMessage for this but its not
2021 GuiData
->hGuiInitEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2022 /* create console */
2023 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
2025 /* wait untill initialization has finished */
2026 WaitForSingleObject(GuiData
->hGuiInitEvent
, INFINITE
);
2027 DPRINT1("received event Console %p GuiData %p X %d Y %d\n", Console
, Console
->PrivateData
, Console
->Size
.X
, Console
->Size
.Y
);
2028 CloseHandle(GuiData
->hGuiInitEvent
);
2029 GuiData
->hGuiInitEvent
= NULL
;
2031 return STATUS_SUCCESS
;