also scroll the selection
[reactos.git] / reactos / subsys / csrss / win32csr / guiconsole.c
1 /* $Id$
2 *
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
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "w32csr.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* Not defined in any header file */
17 extern VOID STDCALL PrivateCsrssManualGuiCheck(LONG Check);
18
19 /* GLOBALS *******************************************************************/
20
21 typedef struct GUI_CONSOLE_DATA_TAG
22 {
23 HFONT Font;
24 unsigned CharWidth;
25 unsigned CharHeight;
26 PWCHAR LineBuffer;
27 BOOL CursorBlinkOn;
28 BOOL ForceCursorOff;
29 CRITICAL_SECTION Lock;
30 RECT Selection;
31 POINT SelectionStart;
32 BOOL MouseDown;
33 } GUI_CONSOLE_DATA, *PGUI_CONSOLE_DATA;
34
35 #ifndef WM_APP
36 #define WM_APP 0x8000
37 #endif
38 #define PM_CREATE_CONSOLE (WM_APP + 1)
39 #define PM_DESTROY_CONSOLE (WM_APP + 2)
40
41 #define CURSOR_BLINK_TIME 500
42
43 static BOOL ConsInitialized = FALSE;
44 static HWND NotifyWnd;
45
46 /* FUNCTIONS *****************************************************************/
47
48 static VOID FASTCALL
49 GuiConsoleGetDataPointers(HWND hWnd, PCSRSS_CONSOLE *Console, PGUI_CONSOLE_DATA *GuiData)
50 {
51 *Console = (PCSRSS_CONSOLE) GetWindowLongPtrW(hWnd, GWL_USERDATA);
52 *GuiData = (NULL == *Console ? NULL : (*Console)->PrivateData);
53 }
54
55 static BOOL FASTCALL
56 GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
57 {
58 RECT Rect;
59 PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) Create->lpCreateParams;
60 PGUI_CONSOLE_DATA GuiData;
61 HDC Dc;
62 HFONT OldFont;
63 TEXTMETRICW Metrics;
64
65 GuiData = HeapAlloc(Win32CsrApiHeap, HEAP_ZERO_MEMORY,
66 sizeof(GUI_CONSOLE_DATA) +
67 (Console->Size.X + 1) * sizeof(WCHAR));
68 if (NULL == GuiData)
69 {
70 DPRINT1("GuiConsoleNcCreate: HeapAlloc failed\n");
71 return FALSE;
72 }
73
74 InitializeCriticalSection(&GuiData->Lock);
75
76 GuiData->LineBuffer = (PWCHAR)(GuiData + 1);
77
78 GuiData->Font = CreateFontW(12, 0, 0, TA_BASELINE, FW_NORMAL,
79 FALSE, FALSE, FALSE, OEM_CHARSET,
80 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
81 NONANTIALIASED_QUALITY, FIXED_PITCH | FF_DONTCARE,
82 L"Bitstream Vera Sans Mono");
83 if (NULL == GuiData->Font)
84 {
85 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
86 DeleteCriticalSection(&GuiData->Lock);
87 HeapFree(Win32CsrApiHeap, 0, GuiData);
88 return FALSE;
89 }
90 Dc = GetDC(hWnd);
91 if (NULL == Dc)
92 {
93 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
94 DeleteObject(GuiData->Font);
95 DeleteCriticalSection(&GuiData->Lock);
96 HeapFree(Win32CsrApiHeap, 0, GuiData);
97 return FALSE;
98 }
99 OldFont = SelectObject(Dc, GuiData->Font);
100 if (NULL == OldFont)
101 {
102 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
103 ReleaseDC(hWnd, Dc);
104 DeleteObject(GuiData->Font);
105 DeleteCriticalSection(&GuiData->Lock);
106 HeapFree(Win32CsrApiHeap, 0, GuiData);
107 return FALSE;
108 }
109 if (! GetTextMetricsW(Dc, &Metrics))
110 {
111 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
112 SelectObject(Dc, OldFont);
113 ReleaseDC(hWnd, Dc);
114 DeleteObject(GuiData->Font);
115 DeleteCriticalSection(&GuiData->Lock);
116 HeapFree(Win32CsrApiHeap, 0, GuiData);
117 return FALSE;
118 }
119 GuiData->CharWidth = Metrics.tmMaxCharWidth;
120 GuiData->CharHeight = Metrics.tmHeight + Metrics.tmExternalLeading;
121 SelectObject(Dc, OldFont);
122
123 ReleaseDC(hWnd, Dc);
124 GuiData->CursorBlinkOn = TRUE;
125 GuiData->ForceCursorOff = FALSE;
126
127 GuiData->Selection.left = -1;
128
129 Console->PrivateData = GuiData;
130 SetWindowLongPtrW(hWnd, GWL_USERDATA, (DWORD_PTR) Console);
131
132 GetWindowRect(hWnd, &Rect);
133 Rect.right = Rect.left + Console->Size.X * GuiData->CharWidth +
134 2 * GetSystemMetrics(SM_CXFIXEDFRAME);
135 Rect.bottom = Rect.top + Console->Size.Y * GuiData->CharHeight +
136 2 * GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION);
137 MoveWindow(hWnd, Rect.left, Rect.top, Rect.right - Rect.left,
138 Rect.bottom - Rect.top, FALSE);
139
140 SetTimer(hWnd, 1, CURSOR_BLINK_TIME, NULL);
141
142 return (BOOL) DefWindowProcW(hWnd, WM_NCCREATE, 0, (LPARAM) Create);
143 }
144
145 static COLORREF FASTCALL
146 GuiConsoleRGBFromAttribute(BYTE Attribute)
147 {
148 int Red = (Attribute & 0x04 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
149 int Green = (Attribute & 0x02 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
150 int Blue = (Attribute & 0x01 ? (Attribute & 0x08 ? 0xff : 0x80) : 0x00);
151
152 return RGB(Red, Green, Blue);
153 }
154
155 static VOID FASTCALL
156 GuiConsoleSetTextColors(HDC Dc, BYTE Attribute)
157 {
158 SetTextColor(Dc, GuiConsoleRGBFromAttribute(Attribute & 0x0f));
159 SetBkColor(Dc, GuiConsoleRGBFromAttribute((Attribute & 0xf0) >> 4));
160 }
161
162 static VOID FASTCALL
163 GuiConsoleGetLogicalCursorPos(PCSRSS_SCREEN_BUFFER Buff, ULONG *CursorX, ULONG *CursorY)
164 {
165 *CursorX = Buff->CurrentX;
166 if (Buff->CurrentY < Buff->ShowY)
167 {
168 *CursorY = Buff->MaxY - Buff->ShowY + Buff->CurrentY;
169 }
170 else
171 {
172 *CursorY = Buff->CurrentY - Buff->ShowY;
173 }
174 }
175
176
177 static VOID FASTCALL
178 GuiConsoleUpdateSelection(HWND hWnd, PRECT rc, PGUI_CONSOLE_DATA GuiData)
179 {
180 RECT oldRect = GuiData->Selection;
181
182 if(rc != NULL)
183 {
184 RECT changeRect = *rc;
185
186 GuiData->Selection = *rc;
187
188 changeRect.left *= GuiData->CharWidth;
189 changeRect.top *= GuiData->CharHeight;
190 changeRect.right *= GuiData->CharWidth;
191 changeRect.bottom *= GuiData->CharHeight;
192
193 if(rc->left != oldRect.left ||
194 rc->top != oldRect.top ||
195 rc->right != oldRect.right ||
196 rc->bottom != oldRect.bottom)
197 {
198 if(oldRect.left != -1)
199 {
200 HRGN rgn1, rgn2;
201
202 oldRect.left *= GuiData->CharWidth;
203 oldRect.top *= GuiData->CharHeight;
204 oldRect.right *= GuiData->CharWidth;
205 oldRect.bottom *= GuiData->CharHeight;
206
207 /* calculate the region that needs to be updated */
208 if((rgn1 = CreateRectRgnIndirect(&oldRect)))
209 {
210 if((rgn2 = CreateRectRgnIndirect(&changeRect)))
211 {
212 if(CombineRgn(rgn1, rgn2, rgn1, RGN_XOR) != ERROR)
213 {
214 InvalidateRgn(hWnd, rgn1, FALSE);
215 }
216
217 DeleteObject(rgn2);
218 }
219 DeleteObject(rgn1);
220 }
221 }
222 else
223 {
224 InvalidateRect(hWnd, &changeRect, FALSE);
225 }
226 }
227 }
228 else if(oldRect.left != -1)
229 {
230 /* clear the selection */
231 GuiData->Selection.left = -1;
232 oldRect.left *= GuiData->CharWidth;
233 oldRect.top *= GuiData->CharHeight;
234 oldRect.right *= GuiData->CharWidth;
235 oldRect.bottom *= GuiData->CharHeight;
236 InvalidateRect(hWnd, &oldRect, FALSE);
237 }
238 }
239
240
241 static VOID FASTCALL
242 GuiConsolePaint(PCSRSS_CONSOLE Console,
243 PGUI_CONSOLE_DATA GuiData,
244 HDC hDC,
245 PRECT rc)
246 {
247 PCSRSS_SCREEN_BUFFER Buff;
248 ULONG TopLine, BottomLine, LeftChar, RightChar;
249 ULONG Line, Char, Start;
250 PBYTE From;
251 PWCHAR To;
252 BYTE LastAttribute, Attribute;
253 ULONG CursorX, CursorY, CursorHeight;
254 HBRUSH CursorBrush, OldBrush;
255 HFONT OldFont;
256
257 Buff = Console->ActiveBuffer;
258
259 TopLine = rc->top / GuiData->CharHeight;
260 BottomLine = (rc->bottom + (GuiData->CharHeight - 1)) / GuiData->CharHeight - 1;
261 LeftChar = rc->left / GuiData->CharWidth;
262 RightChar = (rc->right + (GuiData->CharWidth - 1)) / GuiData->CharWidth - 1;
263 LastAttribute = Buff->Buffer[(TopLine * Buff->MaxX + LeftChar) * 2 + 1];
264
265 GuiConsoleSetTextColors(hDC,
266 LastAttribute);
267
268 EnterCriticalSection(&Buff->Header.Lock);
269
270 OldFont = SelectObject(hDC,
271 GuiData->Font);
272
273 for (Line = TopLine; Line <= BottomLine; Line++)
274 {
275 if (Line + Buff->ShowY < Buff->MaxY)
276 {
277 From = Buff->Buffer + ((Line + Buff->ShowY) * Buff->MaxX + LeftChar) * 2;
278 }
279 else
280 {
281 From = Buff->Buffer +
282 ((Line - (Buff->MaxY - Buff->ShowY)) * Buff->MaxX + LeftChar) * 2;
283 }
284 Start = LeftChar;
285 To = GuiData->LineBuffer;
286
287 for (Char = LeftChar; Char <= RightChar; Char++)
288 {
289 if (*(From + 1) != LastAttribute)
290 {
291 TextOutW(hDC,
292 Start * GuiData->CharWidth,
293 Line * GuiData->CharHeight,
294 GuiData->LineBuffer,
295 Char - Start);
296 Start = Char;
297 To = GuiData->LineBuffer;
298 Attribute = *(From + 1);
299 if (Attribute != LastAttribute)
300 {
301 GuiConsoleSetTextColors(hDC,
302 Attribute);
303 LastAttribute = Attribute;
304 }
305 }
306
307 MultiByteToWideChar(Console->OutputCodePage,
308 0,
309 (PCHAR)From,
310 1,
311 To,
312 1);
313 To++;
314 From += 2;
315 }
316
317 TextOutW(hDC,
318 Start * GuiData->CharWidth,
319 Line * GuiData->CharHeight,
320 GuiData->LineBuffer,
321 RightChar - Start + 1);
322 }
323
324 if (Buff->CursorInfo.bVisible && GuiData->CursorBlinkOn &&
325 !GuiData->ForceCursorOff)
326 {
327 GuiConsoleGetLogicalCursorPos(Buff,
328 &CursorX,
329 &CursorY);
330 if (LeftChar <= CursorX && CursorX <= RightChar &&
331 TopLine <= CursorY && CursorY <= BottomLine)
332 {
333 CursorHeight = (GuiData->CharHeight * Buff->CursorInfo.dwSize) / 100;
334 if (CursorHeight < 1)
335 {
336 CursorHeight = 1;
337 }
338 From = Buff->Buffer + (Buff->CurrentY * Buff->MaxX + Buff->CurrentX) * 2 + 1;
339 CursorBrush = CreateSolidBrush(GuiConsoleRGBFromAttribute(*From));
340 OldBrush = SelectObject(hDC,
341 CursorBrush);
342 PatBlt(hDC,
343 CursorX * GuiData->CharWidth,
344 CursorY * GuiData->CharHeight + (GuiData->CharHeight - CursorHeight),
345 GuiData->CharWidth,
346 CursorHeight,
347 PATCOPY);
348 SelectObject(hDC,
349 OldBrush);
350 DeleteObject(CursorBrush);
351 }
352 }
353
354 LeaveCriticalSection(&Buff->Header.Lock);
355
356 SelectObject(hDC,
357 OldFont);
358 }
359
360 static VOID FASTCALL
361 GuiConsoleHandlePaint(HWND hWnd, HDC hDCPaint)
362 {
363 RECT rcUpdate;
364 HDC hDC;
365 PAINTSTRUCT ps;
366 PCSRSS_CONSOLE Console;
367 PGUI_CONSOLE_DATA GuiData;
368
369 if (GetUpdateRect(hWnd,
370 &rcUpdate,
371 FALSE))
372 {
373 hDC = (hDCPaint != NULL ? hDCPaint : BeginPaint(hWnd,
374 &ps));
375 if (hDC != NULL)
376 {
377 GuiConsoleGetDataPointers(hWnd,
378 &Console,
379 &GuiData);
380 if (Console != NULL && GuiData != NULL &&
381 Console->ActiveBuffer != NULL)
382 {
383 EnterCriticalSection(&GuiData->Lock);
384
385 GuiConsolePaint(Console,
386 GuiData,
387 hDC,
388 &rcUpdate);
389
390 if (GuiData->Selection.left != -1)
391 {
392 RECT rc = GuiData->Selection;
393
394 rc.left *= GuiData->CharWidth;
395 rc.top *= GuiData->CharHeight;
396 rc.right *= GuiData->CharWidth;
397 rc.bottom *= GuiData->CharHeight;
398
399 /* invert the selection */
400 if (IntersectRect(&rc,
401 &rcUpdate,
402 &rc))
403 {
404 PatBlt(hDC,
405 rc.left,
406 rc.top,
407 rc.right - rc.left,
408 rc.bottom - rc.top,
409 DSTINVERT);
410 }
411 }
412
413 LeaveCriticalSection(&GuiData->Lock);
414 }
415
416 if (hDCPaint == NULL)
417 {
418 EndPaint(hWnd,
419 &ps);
420 }
421 }
422 }
423 }
424
425 static VOID FASTCALL
426 GuiConsoleHandleKey(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
427 {
428 PCSRSS_CONSOLE Console;
429 PGUI_CONSOLE_DATA GuiData;
430 MSG Message;
431
432 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
433 Message.hwnd = hWnd;
434 Message.message = msg;
435 Message.wParam = wParam;
436 Message.lParam = lParam;
437
438 if(msg == WM_CHAR || msg == WM_SYSKEYDOWN)
439 {
440 /* clear the selection */
441 GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
442 }
443
444 ConioProcessKey(&Message, Console, FALSE);
445 }
446
447 static VOID FASTCALL
448 GuiIntDrawRegion(PGUI_CONSOLE_DATA GuiData, HWND Wnd, RECT *Region)
449 {
450 RECT RegionRect;
451
452 RegionRect.left = Region->left * GuiData->CharWidth;
453 RegionRect.top = Region->top * GuiData->CharHeight;
454 RegionRect.right = (Region->right + 1) * GuiData->CharWidth;
455 RegionRect.bottom = (Region->bottom + 1) * GuiData->CharHeight;
456
457 InvalidateRect(Wnd, &RegionRect, FALSE);
458 }
459
460 static VOID STDCALL
461 GuiDrawRegion(PCSRSS_CONSOLE Console, RECT *Region)
462 {
463 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
464
465 if (NULL != Console->hWindow && NULL != GuiData)
466 {
467 GuiIntDrawRegion(GuiData, Console->hWindow, Region);
468 }
469 }
470
471 static VOID FASTCALL
472 GuiInvalidateCell(PGUI_CONSOLE_DATA GuiData, HWND Wnd, UINT x, UINT y)
473 {
474 RECT CellRect;
475
476 CellRect.left = x;
477 CellRect.top = y;
478 CellRect.right = x;
479 CellRect.bottom = y;
480
481 GuiIntDrawRegion(GuiData, Wnd, &CellRect);
482 }
483
484 static VOID STDCALL
485 GuiWriteStream(PCSRSS_CONSOLE Console, RECT *Region, LONG CursorStartX, LONG CursorStartY,
486 UINT ScrolledLines, CHAR *Buffer, UINT Length)
487 {
488 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA) Console->PrivateData;
489 PCSRSS_SCREEN_BUFFER Buff = Console->ActiveBuffer;
490 LONG CursorEndX, CursorEndY;
491 RECT ScrollRect;
492
493 if (NULL == Console->hWindow || NULL == GuiData)
494 {
495 return;
496 }
497
498 if (0 != ScrolledLines)
499 {
500 ScrollRect.left = 0;
501 ScrollRect.top = 0;
502 ScrollRect.right = Console->Size.X * GuiData->CharWidth;
503 ScrollRect.bottom = Region->top * GuiData->CharHeight;
504
505 if (GuiData->Selection.left != -1)
506 {
507 /* scroll the selection */
508 if (GuiData->Selection.top > ScrolledLines)
509 {
510 GuiData->Selection.top -= ScrolledLines;
511 GuiData->Selection.bottom -= ScrolledLines;
512 }
513 else if (GuiData->Selection.bottom < ScrolledLines)
514 {
515 GuiData->Selection.left = -1;
516 }
517 else
518 {
519 GuiData->Selection.top = 0;
520 GuiData->Selection.bottom -= ScrolledLines;
521 }
522 }
523
524 ScrollWindowEx(Console->hWindow,
525 0,
526 -(ScrolledLines * GuiData->CharHeight),
527 &ScrollRect,
528 NULL,
529 NULL,
530 NULL,
531 SW_INVALIDATE);
532 }
533
534 GuiIntDrawRegion(GuiData, Console->hWindow, Region);
535
536 if (CursorStartX < Region->left || Region->right < CursorStartX
537 || CursorStartY < Region->top || Region->bottom < CursorStartY)
538 {
539 GuiInvalidateCell(GuiData, Console->hWindow, CursorStartX, CursorStartY);
540 }
541
542 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
543 &CursorEndX, &CursorEndY);
544 if ((CursorEndX < Region->left || Region->right < CursorEndX
545 || CursorEndY < Region->top || Region->bottom < CursorEndY)
546 && (CursorEndX != CursorStartX || CursorEndY != CursorStartY))
547 {
548 GuiInvalidateCell(GuiData, Console->hWindow, CursorEndX, CursorEndY);
549 }
550 }
551
552 static BOOL STDCALL
553 GuiSetCursorInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff)
554 {
555 RECT UpdateRect;
556
557 if (Console->ActiveBuffer == Buff)
558 {
559 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
560 &UpdateRect.left, &UpdateRect.top);
561 UpdateRect.right = UpdateRect.left;
562 UpdateRect.bottom = UpdateRect.top;
563 ConioDrawRegion(Console, &UpdateRect);
564 }
565
566 return TRUE;
567 }
568
569 static BOOL STDCALL
570 GuiSetScreenInfo(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buff, UINT OldCursorX, UINT OldCursorY)
571 {
572 RECT UpdateRect;
573
574 if (Console->ActiveBuffer == Buff)
575 {
576 /* Redraw char at old position (removes cursor) */
577 UpdateRect.left = OldCursorX;
578 UpdateRect.top = OldCursorY;
579 UpdateRect.right = OldCursorX;
580 UpdateRect.bottom = OldCursorY;
581 ConioDrawRegion(Console, &UpdateRect);
582 /* Redraw char at new position (shows cursor) */
583 ConioPhysicalToLogical(Buff, Buff->CurrentX, Buff->CurrentY,
584 &(UpdateRect.left), &(UpdateRect.top));
585 UpdateRect.right = UpdateRect.left;
586 UpdateRect.bottom = UpdateRect.top;
587 ConioDrawRegion(Console, &UpdateRect);
588 }
589
590 return TRUE;
591 }
592
593 static VOID FASTCALL
594 GuiConsoleHandleTimer(HWND hWnd)
595 {
596 PCSRSS_CONSOLE Console;
597 PGUI_CONSOLE_DATA GuiData;
598 RECT CursorRect;
599 ULONG CursorX, CursorY;
600
601 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
602 GuiData->CursorBlinkOn = ! GuiData->CursorBlinkOn;
603
604 GuiConsoleGetLogicalCursorPos(Console->ActiveBuffer, &CursorX, &CursorY);
605 CursorRect.left = CursorX;
606 CursorRect.top = CursorY;
607 CursorRect.right = CursorX;
608 CursorRect.bottom = CursorY;
609 GuiDrawRegion(Console, &CursorRect);
610 }
611
612 static VOID FASTCALL
613 GuiConsoleHandleClose(HWND hWnd)
614 {
615 PCSRSS_CONSOLE Console;
616 PGUI_CONSOLE_DATA GuiData;
617 PLIST_ENTRY current_entry;
618 PCSRSS_PROCESS_DATA current;
619
620 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
621
622 EnterCriticalSection(&Console->Header.Lock);
623
624 current_entry = Console->ProcessList.Flink;
625 while (current_entry != &Console->ProcessList)
626 {
627 current = CONTAINING_RECORD(current_entry, CSRSS_PROCESS_DATA, ProcessEntry);
628 current_entry = current_entry->Flink;
629
630 ConioConsoleCtrlEvent(CTRL_CLOSE_EVENT, current);
631 }
632
633 LeaveCriticalSection(&Console->Header.Lock);
634 }
635
636 static VOID FASTCALL
637 GuiConsoleHandleNcDestroy(HWND hWnd)
638 {
639 PCSRSS_CONSOLE Console;
640 PGUI_CONSOLE_DATA GuiData;
641
642 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
643 KillTimer(hWnd, 1);
644 Console->PrivateData = NULL;
645 DeleteCriticalSection(&GuiData->Lock);
646 HeapFree(Win32CsrApiHeap, 0, GuiData);
647 }
648
649 static VOID FASTCALL
650 GuiConsoleLeftMouseDown(HWND hWnd, LPARAM lParam)
651 {
652 PCSRSS_CONSOLE Console;
653 PGUI_CONSOLE_DATA GuiData;
654 POINTS pt;
655 RECT rc;
656
657 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
658 if (Console == NULL || GuiData == NULL) return;
659
660 pt = MAKEPOINTS(lParam);
661
662 rc.left = pt.x / GuiData->CharWidth;
663 rc.top = pt.y / GuiData->CharHeight;
664 rc.right = rc.left + 1;
665 rc.bottom = rc.top + 1;
666
667 GuiData->SelectionStart.x = rc.left;
668 GuiData->SelectionStart.y = rc.top;
669
670 SetCapture(hWnd);
671
672 GuiData->MouseDown = TRUE;
673
674 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
675 }
676
677 static VOID FASTCALL
678 GuiConsoleLeftMouseUp(HWND hWnd, LPARAM lParam)
679 {
680 PCSRSS_CONSOLE Console;
681 PGUI_CONSOLE_DATA GuiData;
682 RECT rc;
683 POINTS pt;
684
685 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
686 if (Console == NULL || GuiData == NULL) return;
687 if (GuiData->Selection.left == -1 || !GuiData->MouseDown) return;
688
689 pt = MAKEPOINTS(lParam);
690
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);
695
696 /* exchange left/top with right/bottom if required */
697 if(rc.left >= rc.right)
698 {
699 LONG tmp;
700 tmp = rc.left;
701 rc.left = max(rc.right - 1, 0);
702 rc.right = tmp + 1;
703 }
704 if(rc.top >= rc.bottom)
705 {
706 LONG tmp;
707 tmp = rc.top;
708 rc.top = max(rc.bottom - 1, 0);
709 rc.bottom = tmp + 1;
710 }
711
712 GuiData->MouseDown = FALSE;
713
714 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
715
716 ReleaseCapture();
717 }
718
719 static VOID FASTCALL
720 GuiConsoleMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
721 {
722 PCSRSS_CONSOLE Console;
723 PGUI_CONSOLE_DATA GuiData;
724 RECT rc;
725 POINTS pt;
726
727 if (!(wParam & MK_LBUTTON)) return;
728
729 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
730 if (Console == NULL || GuiData == NULL || !GuiData->MouseDown) return;
731
732 pt = MAKEPOINTS(lParam);
733
734 rc.left = GuiData->SelectionStart.x;
735 rc.top = GuiData->SelectionStart.y;
736 rc.right = (pt.x >= 0 ? (pt.x / GuiData->CharWidth) + 1 : 0);
737 rc.bottom = (pt.y >= 0 ? (pt.y / GuiData->CharHeight) + 1 : 0);
738
739 /* exchange left/top with right/bottom if required */
740 if(rc.left >= rc.right)
741 {
742 LONG tmp;
743 tmp = rc.left;
744 rc.left = max(rc.right - 1, 0);
745 rc.right = tmp + 1;
746 }
747 if(rc.top >= rc.bottom)
748 {
749 LONG tmp;
750 tmp = rc.top;
751 rc.top = max(rc.bottom - 1, 0);
752 rc.bottom = tmp + 1;
753 }
754
755 GuiConsoleUpdateSelection(hWnd, &rc, GuiData);
756 }
757
758 static VOID FASTCALL
759 GuiConsoleRightMouseDown(HWND hWnd)
760 {
761 PCSRSS_CONSOLE Console;
762 PGUI_CONSOLE_DATA GuiData;
763
764 GuiConsoleGetDataPointers(hWnd, &Console, &GuiData);
765 if (Console == NULL || GuiData == NULL) return;
766
767 if (GuiData->Selection.left == -1)
768 {
769 /* FIXME - paste text from clipboard */
770 }
771 else
772 {
773 /* FIXME - copy selection to clipboard */
774
775 GuiConsoleUpdateSelection(hWnd, NULL, GuiData);
776 }
777
778 }
779
780 static LRESULT CALLBACK
781 GuiConsoleWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
782 {
783 LRESULT Result = 0;
784
785 switch(msg)
786 {
787 case WM_NCCREATE:
788 Result = (LRESULT) GuiConsoleHandleNcCreate(hWnd, (CREATESTRUCTW *) lParam);
789 break;
790 case WM_PAINT:
791 GuiConsoleHandlePaint(hWnd, (HDC)wParam);
792 break;
793 case WM_KEYDOWN:
794 case WM_KEYUP:
795 case WM_SYSKEYDOWN:
796 case WM_SYSKEYUP:
797 case WM_CHAR:
798 GuiConsoleHandleKey(hWnd, msg, wParam, lParam);
799 break;
800 case WM_TIMER:
801 GuiConsoleHandleTimer(hWnd);
802 break;
803 case WM_CLOSE:
804 GuiConsoleHandleClose(hWnd);
805 break;
806 case WM_NCDESTROY:
807 GuiConsoleHandleNcDestroy(hWnd);
808 break;
809 case WM_LBUTTONDOWN:
810 GuiConsoleLeftMouseDown(hWnd, lParam);
811 break;
812 case WM_LBUTTONUP:
813 GuiConsoleLeftMouseUp(hWnd, lParam);
814 break;
815 case WM_RBUTTONDOWN:
816 GuiConsoleRightMouseDown(hWnd);
817 break;
818 case WM_MOUSEMOVE:
819 GuiConsoleMouseMove(hWnd, wParam, lParam);
820 break;
821 default:
822 Result = DefWindowProcW(hWnd, msg, wParam, lParam);
823 break;
824 }
825
826 return Result;
827 }
828
829 static LRESULT CALLBACK
830 GuiConsoleNotifyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
831 {
832 HWND NewWindow;
833 LONG WindowCount;
834 MSG Msg;
835 PWCHAR Buffer, Title;
836 PCSRSS_CONSOLE Console = (PCSRSS_CONSOLE) lParam;
837
838 switch(msg)
839 {
840 case WM_CREATE:
841 SetWindowLongW(hWnd, GWL_USERDATA, 0);
842 return 0;
843 case PM_CREATE_CONSOLE:
844 Buffer = HeapAlloc(Win32CsrApiHeap, 0,
845 Console->Title.Length + sizeof(WCHAR));
846 if (NULL != Buffer)
847 {
848 memcpy(Buffer, Console->Title.Buffer, Console->Title.Length);
849 Buffer[Console->Title.Length / sizeof(WCHAR)] = L'\0';
850 Title = Buffer;
851 }
852 else
853 {
854 Title = L"";
855 }
856 NewWindow = CreateWindowW(L"ConsoleWindowClass",
857 Title,
858 WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
859 CW_USEDEFAULT,
860 CW_USEDEFAULT,
861 CW_USEDEFAULT,
862 CW_USEDEFAULT,
863 NULL,
864 NULL,
865 (HINSTANCE) GetModuleHandleW(NULL),
866 (PVOID) Console);
867 if (NULL != Buffer)
868 {
869 HeapFree(Win32CsrApiHeap, 0, Buffer);
870 }
871 Console->hWindow = NewWindow;
872 if (NULL != NewWindow)
873 {
874 SetWindowLongW(hWnd, GWL_USERDATA, GetWindowLongW(hWnd, GWL_USERDATA) + 1);
875 ShowWindow(NewWindow, SW_SHOW);
876 }
877 return (LRESULT) NewWindow;
878 case PM_DESTROY_CONSOLE:
879 /* Window creation is done using a PostMessage(), so it's possible that the
880 * window that we want to destroy doesn't exist yet. So first empty the message
881 * queue */
882 while(PeekMessageW(&Msg, NULL, 0, 0, PM_REMOVE))
883 {
884 TranslateMessage(&Msg);
885 DispatchMessageW(&Msg);
886 }
887 DestroyWindow(Console->hWindow);
888 Console->hWindow = NULL;
889 WindowCount = GetWindowLongW(hWnd, GWL_USERDATA);
890 WindowCount--;
891 SetWindowLongW(hWnd, GWL_USERDATA, WindowCount);
892 if (0 == WindowCount)
893 {
894 NotifyWnd = NULL;
895 DestroyWindow(hWnd);
896 PrivateCsrssManualGuiCheck(-1);
897 PostQuitMessage(0);
898 }
899 return 0;
900 default:
901 return DefWindowProcW(hWnd, msg, wParam, lParam);
902 }
903 }
904
905 static DWORD STDCALL
906 GuiConsoleGuiThread(PVOID Data)
907 {
908 MSG msg;
909 PHANDLE GraphicsStartupEvent = (PHANDLE) Data;
910
911 NotifyWnd = CreateWindowW(L"Win32CsrCreateNotify",
912 L"",
913 WS_OVERLAPPEDWINDOW,
914 CW_USEDEFAULT,
915 CW_USEDEFAULT,
916 CW_USEDEFAULT,
917 CW_USEDEFAULT,
918 NULL,
919 NULL,
920 (HINSTANCE) GetModuleHandleW(NULL),
921 NULL);
922 if (NULL == NotifyWnd)
923 {
924 PrivateCsrssManualGuiCheck(-1);
925 SetEvent(*GraphicsStartupEvent);
926 return 1;
927 }
928
929 SetEvent(*GraphicsStartupEvent);
930
931 while(GetMessageW(&msg, NULL, 0, 0))
932 {
933 TranslateMessage(&msg);
934 DispatchMessageW(&msg);
935 }
936
937 return 1;
938 }
939
940 static BOOL FASTCALL
941 GuiInit(VOID)
942 {
943 WNDCLASSEXW wc;
944
945 if (NULL == NotifyWnd)
946 {
947 PrivateCsrssManualGuiCheck(+1);
948 }
949
950 wc.cbSize = sizeof(WNDCLASSEXW);
951 wc.lpszClassName = L"Win32CsrCreateNotify";
952 wc.lpfnWndProc = GuiConsoleNotifyWndProc;
953 wc.style = 0;
954 wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
955 wc.hIcon = NULL;
956 wc.hCursor = NULL;
957 wc.hbrBackground = NULL;
958 wc.lpszMenuName = NULL;
959 wc.cbClsExtra = 0;
960 wc.cbWndExtra = 0;
961 wc.hIconSm = NULL;
962 if (RegisterClassExW(&wc) == 0)
963 {
964 DPRINT1("Failed to register notify wndproc\n");
965 return FALSE;
966 }
967
968 wc.cbSize = sizeof(WNDCLASSEXW);
969 wc.lpszClassName = L"ConsoleWindowClass";
970 wc.lpfnWndProc = GuiConsoleWndProc;
971 wc.style = 0;
972 wc.hInstance = (HINSTANCE) GetModuleHandleW(NULL);
973 wc.hIcon = LoadIconW(Win32CsrDllHandle, MAKEINTRESOURCEW(1));
974 wc.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_ARROW));
975 wc.hbrBackground = NULL;
976 wc.lpszMenuName = NULL;
977 wc.cbClsExtra = 0;
978 wc.cbWndExtra = 0;
979 wc.hIconSm = LoadImageW(Win32CsrDllHandle, MAKEINTRESOURCEW(1), IMAGE_ICON,
980 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
981 LR_SHARED);
982 if (RegisterClassExW(&wc) == 0)
983 {
984 DPRINT1("Failed to register console wndproc\n");
985 return FALSE;
986 }
987
988 return TRUE;
989 }
990
991 static VOID STDCALL
992 GuiInitScreenBuffer(PCSRSS_CONSOLE Console, PCSRSS_SCREEN_BUFFER Buffer)
993 {
994 Buffer->DefaultAttrib = 0x0f;
995 }
996
997 static BOOL STDCALL
998 GuiChangeTitle(PCSRSS_CONSOLE Console)
999 {
1000 PWCHAR Buffer, Title;
1001
1002 Buffer = HeapAlloc(Win32CsrApiHeap, 0,
1003 Console->Title.Length + sizeof(WCHAR));
1004 if (NULL != Buffer)
1005 {
1006 memcpy(Buffer, Console->Title.Buffer, Console->Title.Length);
1007 Buffer[Console->Title.Length / sizeof(WCHAR)] = L'\0';
1008 Title = Buffer;
1009 }
1010 else
1011 {
1012 Title = L"";
1013 }
1014 SendMessageW(Console->hWindow, WM_SETTEXT, 0, (LPARAM) Title);
1015 if (NULL != Buffer)
1016 {
1017 HeapFree(Win32CsrApiHeap, 0, Buffer);
1018 }
1019
1020 return TRUE;
1021 }
1022
1023 static BOOL STDCALL
1024 GuiChangeIcon(PCSRSS_CONSOLE Console)
1025 {
1026 SendMessageW(Console->hWindow, WM_SETICON, ICON_BIG, (LPARAM)Console->hWindowIcon);
1027 SendMessageW(Console->hWindow, WM_SETICON, ICON_SMALL, (LPARAM)Console->hWindowIcon);
1028
1029 return TRUE;
1030 }
1031
1032 static VOID STDCALL
1033 GuiCleanupConsole(PCSRSS_CONSOLE Console)
1034 {
1035 SendMessageW(NotifyWnd, PM_DESTROY_CONSOLE, 0, (LPARAM) Console);
1036 }
1037
1038 static CSRSS_CONSOLE_VTBL GuiVtbl =
1039 {
1040 GuiInitScreenBuffer,
1041 GuiWriteStream,
1042 GuiDrawRegion,
1043 GuiSetCursorInfo,
1044 GuiSetScreenInfo,
1045 GuiChangeTitle,
1046 GuiCleanupConsole,
1047 GuiChangeIcon
1048 };
1049
1050 NTSTATUS FASTCALL
1051 GuiInitConsole(PCSRSS_CONSOLE Console)
1052 {
1053 HANDLE GraphicsStartupEvent;
1054 HANDLE ThreadHandle;
1055
1056 if (! ConsInitialized)
1057 {
1058 ConsInitialized = TRUE;
1059 if (! GuiInit())
1060 {
1061 ConsInitialized = FALSE;
1062 return STATUS_UNSUCCESSFUL;
1063 }
1064 }
1065
1066 Console->Vtbl = &GuiVtbl;
1067 Console->Size.X = 80;
1068 Console->Size.Y = 25;
1069 if (NULL == NotifyWnd)
1070 {
1071 GraphicsStartupEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1072 if (NULL == GraphicsStartupEvent)
1073 {
1074 return STATUS_UNSUCCESSFUL;
1075 }
1076
1077 ThreadHandle = CreateThread(NULL,
1078 0,
1079 GuiConsoleGuiThread,
1080 (PVOID) &GraphicsStartupEvent,
1081 0,
1082 NULL);
1083 if (NULL == ThreadHandle)
1084 {
1085 NtClose(GraphicsStartupEvent);
1086 DPRINT1("Win32Csr: Failed to create graphics console thread. Expect problems\n");
1087 return STATUS_UNSUCCESSFUL;
1088 }
1089 SetThreadPriority(ThreadHandle, THREAD_PRIORITY_HIGHEST);
1090 CloseHandle(ThreadHandle);
1091
1092 WaitForSingleObject(GraphicsStartupEvent, INFINITE);
1093 CloseHandle(GraphicsStartupEvent);
1094
1095 if (NULL == NotifyWnd)
1096 {
1097 DPRINT1("Win32Csr: Failed to create notification window.\n");
1098 return STATUS_UNSUCCESSFUL;
1099 }
1100 }
1101
1102 PostMessageW(NotifyWnd, PM_CREATE_CONSOLE, 0, (LPARAM) Console);
1103
1104 return STATUS_SUCCESS;
1105 }
1106
1107 /* EOF */