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 /* NOTE: Don't delete the "first bitmap", it's done in DeleteDC. */
133 SelectObject(GuiData
->MemoryDC
, GuiData
->MemoryBitmap
);
134 /* NOTE: Don't delete stock font. */
135 SelectObject(GuiData
->MemoryDC
, GuiData
->Font
);
138 GuiData
->CursorBlinkOn
= TRUE
;
139 GuiData
->ForceCursorOff
= FALSE
;
141 GuiData
->Selection
.left
= -1;
143 Console
->PrivateData
= GuiData
;
144 SetWindowLongPtrW(hWnd
, GWL_USERDATA
, (DWORD_PTR
) Console
);
146 GetWindowRect(hWnd
, &Rect
);
147 Rect
.right
= Rect
.left
+ Console
->Size
.X
* GuiData
->CharWidth
+
148 2 * GetSystemMetrics(SM_CXFIXEDFRAME
);
149 Rect
.bottom
= Rect
.top
+ Console
->Size
.Y
* GuiData
->CharHeight
+
150 2 * GetSystemMetrics(SM_CYFIXEDFRAME
) + GetSystemMetrics(SM_CYCAPTION
);
151 MoveWindow(hWnd
, Rect
.left
, Rect
.top
, Rect
.right
- Rect
.left
,
152 Rect
.bottom
- Rect
.top
, FALSE
);
154 SetTimer(hWnd
, 1, CURSOR_BLINK_TIME
, NULL
);
156 return (BOOL
) DefWindowProcW(hWnd
, WM_NCCREATE
, 0, (LPARAM
) Create
);
159 static COLORREF FASTCALL
160 GuiConsoleRGBFromAttribute(BYTE Attribute
)
162 int Red
= (Attribute
& 0x04 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
163 int Green
= (Attribute
& 0x02 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
164 int Blue
= (Attribute
& 0x01 ? (Attribute
& 0x08 ? 0xff : 0x80) : 0x00);
166 return RGB(Red
, Green
, Blue
);
170 GuiConsoleSetTextColors(HDC Dc
, BYTE Attribute
)
172 SetTextColor(Dc
, GuiConsoleRGBFromAttribute(Attribute
& 0x0f));
173 SetBkColor(Dc
, GuiConsoleRGBFromAttribute((Attribute
& 0xf0) >> 4));
177 GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff
, ULONG
*CursorX
, ULONG
*CursorY
)
179 *CursorX
= Buff
->CurrentX
;
180 if (Buff
->CurrentY
< Buff
->ShowY
)
182 *CursorY
= Buff
->MaxY
- Buff
->ShowY
+ Buff
->CurrentY
;
186 *CursorY
= Buff
->CurrentY
- Buff
->ShowY
;
192 GuiConsoleUpdateSelection(HWND hWnd
, PRECT rc
, PGUI_CONSOLE_DATA GuiData
)
194 RECT oldRect
= GuiData
->Selection
;
198 RECT changeRect
= *rc
;
200 GuiData
->Selection
= *rc
;
202 changeRect
.left
*= GuiData
->CharWidth
;
203 changeRect
.top
*= GuiData
->CharHeight
;
204 changeRect
.right
*= GuiData
->CharWidth
;
205 changeRect
.bottom
*= GuiData
->CharHeight
;
207 if(rc
->left
!= oldRect
.left
||
208 rc
->top
!= oldRect
.top
||
209 rc
->right
!= oldRect
.right
||
210 rc
->bottom
!= oldRect
.bottom
)
212 if(oldRect
.left
!= -1)
216 oldRect
.left
*= GuiData
->CharWidth
;
217 oldRect
.top
*= GuiData
->CharHeight
;
218 oldRect
.right
*= GuiData
->CharWidth
;
219 oldRect
.bottom
*= GuiData
->CharHeight
;
221 /* calculate the region that needs to be updated */
222 if((rgn1
= CreateRectRgnIndirect(&oldRect
)))
224 if((rgn2
= CreateRectRgnIndirect(&changeRect
)))
226 if(CombineRgn(rgn1
, rgn2
, rgn1
, RGN_XOR
) != ERROR
)
228 InvalidateRgn(hWnd
, rgn1
, FALSE
);
238 InvalidateRect(hWnd
, &changeRect
, FALSE
);
242 else if(oldRect
.left
!= -1)
244 /* clear the selection */
245 GuiData
->Selection
.left
= -1;
246 oldRect
.left
*= GuiData
->CharWidth
;
247 oldRect
.top
*= GuiData
->CharHeight
;
248 oldRect
.right
*= GuiData
->CharWidth
;
249 oldRect
.bottom
*= GuiData
->CharHeight
;
250 InvalidateRect(hWnd
, &oldRect
, FALSE
);
256 GuiConsoleUpdateBitmap(HWND hWnd
, RECT rc
)
258 PCSRSS_CONSOLE Console
;
259 PGUI_CONSOLE_DATA GuiData
;
260 PCSRSS_SCREEN_BUFFER Buff
;
262 ULONG TopLine
, BottomLine
, LeftChar
, RightChar
;
263 ULONG Line
, Char
, Start
;
266 BYTE LastAttribute
, Attribute
;
267 ULONG CursorX
, CursorY
, CursorHeight
;
268 HBRUSH CursorBrush
, OldBrush
;
270 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
271 if (NULL
!= Console
&& NULL
!= GuiData
&& NULL
!= Console
->ActiveBuffer
)
273 Buff
= Console
->ActiveBuffer
;
274 EnterCriticalSection(&Buff
->Header
.Lock
);
276 if (rc
.right
<= rc
.left
|| rc
.bottom
<= rc
.top
)
279 LeaveCriticalSection(&Buff
->Header
.Lock
);
283 EnterCriticalSection(&GuiData
->Lock
);
285 TopLine
= rc
.top
/ GuiData
->CharHeight
;
286 BottomLine
= (rc
.bottom
+ (GuiData
->CharHeight
- 1)) / GuiData
->CharHeight
- 1;
287 LeftChar
= rc
.left
/ GuiData
->CharWidth
;
288 RightChar
= (rc
.right
+ (GuiData
->CharWidth
- 1)) / GuiData
->CharWidth
- 1;
289 LastAttribute
= Buff
->Buffer
[(TopLine
* Buff
->MaxX
+ LeftChar
) * 2 + 1];
290 GuiConsoleSetTextColors(GuiData
->MemoryDC
, LastAttribute
);
292 for (Line
= TopLine
; Line
<= BottomLine
; Line
++)
294 if (Line
+ Buff
->ShowY
< Buff
->MaxY
)
296 From
= Buff
->Buffer
+ ((Line
+ Buff
->ShowY
) * Buff
->MaxX
+ LeftChar
) * 2;
300 From
= Buff
->Buffer
+
301 ((Line
- (Buff
->MaxY
- Buff
->ShowY
)) * Buff
->MaxX
+ LeftChar
) * 2;
304 To
= GuiData
->LineBuffer
;
305 for (Char
= LeftChar
; Char
<= RightChar
; Char
++)
307 if (*(From
+ 1) != LastAttribute
)
309 TextOutW(GuiData
->MemoryDC
, Start
* GuiData
->CharWidth
, Line
* GuiData
->CharHeight
,
310 GuiData
->LineBuffer
, Char
- Start
);
312 To
= GuiData
->LineBuffer
;
313 Attribute
= *(From
+ 1);
314 if (Attribute
!= LastAttribute
)
316 GuiConsoleSetTextColors(GuiData
->MemoryDC
, Attribute
);
317 LastAttribute
= Attribute
;
320 MultiByteToWideChar(Console
->OutputCodePage
, 0, (PCHAR
)From
, 1, To
, 1);
324 TextOutW(GuiData
->MemoryDC
, Start
* GuiData
->CharWidth
, Line
* GuiData
->CharHeight
,
325 GuiData
->LineBuffer
, RightChar
- Start
+ 1);
328 if (Buff
->CursorInfo
.bVisible
&& GuiData
->CursorBlinkOn
329 &&! GuiData
->ForceCursorOff
)
331 GuiConsoleGetLogicalCursorPos(Buff
, &CursorX
, &CursorY
);
332 if (LeftChar
<= CursorX
&& CursorX
<= RightChar
&&
333 TopLine
<= CursorY
&& CursorY
<= BottomLine
)
335 CursorHeight
= (GuiData
->CharHeight
* Buff
->CursorInfo
.dwSize
) / 100;
336 if (CursorHeight
< 1)
340 From
= Buff
->Buffer
+ (Buff
->CurrentY
* Buff
->MaxX
+ Buff
->CurrentX
) * 2 + 1;
341 CursorBrush
= CreateSolidBrush(GuiConsoleRGBFromAttribute(*From
));
342 OldBrush
= SelectObject(GuiData
->MemoryDC
, CursorBrush
);
343 PatBlt(GuiData
->MemoryDC
, CursorX
* GuiData
->CharWidth
,
344 CursorY
* GuiData
->CharHeight
+ (GuiData
->CharHeight
- CursorHeight
),
345 GuiData
->CharWidth
, CursorHeight
, PATCOPY
);
346 SelectObject(GuiData
->MemoryDC
, OldBrush
);
347 DeleteObject(CursorBrush
);
351 LeaveCriticalSection(&GuiData
->Lock
);
353 LeaveCriticalSection(&Buff
->Header
.Lock
);
354 InvalidateRect(hWnd
, &rc
, FALSE
);
359 GuiConsoleHandlePaint(HWND hWnd
)
363 PCSRSS_CONSOLE Console
;
364 PGUI_CONSOLE_DATA GuiData
;
366 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
367 if (NULL
!= Console
&& NULL
!= GuiData
)
369 EnterCriticalSection(&GuiData
->Lock
);
370 Dc
= BeginPaint (hWnd
, &Ps
);
371 BitBlt(Dc
, Ps
.rcPaint
.left
, Ps
.rcPaint
.top
,
372 Ps
.rcPaint
.right
- Ps
.rcPaint
.left
+ 1,
373 Ps
.rcPaint
.bottom
- Ps
.rcPaint
.top
+ 1, GuiData
->MemoryDC
,
374 Ps
.rcPaint
.left
, Ps
.rcPaint
.top
, SRCCOPY
);
376 if (GuiData
->Selection
.left
!= -1)
378 RECT rc
= GuiData
->Selection
;
380 rc
.left
*= GuiData
->CharWidth
;
381 rc
.top
*= GuiData
->CharHeight
;
382 rc
.right
*= GuiData
->CharWidth
;
383 rc
.bottom
*= GuiData
->CharHeight
;
385 if (IntersectRect(&rc
, &Ps
.rcPaint
, &rc
))
387 PatBlt(Dc
, rc
.left
, rc
.top
, rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, DSTINVERT
);
391 EndPaint (hWnd
, &Ps
);
392 LeaveCriticalSection(&GuiData
->Lock
);
396 Dc
= BeginPaint (hWnd
, &Ps
);
397 EndPaint (hWnd
, &Ps
);
402 GuiConsoleHandleKey(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
404 PCSRSS_CONSOLE Console
;
405 PGUI_CONSOLE_DATA GuiData
;
408 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
410 Message
.message
= msg
;
411 Message
.wParam
= wParam
;
412 Message
.lParam
= lParam
;
414 if(msg
== WM_CHAR
|| msg
== WM_SYSKEYDOWN
)
416 /* clear the selection */
417 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
420 ConioProcessKey(&Message
, Console
, FALSE
);
424 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, RECT
*Region
)
428 RegionRect
.left
= Region
->left
* GuiData
->CharWidth
;
429 RegionRect
.top
= Region
->top
* GuiData
->CharHeight
;
430 RegionRect
.right
= (Region
->right
+ 1) * GuiData
->CharWidth
;
431 RegionRect
.bottom
= (Region
->bottom
+ 1) * GuiData
->CharHeight
;
433 GuiConsoleUpdateBitmap(Wnd
, RegionRect
);
437 GuiDrawRegion(PCSRSS_CONSOLE Console
, RECT
*Region
)
439 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
441 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
446 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
450 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData
, HWND Wnd
, UINT x
, UINT y
)
459 GuiIntDrawRegion(GuiData
, Wnd
, &CellRect
);
463 GuiWriteStream(PCSRSS_CONSOLE Console
, RECT
*Region
, UINT CursorStartX
, UINT CursorStartY
,
464 UINT ScrolledLines
, CHAR
*Buffer
, UINT Length
)
466 PGUI_CONSOLE_DATA GuiData
= (PGUI_CONSOLE_DATA
) Console
->PrivateData
;
467 PCSRSS_SCREEN_BUFFER Buff
= Console
->ActiveBuffer
;
468 LONG CursorEndX
, CursorEndY
;
471 if (NULL
== Console
->hWindow
|| NULL
== GuiData
)
476 if (0 != ScrolledLines
)
479 Source
.top
= ScrolledLines
;
480 Source
.right
= Console
->Size
.X
- 1;
481 Source
.bottom
= ScrolledLines
+ Region
->top
- 1;
484 Dest
.right
= Console
->Size
.X
- 1;
485 Dest
.bottom
= Region
->top
- 1;
487 GuiConsoleCopyRegion(Console
->hWindow
, &Source
, &Dest
);
490 GuiIntDrawRegion(GuiData
, Console
->hWindow
, Region
);
492 if (CursorStartX
< Region
->left
|| Region
->right
< CursorStartX
493 || CursorStartY
< Region
->top
|| Region
->bottom
< CursorStartY
)
495 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorStartX
, CursorStartY
);
498 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
499 &CursorEndX
, &CursorEndY
);
500 if ((CursorEndX
< Region
->left
|| Region
->right
< CursorEndX
501 || CursorEndY
< Region
->top
|| Region
->bottom
< CursorEndY
)
502 && (CursorEndX
!= CursorStartX
|| CursorEndY
!= CursorStartY
))
504 GuiInvalidateCell(GuiData
, Console
->hWindow
, CursorEndX
, CursorEndY
);
509 GuiSetCursorInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
)
513 if (Console
->ActiveBuffer
== Buff
)
515 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
516 &UpdateRect
.left
, &UpdateRect
.top
);
517 UpdateRect
.right
= UpdateRect
.left
;
518 UpdateRect
.bottom
= UpdateRect
.top
;
519 ConioDrawRegion(Console
, &UpdateRect
);
526 GuiSetScreenInfo(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buff
, UINT OldCursorX
, UINT OldCursorY
)
530 if (Console
->ActiveBuffer
== Buff
)
532 /* Redraw char at old position (removes cursor) */
533 UpdateRect
.left
= OldCursorX
;
534 UpdateRect
.top
= OldCursorY
;
535 UpdateRect
.right
= OldCursorX
;
536 UpdateRect
.bottom
= OldCursorY
;
537 ConioDrawRegion(Console
, &UpdateRect
);
538 /* Redraw char at new position (shows cursor) */
539 ConioPhysicalToLogical(Buff
, Buff
->CurrentX
, Buff
->CurrentY
,
540 &(UpdateRect
.left
), &(UpdateRect
.top
));
541 UpdateRect
.right
= UpdateRect
.left
;
542 UpdateRect
.bottom
= UpdateRect
.top
;
543 ConioDrawRegion(Console
, &UpdateRect
);
550 GuiConsoleHandleTimer(HWND hWnd
)
552 PCSRSS_CONSOLE Console
;
553 PGUI_CONSOLE_DATA GuiData
;
555 ULONG CursorX
, CursorY
;
557 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
558 GuiData
->CursorBlinkOn
= ! GuiData
->CursorBlinkOn
;
560 GuiConsoleGetLogicalCursorPos(Console
->ActiveBuffer
, &CursorX
, &CursorY
);
561 CursorRect
.left
= CursorX
;
562 CursorRect
.top
= CursorY
;
563 CursorRect
.right
= CursorX
;
564 CursorRect
.bottom
= CursorY
;
565 GuiDrawRegion(Console
, &CursorRect
);
569 GuiConsoleHandleClose(HWND hWnd
)
571 PCSRSS_CONSOLE Console
;
572 PGUI_CONSOLE_DATA GuiData
;
573 PLIST_ENTRY current_entry
;
574 PCSRSS_PROCESS_DATA current
;
576 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
578 EnterCriticalSection(&Console
->Header
.Lock
);
580 current_entry
= Console
->ProcessList
.Flink
;
581 while (current_entry
!= &Console
->ProcessList
)
583 current
= CONTAINING_RECORD(current_entry
, CSRSS_PROCESS_DATA
, ProcessEntry
);
584 current_entry
= current_entry
->Flink
;
586 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT
, current
);
589 LeaveCriticalSection(&Console
->Header
.Lock
);
593 GuiConsoleHandleNcDestroy(HWND hWnd
)
595 PCSRSS_CONSOLE Console
;
596 PGUI_CONSOLE_DATA GuiData
;
598 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
600 Console
->PrivateData
= NULL
;
601 DeleteDC(GuiData
->MemoryDC
);
602 DeleteCriticalSection(&GuiData
->Lock
);
603 HeapFree(Win32CsrApiHeap
, 0, GuiData
);
607 GuiConsoleLeftMouseDown(HWND hWnd
, LPARAM lParam
)
609 PCSRSS_CONSOLE Console
;
610 PGUI_CONSOLE_DATA GuiData
;
614 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
615 if (Console
== NULL
|| GuiData
== NULL
) return;
617 pt
= MAKEPOINTS(lParam
);
619 rc
.left
= pt
.x
/ GuiData
->CharWidth
;
620 rc
.top
= pt
.y
/ GuiData
->CharHeight
;
621 rc
.right
= rc
.left
+ 1;
622 rc
.bottom
= rc
.top
+ 1;
624 GuiData
->SelectionStart
.x
= rc
.left
;
625 GuiData
->SelectionStart
.y
= rc
.top
;
629 GuiData
->MouseDown
= TRUE
;
631 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
635 GuiConsoleLeftMouseUp(HWND hWnd
, LPARAM lParam
)
637 PCSRSS_CONSOLE Console
;
638 PGUI_CONSOLE_DATA GuiData
;
642 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
643 if (Console
== NULL
|| GuiData
== NULL
) return;
644 if (GuiData
->Selection
.left
== -1 || !GuiData
->MouseDown
) return;
646 pt
= MAKEPOINTS(lParam
);
648 rc
.left
= GuiData
->SelectionStart
.x
;
649 rc
.top
= GuiData
->SelectionStart
.y
;
650 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
651 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
653 /* exchange left/top with right/bottom if required */
654 if(rc
.left
>= rc
.right
)
658 rc
.left
= max(rc
.right
- 1, 0);
661 if(rc
.top
>= rc
.bottom
)
665 rc
.top
= max(rc
.bottom
- 1, 0);
669 GuiData
->MouseDown
= FALSE
;
671 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
677 GuiConsoleMouseMove(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
679 PCSRSS_CONSOLE Console
;
680 PGUI_CONSOLE_DATA GuiData
;
684 if (!(wParam
& MK_LBUTTON
)) return;
686 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
687 if (Console
== NULL
|| GuiData
== NULL
|| !GuiData
->MouseDown
) return;
689 pt
= MAKEPOINTS(lParam
);
691 rc
.left
= GuiData
->SelectionStart
.x
;
692 rc
.top
= GuiData
->SelectionStart
.y
;
693 rc
.right
= (pt
.x
>= 0 ? (pt
.x
/ GuiData
->CharWidth
) + 1 : 0);
694 rc
.bottom
= (pt
.y
>= 0 ? (pt
.y
/ GuiData
->CharHeight
) + 1 : 0);
696 /* exchange left/top with right/bottom if required */
697 if(rc
.left
>= rc
.right
)
701 rc
.left
= max(rc
.right
- 1, 0);
704 if(rc
.top
>= rc
.bottom
)
708 rc
.top
= max(rc
.bottom
- 1, 0);
712 GuiConsoleUpdateSelection(hWnd
, &rc
, GuiData
);
716 GuiConsoleRightMouseDown(HWND hWnd
)
718 PCSRSS_CONSOLE Console
;
719 PGUI_CONSOLE_DATA GuiData
;
721 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
722 if (Console
== NULL
|| GuiData
== NULL
) return;
724 if (GuiData
->Selection
.left
== -1)
726 /* FIXME - paste text from clipboard */
730 /* FIXME - copy selection to clipboard */
732 GuiConsoleUpdateSelection(hWnd
, NULL
, GuiData
);
737 static LRESULT CALLBACK
738 GuiConsoleWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
745 Result
= (LRESULT
) GuiConsoleHandleNcCreate(hWnd
, (CREATESTRUCTW
*) lParam
);
748 GuiConsoleHandlePaint(hWnd
);
756 GuiConsoleHandleKey(hWnd
, msg
, wParam
, lParam
);
760 GuiConsoleHandleTimer(hWnd
);
764 GuiConsoleHandleClose(hWnd
);
768 GuiConsoleHandleNcDestroy(hWnd
);
772 GuiConsoleLeftMouseDown(hWnd
, lParam
);
775 GuiConsoleLeftMouseUp(hWnd
, lParam
);
778 GuiConsoleRightMouseDown(hWnd
);
781 GuiConsoleMouseMove(hWnd
, wParam
, lParam
);
784 Result
= DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
791 static LRESULT CALLBACK
792 GuiConsoleNotifyWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
797 PCSRSS_CONSOLE Console
= (PCSRSS_CONSOLE
) lParam
;
802 SetWindowLongW(hWnd
, GWL_USERDATA
, 0);
804 case PM_CREATE_CONSOLE
:
805 NewWindow
= CreateWindowW(L
"Win32CsrConsole",
806 Console
->Title
.Buffer
,
807 WS_OVERLAPPED
| WS_CAPTION
| WS_SYSMENU
| WS_MINIMIZEBOX
,
814 (HINSTANCE
) GetModuleHandleW(NULL
),
816 Console
->hWindow
= NewWindow
;
817 if (NULL
!= NewWindow
)
819 SetWindowLongW(hWnd
, GWL_USERDATA
, GetWindowLongW(hWnd
, GWL_USERDATA
) + 1);
820 ShowWindow(NewWindow
, SW_SHOW
);
822 return (LRESULT
) NewWindow
;
823 case PM_DESTROY_CONSOLE
:
824 /* Window creation is done using a PostMessage(), so it's possible that the
825 * window that we want to destroy doesn't exist yet. So first empty the message
827 while(PeekMessageW(&Msg
, NULL
, 0, 0, PM_REMOVE
))
829 TranslateMessage(&Msg
);
830 DispatchMessageW(&Msg
);
832 DestroyWindow(Console
->hWindow
);
833 Console
->hWindow
= NULL
;
834 WindowCount
= GetWindowLongW(hWnd
, GWL_USERDATA
);
836 SetWindowLongW(hWnd
, GWL_USERDATA
, WindowCount
);
837 if (0 == WindowCount
)
841 PrivateCsrssManualGuiCheck(-1);
846 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
851 GuiConsoleGuiThread(PVOID Data
)
854 PHANDLE GraphicsStartupEvent
= (PHANDLE
) Data
;
856 NotifyWnd
= CreateWindowW(L
"Win32CsrCreateNotify",
865 (HINSTANCE
) GetModuleHandleW(NULL
),
867 if (NULL
== NotifyWnd
)
869 PrivateCsrssManualGuiCheck(-1);
870 SetEvent(*GraphicsStartupEvent
);
874 SetEvent(*GraphicsStartupEvent
);
876 while(GetMessageW(&msg
, NULL
, 0, 0))
878 TranslateMessage(&msg
);
879 DispatchMessageW(&msg
);
890 if (NULL
== NotifyWnd
)
892 PrivateCsrssManualGuiCheck(+1);
895 wc
.cbSize
= sizeof(WNDCLASSEXW
);
896 wc
.lpszClassName
= L
"Win32CsrCreateNotify";
897 wc
.lpfnWndProc
= GuiConsoleNotifyWndProc
;
899 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
902 wc
.hbrBackground
= NULL
;
903 wc
.lpszMenuName
= NULL
;
907 if (RegisterClassExW(&wc
) == 0)
909 DPRINT1("Failed to register notify wndproc\n");
913 wc
.cbSize
= sizeof(WNDCLASSEXW
);
914 wc
.lpszClassName
= L
"Win32CsrConsole";
915 wc
.lpfnWndProc
= GuiConsoleWndProc
;
917 wc
.hInstance
= (HINSTANCE
) GetModuleHandleW(NULL
);
918 wc
.hIcon
= LoadIconW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1));
919 wc
.hCursor
= LoadCursorW(NULL
, MAKEINTRESOURCEW(IDC_ARROW
));
920 wc
.hbrBackground
= NULL
;
921 wc
.lpszMenuName
= NULL
;
924 wc
.hIconSm
= LoadImageW(Win32CsrDllHandle
, MAKEINTRESOURCEW(1), IMAGE_ICON
,
925 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
),
927 if (RegisterClassExW(&wc
) == 0)
929 DPRINT1("Failed to register console wndproc\n");
937 GuiInitScreenBuffer(PCSRSS_CONSOLE Console
, PCSRSS_SCREEN_BUFFER Buffer
)
939 Buffer
->DefaultAttrib
= 0x0f;
943 GuiChangeTitle(PCSRSS_CONSOLE Console
)
945 SendMessageW(Console
->hWindow
, WM_SETTEXT
, 0, (LPARAM
) Console
->Title
.Buffer
);
951 GuiChangeIcon(PCSRSS_CONSOLE Console
)
953 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_BIG
, (LPARAM
)Console
->hWindowIcon
);
954 SendMessageW(Console
->hWindow
, WM_SETICON
, ICON_SMALL
, (LPARAM
)Console
->hWindowIcon
);
960 GuiCleanupConsole(PCSRSS_CONSOLE Console
)
962 SendMessageW(NotifyWnd
, PM_DESTROY_CONSOLE
, 0, (LPARAM
) Console
);
965 static CSRSS_CONSOLE_VTBL GuiVtbl
=
978 GuiInitConsole(PCSRSS_CONSOLE Console
)
980 HANDLE GraphicsStartupEvent
;
989 return STATUS_UNSUCCESSFUL
;
993 Console
->Vtbl
= &GuiVtbl
;
994 Console
->Size
.X
= 80;
995 Console
->Size
.Y
= 25;
996 if (NULL
== NotifyWnd
)
998 GraphicsStartupEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
999 if (NULL
== GraphicsStartupEvent
)
1001 return STATUS_UNSUCCESSFUL
;
1004 ThreadHandle
= CreateThread(NULL
,
1006 GuiConsoleGuiThread
,
1007 (PVOID
) &GraphicsStartupEvent
,
1010 if (NULL
== ThreadHandle
)
1012 NtClose(GraphicsStartupEvent
);
1013 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1014 return STATUS_UNSUCCESSFUL
;
1016 SetThreadPriority(ThreadHandle
, THREAD_PRIORITY_HIGHEST
);
1017 CloseHandle(ThreadHandle
);
1019 WaitForSingleObject(GraphicsStartupEvent
, INFINITE
);
1020 CloseHandle(GraphicsStartupEvent
);
1022 if (NULL
== NotifyWnd
)
1024 DPRINT1("Win32Csr: Failed to create notification window.\n");
1025 return STATUS_UNSUCCESSFUL
;
1029 PostMessageW(NotifyWnd
, PM_CREATE_CONSOLE
, 0, (LPARAM
) Console
);
1031 return STATUS_SUCCESS
;
1035 GuiConsoleCopyRegion(HWND hWnd
,
1040 PGUI_CONSOLE_DATA GuiData
;
1041 PCSRSS_CONSOLE Console
;
1044 GuiConsoleGetDataPointers(hWnd
, &Console
, &GuiData
);
1046 ScrollRect
.left
= Dest
->left
* GuiData
->CharWidth
;
1047 ScrollRect
.right
= (Dest
->right
+ 1) * GuiData
->CharWidth
;
1048 ScrollRect
.top
= Dest
->top
* GuiData
->CharHeight
;
1049 ScrollRect
.bottom
= (Dest
->bottom
+ 1) * GuiData
->CharHeight
;
1050 EnterCriticalSection(&GuiData
->Lock
);
1051 BitBlt(GuiData
->MemoryDC
, ScrollRect
.left
, ScrollRect
.top
,
1052 ScrollRect
.right
- ScrollRect
.left
, ScrollRect
.bottom
- ScrollRect
.top
,
1053 GuiData
->MemoryDC
, Source
->left
* GuiData
->CharWidth
, Source
->top
* GuiData
->CharHeight
, SRCCOPY
);
1055 LeaveCriticalSection(&GuiData
->Lock
);
1057 InvalidateRect(hWnd
, &ScrollRect
, FALSE
);