1 /* $Id: guiconsole.c,v 1.23 2004/12/04 22:09:02 navaraf Exp $
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 ******************************************************************/
13 #include "guiconsole.h"
19 /* Not defined in any header file */
20 extern VOID STDCALL
PrivateCsrssManualGuiCheck(LONG Check
);
22 /* GLOBALS *******************************************************************/
24 typedef struct GUI_CONSOLE_DATA_TAG
32 CRITICAL_SECTION Lock
;
38 } GUI_CONSOLE_DATA
, *PGUI_CONSOLE_DATA
;
43 #define PM_CREATE_CONSOLE (WM_APP + 1)
44 #define PM_DESTROY_CONSOLE (WM_APP + 2)
46 #define CURSOR_BLINK_TIME 500
48 static BOOL Initialized
= FALSE
;
49 static HWND NotifyWnd
;
51 /* FUNCTIONS *****************************************************************/
54 GuiConsoleGetDataPointers(HWND hWnd
, PCSRSS_CONSOLE
*Console
, PGUI_CONSOLE_DATA
*GuiData
)
56 *Console
= (PCSRSS_CONSOLE
) GetWindowLongPtrW(hWnd
, GWL_USERDATA
);
57 *GuiData
= (NULL
== *Console
? NULL
: (*Console
)->PrivateData
);
61 GuiConsoleHandleNcCreate(HWND hWnd
, CREATESTRUCTW
*Create
)
64 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) Create
->lpCreateParams
;
65 PGUI_CONSOLE_DATA GuiData
;
70 GuiData
= HeapAlloc(Win32CsrApiHeap
, HEAP_ZERO_MEMORY
,
71 sizeof(GUI_CONSOLE_DATA
) +
72 (Console
->Size
.X
+ 1) * sizeof(WCHAR
));
75 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
79 InitializeCriticalSection(&GuiData
->Lock
);
81 GuiData
->LineBuffer
= (PWCHAR
)(GuiData
+ 1);
83 GuiData
->Font
= CreateFontW(12, 0, 0, TA_BASELINE
, FW_NORMAL
,
84 FALSE
, FALSE
, FALSE
, OEM_CHARSET
,
85 OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
86 NONANTIALIASED_QUALITY
, FIXED_PITCH
| FF_DONTCARE
,
87 L
"Bitstream Vera Sans Mono");
88 if (NULL
== GuiData
->Font
)
90 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
91 DeleteCriticalSection(&GuiData
->Lock
);
92 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
98 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
99 DeleteObject(GuiData
->Font
);
100 DeleteCriticalSection(&GuiData
->Lock
);
101 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
104 OldFont
= SelectObject(Dc
, GuiData
->Font
);
107 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
109 DeleteObject(GuiData
->Font
);
110 DeleteCriticalSection(&GuiData
->Lock
);
111 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
114 if (! GetTextMetricsW(Dc
, &Metrics
))
116 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
117 SelectObject(Dc
, OldFont
);
119 DeleteObject(GuiData
->Font
);
120 DeleteCriticalSection(&GuiData
->Lock
);
121 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
124 GuiData
->CharWidth
= Metrics
.tmMaxCharWidth
;
125 GuiData
->CharHeight
= Metrics
.tmHeight
+ Metrics
.tmExternalLeading
;
126 SelectObject(Dc
, OldFont
);
128 GuiData
->MemoryDC
= CreateCompatibleDC(Dc
);
129 GuiData
->MemoryBitmap
= CreateCompatibleBitmap(Dc
,
130 Console
->Size
.X
* GuiData
->CharWidth
,
131 Console
->Size
.Y
* GuiData
->CharHeight
);
132 DeleteObject(SelectObject(GuiData
->MemoryDC
, GuiData
->MemoryBitmap
));
133 SelectObject(GuiData
->MemoryDC
, GuiData
->Font
); /* Don't delete stock font. */
136 GuiData
->CursorBlinkOn
= TRUE
;
137 GuiData
->ForceCursorOff
= FALSE
;
139 GuiData
->Selection
.left
= -1;
141 Console
->PrivateData
= GuiData
;
142 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
144 GetWindowRect(hWnd
, &Rect
);
145 Rect
.right
= Rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+
146 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
147 Rect
.bottom
= Rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+
148 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
149 MoveWindow(hWnd
, Rect
.left
, Rect
.top
, Rect
.right
- Rect
.left
,
150 Rect
.bottom
- Rect
.top
, FALSE
);
152 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
154 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
157 static COLORREF FASTCALL
158 GuiConsoleRGBFromAttribute(BYTE Attribute
)
160 int Red
= (Attribute
& 0x04 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
161 int Green
= (Attribute
& 0x02 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
162 int Blue
= (Attribute
& 0x01 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
164 return RGB(Red
, Green
, Blue
);
168 GuiConsoleSetTextColors(HDC Dc
, BYTE Attribute
)
170 SetTextColor(Dc
, GuiConsoleRGBFromAttribute(Attribute
& 0x0f));
171 SetBkColor(Dc
, GuiConsoleRGBFromAttribute((Attribute
& 0xf0) >> 4));
175 GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff
, ULONG
*CursorX
, ULONG
*CursorY
)
177 *CursorX
= Buff
->CurrentX
;
178 if (Buff
->CurrentY
< Buff
->ShowY
)
180 *CursorY
= Buff
->MaxY
- Buff
->ShowY
+ Buff
->CurrentY
;
184 *CursorY
= Buff
->CurrentY
- Buff
->ShowY
;
190 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
192 RECT oldRect
= GuiData
->Selection
;
196 RECT changeRect
= *rc
;
198 GuiData
->Selection
= *rc
;
200 changeRect
.left
*= GuiData
->CharWidth
;
201 changeRect
.top
*= GuiData
->CharHeight
;
202 changeRect
.right
*= GuiData
->CharWidth
;
203 changeRect
.bottom
*= GuiData
->CharHeight
;
205 if(rc
->left
!= oldRect
.left
||
206 rc
->top
!= oldRect
.top
||
207 rc
->right
!= oldRect
.right
||
208 rc
->bottom
!= oldRect
.bottom
)
210 if(oldRect
.left
!= -1)
214 oldRect
.left
*= GuiData
->CharWidth
;
215 oldRect
.top
*= GuiData
->CharHeight
;
216 oldRect
.right
*= GuiData
->CharWidth
;
217 oldRect
.bottom
*= GuiData
->CharHeight
;
219 /* calculate the region that needs to be updated */
220 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
222 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
224 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
226 InvalidateRgn(hWnd
, rgn1
, FALSE
);
236 InvalidateRect(hWnd
, &changeRect
, FALSE
);
240 else if(oldRect
.left
!= -1)
242 /* clear the selection */
243 GuiData
->Selection
.left
= -1;
244 oldRect
.left
*= GuiData
->CharWidth
;
245 oldRect
.top
*= GuiData
->CharHeight
;
246 oldRect
.right
*= GuiData
->CharWidth
;
247 oldRect
.bottom
*= GuiData
->CharHeight
;
248 InvalidateRect(hWnd
, &oldRect
, FALSE
);
254 GuiConsoleUpdateBitmap(HWND hWnd
, RECT rc
)
256 PCSRSS_CONSOLE Console
;
257 PGUI_CONSOLE_DATA GuiData
;
258 PCSRSS_SCREEN_BUFFER Buff
;
260 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
261 ULONG Line
, Char
, Start
;
264 BYTE LastAttribute
, Attribute
;
265 ULONG CursorX
, CursorY
, CursorHeight
;
266 HBRUSH CursorBrush
, OldBrush
;
268 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
269 if (NULL
!= Console
&& NULL
!= GuiData
&& NULL
!= Console
->ActiveBuffer
)
271 Buff
= Console
->ActiveBuffer
;
272 EnterCriticalSection(&Buff
->Header
.Lock
);
274 if (rc
.right
<= rc
.left
|| rc
.bottom
<= rc
.top
)
277 LeaveCriticalSection(&Buff
->Header
.Lock
);
281 EnterCriticalSection(&GuiData
->Lock
);
283 TopLine
= rc
.top
/ GuiData
->CharHeight
;
284 BottomLine
= (rc
.bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1;
285 LeftChar
= rc
.left
/ GuiData
->CharWidth
;
286 RightChar
= (rc
.right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1;
287 LastAttribute
= Buff
->Buffer
[(TopLine
* Buff
->MaxX
+ LeftChar
) * 2 + 1];
288 GuiConsoleSetTextColors(GuiData
->MemoryDC
, LastAttribute
);
290 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
292 if (Line
+ Buff
->ShowY
< Buff
->MaxY
)
294 From
= Buff
->Buffer
+ ((Line
+ Buff
->ShowY
) * Buff
->MaxX
+ LeftChar
) * 2;
298 From
= Buff
->Buffer
+
299 ((Line
- (Buff
->MaxY
- Buff
->ShowY
)) * Buff
->MaxX
+ LeftChar
) * 2;
302 To
= GuiData
->LineBuffer
;
303 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
305 if (*(From
+ 1) != LastAttribute
)
307 TextOutW(GuiData
->MemoryDC
, Start
* GuiData
->CharWidth
, Line
* GuiData
->CharHeight
,
308 GuiData
->LineBuffer
, Char
- Start
);
310 To
= GuiData
->LineBuffer
;
311 Attribute
= *(From
+ 1);
312 if (Attribute
!= LastAttribute
)
314 GuiConsoleSetTextColors(GuiData
->MemoryDC
, Attribute
);
315 LastAttribute
= Attribute
;
318 MultiByteToWideChar(Console
->OutputCodePage
, 0, From
, 1, To
, 1);
322 TextOutW(GuiData
->MemoryDC
, Start
* GuiData
->CharWidth
, Line
* GuiData
->CharHeight
,
323 GuiData
->LineBuffer
, RightChar
- Start
+ 1);
326 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
327 &&! GuiData
->ForceCursorOff
)
329 GuiConsoleGetLogicalCursorPos(Buff
, &CursorX
, &CursorY
);
330 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
331 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
333 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
334 if (CursorHeight
< 1)
338 From
= Buff
->Buffer
+ (Buff
->CurrentY
* Buff
->MaxX
+ Buff
->CurrentX
) * 2 + 1;
339 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(*From
));
340 OldBrush
= SelectObject(GuiData
->MemoryDC
, CursorBrush
);
341 PatBlt(GuiData
->MemoryDC
, CursorX
* GuiData
->CharWidth
,
342 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
343 GuiData
->CharWidth
, CursorHeight
, PATCOPY
);
344 SelectObject(GuiData
->MemoryDC
, OldBrush
);
345 DeleteObject(CursorBrush
);
349 LeaveCriticalSection(&GuiData
->Lock
);
351 LeaveCriticalSection(&Buff
->Header
.Lock
);
352 InvalidateRect(hWnd
, &rc
, FALSE
);
357 GuiConsoleHandlePaint(HWND hWnd
)
361 PCSRSS_CONSOLE Console
;
362 PGUI_CONSOLE_DATA GuiData
;
364 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
365 if (NULL
!= Console
&& NULL
!= GuiData
)
367 EnterCriticalSection(&GuiData
->Lock
);
368 Dc
= BeginPaint (hWnd
, &Ps
);
369 BitBlt(Dc
, Ps
.rcPaint
.left
, Ps
.rcPaint
.top
,
370 Ps
.rcPaint
.right
- Ps
.rcPaint
.left
+ 1,
371 Ps
.rcPaint
.bottom
- Ps
.rcPaint
.top
+ 1, GuiData
->MemoryDC
,
372 Ps
.rcPaint
.left
, Ps
.rcPaint
.top
, SRCCOPY
);
374 if (GuiData
->Selection
.left
!= -1)
376 RECT rc
= GuiData
->Selection
;
378 rc
.left
*= GuiData
->CharWidth
;
379 rc
.top
*= GuiData
->CharHeight
;
380 rc
.right
*= GuiData
->CharWidth
;
381 rc
.bottom
*= GuiData
->CharHeight
;
383 if (IntersectRect(&rc
, &Ps
.rcPaint
, &rc
))
385 PatBlt(Dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, DSTINVERT
);
389 EndPaint (hWnd
, &Ps
);
390 LeaveCriticalSection(&GuiData
->Lock
);
394 Dc
= BeginPaint (hWnd
, &Ps
);
395 EndPaint (hWnd
, &Ps
);
400 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
402 PCSRSS_CONSOLE Console
;
403 PGUI_CONSOLE_DATA GuiData
;
406 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
408 Message
.message
= msg
;
409 Message
.wParam
= wParam
;
410 Message
.lParam
= lParam
;
412 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
414 /* clear the selection */
415 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
418 ConioProcessKey(&Message
, Console
, FALSE
);
422 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
426 RegionRect
.left
= Region
->left
* GuiData
->CharWidth
;
427 RegionRect
.top
= Region
->top
* GuiData
->CharHeight
;
428 RegionRect
.right
= (Region
->right
+ 1) * GuiData
->CharWidth
;
429 RegionRect
.bottom
= (Region
->bottom
+ 1) * GuiData
->CharHeight
;
431 GuiConsoleUpdateBitmap(Wnd
, RegionRect
);
435 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
437 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
439 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
444 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
448 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
457 GuiIntDrawRegion(GuiData
, Wnd
, &CellRect
);
461 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, UINT CursorStartX
, UINT CursorStartY
,
462 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
464 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
465 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
466 LONG CursorEndX
, CursorEndY
;
469 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
474 if (0 != ScrolledLines
)
477 Source
.top
= ScrolledLines
;
478 Source
.right
= Console
->Size
.X
- 1;
479 Source
.bottom
= ScrolledLines
+ Region
->top
- 1;
482 Dest
.right
= Console
->Size
.X
- 1;
483 Dest
.bottom
= Region
->top
- 1;
485 GuiConsoleCopyRegion(Console
->hWindow
, &Source
, &Dest
);
488 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
490 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
491 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
493 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
496 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
497 &CursorEndX
, &CursorEndY
);
498 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
499 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
500 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
502 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
507 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
511 if (Console
->ActiveBuffer
== Buff
)
513 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
514 &UpdateRect
.left
, &UpdateRect
.top
);
515 UpdateRect
.right
= UpdateRect
.left
;
516 UpdateRect
.bottom
= UpdateRect
.top
;
517 ConioDrawRegion(Console
, &UpdateRect
);
524 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
528 if (Console
->ActiveBuffer
== Buff
)
530 /* Redraw char at old position (removes cursor) */
531 UpdateRect
.left
= OldCursorX
;
532 UpdateRect
.top
= OldCursorY
;
533 UpdateRect
.right
= OldCursorX
;
534 UpdateRect
.bottom
= OldCursorY
;
535 ConioDrawRegion(Console
, &UpdateRect
);
536 /* Redraw char at new position (shows cursor) */
537 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
538 &(UpdateRect
.left
), &(UpdateRect
.top
));
539 UpdateRect
.right
= UpdateRect
.left
;
540 UpdateRect
.bottom
= UpdateRect
.top
;
541 ConioDrawRegion(Console
, &UpdateRect
);
548 GuiConsoleHandleTimer(HWND hWnd
)
550 PCSRSS_CONSOLE Console
;
551 PGUI_CONSOLE_DATA GuiData
;
553 ULONG CursorX
, CursorY
;
555 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
556 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
558 GuiConsoleGetLogicalCursorPos(Console
->ActiveBuffer
, &CursorX
, &CursorY
);
559 CursorRect
.left
= CursorX
;
560 CursorRect
.top
= CursorY
;
561 CursorRect
.right
= CursorX
;
562 CursorRect
.bottom
= CursorY
;
563 GuiDrawRegion(Console
, &CursorRect
);
567 GuiConsoleHandleClose(HWND hWnd
)
569 PCSRSS_CONSOLE Console
;
570 PGUI_CONSOLE_DATA GuiData
;
571 PLIST_ENTRY current_entry
;
572 PCSRSS_PROCESS_DATA current
;
574 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
576 EnterCriticalSection(&Console
->Header
.Lock
);
578 current_entry
= Console
->ProcessList
.Flink
;
579 while (current_entry
!= &Console
->ProcessList
)
581 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
582 current_entry
= current_entry
->Flink
;
584 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
587 LeaveCriticalSection(&Console
->Header
.Lock
);
591 GuiConsoleHandleNcDestroy(HWND hWnd
)
593 PCSRSS_CONSOLE Console
;
594 PGUI_CONSOLE_DATA GuiData
;
596 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
598 Console
->PrivateData
= NULL
;
599 DeleteDC(GuiData
->MemoryDC
);
600 DeleteCriticalSection(&GuiData
->Lock
);
601 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
605 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
607 PCSRSS_CONSOLE Console
;
608 PGUI_CONSOLE_DATA GuiData
;
612 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
613 if (Console
== NULL
|| GuiData
== NULL
) return;
615 pt
= MAKEPOINTS(lParam
);
617 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
618 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
619 rc
.right
= rc
.left
+ 1;
620 rc
.bottom
= rc
.top
+ 1;
622 GuiData
->SelectionStart
.x
= rc
.left
;
623 GuiData
->SelectionStart
.y
= rc
.top
;
627 GuiData
->MouseDown
= TRUE
;
629 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
633 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
635 PCSRSS_CONSOLE Console
;
636 PGUI_CONSOLE_DATA GuiData
;
640 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
641 if (Console
== NULL
|| GuiData
== NULL
) return;
642 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
644 pt
= MAKEPOINTS(lParam
);
646 rc
.left
= GuiData
->SelectionStart
.x
;
647 rc
.top
= GuiData
->SelectionStart
.y
;
648 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
649 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
651 /* exchange left/top with right/bottom if required */
652 if(rc
.left
>= rc
.right
)
656 rc
.left
= max(rc
.right
- 1, 0);
659 if(rc
.top
>= rc
.bottom
)
663 rc
.top
= max(rc
.bottom
- 1, 0);
667 GuiData
->MouseDown
= FALSE
;
669 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
675 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
677 PCSRSS_CONSOLE Console
;
678 PGUI_CONSOLE_DATA GuiData
;
682 if (!(wParam
& MK_LBUTTON
)) return;
684 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
685 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
687 pt
= MAKEPOINTS(lParam
);
689 rc
.left
= GuiData
->SelectionStart
.x
;
690 rc
.top
= GuiData
->SelectionStart
.y
;
691 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
692 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
694 /* exchange left/top with right/bottom if required */
695 if(rc
.left
>= rc
.right
)
699 rc
.left
= max(rc
.right
- 1, 0);
702 if(rc
.top
>= rc
.bottom
)
706 rc
.top
= max(rc
.bottom
- 1, 0);
710 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
714 GuiConsoleRightMouseDown(HWND hWnd
)
716 PCSRSS_CONSOLE Console
;
717 PGUI_CONSOLE_DATA GuiData
;
719 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
720 if (Console
== NULL
|| GuiData
== NULL
) return;
722 if (GuiData
->Selection
.left
== -1)
724 /* FIXME - paste text from clipboard */
728 /* FIXME - copy selection to clipboard */
730 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
735 static LRESULT CALLBACK
736 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
743 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
746 GuiConsoleHandlePaint(hWnd
);
754 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
758 GuiConsoleHandleTimer(hWnd
);
762 GuiConsoleHandleClose(hWnd
);
766 GuiConsoleHandleNcDestroy(hWnd
);
770 GuiConsoleLeftMouseDown(hWnd
, lParam
);
773 GuiConsoleLeftMouseUp(hWnd
, lParam
);
776 GuiConsoleRightMouseDown(hWnd
);
779 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
782 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
789 static LRESULT CALLBACK
790 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
794 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
799 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
801 case PM_CREATE_CONSOLE
:
802 NewWindow
= CreateWindowW(L
"Win32CsrConsole",
803 Console
->Title
.Buffer
,
804 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
,
811 (HINSTANCE
) GetModuleHandleW(NULL
),
813 Console
->hWindow
= NewWindow
;
814 if (NULL
!= NewWindow
)
816 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
817 ShowWindow(NewWindow
, SW_SHOW
);
819 return (LRESULT
) NewWindow
;
820 case PM_DESTROY_CONSOLE
:
821 DestroyWindow(Console
->hWindow
);
822 Console
->hWindow
= NULL
;
823 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
825 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
826 if (0 == WindowCount
)
830 PrivateCsrssManualGuiCheck(-1);
835 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
840 GuiConsoleGuiThread(PVOID Data
)
843 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
845 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
854 (HINSTANCE
) GetModuleHandleW(NULL
),
856 if (NULL
== NotifyWnd
)
858 PrivateCsrssManualGuiCheck(-1);
859 SetEvent(*GraphicsStartupEvent
);
863 SetEvent(*GraphicsStartupEvent
);
865 while(GetMessageW(&msg
, NULL
, 0, 0))
867 TranslateMessage(&msg
);
868 DispatchMessageW(&msg
);
879 if (NULL
== NotifyWnd
)
881 PrivateCsrssManualGuiCheck(+1);
884 wc
.cbSize
= sizeof(WNDCLASSEXW
);
885 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
886 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
888 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
891 wc
.hbrBackground
= NULL
;
892 wc
.lpszMenuName
= NULL
;
896 if (RegisterClassExW(&wc
) == 0)
898 DPRINT1("Failed to register notify wndproc\n");
902 wc
.cbSize
= sizeof(WNDCLASSEXW
);
903 wc
.lpszClassName
= L
"Win32CsrConsole";
904 wc
.lpfnWndProc
= GuiConsoleWndProc
;
906 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
907 wc
.hIcon
= LoadIconW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1));
908 wc
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_ARROW
));
909 wc
.hbrBackground
= NULL
;
910 wc
.lpszMenuName
= NULL
;
913 wc
.hIconSm
= LoadImageW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1), IMAGE_ICON
,
914 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
916 if (RegisterClassExW(&wc
) == 0)
918 DPRINT1("Failed to register console wndproc\n");
926 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
928 Buffer
->DefaultAttrib
= 0x0f;
932 GuiChangeTitle(PCSRSS_CONSOLE Console
)
934 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Console
->Title
.Buffer
);
940 GuiChangeIcon(PCSRSS_CONSOLE Console
)
942 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)Console
->hWindowIcon
);
943 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)Console
->hWindowIcon
);
949 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
951 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
954 static CSRSS_CONSOLE_VTBL GuiVtbl
=
967 GuiInitConsole(PCSRSS_CONSOLE Console
)
969 HANDLE GraphicsStartupEvent
;
978 return STATUS_UNSUCCESSFUL
;
982 Console
->Vtbl
= &GuiVtbl
;
983 Console
->Size
.X
= 80;
984 Console
->Size
.Y
= 25;
985 if (NULL
== NotifyWnd
)
987 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
988 if (NULL
== GraphicsStartupEvent
)
990 return STATUS_UNSUCCESSFUL
;
993 ThreadHandle
= CreateThread(NULL
,
996 (PVOID
) &GraphicsStartupEvent
,
999 if (NULL
== ThreadHandle
)
1001 NtClose(GraphicsStartupEvent
);
1002 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1003 return STATUS_UNSUCCESSFUL
;
1005 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
1006 CloseHandle(ThreadHandle
);
1008 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
1009 CloseHandle(GraphicsStartupEvent
);
1011 if (NULL
== NotifyWnd
)
1013 DPRINT1("Win32Csr: Failed to create notification window.\n");
1014 return STATUS_UNSUCCESSFUL
;
1018 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
1020 return STATUS_SUCCESS
;
1024 GuiConsoleCopyRegion(HWND hWnd
,
1029 PGUI_CONSOLE_DATA GuiData
;
1030 PCSRSS_CONSOLE Console
;
1033 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1035 ScrollRect
.left
= Dest
->left
* GuiData
->CharWidth
;
1036 ScrollRect
.right
= (Dest
->right
+ 1) * GuiData
->CharWidth
;
1037 ScrollRect
.top
= Dest
->top
* GuiData
->CharHeight
;
1038 ScrollRect
.bottom
= (Dest
->bottom
+ 1) * GuiData
->CharHeight
;
1039 EnterCriticalSection(&GuiData
->Lock
);
1040 BitBlt(GuiData
->MemoryDC
, ScrollRect
.left
, ScrollRect
.top
,
1041 ScrollRect
.right
- ScrollRect
.left
, ScrollRect
.bottom
- ScrollRect
.top
,
1042 GuiData
->MemoryDC
, Source
->left
* GuiData
->CharWidth
, Source
->top
* GuiData
->CharHeight
, SRCCOPY
);
1044 LeaveCriticalSection(&GuiData
->Lock
);
1046 InvalidateRect(hWnd
, &ScrollRect
, FALSE
);