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)
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 #define GuiConsoleRGBFromAttribute(GuiData, Attribute) ((GuiData)->Colors[(Attribute) & 0xF])
120 /* FUNCTIONS *****************************************************************/
123 GuiConsoleAppendMenuItems(HMENU hMenu
,
124 const GUICONSOLE_MENUITEM
*Items
)
127 WCHAR szMenuString
[255];
129 HINSTANCE hInst
= GetModuleHandleW(L
"win32csr");
133 if (Items
[i
].uID
!= (UINT
)-1)
135 if (LoadStringW(hInst
,
138 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
140 if (Items
[i
].SubMenu
!= NULL
)
142 hSubMenu
= CreatePopupMenu();
143 if (hSubMenu
!= NULL
)
145 GuiConsoleAppendMenuItems(hSubMenu
,
148 if (!AppendMenuW(hMenu
,
149 MF_STRING
| MF_POPUP
,
153 DestroyMenu(hSubMenu
);
174 }while(!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
178 GuiConsoleCreateSysMenu(PCSRSS_CONSOLE Console
)
181 hMenu
= GetSystemMenu(Console
->hWindow
,
185 GuiConsoleAppendMenuItems(hMenu
,
186 GuiConsoleMainMenuItems
);
187 DrawMenuBar(Console
->hWindow
);
192 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
194 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
195 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
199 GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId
, PHANDLE hProcHandle
, PHKEY hResult
, REGSAM samDesired
)
201 HANDLE hProcessToken
= NULL
;
206 UNICODE_STRING SidName
;
210 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| READ_CONTROL
, FALSE
, ProcessId
);
213 DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
217 if (!OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
219 DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
220 CloseHandle(hProcess
);
224 if (!GetTokenInformation(hProcessToken
, TokenUser
, (PVOID
)Buffer
, sizeof(Buffer
), &Length
))
226 DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
227 CloseHandle(hProcess
);
228 CloseHandle(hProcessToken
);
232 TokUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
233 if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName
, TokUser
, TRUE
)))
235 DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
239 res
= RegOpenKeyExW(HKEY_USERS
, SidName
.Buffer
, 0, samDesired
, hResult
);
240 RtlFreeUnicodeString(&SidName
);
242 CloseHandle(hProcessToken
);
244 *hProcHandle
= hProcess
;
246 CloseHandle(hProcess
);
248 if (res
!= ERROR_SUCCESS
)
255 GuiConsoleOpenUserSettings(PGUI_CONSOLE_DATA GuiData
, DWORD ProcessId
, PHKEY hSubKey
, REGSAM samDesired
, BOOL bCreate
)
257 WCHAR szProcessName
[MAX_PATH
];
258 WCHAR szBuffer
[MAX_PATH
];
259 UINT fLength
, wLength
;
260 DWORD dwBitmask
, dwLength
;
261 WCHAR CurDrive
[] = { 'A',':', 0 };
267 * console properties are stored under
270 * There are 3 ways to store console properties
272 * 1. use console title as subkey name
275 * 2. use application name as subkey name
277 * 3. use unexpanded path to console application.
278 * i.e. %SystemRoot%_system32_cmd.exe
281 DPRINT("GuiConsoleOpenUserSettings entered\n");
283 if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId
, &hProcess
, &hKey
, samDesired
))
285 DPRINT("GuiConsoleOpenUserRegistryPathPerProcessId failed\n");
289 /* FIXME we do not getting the process name so no menu will be loading, why ?*/
290 fLength
= GetProcessImageFileNameW(hProcess
, szProcessName
, sizeof(GuiData
->szProcessName
) / sizeof(WCHAR
));
291 CloseHandle(hProcess
);
293 //DPRINT1("szProcessName3 : %S\n",szProcessName);
297 DPRINT("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess
);
301 * try the process name as path
304 ptr
= wcsrchr(szProcessName
, L
'\\');
305 wcscpy(GuiData
->szProcessName
, ptr
);
307 swprintf(szBuffer
, L
"Console%s",ptr
);
308 DPRINT("#1 Path : %S\n", szBuffer
);
312 if (RegCreateKeyW(hKey
, szBuffer
, hSubKey
) == ERROR_SUCCESS
)
321 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
328 * try the "Shortcut to processname" as path
329 * FIXME: detect wheter the process was started as a shortcut
332 swprintf(szBuffer
, L
"Console\\Shortcut to %S", ptr
);
333 DPRINT("#2 Path : %S\n", szBuffer
);
334 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
336 swprintf(GuiData
->szProcessName
, L
"Shortcut to %S", ptr
);
342 * if the path contains \\Device\\HarddiskVolume1\... remove it
345 if (szProcessName
[0] == L
'\\')
347 dwBitmask
= GetLogicalDrives();
352 dwLength
= QueryDosDeviceW(CurDrive
, szBuffer
, MAX_PATH
);
355 if (!memcmp(szBuffer
, szProcessName
, (dwLength
-2)*sizeof(WCHAR
)))
357 wcscpy(szProcessName
, CurDrive
);
358 RtlMoveMemory(&szProcessName
[2], &szProcessName
[dwLength
-1], fLength
- dwLength
-1);
363 dwBitmask
= (dwBitmask
>> 1);
369 * last attempt: check whether the file is under %SystemRoot%
370 * and use path like Console\%SystemRoot%_dir_dir2_file.exe
373 wLength
= GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
376 if (!wcsncmp(szProcessName
, szBuffer
, wLength
))
378 /* replace slashes by underscores */
379 while((ptr
= wcschr(szProcessName
, L
'\\')))
382 swprintf(szBuffer
, L
"Console\\\%SystemRoot\%%S", &szProcessName
[wLength
]);
383 DPRINT("#3 Path : %S\n", szBuffer
);
384 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
386 swprintf(GuiData
->szProcessName
, L
"\%SystemRoot\%%S", &szProcessName
[wLength
]);
397 GuiConsoleWriteUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
)
400 PCSRSS_PROCESS_DATA ProcessData
;
402 if (Console
->ProcessList
.Flink
== &Console
->ProcessList
)
404 DPRINT("GuiConsoleWriteUserSettings: No Process!!!\n");
407 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
408 if (!GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
| KEY_WRITE
, TRUE
))
413 if (Console
->ActiveBuffer
->CursorInfo
.dwSize
<= 1)
415 RegDeleteKeyW(hKey
, L
"CursorSize");
419 RegSetValueExW(hKey
, L
"CursorSize", 0, REG_DWORD
, (const BYTE
*)&Console
->ActiveBuffer
->CursorInfo
.dwSize
, sizeof(DWORD
));
422 if (GuiData
->NumberOfHistoryBuffers
== 5)
424 RegDeleteKeyW(hKey
, L
"NumberOfHistoryBuffers");
428 RegSetValueExW(hKey
, L
"NumberOfHistoryBuffers", 0, REG_DWORD
, (const BYTE
*)&GuiData
->NumberOfHistoryBuffers
, sizeof(DWORD
));
431 if (GuiData
->HistoryBufferSize
== 50)
433 RegDeleteKeyW(hKey
, L
"HistoryBufferSize");
437 RegSetValueExW(hKey
, L
"HistoryBufferSize", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryBufferSize
, sizeof(DWORD
));
440 if (GuiData
->FullScreen
== FALSE
)
442 RegDeleteKeyW(hKey
, L
"FullScreen");
446 RegSetValueExW(hKey
, L
"FullScreen", 0, REG_DWORD
, (const BYTE
*)&GuiData
->FullScreen
, sizeof(DWORD
));
449 if ( GuiData
->QuickEdit
== FALSE
)
451 RegDeleteKeyW(hKey
, L
"QuickEdit");
455 RegSetValueExW(hKey
, L
"QuickEdit", 0, REG_DWORD
, (const BYTE
*)&GuiData
->QuickEdit
, sizeof(DWORD
));
458 if (GuiData
->InsertMode
== TRUE
)
460 RegDeleteKeyW(hKey
, L
"InsertMode");
464 RegSetValueExW(hKey
, L
"InsertMode", 0, REG_DWORD
, (const BYTE
*)&GuiData
->InsertMode
, sizeof(DWORD
));
467 if (GuiData
->HistoryNoDup
== FALSE
)
469 RegDeleteKeyW(hKey
, L
"HistoryNoDup");
473 RegSetValueExW(hKey
, L
"HistoryNoDup", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryNoDup
, sizeof(DWORD
));
476 if (GuiData
->ScreenText
== RGB(192, 192, 192))
479 * MS uses console attributes instead of real color
481 RegDeleteKeyW(hKey
, L
"ScreenText");
485 RegSetValueExW(hKey
, L
"ScreenText", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenText
, sizeof(COLORREF
));
488 if (GuiData
->ScreenBackground
== RGB(0, 0, 0))
490 RegDeleteKeyW(hKey
, L
"ScreenBackground");
494 RegSetValueExW(hKey
, L
"ScreenBackground", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenBackground
, sizeof(COLORREF
));
501 GuiConsoleReadUserSettings(HKEY hKey
, PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
503 DWORD dwNumSubKeys
= 0;
508 WCHAR szValueName
[MAX_PATH
];
509 WCHAR szValue
[LF_FACESIZE
] = L
"\0";
512 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
514 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
518 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
520 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
522 dwValue
= sizeof(Value
);
523 dwValueName
= MAX_PATH
;
525 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
527 if (dwType
== REG_SZ
)
530 * retry in case of string value
532 dwValue
= sizeof(szValue
);
533 dwValueName
= LF_FACESIZE
;
534 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
540 if (!wcscmp(szValueName
, L
"CursorSize"))
544 Buffer
->CursorInfo
.dwSize
= Value
;
546 else if (Value
== 0x64)
548 Buffer
->CursorInfo
.dwSize
= Value
;
551 else if (!wcscmp(szValueName
, L
"ScreenText"))
553 GuiData
->ScreenText
= Value
;
555 else if (!wcscmp(szValueName
, L
"ScreenBackground"))
557 GuiData
->ScreenBackground
= Value
;
559 else if (!wcscmp(szValueName
, L
"FaceName"))
561 wcscpy(GuiData
->FontName
, szValue
);
563 else if (!wcscmp(szValueName
, L
"FontSize"))
565 GuiData
->FontSize
= Value
;
567 else if (!wcscmp(szValueName
, L
"FontWeight"))
569 GuiData
->FontWeight
= Value
;
571 else if (!wcscmp(szValueName
, L
"HistoryNoDup"))
573 GuiData
->HistoryNoDup
= Value
;
575 else if (!wcscmp(szValueName
, L
"WindowSize"))
577 Console
->Size
.X
= LOWORD(Value
);
578 Console
->Size
.Y
= HIWORD(Value
);
580 else if (!wcscmp(szValueName
, L
"ScreenBufferSize"))
584 Buffer
->MaxX
= LOWORD(Value
);
585 Buffer
->MaxY
= HIWORD(Value
);
588 else if (!wcscmp(szValueName
, L
"FullScreen"))
590 GuiData
->FullScreen
= Value
;
592 else if (!wcscmp(szValueName
, L
"QuickEdit"))
594 GuiData
->QuickEdit
= Value
;
596 else if (!wcscmp(szValueName
, L
"InsertMode"))
598 GuiData
->InsertMode
= Value
;
603 GuiConsoleUseDefaults(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
606 * init guidata with default properties
609 wcscpy(GuiData
->FontName
, L
"DejaVu Sans Mono");
610 GuiData
->FontSize
= 0x0008000C; // font is 8x12
611 GuiData
->FontWeight
= FW_NORMAL
;
612 GuiData
->HistoryNoDup
= FALSE
;
613 GuiData
->FullScreen
= FALSE
;
614 GuiData
->QuickEdit
= FALSE
;
615 GuiData
->InsertMode
= TRUE
;
616 GuiData
->HistoryBufferSize
= 50;
617 GuiData
->NumberOfHistoryBuffers
= 5;
618 GuiData
->ScreenText
= RGB(192, 192, 192);
619 GuiData
->ScreenBackground
= RGB(0, 0, 0);
620 GuiData
->PopupText
= RGB(128, 0, 128);
621 GuiData
->PopupBackground
= RGB(255, 255, 255);
622 GuiData
->WindowPosition
= UINT_MAX
;
623 GuiData
->ScreenBufferSize
= MAKELONG(80, 300); //FIXME
624 GuiData
->UseRasterFonts
= TRUE
;
625 memcpy(GuiData
->Colors
, s_Colors
, sizeof(s_Colors
));
627 Console
->Size
.X
= 80;
628 Console
->Size
.Y
= 25;
634 Buffer
->CursorInfo
.bVisible
= TRUE
;
635 Buffer
->CursorInfo
.dwSize
= 5;
641 GuiConsoleInitScrollbar(PCSRSS_CONSOLE Console
, HWND hwnd
)
645 /* set scrollbar sizes */
646 sInfo
.cbSize
= sizeof(SCROLLINFO
);
647 sInfo
.fMask
= SIF_RANGE
| SIF_POS
;
649 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
;
651 SetScrollInfo(hwnd
, SB_HORZ
, &sInfo
, TRUE
);
652 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
654 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
656 sInfo
.cbSize
= sizeof(SCROLLINFO
);
657 sInfo
.fMask
= SIF_RANGE
| SIF_POS
;
660 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
;
661 SetScrollInfo(hwnd
, SB_HORZ
, &sInfo
, TRUE
);
665 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
670 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
673 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
674 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)Console
->PrivateData
;
678 PCSRSS_PROCESS_DATA ProcessData
;
681 Console
->hWindow
= hWnd
;
685 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
689 GuiConsoleUseDefaults(Console
, GuiData
, Console
->ActiveBuffer
);
690 if (Console
->ProcessList
.Flink
!= &Console
->ProcessList
)
692 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
693 if (GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
, FALSE
))
695 GuiConsoleReadUserSettings(hKey
, Console
, GuiData
, Console
->ActiveBuffer
);
700 InitializeCriticalSection(&GuiData
->Lock
);
702 GuiData
->LineBuffer
= (PWCHAR
)HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
703 Console
->Size
.X
* sizeof(WCHAR
));
705 GuiData
->Font
= CreateFontW(LOWORD(GuiData
->FontSize
),
706 0, //HIWORD(GuiData->FontSize),
714 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
715 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
717 if (NULL
== GuiData
->Font
)
719 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
720 DeleteCriticalSection(&GuiData
->Lock
);
721 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
727 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
728 DeleteObject(GuiData
->Font
);
729 DeleteCriticalSection(&GuiData
->Lock
);
730 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
733 OldFont
= SelectObject(Dc
, GuiData
->Font
);
736 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
738 DeleteObject(GuiData
->Font
);
739 DeleteCriticalSection(&GuiData
->Lock
);
740 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
743 if (! GetTextMetricsW(Dc
, &Metrics
))
745 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
746 SelectObject(Dc
, OldFont
);
748 DeleteObject(GuiData
->Font
);
749 DeleteCriticalSection(&GuiData
->Lock
);
750 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
753 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
754 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
755 SelectObject(Dc
, OldFont
);
758 GuiData
->CursorBlinkOn
= TRUE
;
759 GuiData
->ForceCursorOff
= FALSE
;
761 GuiData
->Selection
.left
= -1;
762 DPRINT("Console %p GuiData %p\n", Console
, GuiData
);
763 Console
->PrivateData
= GuiData
;
764 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
766 GetWindowRect(hWnd
, &Rect
);
767 Rect
.right
= Rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+
768 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
769 Rect
.bottom
= Rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+
770 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
771 MoveWindow(hWnd
, Rect
.left
, Rect
.top
, Rect
.right
- Rect
.left
,
772 Rect
.bottom
- Rect
.top
, FALSE
);
774 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
775 GuiConsoleCreateSysMenu(Console
);
776 GuiConsoleInitScrollbar(Console
, hWnd
);
777 SetEvent(GuiData
->hGuiInitEvent
);
779 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
783 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
785 RECT oldRect
= GuiData
->Selection
;
789 RECT changeRect
= *rc
;
791 GuiData
->Selection
= *rc
;
793 changeRect
.left
*= GuiData
->CharWidth
;
794 changeRect
.top
*= GuiData
->CharHeight
;
795 changeRect
.right
*= GuiData
->CharWidth
;
796 changeRect
.bottom
*= GuiData
->CharHeight
;
798 if(rc
->left
!= oldRect
.left
||
799 rc
->top
!= oldRect
.top
||
800 rc
->right
!= oldRect
.right
||
801 rc
->bottom
!= oldRect
.bottom
)
803 if(oldRect
.left
!= -1)
807 oldRect
.left
*= GuiData
->CharWidth
;
808 oldRect
.top
*= GuiData
->CharHeight
;
809 oldRect
.right
*= GuiData
->CharWidth
;
810 oldRect
.bottom
*= GuiData
->CharHeight
;
812 /* calculate the region that needs to be updated */
813 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
815 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
817 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
819 InvalidateRgn(hWnd
, rgn1
, FALSE
);
829 InvalidateRect(hWnd
, &changeRect
, FALSE
);
833 else if(oldRect
.left
!= -1)
835 /* clear the selection */
836 GuiData
->Selection
.left
= -1;
837 oldRect
.left
*= GuiData
->CharWidth
;
838 oldRect
.top
*= GuiData
->CharHeight
;
839 oldRect
.right
*= GuiData
->CharWidth
;
840 oldRect
.bottom
*= GuiData
->CharHeight
;
841 InvalidateRect(hWnd
, &oldRect
, FALSE
);
847 GuiConsolePaint(PCSRSS_CONSOLE Console
,
848 PGUI_CONSOLE_DATA GuiData
,
852 PCSRSS_SCREEN_BUFFER Buff
;
853 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
854 ULONG Line
, Char
, Start
;
857 BYTE LastAttribute
, Attribute
;
858 ULONG CursorX
, CursorY
, CursorHeight
;
859 HBRUSH CursorBrush
, OldBrush
;
862 Buff
= Console
->ActiveBuffer
;
864 TopLine
= rc
->top
/ GuiData
->CharHeight
;
865 BottomLine
= (rc
->bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1;
866 LeftChar
= rc
->left
/ GuiData
->CharWidth
;
867 RightChar
= (rc
->right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1;
868 LastAttribute
= Buff
->Buffer
[(TopLine
* Buff
->MaxX
+ LeftChar
) * 2 + 1];
870 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
));
871 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
>> 4));
873 EnterCriticalSection(&Buff
->Header
.Lock
);
875 if (BottomLine
>= Buff
->MaxY
) BottomLine
= Buff
->MaxY
- 1;
876 if (RightChar
>= Buff
->MaxX
) RightChar
= Buff
->MaxX
- 1;
878 OldFont
= SelectObject(hDC
,
881 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
883 From
= Buff
->Buffer
+ ConioGetBufferOffset(Buff
, LeftChar
, Line
);
885 To
= GuiData
->LineBuffer
;
887 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
889 if (*(From
+ 1) != LastAttribute
)
892 Start
* GuiData
->CharWidth
,
893 Line
* GuiData
->CharHeight
,
897 To
= GuiData
->LineBuffer
;
898 Attribute
= *(From
+ 1);
899 if (Attribute
!= LastAttribute
)
901 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
));
902 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
>> 4));
903 LastAttribute
= Attribute
;
907 MultiByteToWideChar(Console
->OutputCodePage
,
918 Start
* GuiData
->CharWidth
,
919 Line
* GuiData
->CharHeight
,
921 RightChar
- Start
+ 1);
924 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
925 !GuiData
->ForceCursorOff
)
927 CursorX
= Buff
->CurrentX
;
928 CursorY
= Buff
->CurrentY
;
929 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
930 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
932 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
933 if (CursorHeight
< 1)
937 From
= Buff
->Buffer
+ ConioGetBufferOffset(Buff
, Buff
->CurrentX
, Buff
->CurrentY
) + 1;
939 if (*From
!= DEFAULT_ATTRIB
)
941 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(GuiData
, *From
));
945 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
948 OldBrush
= SelectObject(hDC
,
951 CursorX
* GuiData
->CharWidth
,
952 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
958 DeleteObject(CursorBrush
);
962 LeaveCriticalSection(&Buff
->Header
.Lock
);
969 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
973 PCSRSS_CONSOLE Console
;
974 PGUI_CONSOLE_DATA GuiData
;
976 hDC
= BeginPaint(hWnd
, &ps
);
978 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
979 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
981 GuiConsoleGetDataPointers(hWnd
,
984 if (Console
!= NULL
&& GuiData
!= NULL
&&
985 Console
->ActiveBuffer
!= NULL
)
987 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
989 EnterCriticalSection(&GuiData
->Lock
);
991 GuiConsolePaint(Console
,
996 if (GuiData
->Selection
.left
!= -1)
998 RECT rc
= GuiData
->Selection
;
1000 rc
.left
*= GuiData
->CharWidth
;
1001 rc
.top
*= GuiData
->CharHeight
;
1002 rc
.right
*= GuiData
->CharWidth
;
1003 rc
.bottom
*= GuiData
->CharHeight
;
1005 /* invert the selection */
1006 if (IntersectRect(&rc
,
1019 LeaveCriticalSection(&GuiData
->Lock
);
1023 EndPaint(hWnd
, &ps
);
1027 static VOID FASTCALL
1028 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1030 PCSRSS_CONSOLE Console
;
1031 PGUI_CONSOLE_DATA GuiData
;
1034 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1035 Message
.hwnd
= hWnd
;
1036 Message
.message
= msg
;
1037 Message
.wParam
= wParam
;
1038 Message
.lParam
= lParam
;
1040 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1042 /* clear the selection */
1043 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1046 ConioProcessKey(&Message
, Console
, FALSE
);
1049 static VOID FASTCALL
1050 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1054 RegionRect
.left
= Region
->left
* GuiData
->CharWidth
;
1055 RegionRect
.top
= Region
->top
* GuiData
->CharHeight
;
1056 RegionRect
.right
= (Region
->right
+ 1) * GuiData
->CharWidth
;
1057 RegionRect
.bottom
= (Region
->bottom
+ 1) * GuiData
->CharHeight
;
1059 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1063 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1065 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1067 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1069 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1073 static VOID FASTCALL
1074 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1081 CellRect
.bottom
= y
;
1083 GuiIntDrawRegion(GuiData
, Wnd
, &CellRect
);
1087 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1088 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1090 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1091 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1092 LONG CursorEndX
, CursorEndY
;
1095 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1100 if (0 != ScrolledLines
)
1102 ScrollRect
.left
= 0;
1104 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1105 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1107 if (GuiData
->Selection
.left
!= -1)
1109 /* scroll the selection */
1110 if (GuiData
->Selection
.top
> ScrolledLines
)
1112 GuiData
->Selection
.top
-= ScrolledLines
;
1113 GuiData
->Selection
.bottom
-= ScrolledLines
;
1115 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1117 GuiData
->Selection
.left
= -1;
1121 GuiData
->Selection
.top
= 0;
1122 GuiData
->Selection
.bottom
-= ScrolledLines
;
1126 ScrollWindowEx(Console
->hWindow
,
1128 -(ScrolledLines
* GuiData
->CharHeight
),
1136 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
1138 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1139 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1141 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1144 CursorEndX
= Buff
->CurrentX
;
1145 CursorEndY
= Buff
->CurrentY
;
1146 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1147 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1148 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1150 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1155 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1159 if (Console
->ActiveBuffer
== Buff
)
1161 UpdateRect
.left
= Buff
->CurrentX
;
1162 UpdateRect
.top
= Buff
->CurrentY
;
1163 UpdateRect
.right
= UpdateRect
.left
;
1164 UpdateRect
.bottom
= UpdateRect
.top
;
1165 ConioDrawRegion(Console
, &UpdateRect
);
1172 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
1176 if (Console
->ActiveBuffer
== Buff
)
1178 /* Redraw char at old position (removes cursor) */
1179 UpdateRect
.left
= OldCursorX
;
1180 UpdateRect
.top
= OldCursorY
;
1181 UpdateRect
.right
= OldCursorX
;
1182 UpdateRect
.bottom
= OldCursorY
;
1183 ConioDrawRegion(Console
, &UpdateRect
);
1184 /* Redraw char at new position (shows cursor) */
1185 UpdateRect
.left
= Buff
->CurrentX
;
1186 UpdateRect
.top
= Buff
->CurrentY
;
1187 UpdateRect
.right
= UpdateRect
.left
;
1188 UpdateRect
.bottom
= UpdateRect
.top
;
1189 ConioDrawRegion(Console
, &UpdateRect
);
1196 GuiUpdateScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1198 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1200 if (Console
->ActiveBuffer
== Buff
)
1202 GuiData
->ScreenText
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
);
1203 GuiData
->ScreenBackground
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
>> 4);
1209 static VOID FASTCALL
1210 GuiConsoleHandleTimer(HWND hWnd
)
1212 PCSRSS_CONSOLE Console
;
1213 PGUI_CONSOLE_DATA GuiData
;
1216 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1217 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1219 CursorRect
.left
= Console
->ActiveBuffer
->CurrentX
;
1220 CursorRect
.top
= Console
->ActiveBuffer
->CurrentY
;
1221 CursorRect
.right
= CursorRect
.left
;
1222 CursorRect
.bottom
= CursorRect
.top
;
1223 GuiDrawRegion(Console
, &CursorRect
);
1226 static VOID FASTCALL
1227 GuiConsoleHandleClose(HWND hWnd
)
1229 PCSRSS_CONSOLE Console
;
1230 PGUI_CONSOLE_DATA GuiData
;
1231 PLIST_ENTRY current_entry
;
1232 PCSRSS_PROCESS_DATA current
;
1234 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1236 EnterCriticalSection(&Console
->Header
.Lock
);
1238 current_entry
= Console
->ProcessList
.Flink
;
1239 while (current_entry
!= &Console
->ProcessList
)
1241 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1242 current_entry
= current_entry
->Flink
;
1244 /* FIXME: Windows will wait up to 5 seconds for the thread to exit.
1245 * We shouldn't wait here, though, since the console lock is entered.
1246 * A copy of the thread list probably needs to be made. */
1247 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1250 LeaveCriticalSection(&Console
->Header
.Lock
);
1253 static VOID FASTCALL
1254 GuiConsoleHandleNcDestroy(HWND hWnd
)
1256 PCSRSS_CONSOLE Console
;
1257 PGUI_CONSOLE_DATA GuiData
;
1260 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1262 Console
->PrivateData
= NULL
;
1263 DeleteCriticalSection(&GuiData
->Lock
);
1264 GetSystemMenu(hWnd
, TRUE
);
1265 if (GuiData
->ConsoleLibrary
)
1266 FreeLibrary(GuiData
->ConsoleLibrary
);
1268 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1271 static VOID FASTCALL
1272 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1274 PCSRSS_CONSOLE Console
;
1275 PGUI_CONSOLE_DATA GuiData
;
1279 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1280 if (Console
== NULL
|| GuiData
== NULL
) return;
1282 pt
= MAKEPOINTS(lParam
);
1284 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1285 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1286 rc
.right
= rc
.left
+ 1;
1287 rc
.bottom
= rc
.top
+ 1;
1289 GuiData
->SelectionStart
.x
= rc
.left
;
1290 GuiData
->SelectionStart
.y
= rc
.top
;
1294 GuiData
->MouseDown
= TRUE
;
1296 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1299 static VOID FASTCALL
1300 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1302 PCSRSS_CONSOLE Console
;
1303 PGUI_CONSOLE_DATA GuiData
;
1307 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1308 if (Console
== NULL
|| GuiData
== NULL
) return;
1309 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1311 pt
= MAKEPOINTS(lParam
);
1313 rc
.left
= GuiData
->SelectionStart
.x
;
1314 rc
.top
= GuiData
->SelectionStart
.y
;
1315 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1316 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1318 /* exchange left/top with right/bottom if required */
1319 if(rc
.left
>= rc
.right
)
1323 rc
.left
= max(rc
.right
- 1, 0);
1326 if(rc
.top
>= rc
.bottom
)
1330 rc
.top
= max(rc
.bottom
- 1, 0);
1331 rc
.bottom
= tmp
+ 1;
1334 GuiData
->MouseDown
= FALSE
;
1336 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1341 static VOID FASTCALL
1342 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1344 PCSRSS_CONSOLE Console
;
1345 PGUI_CONSOLE_DATA GuiData
;
1349 if (!(wParam
& MK_LBUTTON
)) return;
1351 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1352 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1354 pt
= MAKEPOINTS(lParam
);
1356 rc
.left
= GuiData
->SelectionStart
.x
;
1357 rc
.top
= GuiData
->SelectionStart
.y
;
1358 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1359 if (Console
->Size
.X
< rc
.right
)
1361 rc
.right
= Console
->Size
.X
;
1363 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1364 if (Console
->Size
.Y
< rc
.bottom
)
1366 rc
.bottom
= Console
->Size
.Y
;
1369 /* exchange left/top with right/bottom if required */
1370 if(rc
.left
>= rc
.right
)
1374 rc
.left
= max(rc
.right
- 1, 0);
1377 if(rc
.top
>= rc
.bottom
)
1381 rc
.top
= max(rc
.bottom
- 1, 0);
1382 rc
.bottom
= tmp
+ 1;
1385 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1388 static VOID FASTCALL
1389 GuiConsoleRightMouseDown(HWND hWnd
)
1391 PCSRSS_CONSOLE Console
;
1392 PGUI_CONSOLE_DATA GuiData
;
1394 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1395 if (Console
== NULL
|| GuiData
== NULL
) return;
1397 if (GuiData
->Selection
.left
== -1)
1399 /* FIXME - paste text from clipboard */
1403 /* FIXME - copy selection to clipboard */
1405 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1412 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1414 PCSRSS_CONSOLE Console
;
1415 APPLET_PROC CPLFunc
;
1416 TCHAR szBuffer
[MAX_PATH
];
1417 ConsoleInfo SharedInfo
;
1419 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1421 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1423 if (GuiData
== NULL
)
1425 DPRINT("GuiConsoleGetDataPointers failed\n");
1429 if (GuiData
->ConsoleLibrary
== NULL
)
1431 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1432 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1433 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1435 if (GuiData
->ConsoleLibrary
== NULL
)
1437 DPRINT1("failed to load console.dll");
1442 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1445 DPRINT("Error: Console.dll misses CPlApplet export\n");
1450 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1451 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1452 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1453 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1454 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1455 SharedInfo
.PopupText
= GuiData
->PopupText
;
1456 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1457 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1458 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1459 SharedInfo
.ScreenBuffer
= GuiData
->ScreenBufferSize
;
1460 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1461 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1462 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1463 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1464 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1465 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1466 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1467 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1469 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1471 DPRINT("Error: failed to initialize console.dll\n");
1475 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1477 DPRINT("Error: console.dll returned unexpected CPL count\n");
1481 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1483 static LRESULT FASTCALL
1484 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, PGUI_CONSOLE_DATA GuiData
)
1490 case ID_SYSTEM_EDIT_MARK
:
1491 case ID_SYSTEM_EDIT_COPY
:
1492 case ID_SYSTEM_EDIT_PASTE
:
1493 case ID_SYSTEM_EDIT_SELECTALL
:
1494 case ID_SYSTEM_EDIT_SCROLL
:
1495 case ID_SYSTEM_EDIT_FIND
:
1498 case ID_SYSTEM_DEFAULTS
:
1499 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1502 case ID_SYSTEM_PROPERTIES
:
1503 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1507 Ret
= DefWindowProcW(hWnd
, WM_SYSCOMMAND
, wParam
, lParam
);
1513 static VOID FASTCALL
1514 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1516 PCSRSS_CONSOLE Console
;
1517 PGUI_CONSOLE_DATA GuiData
;
1519 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1520 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
)
1522 DPRINT1("GuiConsoleResize X %d Y %d\n", LOWORD(lParam
), HIWORD(lParam
));
1527 GuiConsoleHandleScrollbarMenu()
1531 hMenu
= CreatePopupMenu();
1534 DPRINT("CreatePopupMenu failed\n");
1537 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1538 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1539 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1540 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1541 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1542 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1543 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1544 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1545 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1546 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1550 static VOID FASTCALL
1551 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1555 PCSRSS_SCREEN_BUFFER ActiveBuffer
= Console
->ActiveBuffer
;
1557 EnterCriticalSection(&ActiveBuffer
->Header
.Lock
);
1559 /* apply text / background color */
1560 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1561 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1563 /* apply cursor size */
1564 ActiveBuffer
->CursorInfo
.dwSize
= max(min(pConInfo
->CursorSize
, 1), 100);
1566 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1567 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1569 if (windx
!= ActiveBuffer
->MaxX
|| windy
!= ActiveBuffer
->MaxY
)
1571 BYTE
* Buffer
= HeapAlloc(Win32CsrApiHeap
, 0, windx
* windy
* 2);
1576 DWORD BufferOffset
= 0;
1583 value
= MAKEWORD(' ', ActiveBuffer
->DefaultAttrib
);
1585 DPRINT("MaxX %d MaxY %d windx %d windy %d value %04x DefaultAttrib %d\n",ActiveBuffer
->MaxX
, ActiveBuffer
->MaxY
, windx
, windy
, value
, ActiveBuffer
->DefaultAttrib
);
1586 OldBuffer
= ActiveBuffer
->Buffer
;
1588 for (CurrentY
= 0; CurrentY
< min(ActiveBuffer
->MaxY
, windy
); CurrentY
++)
1590 if (windx
<= ActiveBuffer
->MaxX
)
1593 RtlCopyMemory(&Buffer
[Offset
], &OldBuffer
[BufferOffset
], windx
* 2);
1594 Offset
+= (windx
* 2);
1595 BufferOffset
+= (ActiveBuffer
->MaxX
* 2);
1600 RtlCopyMemory(&Buffer
[Offset
], &OldBuffer
[BufferOffset
], ActiveBuffer
->MaxX
* 2);
1601 Offset
+= (ActiveBuffer
->MaxX
* 2);
1603 diff
= windx
- ActiveBuffer
->MaxX
;
1604 /* zero new part of it */
1606 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
);
1608 for (i
= 0; i
< diff
* 2; i
++)
1610 Buffer
[Offset
* 2] = ' ';
1611 Buffer
[Offset
* 2 + 1] = ActiveBuffer
->DefaultAttrib
;
1614 Offset
+= (diff
* 2);
1615 BufferOffset
+= (ActiveBuffer
->MaxX
* 2);
1619 if (windy
> ActiveBuffer
->MaxY
)
1621 diff
= windy
- ActiveBuffer
->MaxX
;
1623 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
* windx
);
1625 for (i
= 0; i
< diff
* 2; i
++)
1627 Buffer
[Offset
* 2] = ' ';
1628 Buffer
[Offset
* 2 + 1] = ActiveBuffer
->DefaultAttrib
;
1633 (void)InterlockedExchangePointer((PVOID
volatile *)&ActiveBuffer
->Buffer
, Buffer
);
1634 HeapFree(Win32CsrApiHeap
, 0, OldBuffer
);
1635 ActiveBuffer
->MaxX
= windx
;
1636 ActiveBuffer
->MaxY
= windy
;
1637 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1641 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1646 windx
= LOWORD(pConInfo
->WindowSize
);
1647 windy
= HIWORD(pConInfo
->WindowSize
);
1649 if (windx
> Console
->Size
.X
)
1651 PWCHAR LineBuffer
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
, windx
* sizeof(WCHAR
));
1654 HeapFree(Win32CsrApiHeap
, 0, GuiData
->LineBuffer
);
1655 GuiData
->LineBuffer
= LineBuffer
;
1659 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1665 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1668 Console
->Size
.X
= windx
;
1669 Console
->Size
.Y
= windy
;
1671 GetWindowRect(pConInfo
->hConsoleWindow
, &rect
);
1673 rect
.right
= rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+ 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
1674 rect
.bottom
= rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
1676 MoveWindow(pConInfo
->hConsoleWindow
, rect
.left
, rect
.top
, rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, FALSE
);
1678 if (Console
->Size
.X
< ActiveBuffer
->MaxX
)
1680 /* show scrollbar when window becomes smaller than active screen buffer */
1681 ShowScrollBar(pConInfo
->hConsoleWindow
, SB_CTL
, TRUE
);
1685 /* hide scrollbar */
1686 ShowScrollBar(pConInfo
->hConsoleWindow
, SB_CTL
, FALSE
);
1689 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1690 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1695 GuiConsoleHandleScroll(HWND hwnd
, UINT uMsg
, WPARAM wParam
, PGUI_CONSOLE_DATA GuiData
)
1700 /* set scrollbar sizes */
1701 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1702 sInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_PAGE
| SIF_TRACKPOS
;
1704 if (!GetScrollInfo(hwnd
,
1705 (uMsg
== WM_HSCROLL
? SB_HORZ
: SB_VERT
),
1711 old_pos
= sInfo
.nPos
;
1713 switch(LOWORD(wParam
))
1724 sInfo
.nPos
-= sInfo
.nPage
;
1728 sInfo
.nPos
+= sInfo
.nPage
;
1732 sInfo
.nPage
= sInfo
.nTrackPos
;
1736 sInfo
.nPos
= sInfo
.nMin
;
1740 sInfo
.nPos
= sInfo
.nMax
;
1747 sInfo
.fMask
= SIF_POS
;
1748 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1751 (uMsg
== WM_HSCROLL
? SB_HORZ
: SB_VERT
),
1755 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1756 sInfo
.fMask
= SIF_POS
;
1758 if (!GetScrollInfo(hwnd
,
1759 (uMsg
== WM_HSCROLL
? SB_HORZ
: SB_VERT
),
1765 if (old_pos
!= sInfo
.nPos
)
1768 /// fixme scroll window
1771 ScrollWindowEx(hwnd
,
1773 GuiData
->CharHeight
* (old_pos
- sInfo
.nPos
),
1785 static LRESULT CALLBACK
1786 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1789 PGUI_CONSOLE_DATA GuiData
= NULL
;
1790 PCSRSS_CONSOLE Console
= NULL
;
1792 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1797 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
1800 GuiConsoleHandlePaint(hWnd
, (HDC
)wParam
);
1807 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
1810 GuiConsoleHandleTimer(hWnd
);
1813 GuiConsoleHandleClose(hWnd
);
1816 GuiConsoleHandleNcDestroy(hWnd
);
1818 case WM_LBUTTONDOWN
:
1819 GuiConsoleLeftMouseDown(hWnd
, lParam
);
1822 GuiConsoleLeftMouseUp(hWnd
, lParam
);
1824 case WM_RBUTTONDOWN
:
1825 GuiConsoleRightMouseDown(hWnd
);
1828 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
1831 Result
= GuiConsoleHandleSysMenuCommand(hWnd
, wParam
, lParam
, GuiData
);
1835 Result
= GuiConsoleHandleScroll(hWnd
, msg
, wParam
, GuiData
);
1838 GuiConsoleResize(hWnd
, wParam
, lParam
);
1840 case PM_APPLY_CONSOLE_INFO
:
1841 GuiApplyUserSettings(Console
, GuiData
, (PConsoleInfo
)wParam
);
1844 GuiConsoleWriteUserSettings(Console
, GuiData
);
1848 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1855 static LRESULT CALLBACK
1856 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1861 PWCHAR Buffer
, Title
;
1862 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
1869 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
1871 case PM_CREATE_CONSOLE
:
1872 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1873 Console
->Title
.Length
+ sizeof(WCHAR
));
1876 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1877 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1884 NewWindow
= CreateWindowW(L
"ConsoleWindowClass",
1886 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
| WS_HSCROLL
| WS_VSCROLL
, //WS_OVERLAPPEDWINDOW
1893 (HINSTANCE
) GetModuleHandleW(NULL
),
1897 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1899 if (NULL
!= NewWindow
)
1901 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
1902 ShowWindow(NewWindow
, SW_SHOW
);
1904 return (LRESULT
) NewWindow
;
1905 case PM_DESTROY_CONSOLE
:
1906 /* Window creation is done using a PostMessage(), so it's possible that the
1907 * window that we want to destroy doesn't exist yet. So first empty the message
1909 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
1911 TranslateMessage(&Msg
);
1912 DispatchMessageW(&Msg
);
1914 DestroyWindow(Console
->hWindow
);
1915 Console
->hWindow
= NULL
;
1916 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
1918 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
1919 if (0 == WindowCount
)
1922 DestroyWindow(hWnd
);
1923 PrivateCsrssManualGuiCheck(-1);
1928 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1932 static DWORD STDCALL
1933 GuiConsoleGuiThread(PVOID Data
)
1936 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
1938 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
1940 WS_OVERLAPPEDWINDOW
,
1947 (HINSTANCE
) GetModuleHandleW(NULL
),
1949 if (NULL
== NotifyWnd
)
1951 PrivateCsrssManualGuiCheck(-1);
1952 SetEvent(*GraphicsStartupEvent
);
1956 SetEvent(*GraphicsStartupEvent
);
1958 while(GetMessageW(&msg
, NULL
, 0, 0))
1960 TranslateMessage(&msg
);
1961 DispatchMessageW(&msg
);
1967 static BOOL FASTCALL
1972 if (NULL
== NotifyWnd
)
1974 PrivateCsrssManualGuiCheck(+1);
1977 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1978 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
1979 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
1981 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1984 wc
.hbrBackground
= NULL
;
1985 wc
.lpszMenuName
= NULL
;
1989 if (RegisterClassExW(&wc
) == 0)
1991 DPRINT1("Failed to register notify wndproc\n");
1995 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1996 wc
.lpszClassName
= L
"ConsoleWindowClass";
1997 wc
.lpfnWndProc
= GuiConsoleWndProc
;
1999 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
2000 wc
.hIcon
= LoadIconW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1));
2001 wc
.hCursor
= LoadCursorW(NULL
, (LPCWSTR
) IDC_ARROW
);
2002 wc
.hbrBackground
= NULL
;
2003 wc
.lpszMenuName
= NULL
;
2006 wc
.hIconSm
= LoadImageW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1), IMAGE_ICON
,
2007 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
2009 if (RegisterClassExW(&wc
) == 0)
2011 DPRINT1("Failed to register console wndproc\n");
2019 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
2021 Buffer
->DefaultAttrib
= DEFAULT_ATTRIB
;
2025 GuiChangeTitle(PCSRSS_CONSOLE Console
)
2027 PWCHAR Buffer
, Title
;
2029 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
2030 Console
->Title
.Length
+ sizeof(WCHAR
));
2033 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
2034 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
2042 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Title
);
2046 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
2053 GuiChangeIcon(PCSRSS_CONSOLE Console
, HICON hWindowIcon
)
2055 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)hWindowIcon
);
2056 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hWindowIcon
);
2062 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
2064 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
2067 static CSRSS_CONSOLE_VTBL GuiVtbl
=
2069 GuiInitScreenBuffer
,
2074 GuiUpdateScreenInfo
,
2081 GuiInitConsole(PCSRSS_CONSOLE Console
)
2083 HANDLE GraphicsStartupEvent
;
2084 HANDLE ThreadHandle
;
2085 PGUI_CONSOLE_DATA GuiData
;
2087 if (! ConsInitialized
)
2089 ConsInitialized
= TRUE
;
2092 ConsInitialized
= FALSE
;
2093 return STATUS_UNSUCCESSFUL
;
2097 Console
->Vtbl
= &GuiVtbl
;
2098 if (NULL
== NotifyWnd
)
2100 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2101 if (NULL
== GraphicsStartupEvent
)
2103 return STATUS_UNSUCCESSFUL
;
2106 ThreadHandle
= CreateThread(NULL
,
2108 GuiConsoleGuiThread
,
2109 (PVOID
) &GraphicsStartupEvent
,
2112 if (NULL
== ThreadHandle
)
2114 NtClose(GraphicsStartupEvent
);
2115 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
2116 return STATUS_UNSUCCESSFUL
;
2118 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
2119 CloseHandle(ThreadHandle
);
2121 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
2122 CloseHandle(GraphicsStartupEvent
);
2124 if (NULL
== NotifyWnd
)
2126 DPRINT1("Win32Csr: Failed to create notification window.\n");
2127 return STATUS_UNSUCCESSFUL
;
2130 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
2131 sizeof(GUI_CONSOLE_DATA
));
2134 DPRINT1("Win32Csr: Failed to create GUI_CONSOLE_DATA\n");
2135 return STATUS_UNSUCCESSFUL
;
2138 Console
->PrivateData
= (PVOID
) GuiData
;
2140 * we need to wait untill the GUI has been fully initialized
2141 * to retrieve custom settings i.e. WindowSize etc..
2142 * Ideally we could use SendNotifyMessage for this but its not
2146 GuiData
->hGuiInitEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2147 /* create console */
2148 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
2150 /* wait untill initialization has finished */
2151 WaitForSingleObject(GuiData
->hGuiInitEvent
, INFINITE
);
2152 DPRINT1("received event Console %p GuiData %p X %d Y %d\n", Console
, Console
->PrivateData
, Console
->Size
.X
, Console
->Size
.Y
);
2153 CloseHandle(GuiData
->hGuiInitEvent
);
2154 GuiData
->hGuiInitEvent
= NULL
;
2156 return STATUS_SUCCESS
;