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 WINAPI
PrivateCsrssManualGuiCheck(LONG Check
);
19 /* GLOBALS *******************************************************************/
21 typedef struct GUI_CONSOLE_DATA_TAG
29 CRITICAL_SECTION Lock
;
33 HMODULE ConsoleLibrary
;
35 WCHAR FontName
[LF_FACESIZE
];
42 DWORD NumberOfHistoryBuffers
;
43 DWORD HistoryBufferSize
;
47 COLORREF ScreenBackground
;
48 COLORREF PopupBackground
;
51 WCHAR szProcessName
[MAX_PATH
];
52 } GUI_CONSOLE_DATA
, *PGUI_CONSOLE_DATA
;
57 #define PM_CREATE_CONSOLE (WM_APP + 1)
58 #define PM_DESTROY_CONSOLE (WM_APP + 2)
60 #define CURSOR_BLINK_TIME 500
61 #define DEFAULT_ATTRIB (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
63 static BOOL ConsInitialized
= FALSE
;
64 static HWND NotifyWnd
;
66 typedef struct _GUICONSOLE_MENUITEM
69 const struct _GUICONSOLE_MENUITEM
*SubMenu
;
71 } GUICONSOLE_MENUITEM
, *PGUICONSOLE_MENUITEM
;
73 static const GUICONSOLE_MENUITEM GuiConsoleEditMenuItems
[] =
75 { IDS_MARK
, NULL
, ID_SYSTEM_EDIT_MARK
},
76 { IDS_COPY
, NULL
, ID_SYSTEM_EDIT_COPY
},
77 { IDS_PASTE
, NULL
, ID_SYSTEM_EDIT_PASTE
},
78 { IDS_SELECTALL
, NULL
, ID_SYSTEM_EDIT_SELECTALL
},
79 { IDS_SCROLL
, NULL
, ID_SYSTEM_EDIT_SCROLL
},
80 { IDS_FIND
, NULL
, ID_SYSTEM_EDIT_FIND
},
82 { 0, NULL
, 0 } /* End of list */
85 static const GUICONSOLE_MENUITEM GuiConsoleMainMenuItems
[] =
87 { (UINT
)-1, NULL
, 0 }, /* Separator */
88 { IDS_EDIT
, GuiConsoleEditMenuItems
, 0 },
89 { IDS_DEFAULTS
, NULL
, ID_SYSTEM_DEFAULTS
},
90 { IDS_PROPERTIES
, NULL
, ID_SYSTEM_PROPERTIES
},
92 { 0, NULL
, 0 } /* End of list */
95 static const COLORREF s_Colors
[] =
115 #define GuiConsoleRGBFromAttribute(GuiData, Attribute) ((GuiData)->Colors[(Attribute) & 0xF])
117 /* FUNCTIONS *****************************************************************/
120 GuiConsoleAppendMenuItems(HMENU hMenu
,
121 const GUICONSOLE_MENUITEM
*Items
)
124 WCHAR szMenuString
[255];
126 HINSTANCE hInst
= GetModuleHandleW(L
"win32csr");
130 if (Items
[i
].uID
!= (UINT
)-1)
132 if (LoadStringW(hInst
,
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
);
171 }while(!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
175 GuiConsoleCreateSysMenu(PCSRSS_CONSOLE Console
)
178 hMenu
= GetSystemMenu(Console
->hWindow
,
182 GuiConsoleAppendMenuItems(hMenu
,
183 GuiConsoleMainMenuItems
);
184 DrawMenuBar(Console
->hWindow
);
189 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
191 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
192 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
196 GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId
, PHANDLE hProcHandle
, PHKEY hResult
, REGSAM samDesired
)
198 HANDLE hProcessToken
= NULL
;
203 UNICODE_STRING SidName
;
207 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| READ_CONTROL
, FALSE
, ProcessId
);
210 DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
214 if (!OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
216 DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
217 CloseHandle(hProcess
);
221 if (!GetTokenInformation(hProcessToken
, TokenUser
, (PVOID
)Buffer
, sizeof(Buffer
), &Length
))
223 DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
224 CloseHandle(hProcess
);
225 CloseHandle(hProcessToken
);
229 TokUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
230 if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName
, TokUser
, TRUE
)))
232 DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
236 res
= RegOpenKeyExW(HKEY_USERS
, SidName
.Buffer
, 0, samDesired
, hResult
);
237 RtlFreeUnicodeString(&SidName
);
239 CloseHandle(hProcessToken
);
241 *hProcHandle
= hProcess
;
243 CloseHandle(hProcess
);
245 if (res
!= ERROR_SUCCESS
)
252 GuiConsoleOpenUserSettings(PGUI_CONSOLE_DATA GuiData
, DWORD ProcessId
, PHKEY hSubKey
, REGSAM samDesired
, BOOL bCreate
)
254 WCHAR szProcessName
[MAX_PATH
];
255 WCHAR szBuffer
[MAX_PATH
];
256 UINT fLength
, wLength
;
257 DWORD dwBitmask
, dwLength
;
258 WCHAR CurDrive
[] = { 'A',':', 0 };
264 * console properties are stored under
267 * There are 3 ways to store console properties
269 * 1. use console title as subkey name
272 * 2. use application name as subkey name
274 * 3. use unexpanded path to console application.
275 * i.e. %SystemRoot%_system32_cmd.exe
278 DPRINT("GuiConsoleOpenUserSettings entered\n");
280 if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId
, &hProcess
, &hKey
, samDesired
))
282 DPRINT("GuiConsoleOpenUserRegistryPathPerProcessId failed\n");
286 /* FIXME we do not getting the process name so no menu will be loading, why ?*/
287 fLength
= GetProcessImageFileNameW(hProcess
, szProcessName
, sizeof(GuiData
->szProcessName
) / sizeof(WCHAR
));
288 CloseHandle(hProcess
);
290 //DPRINT1("szProcessName3 : %S\n",szProcessName);
294 DPRINT("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess
);
298 * try the process name as path
301 ptr
= wcsrchr(szProcessName
, L
'\\');
302 wcscpy(GuiData
->szProcessName
, ptr
);
304 swprintf(szBuffer
, L
"Console%s",ptr
);
305 DPRINT("#1 Path : %S\n", szBuffer
);
309 if (RegCreateKeyW(hKey
, szBuffer
, hSubKey
) == ERROR_SUCCESS
)
318 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
325 * try the "Shortcut to processname" as path
326 * FIXME: detect wheter the process was started as a shortcut
329 swprintf(szBuffer
, L
"Console\\Shortcut to %S", ptr
);
330 DPRINT("#2 Path : %S\n", szBuffer
);
331 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
333 swprintf(GuiData
->szProcessName
, L
"Shortcut to %S", ptr
);
339 * if the path contains \\Device\\HarddiskVolume1\... remove it
342 if (szProcessName
[0] == L
'\\')
344 dwBitmask
= GetLogicalDrives();
349 dwLength
= QueryDosDeviceW(CurDrive
, szBuffer
, MAX_PATH
);
352 if (!memcmp(szBuffer
, szProcessName
, (dwLength
-2)*sizeof(WCHAR
)))
354 wcscpy(szProcessName
, CurDrive
);
355 RtlMoveMemory(&szProcessName
[2], &szProcessName
[dwLength
-1], fLength
- dwLength
-1);
360 dwBitmask
= (dwBitmask
>> 1);
366 * last attempt: check whether the file is under %SystemRoot%
367 * and use path like Console\%SystemRoot%_dir_dir2_file.exe
370 wLength
= GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
373 if (!wcsncmp(szProcessName
, szBuffer
, wLength
))
375 /* replace slashes by underscores */
376 while((ptr
= wcschr(szProcessName
, L
'\\')))
379 swprintf(szBuffer
, L
"Console\\\%SystemRoot\%%S", &szProcessName
[wLength
]);
380 DPRINT("#3 Path : %S\n", szBuffer
);
381 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
383 swprintf(GuiData
->szProcessName
, L
"\%SystemRoot\%%S", &szProcessName
[wLength
]);
394 GuiConsoleWriteUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
)
397 PCSRSS_PROCESS_DATA ProcessData
;
399 if (Console
->ProcessList
.Flink
== &Console
->ProcessList
)
401 DPRINT("GuiConsoleWriteUserSettings: No Process!!!\n");
404 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
405 if (!GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
| KEY_WRITE
, TRUE
))
410 if (Console
->ActiveBuffer
->CursorInfo
.dwSize
<= 1)
412 RegDeleteKeyW(hKey
, L
"CursorSize");
416 RegSetValueExW(hKey
, L
"CursorSize", 0, REG_DWORD
, (const BYTE
*)&Console
->ActiveBuffer
->CursorInfo
.dwSize
, sizeof(DWORD
));
419 if (GuiData
->NumberOfHistoryBuffers
== 5)
421 RegDeleteKeyW(hKey
, L
"NumberOfHistoryBuffers");
425 RegSetValueExW(hKey
, L
"NumberOfHistoryBuffers", 0, REG_DWORD
, (const BYTE
*)&GuiData
->NumberOfHistoryBuffers
, sizeof(DWORD
));
428 if (GuiData
->HistoryBufferSize
== 50)
430 RegDeleteKeyW(hKey
, L
"HistoryBufferSize");
434 RegSetValueExW(hKey
, L
"HistoryBufferSize", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryBufferSize
, sizeof(DWORD
));
437 if (GuiData
->FullScreen
== FALSE
)
439 RegDeleteKeyW(hKey
, L
"FullScreen");
443 RegSetValueExW(hKey
, L
"FullScreen", 0, REG_DWORD
, (const BYTE
*)&GuiData
->FullScreen
, sizeof(DWORD
));
446 if ( GuiData
->QuickEdit
== FALSE
)
448 RegDeleteKeyW(hKey
, L
"QuickEdit");
452 RegSetValueExW(hKey
, L
"QuickEdit", 0, REG_DWORD
, (const BYTE
*)&GuiData
->QuickEdit
, sizeof(DWORD
));
455 if (GuiData
->InsertMode
== TRUE
)
457 RegDeleteKeyW(hKey
, L
"InsertMode");
461 RegSetValueExW(hKey
, L
"InsertMode", 0, REG_DWORD
, (const BYTE
*)&GuiData
->InsertMode
, sizeof(DWORD
));
464 if (GuiData
->HistoryNoDup
== FALSE
)
466 RegDeleteKeyW(hKey
, L
"HistoryNoDup");
470 RegSetValueExW(hKey
, L
"HistoryNoDup", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryNoDup
, sizeof(DWORD
));
473 if (GuiData
->ScreenText
== RGB(192, 192, 192))
476 * MS uses console attributes instead of real color
478 RegDeleteKeyW(hKey
, L
"ScreenText");
482 RegSetValueExW(hKey
, L
"ScreenText", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenText
, sizeof(COLORREF
));
485 if (GuiData
->ScreenBackground
== RGB(0, 0, 0))
487 RegDeleteKeyW(hKey
, L
"ScreenBackground");
491 RegSetValueExW(hKey
, L
"ScreenBackground", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenBackground
, sizeof(COLORREF
));
498 GuiConsoleReadUserSettings(HKEY hKey
, PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
500 DWORD dwNumSubKeys
= 0;
505 WCHAR szValueName
[MAX_PATH
];
506 WCHAR szValue
[LF_FACESIZE
] = L
"\0";
509 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
511 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
515 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
517 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
519 dwValue
= sizeof(Value
);
520 dwValueName
= MAX_PATH
;
522 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
524 if (dwType
== REG_SZ
)
527 * retry in case of string value
529 dwValue
= sizeof(szValue
);
530 dwValueName
= LF_FACESIZE
;
531 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
537 if (!wcscmp(szValueName
, L
"CursorSize"))
541 Buffer
->CursorInfo
.dwSize
= Value
;
543 else if (Value
== 0x64)
545 Buffer
->CursorInfo
.dwSize
= Value
;
548 else if (!wcscmp(szValueName
, L
"ScreenText"))
550 GuiData
->ScreenText
= Value
;
552 else if (!wcscmp(szValueName
, L
"ScreenBackground"))
554 GuiData
->ScreenBackground
= Value
;
556 else if (!wcscmp(szValueName
, L
"FaceName"))
558 wcscpy(GuiData
->FontName
, szValue
);
560 else if (!wcscmp(szValueName
, L
"FontSize"))
562 GuiData
->FontSize
= Value
;
564 else if (!wcscmp(szValueName
, L
"FontWeight"))
566 GuiData
->FontWeight
= Value
;
568 else if (!wcscmp(szValueName
, L
"HistoryNoDup"))
570 GuiData
->HistoryNoDup
= Value
;
572 else if (!wcscmp(szValueName
, L
"WindowSize"))
574 Console
->Size
.X
= LOWORD(Value
);
575 Console
->Size
.Y
= HIWORD(Value
);
577 else if (!wcscmp(szValueName
, L
"ScreenBufferSize"))
581 Buffer
->MaxX
= LOWORD(Value
);
582 Buffer
->MaxY
= HIWORD(Value
);
585 else if (!wcscmp(szValueName
, L
"FullScreen"))
587 GuiData
->FullScreen
= Value
;
589 else if (!wcscmp(szValueName
, L
"QuickEdit"))
591 GuiData
->QuickEdit
= Value
;
593 else if (!wcscmp(szValueName
, L
"InsertMode"))
595 GuiData
->InsertMode
= Value
;
600 GuiConsoleUseDefaults(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
603 * init guidata with default properties
606 wcscpy(GuiData
->FontName
, L
"DejaVu Sans Mono");
607 GuiData
->FontSize
= 0x0008000C; // font is 8x12
608 GuiData
->FontWeight
= FW_NORMAL
;
609 GuiData
->HistoryNoDup
= FALSE
;
610 GuiData
->FullScreen
= FALSE
;
611 GuiData
->QuickEdit
= FALSE
;
612 GuiData
->InsertMode
= TRUE
;
613 GuiData
->HistoryBufferSize
= 50;
614 GuiData
->NumberOfHistoryBuffers
= 5;
615 GuiData
->ScreenText
= RGB(192, 192, 192);
616 GuiData
->ScreenBackground
= RGB(0, 0, 0);
617 GuiData
->PopupText
= RGB(128, 0, 128);
618 GuiData
->PopupBackground
= RGB(255, 255, 255);
619 GuiData
->WindowPosition
= UINT_MAX
;
620 GuiData
->UseRasterFonts
= TRUE
;
621 memcpy(GuiData
->Colors
, s_Colors
, sizeof(s_Colors
));
623 Console
->Size
.X
= 80;
624 Console
->Size
.Y
= 25;
630 Buffer
->CursorInfo
.bVisible
= TRUE
;
631 Buffer
->CursorInfo
.dwSize
= 5;
637 GuiConsoleInitScrollbar(PCSRSS_CONSOLE Console
, HWND hwnd
)
640 PGUI_CONSOLE_DATA GuiData
= Console
->PrivateData
;
642 DWORD Width
= Console
->Size
.X
* GuiData
->CharWidth
+ 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
643 DWORD Height
= Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
645 /* set scrollbar sizes */
646 sInfo
.cbSize
= sizeof(SCROLLINFO
);
647 sInfo
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_POS
;
649 if (Console
->ActiveBuffer
->MaxY
> Console
->Size
.Y
)
651 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
- 1;
652 sInfo
.nPage
= Console
->Size
.Y
;
653 sInfo
.nPos
= Console
->ActiveBuffer
->ShowY
;
654 SetScrollInfo(hwnd
, SB_VERT
, &sInfo
, TRUE
);
655 Width
+= GetSystemMetrics(SM_CXVSCROLL
);
656 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
660 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
663 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
665 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
- 1;
666 sInfo
.nPage
= Console
->Size
.X
;
667 sInfo
.nPos
= Console
->ActiveBuffer
->ShowX
;
668 SetScrollInfo(hwnd
, SB_HORZ
, &sInfo
, TRUE
);
669 Height
+= GetSystemMetrics(SM_CYHSCROLL
);
670 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
674 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
677 SetWindowPos(hwnd
, NULL
, 0, 0, Width
, Height
,
678 SWP_NOZORDER
| SWP_NOMOVE
| SWP_NOACTIVATE
);
682 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
684 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
685 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)Console
->PrivateData
;
689 PCSRSS_PROCESS_DATA ProcessData
;
692 Console
->hWindow
= hWnd
;
696 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
700 GuiConsoleUseDefaults(Console
, GuiData
, Console
->ActiveBuffer
);
701 if (Console
->ProcessList
.Flink
!= &Console
->ProcessList
)
703 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
704 if (GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
, FALSE
))
706 GuiConsoleReadUserSettings(hKey
, Console
, GuiData
, Console
->ActiveBuffer
);
711 InitializeCriticalSection(&GuiData
->Lock
);
713 GuiData
->LineBuffer
= (PWCHAR
)HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
714 Console
->Size
.X
* sizeof(WCHAR
));
716 GuiData
->Font
= CreateFontW(LOWORD(GuiData
->FontSize
),
717 0, //HIWORD(GuiData->FontSize),
725 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
726 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
728 if (NULL
== GuiData
->Font
)
730 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
731 DeleteCriticalSection(&GuiData
->Lock
);
732 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
738 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
739 DeleteObject(GuiData
->Font
);
740 DeleteCriticalSection(&GuiData
->Lock
);
741 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
744 OldFont
= SelectObject(Dc
, GuiData
->Font
);
747 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
749 DeleteObject(GuiData
->Font
);
750 DeleteCriticalSection(&GuiData
->Lock
);
751 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
754 if (! GetTextMetricsW(Dc
, &Metrics
))
756 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
757 SelectObject(Dc
, OldFont
);
759 DeleteObject(GuiData
->Font
);
760 DeleteCriticalSection(&GuiData
->Lock
);
761 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
764 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
765 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
766 SelectObject(Dc
, OldFont
);
769 GuiData
->CursorBlinkOn
= TRUE
;
770 GuiData
->ForceCursorOff
= FALSE
;
772 GuiData
->Selection
.left
= -1;
773 DPRINT("Console %p GuiData %p\n", Console
, GuiData
);
774 Console
->PrivateData
= GuiData
;
775 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
777 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
778 GuiConsoleCreateSysMenu(Console
);
779 GuiConsoleInitScrollbar(Console
, hWnd
);
780 SetEvent(GuiData
->hGuiInitEvent
);
782 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
786 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
788 RECT oldRect
= GuiData
->Selection
;
792 RECT changeRect
= *rc
;
794 GuiData
->Selection
= *rc
;
796 changeRect
.left
*= GuiData
->CharWidth
;
797 changeRect
.top
*= GuiData
->CharHeight
;
798 changeRect
.right
*= GuiData
->CharWidth
;
799 changeRect
.bottom
*= GuiData
->CharHeight
;
801 if(rc
->left
!= oldRect
.left
||
802 rc
->top
!= oldRect
.top
||
803 rc
->right
!= oldRect
.right
||
804 rc
->bottom
!= oldRect
.bottom
)
806 if(oldRect
.left
!= -1)
810 oldRect
.left
*= GuiData
->CharWidth
;
811 oldRect
.top
*= GuiData
->CharHeight
;
812 oldRect
.right
*= GuiData
->CharWidth
;
813 oldRect
.bottom
*= GuiData
->CharHeight
;
815 /* calculate the region that needs to be updated */
816 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
818 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
820 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
822 InvalidateRgn(hWnd
, rgn1
, FALSE
);
832 InvalidateRect(hWnd
, &changeRect
, FALSE
);
836 else if(oldRect
.left
!= -1)
838 /* clear the selection */
839 GuiData
->Selection
.left
= -1;
840 oldRect
.left
*= GuiData
->CharWidth
;
841 oldRect
.top
*= GuiData
->CharHeight
;
842 oldRect
.right
*= GuiData
->CharWidth
;
843 oldRect
.bottom
*= GuiData
->CharHeight
;
844 InvalidateRect(hWnd
, &oldRect
, FALSE
);
850 GuiConsolePaint(PCSRSS_CONSOLE Console
,
851 PGUI_CONSOLE_DATA GuiData
,
855 PCSRSS_SCREEN_BUFFER Buff
;
856 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
857 ULONG Line
, Char
, Start
;
860 BYTE LastAttribute
, Attribute
;
861 ULONG CursorX
, CursorY
, CursorHeight
;
862 HBRUSH CursorBrush
, OldBrush
;
865 Buff
= Console
->ActiveBuffer
;
867 EnterCriticalSection(&Buff
->Header
.Lock
);
869 TopLine
= rc
->top
/ GuiData
->CharHeight
+ Buff
->ShowY
;
870 BottomLine
= (rc
->bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1 + Buff
->ShowY
;
871 LeftChar
= rc
->left
/ GuiData
->CharWidth
+ Buff
->ShowX
;
872 RightChar
= (rc
->right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1 + Buff
->ShowX
;
873 LastAttribute
= ConioCoordToPointer(Buff
, LeftChar
, TopLine
)[1];
875 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
));
876 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
>> 4));
878 if (BottomLine
>= Buff
->MaxY
) BottomLine
= Buff
->MaxY
- 1;
879 if (RightChar
>= Buff
->MaxX
) RightChar
= Buff
->MaxX
- 1;
881 OldFont
= SelectObject(hDC
,
884 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
886 From
= ConioCoordToPointer(Buff
, LeftChar
, Line
);
888 To
= GuiData
->LineBuffer
;
890 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
892 if (*(From
+ 1) != LastAttribute
)
895 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
896 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
900 To
= GuiData
->LineBuffer
;
901 Attribute
= *(From
+ 1);
902 if (Attribute
!= LastAttribute
)
904 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
));
905 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
>> 4));
906 LastAttribute
= Attribute
;
910 MultiByteToWideChar(Console
->OutputCodePage
,
921 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
922 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
924 RightChar
- Start
+ 1);
927 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
928 !GuiData
->ForceCursorOff
)
930 CursorX
= Buff
->CurrentX
;
931 CursorY
= Buff
->CurrentY
;
932 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
933 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
935 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
936 if (CursorHeight
< 1)
940 From
= ConioCoordToPointer(Buff
, Buff
->CurrentX
, Buff
->CurrentY
) + 1;
942 if (*From
!= DEFAULT_ATTRIB
)
944 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(GuiData
, *From
));
948 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
951 OldBrush
= SelectObject(hDC
,
954 (CursorX
- Buff
->ShowX
) * GuiData
->CharWidth
,
955 (CursorY
- Buff
->ShowY
) * GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
961 DeleteObject(CursorBrush
);
965 LeaveCriticalSection(&Buff
->Header
.Lock
);
972 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
976 PCSRSS_CONSOLE Console
;
977 PGUI_CONSOLE_DATA GuiData
;
979 hDC
= BeginPaint(hWnd
, &ps
);
981 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
982 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
984 GuiConsoleGetDataPointers(hWnd
,
987 if (Console
!= NULL
&& GuiData
!= NULL
&&
988 Console
->ActiveBuffer
!= NULL
)
990 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
992 EnterCriticalSection(&GuiData
->Lock
);
994 GuiConsolePaint(Console
,
999 if (GuiData
->Selection
.left
!= -1)
1001 RECT rc
= GuiData
->Selection
;
1003 rc
.left
*= GuiData
->CharWidth
;
1004 rc
.top
*= GuiData
->CharHeight
;
1005 rc
.right
*= GuiData
->CharWidth
;
1006 rc
.bottom
*= GuiData
->CharHeight
;
1008 /* invert the selection */
1009 if (IntersectRect(&rc
,
1022 LeaveCriticalSection(&GuiData
->Lock
);
1027 EndPaint(hWnd
, &ps
);
1030 static VOID FASTCALL
1031 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1033 PCSRSS_CONSOLE Console
;
1034 PGUI_CONSOLE_DATA GuiData
;
1037 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1038 Message
.hwnd
= hWnd
;
1039 Message
.message
= msg
;
1040 Message
.wParam
= wParam
;
1041 Message
.lParam
= lParam
;
1043 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1045 /* clear the selection */
1046 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1049 ConioProcessKey(&Message
, Console
, FALSE
);
1052 static VOID FASTCALL
1053 GuiIntDrawRegion(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1057 RegionRect
.left
= (Region
->left
- Buff
->ShowX
) * GuiData
->CharWidth
;
1058 RegionRect
.top
= (Region
->top
- Buff
->ShowY
) * GuiData
->CharHeight
;
1059 RegionRect
.right
= (Region
->right
+ 1 - Buff
->ShowX
) * GuiData
->CharWidth
;
1060 RegionRect
.bottom
= (Region
->bottom
+ 1 - Buff
->ShowY
) * GuiData
->CharHeight
;
1062 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1066 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1068 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1070 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1072 GuiIntDrawRegion(Console
->ActiveBuffer
, GuiData
, Console
->hWindow
, Region
);
1076 static VOID FASTCALL
1077 GuiInvalidateCell(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1084 CellRect
.bottom
= y
;
1086 GuiIntDrawRegion(Buff
, GuiData
, Wnd
, &CellRect
);
1090 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1091 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1093 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1094 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1095 LONG CursorEndX
, CursorEndY
;
1098 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1103 if (0 != ScrolledLines
)
1105 ScrollRect
.left
= 0;
1107 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1108 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1110 if (GuiData
->Selection
.left
!= -1)
1112 /* scroll the selection */
1113 if (GuiData
->Selection
.top
> ScrolledLines
)
1115 GuiData
->Selection
.top
-= ScrolledLines
;
1116 GuiData
->Selection
.bottom
-= ScrolledLines
;
1118 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1120 GuiData
->Selection
.left
= -1;
1124 GuiData
->Selection
.top
= 0;
1125 GuiData
->Selection
.bottom
-= ScrolledLines
;
1129 ScrollWindowEx(Console
->hWindow
,
1131 -(ScrolledLines
* GuiData
->CharHeight
),
1139 GuiIntDrawRegion(Buff
, GuiData
, Console
->hWindow
, Region
);
1141 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1142 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1144 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1147 CursorEndX
= Buff
->CurrentX
;
1148 CursorEndY
= Buff
->CurrentY
;
1149 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1150 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1151 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1153 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1158 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1162 if (Console
->ActiveBuffer
== Buff
)
1164 UpdateRect
.left
= Buff
->CurrentX
;
1165 UpdateRect
.top
= Buff
->CurrentY
;
1166 UpdateRect
.right
= UpdateRect
.left
;
1167 UpdateRect
.bottom
= UpdateRect
.top
;
1168 ConioDrawRegion(Console
, &UpdateRect
);
1175 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
1179 if (Console
->ActiveBuffer
== Buff
)
1181 /* Redraw char at old position (removes cursor) */
1182 UpdateRect
.left
= OldCursorX
;
1183 UpdateRect
.top
= OldCursorY
;
1184 UpdateRect
.right
= OldCursorX
;
1185 UpdateRect
.bottom
= OldCursorY
;
1186 ConioDrawRegion(Console
, &UpdateRect
);
1187 /* Redraw char at new position (shows cursor) */
1188 UpdateRect
.left
= Buff
->CurrentX
;
1189 UpdateRect
.top
= Buff
->CurrentY
;
1190 UpdateRect
.right
= UpdateRect
.left
;
1191 UpdateRect
.bottom
= UpdateRect
.top
;
1192 ConioDrawRegion(Console
, &UpdateRect
);
1199 GuiUpdateScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1201 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1203 if (Console
->ActiveBuffer
== Buff
)
1205 GuiData
->ScreenText
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
);
1206 GuiData
->ScreenBackground
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
>> 4);
1212 static VOID FASTCALL
1213 GuiConsoleHandleTimer(HWND hWnd
)
1215 PCSRSS_CONSOLE Console
;
1216 PGUI_CONSOLE_DATA GuiData
;
1219 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1220 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1222 CursorRect
.left
= Console
->ActiveBuffer
->CurrentX
;
1223 CursorRect
.top
= Console
->ActiveBuffer
->CurrentY
;
1224 CursorRect
.right
= CursorRect
.left
;
1225 CursorRect
.bottom
= CursorRect
.top
;
1226 GuiDrawRegion(Console
, &CursorRect
);
1229 static VOID FASTCALL
1230 GuiConsoleHandleClose(HWND hWnd
)
1232 PCSRSS_CONSOLE Console
;
1233 PGUI_CONSOLE_DATA GuiData
;
1234 PLIST_ENTRY current_entry
;
1235 PCSRSS_PROCESS_DATA current
;
1237 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1239 EnterCriticalSection(&Console
->Header
.Lock
);
1241 current_entry
= Console
->ProcessList
.Flink
;
1242 while (current_entry
!= &Console
->ProcessList
)
1244 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1245 current_entry
= current_entry
->Flink
;
1247 /* FIXME: Windows will wait up to 5 seconds for the thread to exit.
1248 * We shouldn't wait here, though, since the console lock is entered.
1249 * A copy of the thread list probably needs to be made. */
1250 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1253 LeaveCriticalSection(&Console
->Header
.Lock
);
1256 static VOID FASTCALL
1257 GuiConsoleHandleNcDestroy(HWND hWnd
)
1259 PCSRSS_CONSOLE Console
;
1260 PGUI_CONSOLE_DATA GuiData
;
1263 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1265 Console
->PrivateData
= NULL
;
1266 DeleteCriticalSection(&GuiData
->Lock
);
1267 GetSystemMenu(hWnd
, TRUE
);
1268 if (GuiData
->ConsoleLibrary
)
1269 FreeLibrary(GuiData
->ConsoleLibrary
);
1271 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1274 static VOID FASTCALL
1275 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1277 PCSRSS_CONSOLE Console
;
1278 PGUI_CONSOLE_DATA GuiData
;
1282 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1283 if (Console
== NULL
|| GuiData
== NULL
) return;
1285 pt
= MAKEPOINTS(lParam
);
1287 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1288 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1289 rc
.right
= rc
.left
+ 1;
1290 rc
.bottom
= rc
.top
+ 1;
1292 GuiData
->SelectionStart
.x
= rc
.left
;
1293 GuiData
->SelectionStart
.y
= rc
.top
;
1297 GuiData
->MouseDown
= TRUE
;
1299 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1302 static VOID FASTCALL
1303 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1305 PCSRSS_CONSOLE Console
;
1306 PGUI_CONSOLE_DATA GuiData
;
1310 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1311 if (Console
== NULL
|| GuiData
== NULL
) return;
1312 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1314 pt
= MAKEPOINTS(lParam
);
1316 rc
.left
= GuiData
->SelectionStart
.x
;
1317 rc
.top
= GuiData
->SelectionStart
.y
;
1318 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1319 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1321 /* exchange left/top with right/bottom if required */
1322 if(rc
.left
>= rc
.right
)
1326 rc
.left
= max(rc
.right
- 1, 0);
1329 if(rc
.top
>= rc
.bottom
)
1333 rc
.top
= max(rc
.bottom
- 1, 0);
1334 rc
.bottom
= tmp
+ 1;
1337 GuiData
->MouseDown
= FALSE
;
1339 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1344 static VOID FASTCALL
1345 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1347 PCSRSS_CONSOLE Console
;
1348 PGUI_CONSOLE_DATA GuiData
;
1352 if (!(wParam
& MK_LBUTTON
)) return;
1354 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1355 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1357 pt
= MAKEPOINTS(lParam
);
1359 rc
.left
= GuiData
->SelectionStart
.x
;
1360 rc
.top
= GuiData
->SelectionStart
.y
;
1361 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1362 if (Console
->Size
.X
< rc
.right
)
1364 rc
.right
= Console
->Size
.X
;
1366 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1367 if (Console
->Size
.Y
< rc
.bottom
)
1369 rc
.bottom
= Console
->Size
.Y
;
1372 /* exchange left/top with right/bottom if required */
1373 if(rc
.left
>= rc
.right
)
1377 rc
.left
= max(rc
.right
- 1, 0);
1380 if(rc
.top
>= rc
.bottom
)
1384 rc
.top
= max(rc
.bottom
- 1, 0);
1385 rc
.bottom
= tmp
+ 1;
1388 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1391 static VOID FASTCALL
1392 GuiConsoleRightMouseDown(HWND hWnd
)
1394 PCSRSS_CONSOLE Console
;
1395 PGUI_CONSOLE_DATA GuiData
;
1397 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1398 if (Console
== NULL
|| GuiData
== NULL
) return;
1400 if (GuiData
->Selection
.left
== -1)
1402 /* FIXME - paste text from clipboard */
1406 /* FIXME - copy selection to clipboard */
1408 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1415 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1417 PCSRSS_CONSOLE Console
;
1418 APPLET_PROC CPLFunc
;
1419 TCHAR szBuffer
[MAX_PATH
];
1420 ConsoleInfo SharedInfo
;
1422 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1424 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1426 if (GuiData
== NULL
)
1428 DPRINT("GuiConsoleGetDataPointers failed\n");
1432 if (GuiData
->ConsoleLibrary
== NULL
)
1434 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1435 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1436 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1438 if (GuiData
->ConsoleLibrary
== NULL
)
1440 DPRINT1("failed to load console.dll");
1445 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1448 DPRINT("Error: Console.dll misses CPlApplet export\n");
1453 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1454 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1455 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1456 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1457 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1458 SharedInfo
.PopupText
= GuiData
->PopupText
;
1459 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1460 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1461 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1462 SharedInfo
.ScreenBuffer
= (DWORD
)MAKELONG(Console
->ActiveBuffer
->MaxX
, Console
->ActiveBuffer
->MaxY
);
1463 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1464 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1465 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1466 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1467 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1468 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1469 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1470 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1472 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1474 DPRINT("Error: failed to initialize console.dll\n");
1478 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1480 DPRINT("Error: console.dll returned unexpected CPL count\n");
1484 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1486 static LRESULT FASTCALL
1487 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, PGUI_CONSOLE_DATA GuiData
)
1493 case ID_SYSTEM_EDIT_MARK
:
1494 case ID_SYSTEM_EDIT_COPY
:
1495 case ID_SYSTEM_EDIT_PASTE
:
1496 case ID_SYSTEM_EDIT_SELECTALL
:
1497 case ID_SYSTEM_EDIT_SCROLL
:
1498 case ID_SYSTEM_EDIT_FIND
:
1501 case ID_SYSTEM_DEFAULTS
:
1502 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1505 case ID_SYSTEM_PROPERTIES
:
1506 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1510 Ret
= DefWindowProcW(hWnd
, WM_SYSCOMMAND
, wParam
, lParam
);
1516 static VOID FASTCALL
1517 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1519 PCSRSS_CONSOLE Console
;
1520 PGUI_CONSOLE_DATA GuiData
;
1522 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1523 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
)
1525 DPRINT1("GuiConsoleResize X %d Y %d\n", LOWORD(lParam
), HIWORD(lParam
));
1530 GuiConsoleHandleScrollbarMenu()
1534 hMenu
= CreatePopupMenu();
1537 DPRINT("CreatePopupMenu failed\n");
1540 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1541 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1542 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1543 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1544 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1545 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1546 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1547 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1548 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1549 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1553 static VOID FASTCALL
1554 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1557 PCSRSS_SCREEN_BUFFER ActiveBuffer
= Console
->ActiveBuffer
;
1558 BOOL SizeChanged
= FALSE
;
1560 EnterCriticalSection(&ActiveBuffer
->Header
.Lock
);
1562 /* apply text / background color */
1563 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1564 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1566 /* apply cursor size */
1567 ActiveBuffer
->CursorInfo
.dwSize
= min(max(pConInfo
->CursorSize
, 1), 100);
1569 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1570 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1572 if (windx
!= ActiveBuffer
->MaxX
|| windy
!= ActiveBuffer
->MaxY
)
1574 BYTE
* Buffer
= HeapAlloc(Win32CsrApiHeap
, 0, windx
* windy
* 2);
1586 value
= MAKEWORD(' ', ActiveBuffer
->DefaultAttrib
);
1588 DPRINT("MaxX %d MaxY %d windx %d windy %d value %04x DefaultAttrib %d\n",ActiveBuffer
->MaxX
, ActiveBuffer
->MaxY
, windx
, windy
, value
, ActiveBuffer
->DefaultAttrib
);
1589 OldBuffer
= ActiveBuffer
->Buffer
;
1591 for (CurrentY
= 0; CurrentY
< min(ActiveBuffer
->MaxY
, windy
); CurrentY
++)
1593 OldPtr
= ConioCoordToPointer(ActiveBuffer
, 0, CurrentY
);
1594 if (windx
<= ActiveBuffer
->MaxX
)
1597 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, windx
* 2);
1598 Offset
+= (windx
* 2);
1603 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, ActiveBuffer
->MaxX
* 2);
1604 Offset
+= (ActiveBuffer
->MaxX
* 2);
1606 diff
= windx
- ActiveBuffer
->MaxX
;
1607 /* zero new part of it */
1609 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
);
1611 for (i
= 0; i
< diff
; i
++)
1613 Buffer
[Offset
++] = ' ';
1614 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1620 if (windy
> ActiveBuffer
->MaxY
)
1622 diff
= windy
- ActiveBuffer
->MaxY
;
1624 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
* windx
);
1626 for (i
= 0; i
< diff
* windx
; i
++)
1628 Buffer
[Offset
++] = ' ';
1629 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1634 (void)InterlockedExchangePointer((PVOID
volatile *)&ActiveBuffer
->Buffer
, Buffer
);
1635 HeapFree(Win32CsrApiHeap
, 0, OldBuffer
);
1636 ActiveBuffer
->MaxX
= windx
;
1637 ActiveBuffer
->MaxY
= windy
;
1638 ActiveBuffer
->VirtualY
= 0;
1643 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1648 windx
= LOWORD(pConInfo
->WindowSize
);
1649 windy
= HIWORD(pConInfo
->WindowSize
);
1651 if (windx
> Console
->Size
.X
)
1653 PWCHAR LineBuffer
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
, windx
* sizeof(WCHAR
));
1656 HeapFree(Win32CsrApiHeap
, 0, GuiData
->LineBuffer
);
1657 GuiData
->LineBuffer
= LineBuffer
;
1661 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1667 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1670 Console
->Size
.X
= windx
;
1671 Console
->Size
.Y
= windy
;
1677 GuiConsoleInitScrollbar(Console
, pConInfo
->hConsoleWindow
);
1680 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1681 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1686 GuiConsoleHandleScroll(HWND hwnd
, UINT uMsg
, WPARAM wParam
)
1688 PCSRSS_CONSOLE Console
;
1689 PCSRSS_SCREEN_BUFFER Buff
;
1690 PGUI_CONSOLE_DATA GuiData
;
1693 int old_pos
, Maximum
;
1696 GuiConsoleGetDataPointers(hwnd
, &Console
, &GuiData
);
1697 if (Console
== NULL
|| GuiData
== NULL
)
1699 Buff
= Console
->ActiveBuffer
;
1701 if (uMsg
== WM_HSCROLL
)
1704 Maximum
= Buff
->MaxX
- Console
->Size
.X
;
1705 pShowXY
= &Buff
->ShowX
;
1710 Maximum
= Buff
->MaxY
- Console
->Size
.Y
;
1711 pShowXY
= &Buff
->ShowY
;
1714 /* set scrollbar sizes */
1715 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1716 sInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_PAGE
| SIF_TRACKPOS
;
1718 if (!GetScrollInfo(hwnd
, fnBar
, &sInfo
))
1723 old_pos
= sInfo
.nPos
;
1725 switch(LOWORD(wParam
))
1736 sInfo
.nPos
-= sInfo
.nPage
;
1740 sInfo
.nPos
+= sInfo
.nPage
;
1744 sInfo
.nPos
= sInfo
.nTrackPos
;
1748 sInfo
.nPos
= sInfo
.nMin
;
1752 sInfo
.nPos
= sInfo
.nMax
;
1759 sInfo
.nPos
= max(sInfo
.nPos
, 0);
1760 sInfo
.nPos
= min(sInfo
.nPos
, Maximum
);
1762 if (old_pos
!= sInfo
.nPos
)
1764 USHORT OldX
= Buff
->ShowX
;
1765 USHORT OldY
= Buff
->ShowY
;
1766 *pShowXY
= sInfo
.nPos
;
1768 ScrollWindowEx(hwnd
,
1769 (OldX
- Buff
->ShowX
) * GuiData
->CharWidth
,
1770 (OldY
- Buff
->ShowY
) * GuiData
->CharHeight
,
1777 sInfo
.fMask
= SIF_POS
;
1778 SetScrollInfo(hwnd
, fnBar
, &sInfo
, TRUE
);
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
);
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
);
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
;