3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: subsys/csrss/win32csr/guiconsole.c
6 * PURPOSE: Implementation of gui-mode consoles
9 /* INCLUDES ******************************************************************/
15 /* Not defined in any header file */
16 extern VOID WINAPI
PrivateCsrssManualGuiCheck(LONG Check
);
18 /* GLOBALS *******************************************************************/
20 typedef struct GUI_CONSOLE_DATA_TAG
28 CRITICAL_SECTION Lock
;
32 HMODULE ConsoleLibrary
;
34 WCHAR FontName
[LF_FACESIZE
];
41 DWORD NumberOfHistoryBuffers
;
42 DWORD HistoryBufferSize
;
46 COLORREF ScreenBackground
;
47 COLORREF PopupBackground
;
50 WCHAR szProcessName
[MAX_PATH
];
51 } GUI_CONSOLE_DATA
, *PGUI_CONSOLE_DATA
;
56 #define PM_CREATE_CONSOLE (WM_APP + 1)
57 #define PM_DESTROY_CONSOLE (WM_APP + 2)
59 #define CURSOR_BLINK_TIME 500
60 #define DEFAULT_ATTRIB (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
62 static BOOL ConsInitialized
= FALSE
;
63 static HWND NotifyWnd
;
65 typedef struct _GUICONSOLE_MENUITEM
68 const struct _GUICONSOLE_MENUITEM
*SubMenu
;
70 } GUICONSOLE_MENUITEM
, *PGUICONSOLE_MENUITEM
;
72 static const GUICONSOLE_MENUITEM GuiConsoleEditMenuItems
[] =
74 { IDS_MARK
, NULL
, ID_SYSTEM_EDIT_MARK
},
75 { IDS_COPY
, NULL
, ID_SYSTEM_EDIT_COPY
},
76 { IDS_PASTE
, NULL
, ID_SYSTEM_EDIT_PASTE
},
77 { IDS_SELECTALL
, NULL
, ID_SYSTEM_EDIT_SELECTALL
},
78 { IDS_SCROLL
, NULL
, ID_SYSTEM_EDIT_SCROLL
},
79 { IDS_FIND
, NULL
, ID_SYSTEM_EDIT_FIND
},
81 { 0, NULL
, 0 } /* End of list */
84 static const GUICONSOLE_MENUITEM GuiConsoleMainMenuItems
[] =
86 { (UINT
)-1, NULL
, 0 }, /* Separator */
87 { IDS_EDIT
, GuiConsoleEditMenuItems
, 0 },
88 { IDS_DEFAULTS
, NULL
, ID_SYSTEM_DEFAULTS
},
89 { IDS_PROPERTIES
, NULL
, ID_SYSTEM_PROPERTIES
},
91 { 0, NULL
, 0 } /* End of list */
94 static const COLORREF s_Colors
[] =
114 #define GuiConsoleRGBFromAttribute(GuiData, Attribute) ((GuiData)->Colors[(Attribute) & 0xF])
116 /* FUNCTIONS *****************************************************************/
119 GuiConsoleAppendMenuItems(HMENU hMenu
,
120 const GUICONSOLE_MENUITEM
*Items
)
123 WCHAR szMenuString
[255];
125 HINSTANCE hInst
= GetModuleHandleW(L
"win32csr");
129 if (Items
[i
].uID
!= (UINT
)-1)
131 if (LoadStringW(hInst
,
134 sizeof(szMenuString
) / sizeof(szMenuString
[0])) > 0)
136 if (Items
[i
].SubMenu
!= NULL
)
138 hSubMenu
= CreatePopupMenu();
139 if (hSubMenu
!= NULL
)
141 GuiConsoleAppendMenuItems(hSubMenu
,
144 if (!AppendMenuW(hMenu
,
145 MF_STRING
| MF_POPUP
,
149 DestroyMenu(hSubMenu
);
170 }while(!(Items
[i
].uID
== 0 && Items
[i
].SubMenu
== NULL
&& Items
[i
].wCmdID
== 0));
174 GuiConsoleCreateSysMenu(PCSRSS_CONSOLE Console
)
177 hMenu
= GetSystemMenu(Console
->hWindow
,
181 GuiConsoleAppendMenuItems(hMenu
,
182 GuiConsoleMainMenuItems
);
183 DrawMenuBar(Console
->hWindow
);
188 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
190 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
191 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
195 GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId
, PHANDLE hProcHandle
, PHKEY hResult
, REGSAM samDesired
)
197 HANDLE hProcessToken
= NULL
;
202 UNICODE_STRING SidName
;
206 hProcess
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
| READ_CONTROL
, FALSE
, ProcessId
);
209 DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
213 if (!OpenProcessToken(hProcess
, TOKEN_QUERY
, &hProcessToken
))
215 DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
216 CloseHandle(hProcess
);
220 if (!GetTokenInformation(hProcessToken
, TokenUser
, (PVOID
)Buffer
, sizeof(Buffer
), &Length
))
222 DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
223 CloseHandle(hProcess
);
224 CloseHandle(hProcessToken
);
228 TokUser
= ((PTOKEN_USER
)Buffer
)->User
.Sid
;
229 if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName
, TokUser
, TRUE
)))
231 DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
235 res
= RegOpenKeyExW(HKEY_USERS
, SidName
.Buffer
, 0, samDesired
, hResult
);
236 RtlFreeUnicodeString(&SidName
);
238 CloseHandle(hProcessToken
);
240 *hProcHandle
= hProcess
;
242 CloseHandle(hProcess
);
244 if (res
!= ERROR_SUCCESS
)
251 GuiConsoleOpenUserSettings(PGUI_CONSOLE_DATA GuiData
, DWORD ProcessId
, PHKEY hSubKey
, REGSAM samDesired
, BOOL bCreate
)
253 WCHAR szProcessName
[MAX_PATH
];
254 WCHAR szBuffer
[MAX_PATH
];
255 UINT fLength
, wLength
;
256 DWORD dwBitmask
, dwLength
;
257 WCHAR CurDrive
[] = { 'A',':', 0 };
263 * console properties are stored under
266 * There are 3 ways to store console properties
268 * 1. use console title as subkey name
271 * 2. use application name as subkey name
273 * 3. use unexpanded path to console application.
274 * i.e. %SystemRoot%_system32_cmd.exe
277 DPRINT("GuiConsoleOpenUserSettings entered\n");
279 if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId
, &hProcess
, &hKey
, samDesired
))
281 DPRINT("GuiConsoleOpenUserRegistryPathPerProcessId failed\n");
285 /* FIXME we do not getting the process name so no menu will be loading, why ?*/
286 fLength
= GetProcessImageFileNameW(hProcess
, szProcessName
, sizeof(GuiData
->szProcessName
) / sizeof(WCHAR
));
287 CloseHandle(hProcess
);
289 //DPRINT1("szProcessName3 : %S\n",szProcessName);
293 DPRINT("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess
);
297 * try the process name as path
300 ptr
= wcsrchr(szProcessName
, L
'\\');
301 wcscpy(GuiData
->szProcessName
, ptr
);
303 swprintf(szBuffer
, L
"Console%s",ptr
);
304 DPRINT("#1 Path : %S\n", szBuffer
);
308 if (RegCreateKeyW(hKey
, szBuffer
, hSubKey
) == ERROR_SUCCESS
)
317 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
324 * try the "Shortcut to processname" as path
325 * FIXME: detect wheter the process was started as a shortcut
328 swprintf(szBuffer
, L
"Console\\Shortcut to %S", ptr
);
329 DPRINT("#2 Path : %S\n", szBuffer
);
330 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
332 swprintf(GuiData
->szProcessName
, L
"Shortcut to %S", ptr
);
338 * if the path contains \\Device\\HarddiskVolume1\... remove it
341 if (szProcessName
[0] == L
'\\')
343 dwBitmask
= GetLogicalDrives();
348 dwLength
= QueryDosDeviceW(CurDrive
, szBuffer
, MAX_PATH
);
351 if (!memcmp(szBuffer
, szProcessName
, (dwLength
-2)*sizeof(WCHAR
)))
353 wcscpy(szProcessName
, CurDrive
);
354 RtlMoveMemory(&szProcessName
[2], &szProcessName
[dwLength
-1], fLength
- dwLength
-1);
359 dwBitmask
= (dwBitmask
>> 1);
365 * last attempt: check whether the file is under %SystemRoot%
366 * and use path like Console\%SystemRoot%_dir_dir2_file.exe
369 wLength
= GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
372 if (!wcsncmp(szProcessName
, szBuffer
, wLength
))
374 /* replace slashes by underscores */
375 while((ptr
= wcschr(szProcessName
, L
'\\')))
378 swprintf(szBuffer
, L
"Console\\\%SystemRoot\%%S", &szProcessName
[wLength
]);
379 DPRINT("#3 Path : %S\n", szBuffer
);
380 if (RegOpenKeyExW(hKey
, szBuffer
, 0, samDesired
, hSubKey
) == ERROR_SUCCESS
)
382 swprintf(GuiData
->szProcessName
, L
"\%SystemRoot\%%S", &szProcessName
[wLength
]);
393 GuiConsoleWriteUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
)
396 PCSRSS_PROCESS_DATA ProcessData
;
398 if (Console
->ProcessList
.Flink
== &Console
->ProcessList
)
400 DPRINT("GuiConsoleWriteUserSettings: No Process!!!\n");
403 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
404 if (!GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
| KEY_WRITE
, TRUE
))
409 if (Console
->ActiveBuffer
->CursorInfo
.dwSize
<= 1)
411 RegDeleteKeyW(hKey
, L
"CursorSize");
415 RegSetValueExW(hKey
, L
"CursorSize", 0, REG_DWORD
, (const BYTE
*)&Console
->ActiveBuffer
->CursorInfo
.dwSize
, sizeof(DWORD
));
418 if (GuiData
->NumberOfHistoryBuffers
== 5)
420 RegDeleteKeyW(hKey
, L
"NumberOfHistoryBuffers");
424 RegSetValueExW(hKey
, L
"NumberOfHistoryBuffers", 0, REG_DWORD
, (const BYTE
*)&GuiData
->NumberOfHistoryBuffers
, sizeof(DWORD
));
427 if (GuiData
->HistoryBufferSize
== 50)
429 RegDeleteKeyW(hKey
, L
"HistoryBufferSize");
433 RegSetValueExW(hKey
, L
"HistoryBufferSize", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryBufferSize
, sizeof(DWORD
));
436 if (GuiData
->FullScreen
== FALSE
)
438 RegDeleteKeyW(hKey
, L
"FullScreen");
442 RegSetValueExW(hKey
, L
"FullScreen", 0, REG_DWORD
, (const BYTE
*)&GuiData
->FullScreen
, sizeof(DWORD
));
445 if ( GuiData
->QuickEdit
== FALSE
)
447 RegDeleteKeyW(hKey
, L
"QuickEdit");
451 RegSetValueExW(hKey
, L
"QuickEdit", 0, REG_DWORD
, (const BYTE
*)&GuiData
->QuickEdit
, sizeof(DWORD
));
454 if (GuiData
->InsertMode
== TRUE
)
456 RegDeleteKeyW(hKey
, L
"InsertMode");
460 RegSetValueExW(hKey
, L
"InsertMode", 0, REG_DWORD
, (const BYTE
*)&GuiData
->InsertMode
, sizeof(DWORD
));
463 if (GuiData
->HistoryNoDup
== FALSE
)
465 RegDeleteKeyW(hKey
, L
"HistoryNoDup");
469 RegSetValueExW(hKey
, L
"HistoryNoDup", 0, REG_DWORD
, (const BYTE
*)&GuiData
->HistoryNoDup
, sizeof(DWORD
));
472 if (GuiData
->ScreenText
== RGB(192, 192, 192))
475 * MS uses console attributes instead of real color
477 RegDeleteKeyW(hKey
, L
"ScreenText");
481 RegSetValueExW(hKey
, L
"ScreenText", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenText
, sizeof(COLORREF
));
484 if (GuiData
->ScreenBackground
== RGB(0, 0, 0))
486 RegDeleteKeyW(hKey
, L
"ScreenBackground");
490 RegSetValueExW(hKey
, L
"ScreenBackground", 0, REG_DWORD
, (const BYTE
*)&GuiData
->ScreenBackground
, sizeof(COLORREF
));
497 GuiConsoleReadUserSettings(HKEY hKey
, PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
499 DWORD dwNumSubKeys
= 0;
504 WCHAR szValueName
[MAX_PATH
];
505 WCHAR szValue
[LF_FACESIZE
] = L
"\0";
508 if (RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &dwNumSubKeys
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
510 DPRINT("GuiConsoleReadUserSettings: RegQueryInfoKey failed\n");
514 DPRINT("GuiConsoleReadUserSettings entered dwNumSubKeys %d\n", dwNumSubKeys
);
516 for (dwIndex
= 0; dwIndex
< dwNumSubKeys
; dwIndex
++)
518 dwValue
= sizeof(Value
);
519 dwValueName
= MAX_PATH
;
521 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, &dwType
, (BYTE
*)&Value
, &dwValue
) != ERROR_SUCCESS
)
523 if (dwType
== REG_SZ
)
526 * retry in case of string value
528 dwValue
= sizeof(szValue
);
529 dwValueName
= LF_FACESIZE
;
530 if (RegEnumValueW(hKey
, dwIndex
, szValueName
, &dwValueName
, NULL
, NULL
, (BYTE
*)szValue
, &dwValue
) != ERROR_SUCCESS
)
536 if (!wcscmp(szValueName
, L
"CursorSize"))
540 Buffer
->CursorInfo
.dwSize
= Value
;
542 else if (Value
== 0x64)
544 Buffer
->CursorInfo
.dwSize
= Value
;
547 else if (!wcscmp(szValueName
, L
"ScreenText"))
549 GuiData
->ScreenText
= Value
;
551 else if (!wcscmp(szValueName
, L
"ScreenBackground"))
553 GuiData
->ScreenBackground
= Value
;
555 else if (!wcscmp(szValueName
, L
"FaceName"))
557 wcscpy(GuiData
->FontName
, szValue
);
559 else if (!wcscmp(szValueName
, L
"FontSize"))
561 GuiData
->FontSize
= Value
;
563 else if (!wcscmp(szValueName
, L
"FontWeight"))
565 GuiData
->FontWeight
= Value
;
567 else if (!wcscmp(szValueName
, L
"HistoryNoDup"))
569 GuiData
->HistoryNoDup
= Value
;
571 else if (!wcscmp(szValueName
, L
"WindowSize"))
573 Console
->Size
.X
= LOWORD(Value
);
574 Console
->Size
.Y
= HIWORD(Value
);
576 else if (!wcscmp(szValueName
, L
"ScreenBufferSize"))
580 Buffer
->MaxX
= LOWORD(Value
);
581 Buffer
->MaxY
= HIWORD(Value
);
584 else if (!wcscmp(szValueName
, L
"FullScreen"))
586 GuiData
->FullScreen
= Value
;
588 else if (!wcscmp(szValueName
, L
"QuickEdit"))
590 GuiData
->QuickEdit
= Value
;
592 else if (!wcscmp(szValueName
, L
"InsertMode"))
594 GuiData
->InsertMode
= Value
;
599 GuiConsoleUseDefaults(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PCSRSS_SCREEN_BUFFER Buffer
)
602 * init guidata with default properties
605 wcscpy(GuiData
->FontName
, L
"DejaVu Sans Mono");
606 GuiData
->FontSize
= 0x0008000C; // font is 8x12
607 GuiData
->FontWeight
= FW_NORMAL
;
608 GuiData
->HistoryNoDup
= FALSE
;
609 GuiData
->FullScreen
= FALSE
;
610 GuiData
->QuickEdit
= FALSE
;
611 GuiData
->InsertMode
= TRUE
;
612 GuiData
->HistoryBufferSize
= 50;
613 GuiData
->NumberOfHistoryBuffers
= 5;
614 GuiData
->ScreenText
= RGB(192, 192, 192);
615 GuiData
->ScreenBackground
= RGB(0, 0, 0);
616 GuiData
->PopupText
= RGB(128, 0, 128);
617 GuiData
->PopupBackground
= RGB(255, 255, 255);
618 GuiData
->WindowPosition
= UINT_MAX
;
619 GuiData
->UseRasterFonts
= TRUE
;
620 memcpy(GuiData
->Colors
, s_Colors
, sizeof(s_Colors
));
622 Console
->Size
.X
= 80;
623 Console
->Size
.Y
= 25;
629 Buffer
->CursorInfo
.bVisible
= TRUE
;
630 Buffer
->CursorInfo
.dwSize
= CSR_DEFAULT_CURSOR_SIZE
;
636 GuiConsoleInitScrollbar(PCSRSS_CONSOLE Console
, HWND hwnd
)
639 PGUI_CONSOLE_DATA GuiData
= Console
->PrivateData
;
641 DWORD Width
= Console
->Size
.X
* GuiData
->CharWidth
+ 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
642 DWORD Height
= Console
->Size
.Y
* GuiData
->CharHeight
+ 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
644 /* set scrollbar sizes */
645 sInfo
.cbSize
= sizeof(SCROLLINFO
);
646 sInfo
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_POS
;
648 if (Console
->ActiveBuffer
->MaxY
> Console
->Size
.Y
)
650 sInfo
.nMax
= Console
->ActiveBuffer
->MaxY
- 1;
651 sInfo
.nPage
= Console
->Size
.Y
;
652 sInfo
.nPos
= Console
->ActiveBuffer
->ShowY
;
653 SetScrollInfo(hwnd
, SB_VERT
, &sInfo
, TRUE
);
654 Width
+= GetSystemMetrics(SM_CXVSCROLL
);
655 ShowScrollBar(hwnd
, SB_VERT
, TRUE
);
659 ShowScrollBar(hwnd
, SB_VERT
, FALSE
);
662 if (Console
->ActiveBuffer
->MaxX
> Console
->Size
.X
)
664 sInfo
.nMax
= Console
->ActiveBuffer
->MaxX
- 1;
665 sInfo
.nPage
= Console
->Size
.X
;
666 sInfo
.nPos
= Console
->ActiveBuffer
->ShowX
;
667 SetScrollInfo(hwnd
, SB_HORZ
, &sInfo
, TRUE
);
668 Height
+= GetSystemMetrics(SM_CYHSCROLL
);
669 ShowScrollBar(hwnd
, SB_HORZ
, TRUE
);
673 ShowScrollBar(hwnd
, SB_HORZ
, FALSE
);
676 SetWindowPos(hwnd
, NULL
, 0, 0, Width
, Height
,
677 SWP_NOZORDER
| SWP_NOMOVE
| SWP_NOACTIVATE
);
681 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
683 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
684 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
)Console
->PrivateData
;
688 PCSRSS_PROCESS_DATA ProcessData
;
691 Console
->hWindow
= hWnd
;
695 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
699 GuiConsoleUseDefaults(Console
, GuiData
, Console
->ActiveBuffer
);
700 if (Console
->ProcessList
.Flink
!= &Console
->ProcessList
)
702 ProcessData
= CONTAINING_RECORD(Console
->ProcessList
.Flink
, CSRSS_PROCESS_DATA
, ProcessEntry
);
703 if (GuiConsoleOpenUserSettings(GuiData
, PtrToUlong(ProcessData
->ProcessId
), &hKey
, KEY_READ
, FALSE
))
705 GuiConsoleReadUserSettings(hKey
, Console
, GuiData
, Console
->ActiveBuffer
);
710 InitializeCriticalSection(&GuiData
->Lock
);
712 GuiData
->LineBuffer
= (PWCHAR
)HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
713 Console
->Size
.X
* sizeof(WCHAR
));
715 GuiData
->Font
= CreateFontW(LOWORD(GuiData
->FontSize
),
716 0, //HIWORD(GuiData->FontSize),
724 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
725 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
727 if (NULL
== GuiData
->Font
)
729 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
730 DeleteCriticalSection(&GuiData
->Lock
);
731 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
737 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
738 DeleteObject(GuiData
->Font
);
739 DeleteCriticalSection(&GuiData
->Lock
);
740 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
743 OldFont
= SelectObject(Dc
, GuiData
->Font
);
746 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
748 DeleteObject(GuiData
->Font
);
749 DeleteCriticalSection(&GuiData
->Lock
);
750 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
753 if (! GetTextMetricsW(Dc
, &Metrics
))
755 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
756 SelectObject(Dc
, OldFont
);
758 DeleteObject(GuiData
->Font
);
759 DeleteCriticalSection(&GuiData
->Lock
);
760 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
763 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
764 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
765 SelectObject(Dc
, OldFont
);
768 GuiData
->CursorBlinkOn
= TRUE
;
769 GuiData
->ForceCursorOff
= FALSE
;
771 GuiData
->Selection
.left
= -1;
772 DPRINT("Console %p GuiData %p\n", Console
, GuiData
);
773 Console
->PrivateData
= GuiData
;
774 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
776 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
777 GuiConsoleCreateSysMenu(Console
);
778 GuiConsoleInitScrollbar(Console
, hWnd
);
779 SetEvent(GuiData
->hGuiInitEvent
);
781 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
785 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
787 RECT oldRect
= GuiData
->Selection
;
791 RECT changeRect
= *rc
;
793 GuiData
->Selection
= *rc
;
795 changeRect
.left
*= GuiData
->CharWidth
;
796 changeRect
.top
*= GuiData
->CharHeight
;
797 changeRect
.right
*= GuiData
->CharWidth
;
798 changeRect
.bottom
*= GuiData
->CharHeight
;
800 if(rc
->left
!= oldRect
.left
||
801 rc
->top
!= oldRect
.top
||
802 rc
->right
!= oldRect
.right
||
803 rc
->bottom
!= oldRect
.bottom
)
805 if(oldRect
.left
!= -1)
809 oldRect
.left
*= GuiData
->CharWidth
;
810 oldRect
.top
*= GuiData
->CharHeight
;
811 oldRect
.right
*= GuiData
->CharWidth
;
812 oldRect
.bottom
*= GuiData
->CharHeight
;
814 /* calculate the region that needs to be updated */
815 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
817 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
819 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
821 InvalidateRgn(hWnd
, rgn1
, FALSE
);
831 InvalidateRect(hWnd
, &changeRect
, FALSE
);
835 else if(oldRect
.left
!= -1)
837 /* clear the selection */
838 GuiData
->Selection
.left
= -1;
839 oldRect
.left
*= GuiData
->CharWidth
;
840 oldRect
.top
*= GuiData
->CharHeight
;
841 oldRect
.right
*= GuiData
->CharWidth
;
842 oldRect
.bottom
*= GuiData
->CharHeight
;
843 InvalidateRect(hWnd
, &oldRect
, FALSE
);
849 GuiConsolePaint(PCSRSS_CONSOLE Console
,
850 PGUI_CONSOLE_DATA GuiData
,
854 PCSRSS_SCREEN_BUFFER Buff
;
855 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
856 ULONG Line
, Char
, Start
;
859 BYTE LastAttribute
, Attribute
;
860 ULONG CursorX
, CursorY
, CursorHeight
;
861 HBRUSH CursorBrush
, OldBrush
;
864 Buff
= Console
->ActiveBuffer
;
866 EnterCriticalSection(&Buff
->Header
.Lock
);
868 TopLine
= rc
->top
/ GuiData
->CharHeight
+ Buff
->ShowY
;
869 BottomLine
= (rc
->bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1 + Buff
->ShowY
;
870 LeftChar
= rc
->left
/ GuiData
->CharWidth
+ Buff
->ShowX
;
871 RightChar
= (rc
->right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1 + Buff
->ShowX
;
872 LastAttribute
= ConioCoordToPointer(Buff
, LeftChar
, TopLine
)[1];
874 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
));
875 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, LastAttribute
>> 4));
877 if (BottomLine
>= Buff
->MaxY
) BottomLine
= Buff
->MaxY
- 1;
878 if (RightChar
>= Buff
->MaxX
) RightChar
= Buff
->MaxX
- 1;
880 OldFont
= SelectObject(hDC
,
883 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
885 From
= ConioCoordToPointer(Buff
, LeftChar
, Line
);
887 To
= GuiData
->LineBuffer
;
889 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
891 if (*(From
+ 1) != LastAttribute
)
894 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
895 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
899 To
= GuiData
->LineBuffer
;
900 Attribute
= *(From
+ 1);
901 if (Attribute
!= LastAttribute
)
903 SetTextColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
));
904 SetBkColor(hDC
, GuiConsoleRGBFromAttribute(GuiData
, Attribute
>> 4));
905 LastAttribute
= Attribute
;
909 MultiByteToWideChar(Console
->OutputCodePage
,
920 (Start
- Buff
->ShowX
) * GuiData
->CharWidth
,
921 (Line
- Buff
->ShowY
) * GuiData
->CharHeight
,
923 RightChar
- Start
+ 1);
926 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
&&
927 !GuiData
->ForceCursorOff
)
929 CursorX
= Buff
->CurrentX
;
930 CursorY
= Buff
->CurrentY
;
931 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
932 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
934 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
935 if (CursorHeight
< 1)
939 From
= ConioCoordToPointer(Buff
, Buff
->CurrentX
, Buff
->CurrentY
) + 1;
941 if (*From
!= DEFAULT_ATTRIB
)
943 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(GuiData
, *From
));
947 CursorBrush
= CreateSolidBrush(GuiData
->ScreenText
);
950 OldBrush
= SelectObject(hDC
,
953 (CursorX
- Buff
->ShowX
) * GuiData
->CharWidth
,
954 (CursorY
- Buff
->ShowY
) * GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
960 DeleteObject(CursorBrush
);
964 LeaveCriticalSection(&Buff
->Header
.Lock
);
971 GuiConsoleHandlePaint(HWND hWnd
, HDC hDCPaint
)
975 PCSRSS_CONSOLE Console
;
976 PGUI_CONSOLE_DATA GuiData
;
978 hDC
= BeginPaint(hWnd
, &ps
);
980 ps
.rcPaint
.left
< ps
.rcPaint
.right
&&
981 ps
.rcPaint
.top
< ps
.rcPaint
.bottom
)
983 GuiConsoleGetDataPointers(hWnd
,
986 if (Console
!= NULL
&& GuiData
!= NULL
&&
987 Console
->ActiveBuffer
!= NULL
)
989 if (Console
->ActiveBuffer
->Buffer
!= NULL
)
991 EnterCriticalSection(&GuiData
->Lock
);
993 GuiConsolePaint(Console
,
998 if (GuiData
->Selection
.left
!= -1)
1000 RECT rc
= GuiData
->Selection
;
1002 rc
.left
*= GuiData
->CharWidth
;
1003 rc
.top
*= GuiData
->CharHeight
;
1004 rc
.right
*= GuiData
->CharWidth
;
1005 rc
.bottom
*= GuiData
->CharHeight
;
1007 /* invert the selection */
1008 if (IntersectRect(&rc
,
1021 LeaveCriticalSection(&GuiData
->Lock
);
1026 EndPaint(hWnd
, &ps
);
1029 static VOID FASTCALL
1030 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1032 PCSRSS_CONSOLE Console
;
1033 PGUI_CONSOLE_DATA GuiData
;
1036 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1037 Message
.hwnd
= hWnd
;
1038 Message
.message
= msg
;
1039 Message
.wParam
= wParam
;
1040 Message
.lParam
= lParam
;
1042 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
1044 /* clear the selection */
1045 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1048 ConioProcessKey(&Message
, Console
, FALSE
);
1051 static VOID FASTCALL
1052 GuiIntDrawRegion(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
1056 RegionRect
.left
= (Region
->left
- Buff
->ShowX
) * GuiData
->CharWidth
;
1057 RegionRect
.top
= (Region
->top
- Buff
->ShowY
) * GuiData
->CharHeight
;
1058 RegionRect
.right
= (Region
->right
+ 1 - Buff
->ShowX
) * GuiData
->CharWidth
;
1059 RegionRect
.bottom
= (Region
->bottom
+ 1 - Buff
->ShowY
) * GuiData
->CharHeight
;
1061 InvalidateRect(Wnd
, &RegionRect
, FALSE
);
1065 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
1067 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1069 if (NULL
!= Console
->hWindow
&& NULL
!= GuiData
)
1071 GuiIntDrawRegion(Console
->ActiveBuffer
, GuiData
, Console
->hWindow
, Region
);
1075 static VOID FASTCALL
1076 GuiInvalidateCell(PCSRSS_SCREEN_BUFFER Buff
, PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
1083 CellRect
.bottom
= y
;
1085 GuiIntDrawRegion(Buff
, GuiData
, Wnd
, &CellRect
);
1089 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, LONG CursorStartX
, LONG CursorStartY
,
1090 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
1092 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1093 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
1094 LONG CursorEndX
, CursorEndY
;
1097 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
1102 if (0 != ScrolledLines
)
1104 ScrollRect
.left
= 0;
1106 ScrollRect
.right
= Console
->Size
.X
* GuiData
->CharWidth
;
1107 ScrollRect
.bottom
= Region
->top
* GuiData
->CharHeight
;
1109 if (GuiData
->Selection
.left
!= -1)
1111 /* scroll the selection */
1112 if (GuiData
->Selection
.top
> ScrolledLines
)
1114 GuiData
->Selection
.top
-= ScrolledLines
;
1115 GuiData
->Selection
.bottom
-= ScrolledLines
;
1117 else if (GuiData
->Selection
.bottom
< ScrolledLines
)
1119 GuiData
->Selection
.left
= -1;
1123 GuiData
->Selection
.top
= 0;
1124 GuiData
->Selection
.bottom
-= ScrolledLines
;
1128 ScrollWindowEx(Console
->hWindow
,
1130 -(ScrolledLines
* GuiData
->CharHeight
),
1138 GuiIntDrawRegion(Buff
, GuiData
, Console
->hWindow
, Region
);
1140 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
1141 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
1143 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
1146 CursorEndX
= Buff
->CurrentX
;
1147 CursorEndY
= Buff
->CurrentY
;
1148 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
1149 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
1150 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
1152 GuiInvalidateCell(Buff
, GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
1157 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1161 if (Console
->ActiveBuffer
== Buff
)
1163 UpdateRect
.left
= Buff
->CurrentX
;
1164 UpdateRect
.top
= Buff
->CurrentY
;
1165 UpdateRect
.right
= UpdateRect
.left
;
1166 UpdateRect
.bottom
= UpdateRect
.top
;
1167 ConioDrawRegion(Console
, &UpdateRect
);
1174 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
1178 if (Console
->ActiveBuffer
== Buff
)
1180 /* Redraw char at old position (removes cursor) */
1181 UpdateRect
.left
= OldCursorX
;
1182 UpdateRect
.top
= OldCursorY
;
1183 UpdateRect
.right
= OldCursorX
;
1184 UpdateRect
.bottom
= OldCursorY
;
1185 ConioDrawRegion(Console
, &UpdateRect
);
1186 /* Redraw char at new position (shows cursor) */
1187 UpdateRect
.left
= Buff
->CurrentX
;
1188 UpdateRect
.top
= Buff
->CurrentY
;
1189 UpdateRect
.right
= UpdateRect
.left
;
1190 UpdateRect
.bottom
= UpdateRect
.top
;
1191 ConioDrawRegion(Console
, &UpdateRect
);
1198 GuiUpdateScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
1200 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
1202 if (Console
->ActiveBuffer
== Buff
)
1204 GuiData
->ScreenText
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
);
1205 GuiData
->ScreenBackground
= GuiConsoleRGBFromAttribute(GuiData
, Buff
->DefaultAttrib
>> 4);
1211 static VOID FASTCALL
1212 GuiConsoleHandleTimer(HWND hWnd
)
1214 PCSRSS_CONSOLE Console
;
1215 PGUI_CONSOLE_DATA GuiData
;
1218 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1219 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
1221 CursorRect
.left
= Console
->ActiveBuffer
->CurrentX
;
1222 CursorRect
.top
= Console
->ActiveBuffer
->CurrentY
;
1223 CursorRect
.right
= CursorRect
.left
;
1224 CursorRect
.bottom
= CursorRect
.top
;
1225 GuiDrawRegion(Console
, &CursorRect
);
1228 static VOID FASTCALL
1229 GuiConsoleHandleClose(HWND hWnd
)
1231 PCSRSS_CONSOLE Console
;
1232 PGUI_CONSOLE_DATA GuiData
;
1233 PLIST_ENTRY current_entry
;
1234 PCSRSS_PROCESS_DATA current
;
1236 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1238 EnterCriticalSection(&Console
->Header
.Lock
);
1240 current_entry
= Console
->ProcessList
.Flink
;
1241 while (current_entry
!= &Console
->ProcessList
)
1243 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
1244 current_entry
= current_entry
->Flink
;
1246 /* FIXME: Windows will wait up to 5 seconds for the thread to exit.
1247 * We shouldn't wait here, though, since the console lock is entered.
1248 * A copy of the thread list probably needs to be made. */
1249 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
1252 LeaveCriticalSection(&Console
->Header
.Lock
);
1255 static VOID FASTCALL
1256 GuiConsoleHandleNcDestroy(HWND hWnd
)
1258 PCSRSS_CONSOLE Console
;
1259 PGUI_CONSOLE_DATA GuiData
;
1262 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1264 Console
->PrivateData
= NULL
;
1265 DeleteCriticalSection(&GuiData
->Lock
);
1266 GetSystemMenu(hWnd
, TRUE
);
1267 if (GuiData
->ConsoleLibrary
)
1268 FreeLibrary(GuiData
->ConsoleLibrary
);
1270 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
1273 static VOID FASTCALL
1274 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
1276 PCSRSS_CONSOLE Console
;
1277 PGUI_CONSOLE_DATA GuiData
;
1281 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1282 if (Console
== NULL
|| GuiData
== NULL
) return;
1284 pt
= MAKEPOINTS(lParam
);
1286 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
1287 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
1288 rc
.right
= rc
.left
+ 1;
1289 rc
.bottom
= rc
.top
+ 1;
1291 GuiData
->SelectionStart
.x
= rc
.left
;
1292 GuiData
->SelectionStart
.y
= rc
.top
;
1296 GuiData
->MouseDown
= TRUE
;
1298 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1301 static VOID FASTCALL
1302 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
1304 PCSRSS_CONSOLE Console
;
1305 PGUI_CONSOLE_DATA GuiData
;
1309 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1310 if (Console
== NULL
|| GuiData
== NULL
) return;
1311 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
1313 pt
= MAKEPOINTS(lParam
);
1315 rc
.left
= GuiData
->SelectionStart
.x
;
1316 rc
.top
= GuiData
->SelectionStart
.y
;
1317 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1318 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1320 /* exchange left/top with right/bottom if required */
1321 if(rc
.left
>= rc
.right
)
1325 rc
.left
= max(rc
.right
- 1, 0);
1328 if(rc
.top
>= rc
.bottom
)
1332 rc
.top
= max(rc
.bottom
- 1, 0);
1333 rc
.bottom
= tmp
+ 1;
1336 GuiData
->MouseDown
= FALSE
;
1338 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1343 static VOID FASTCALL
1344 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1346 PCSRSS_CONSOLE Console
;
1347 PGUI_CONSOLE_DATA GuiData
;
1351 if (!(wParam
& MK_LBUTTON
)) return;
1353 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1354 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
1356 pt
= MAKEPOINTS(lParam
);
1358 rc
.left
= GuiData
->SelectionStart
.x
;
1359 rc
.top
= GuiData
->SelectionStart
.y
;
1360 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
1361 if (Console
->Size
.X
< rc
.right
)
1363 rc
.right
= Console
->Size
.X
;
1365 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
1366 if (Console
->Size
.Y
< rc
.bottom
)
1368 rc
.bottom
= Console
->Size
.Y
;
1371 /* exchange left/top with right/bottom if required */
1372 if(rc
.left
>= rc
.right
)
1376 rc
.left
= max(rc
.right
- 1, 0);
1379 if(rc
.top
>= rc
.bottom
)
1383 rc
.top
= max(rc
.bottom
- 1, 0);
1384 rc
.bottom
= tmp
+ 1;
1387 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
1390 static VOID FASTCALL
1391 GuiConsoleRightMouseDown(HWND hWnd
)
1393 PCSRSS_CONSOLE Console
;
1394 PGUI_CONSOLE_DATA GuiData
;
1396 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1397 if (Console
== NULL
|| GuiData
== NULL
) return;
1399 if (GuiData
->Selection
.left
== -1)
1401 /* FIXME - paste text from clipboard */
1405 /* FIXME - copy selection to clipboard */
1407 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
1414 GuiConsoleShowConsoleProperties(HWND hWnd
, BOOL Defaults
, PGUI_CONSOLE_DATA GuiData
)
1416 PCSRSS_CONSOLE Console
;
1417 APPLET_PROC CPLFunc
;
1418 TCHAR szBuffer
[MAX_PATH
];
1419 ConsoleInfo SharedInfo
;
1421 DPRINT("GuiConsoleShowConsoleProperties entered\n");
1423 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1425 if (GuiData
== NULL
)
1427 DPRINT("GuiConsoleGetDataPointers failed\n");
1431 if (GuiData
->ConsoleLibrary
== NULL
)
1433 GetWindowsDirectory(szBuffer
,MAX_PATH
);
1434 _tcscat(szBuffer
, _T("\\system32\\console.dll"));
1435 GuiData
->ConsoleLibrary
= LoadLibrary(szBuffer
);
1437 if (GuiData
->ConsoleLibrary
== NULL
)
1439 DPRINT1("failed to load console.dll");
1444 CPLFunc
= (APPLET_PROC
) GetProcAddress(GuiData
->ConsoleLibrary
, _T("CPlApplet"));
1447 DPRINT("Error: Console.dll misses CPlApplet export\n");
1452 SharedInfo
.InsertMode
= GuiData
->InsertMode
;
1453 SharedInfo
.HistoryBufferSize
= GuiData
->HistoryBufferSize
;
1454 SharedInfo
.NumberOfHistoryBuffers
= GuiData
->NumberOfHistoryBuffers
;
1455 SharedInfo
.ScreenText
= GuiData
->ScreenText
;
1456 SharedInfo
.ScreenBackground
= GuiData
->ScreenBackground
;
1457 SharedInfo
.PopupText
= GuiData
->PopupText
;
1458 SharedInfo
.PopupBackground
= GuiData
->PopupBackground
;
1459 SharedInfo
.WindowSize
= (DWORD
)MAKELONG(Console
->Size
.X
, Console
->Size
.Y
);
1460 SharedInfo
.WindowPosition
= GuiData
->WindowPosition
;
1461 SharedInfo
.ScreenBuffer
= (DWORD
)MAKELONG(Console
->ActiveBuffer
->MaxX
, Console
->ActiveBuffer
->MaxY
);
1462 SharedInfo
.UseRasterFonts
= GuiData
->UseRasterFonts
;
1463 SharedInfo
.FontSize
= (DWORD
)GuiData
->FontSize
;
1464 SharedInfo
.FontWeight
= GuiData
->FontWeight
;
1465 SharedInfo
.CursorSize
= Console
->ActiveBuffer
->CursorInfo
.dwSize
;
1466 SharedInfo
.HistoryNoDup
= GuiData
->HistoryNoDup
;
1467 SharedInfo
.FullScreen
= GuiData
->FullScreen
;
1468 SharedInfo
.QuickEdit
= GuiData
->QuickEdit
;
1469 memcpy(&SharedInfo
.Colors
[0], GuiData
->Colors
, sizeof(s_Colors
));
1471 if (!CPLFunc(hWnd
, CPL_INIT
, 0, 0))
1473 DPRINT("Error: failed to initialize console.dll\n");
1477 if (CPLFunc(hWnd
, CPL_GETCOUNT
, 0, 0) != 1)
1479 DPRINT("Error: console.dll returned unexpected CPL count\n");
1483 CPLFunc(hWnd
, CPL_DBLCLK
, (LPARAM
)&SharedInfo
, Defaults
);
1485 static LRESULT FASTCALL
1486 GuiConsoleHandleSysMenuCommand(HWND hWnd
, WPARAM wParam
, LPARAM lParam
, PGUI_CONSOLE_DATA GuiData
)
1492 case ID_SYSTEM_EDIT_MARK
:
1493 case ID_SYSTEM_EDIT_COPY
:
1494 case ID_SYSTEM_EDIT_PASTE
:
1495 case ID_SYSTEM_EDIT_SELECTALL
:
1496 case ID_SYSTEM_EDIT_SCROLL
:
1497 case ID_SYSTEM_EDIT_FIND
:
1500 case ID_SYSTEM_DEFAULTS
:
1501 GuiConsoleShowConsoleProperties(hWnd
, TRUE
, GuiData
);
1504 case ID_SYSTEM_PROPERTIES
:
1505 GuiConsoleShowConsoleProperties(hWnd
, FALSE
, GuiData
);
1509 Ret
= DefWindowProcW(hWnd
, WM_SYSCOMMAND
, wParam
, lParam
);
1515 static VOID FASTCALL
1516 GuiConsoleResize(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1518 PCSRSS_CONSOLE Console
;
1519 PGUI_CONSOLE_DATA GuiData
;
1521 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1522 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
|| wParam
== SIZE_MINIMIZED
)
1524 DPRINT1("GuiConsoleResize X %d Y %d\n", LOWORD(lParam
), HIWORD(lParam
));
1529 GuiConsoleHandleScrollbarMenu()
1533 hMenu
= CreatePopupMenu();
1536 DPRINT("CreatePopupMenu failed\n");
1539 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1540 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1541 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1542 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1543 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1544 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1545 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1546 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1547 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1548 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1552 static VOID FASTCALL
1553 GuiApplyUserSettings(PCSRSS_CONSOLE Console
, PGUI_CONSOLE_DATA GuiData
, PConsoleInfo pConInfo
)
1556 PCSRSS_SCREEN_BUFFER ActiveBuffer
= Console
->ActiveBuffer
;
1557 BOOL SizeChanged
= FALSE
;
1559 EnterCriticalSection(&ActiveBuffer
->Header
.Lock
);
1561 /* apply text / background color */
1562 GuiData
->ScreenText
= pConInfo
->ScreenText
;
1563 GuiData
->ScreenBackground
= pConInfo
->ScreenBackground
;
1565 /* apply cursor size */
1566 ActiveBuffer
->CursorInfo
.dwSize
= min(max(pConInfo
->CursorSize
, 1), 100);
1568 windx
= LOWORD(pConInfo
->ScreenBuffer
);
1569 windy
= HIWORD(pConInfo
->ScreenBuffer
);
1571 if (windx
!= ActiveBuffer
->MaxX
|| windy
!= ActiveBuffer
->MaxY
)
1573 BYTE
* Buffer
= HeapAlloc(Win32CsrApiHeap
, 0, windx
* windy
* 2);
1585 value
= MAKEWORD(' ', ActiveBuffer
->DefaultAttrib
);
1587 DPRINT("MaxX %d MaxY %d windx %d windy %d value %04x DefaultAttrib %d\n",ActiveBuffer
->MaxX
, ActiveBuffer
->MaxY
, windx
, windy
, value
, ActiveBuffer
->DefaultAttrib
);
1588 OldBuffer
= ActiveBuffer
->Buffer
;
1590 for (CurrentY
= 0; CurrentY
< min(ActiveBuffer
->MaxY
, windy
); CurrentY
++)
1592 OldPtr
= ConioCoordToPointer(ActiveBuffer
, 0, CurrentY
);
1593 if (windx
<= ActiveBuffer
->MaxX
)
1596 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, windx
* 2);
1597 Offset
+= (windx
* 2);
1602 RtlCopyMemory(&Buffer
[Offset
], OldPtr
, ActiveBuffer
->MaxX
* 2);
1603 Offset
+= (ActiveBuffer
->MaxX
* 2);
1605 diff
= windx
- ActiveBuffer
->MaxX
;
1606 /* zero new part of it */
1608 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
);
1610 for (i
= 0; i
< diff
; i
++)
1612 Buffer
[Offset
++] = ' ';
1613 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1619 if (windy
> ActiveBuffer
->MaxY
)
1621 diff
= windy
- ActiveBuffer
->MaxY
;
1623 wmemset((WCHAR
*)&Buffer
[Offset
], value
, diff
* windx
);
1625 for (i
= 0; i
< diff
* windx
; i
++)
1627 Buffer
[Offset
++] = ' ';
1628 Buffer
[Offset
++] = ActiveBuffer
->DefaultAttrib
;
1633 (void)InterlockedExchangePointer((PVOID
volatile *)&ActiveBuffer
->Buffer
, Buffer
);
1634 HeapFree(Win32CsrApiHeap
, 0, OldBuffer
);
1635 ActiveBuffer
->MaxX
= windx
;
1636 ActiveBuffer
->MaxY
= windy
;
1637 ActiveBuffer
->VirtualY
= 0;
1642 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1647 windx
= LOWORD(pConInfo
->WindowSize
);
1648 windy
= HIWORD(pConInfo
->WindowSize
);
1650 if (windx
> Console
->Size
.X
)
1652 PWCHAR LineBuffer
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
, windx
* sizeof(WCHAR
));
1655 HeapFree(Win32CsrApiHeap
, 0, GuiData
->LineBuffer
);
1656 GuiData
->LineBuffer
= LineBuffer
;
1660 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1666 if (windx
!= Console
->Size
.X
|| windy
!= Console
->Size
.Y
)
1669 Console
->Size
.X
= windx
;
1670 Console
->Size
.Y
= windy
;
1676 GuiConsoleInitScrollbar(Console
, pConInfo
->hConsoleWindow
);
1679 LeaveCriticalSection(&ActiveBuffer
->Header
.Lock
);
1680 InvalidateRect(pConInfo
->hConsoleWindow
, NULL
, TRUE
);
1685 GuiConsoleHandleScroll(HWND hwnd
, UINT uMsg
, WPARAM wParam
)
1687 PCSRSS_CONSOLE Console
;
1688 PCSRSS_SCREEN_BUFFER Buff
;
1689 PGUI_CONSOLE_DATA GuiData
;
1692 int old_pos
, Maximum
;
1695 GuiConsoleGetDataPointers(hwnd
, &Console
, &GuiData
);
1696 if (Console
== NULL
|| GuiData
== NULL
)
1698 Buff
= Console
->ActiveBuffer
;
1700 if (uMsg
== WM_HSCROLL
)
1703 Maximum
= Buff
->MaxX
- Console
->Size
.X
;
1704 pShowXY
= &Buff
->ShowX
;
1709 Maximum
= Buff
->MaxY
- Console
->Size
.Y
;
1710 pShowXY
= &Buff
->ShowY
;
1713 /* set scrollbar sizes */
1714 sInfo
.cbSize
= sizeof(SCROLLINFO
);
1715 sInfo
.fMask
= SIF_RANGE
| SIF_POS
| SIF_PAGE
| SIF_TRACKPOS
;
1717 if (!GetScrollInfo(hwnd
, fnBar
, &sInfo
))
1722 old_pos
= sInfo
.nPos
;
1724 switch(LOWORD(wParam
))
1735 sInfo
.nPos
-= sInfo
.nPage
;
1739 sInfo
.nPos
+= sInfo
.nPage
;
1743 sInfo
.nPos
= sInfo
.nTrackPos
;
1747 sInfo
.nPos
= sInfo
.nMin
;
1751 sInfo
.nPos
= sInfo
.nMax
;
1758 sInfo
.nPos
= max(sInfo
.nPos
, 0);
1759 sInfo
.nPos
= min(sInfo
.nPos
, Maximum
);
1761 if (old_pos
!= sInfo
.nPos
)
1763 USHORT OldX
= Buff
->ShowX
;
1764 USHORT OldY
= Buff
->ShowY
;
1765 *pShowXY
= sInfo
.nPos
;
1767 ScrollWindowEx(hwnd
,
1768 (OldX
- Buff
->ShowX
) * GuiData
->CharWidth
,
1769 (OldY
- Buff
->ShowY
) * GuiData
->CharHeight
,
1776 sInfo
.fMask
= SIF_POS
;
1777 SetScrollInfo(hwnd
, fnBar
, &sInfo
, TRUE
);
1784 static LRESULT CALLBACK
1785 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1788 PGUI_CONSOLE_DATA GuiData
= NULL
;
1789 PCSRSS_CONSOLE Console
= NULL
;
1791 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1796 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
1799 GuiConsoleHandlePaint(hWnd
, (HDC
)wParam
);
1806 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
1809 GuiConsoleHandleTimer(hWnd
);
1812 GuiConsoleHandleClose(hWnd
);
1815 GuiConsoleHandleNcDestroy(hWnd
);
1817 case WM_LBUTTONDOWN
:
1818 GuiConsoleLeftMouseDown(hWnd
, lParam
);
1821 GuiConsoleLeftMouseUp(hWnd
, lParam
);
1823 case WM_RBUTTONDOWN
:
1824 GuiConsoleRightMouseDown(hWnd
);
1827 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
1830 Result
= GuiConsoleHandleSysMenuCommand(hWnd
, wParam
, lParam
, GuiData
);
1834 Result
= GuiConsoleHandleScroll(hWnd
, msg
, wParam
);
1837 GuiConsoleResize(hWnd
, wParam
, lParam
);
1839 case PM_APPLY_CONSOLE_INFO
:
1840 GuiApplyUserSettings(Console
, GuiData
, (PConsoleInfo
)wParam
);
1843 GuiConsoleWriteUserSettings(Console
, GuiData
);
1847 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1854 static LRESULT CALLBACK
1855 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
1860 PWCHAR Buffer
, Title
;
1861 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
1868 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
1870 case PM_CREATE_CONSOLE
:
1871 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
1872 Console
->Title
.Length
+ sizeof(WCHAR
));
1875 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
1876 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
1883 NewWindow
= CreateWindowW(L
"ConsoleWindowClass",
1885 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
| WS_HSCROLL
| WS_VSCROLL
, //WS_OVERLAPPEDWINDOW
1892 (HINSTANCE
) GetModuleHandleW(NULL
),
1896 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
1898 if (NULL
!= NewWindow
)
1900 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
1901 ShowWindow(NewWindow
, SW_SHOW
);
1903 return (LRESULT
) NewWindow
;
1904 case PM_DESTROY_CONSOLE
:
1905 /* Window creation is done using a PostMessage(), so it's possible that the
1906 * window that we want to destroy doesn't exist yet. So first empty the message
1908 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
1910 TranslateMessage(&Msg
);
1911 DispatchMessageW(&Msg
);
1913 DestroyWindow(Console
->hWindow
);
1914 Console
->hWindow
= NULL
;
1915 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
1917 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
1918 if (0 == WindowCount
)
1921 DestroyWindow(hWnd
);
1922 PrivateCsrssManualGuiCheck(-1);
1927 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
1932 GuiConsoleGuiThread(PVOID Data
)
1935 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
1937 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
1939 WS_OVERLAPPEDWINDOW
,
1946 (HINSTANCE
) GetModuleHandleW(NULL
),
1948 if (NULL
== NotifyWnd
)
1950 PrivateCsrssManualGuiCheck(-1);
1951 SetEvent(*GraphicsStartupEvent
);
1955 SetEvent(*GraphicsStartupEvent
);
1957 while(GetMessageW(&msg
, NULL
, 0, 0))
1959 TranslateMessage(&msg
);
1960 DispatchMessageW(&msg
);
1966 static BOOL FASTCALL
1971 if (NULL
== NotifyWnd
)
1973 PrivateCsrssManualGuiCheck(+1);
1976 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1977 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
1978 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
1980 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1983 wc
.hbrBackground
= NULL
;
1984 wc
.lpszMenuName
= NULL
;
1988 if (RegisterClassExW(&wc
) == 0)
1990 DPRINT1("Failed to register notify wndproc\n");
1994 wc
.cbSize
= sizeof(WNDCLASSEXW
);
1995 wc
.lpszClassName
= L
"ConsoleWindowClass";
1996 wc
.lpfnWndProc
= GuiConsoleWndProc
;
1998 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
1999 wc
.hIcon
= LoadIconW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1));
2000 wc
.hCursor
= LoadCursorW(NULL
, (LPCWSTR
) IDC_ARROW
);
2001 wc
.hbrBackground
= NULL
;
2002 wc
.lpszMenuName
= NULL
;
2005 wc
.hIconSm
= LoadImageW(GetModuleHandleW(L
"win32csr"), MAKEINTRESOURCEW(1), IMAGE_ICON
,
2006 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
2008 if (RegisterClassExW(&wc
) == 0)
2010 DPRINT1("Failed to register console wndproc\n");
2018 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
2020 Buffer
->DefaultAttrib
= DEFAULT_ATTRIB
;
2024 GuiChangeTitle(PCSRSS_CONSOLE Console
)
2026 PWCHAR Buffer
, Title
;
2028 Buffer
= HeapAlloc(Win32CsrApiHeap
, 0,
2029 Console
->Title
.Length
+ sizeof(WCHAR
));
2032 memcpy(Buffer
, Console
->Title
.Buffer
, Console
->Title
.Length
);
2033 Buffer
[Console
->Title
.Length
/ sizeof(WCHAR
)] = L
'\0';
2041 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Title
);
2045 HeapFree(Win32CsrApiHeap
, 0, Buffer
);
2052 GuiChangeIcon(PCSRSS_CONSOLE Console
, HICON hWindowIcon
)
2054 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)hWindowIcon
);
2055 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)hWindowIcon
);
2061 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
2063 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
2066 static CSRSS_CONSOLE_VTBL GuiVtbl
=
2068 GuiInitScreenBuffer
,
2073 GuiUpdateScreenInfo
,
2080 GuiInitConsole(PCSRSS_CONSOLE Console
)
2082 HANDLE GraphicsStartupEvent
;
2083 HANDLE ThreadHandle
;
2084 PGUI_CONSOLE_DATA GuiData
;
2086 if (! ConsInitialized
)
2088 ConsInitialized
= TRUE
;
2091 ConsInitialized
= FALSE
;
2092 return STATUS_UNSUCCESSFUL
;
2096 Console
->Vtbl
= &GuiVtbl
;
2097 if (NULL
== NotifyWnd
)
2099 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2100 if (NULL
== GraphicsStartupEvent
)
2102 return STATUS_UNSUCCESSFUL
;
2105 ThreadHandle
= CreateThread(NULL
,
2107 GuiConsoleGuiThread
,
2108 (PVOID
) &GraphicsStartupEvent
,
2111 if (NULL
== ThreadHandle
)
2113 NtClose(GraphicsStartupEvent
);
2114 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
2115 return STATUS_UNSUCCESSFUL
;
2117 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
2118 CloseHandle(ThreadHandle
);
2120 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
2121 CloseHandle(GraphicsStartupEvent
);
2123 if (NULL
== NotifyWnd
)
2125 DPRINT1("Win32Csr: Failed to create notification window.\n");
2126 return STATUS_UNSUCCESSFUL
;
2129 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
2130 sizeof(GUI_CONSOLE_DATA
));
2133 DPRINT1("Win32Csr: Failed to create GUI_CONSOLE_DATA\n");
2134 return STATUS_UNSUCCESSFUL
;
2137 Console
->PrivateData
= (PVOID
) GuiData
;
2139 * we need to wait untill the GUI has been fully initialized
2140 * to retrieve custom settings i.e. WindowSize etc..
2141 * Ideally we could use SendNotifyMessage for this but its not
2145 GuiData
->hGuiInitEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2146 /* create console */
2147 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
2149 /* wait untill initialization has finished */
2150 WaitForSingleObject(GuiData
->hGuiInitEvent
, INFINITE
);
2151 DPRINT1("received event Console %p GuiData %p X %d Y %d\n", Console
, Console
->PrivateData
, Console
->Size
.X
, Console
->Size
.Y
);
2152 CloseHandle(GuiData
->hGuiInitEvent
);
2153 GuiData
->hGuiInitEvent
= NULL
;
2155 return STATUS_SUCCESS
;