ee8d094f5dd7a5ba8f7c714bbdb8e3d91c876b9b
[reactos.git] / reactos / win32ss / user / winsrv / consrv / frontends / gui / conwnd.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Console Server DLL
4 * FILE: frontends/gui/conwnd.c
5 * PURPOSE: GUI Console Window Class
6 * PROGRAMMERS: Gé van Geldorp
7 * Johannes Anderwald
8 * Jeffrey Morlan
9 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
10 */
11
12 /* INCLUDES *******************************************************************/
13
14 #include <consrv.h>
15 #include <intrin.h>
16 #include <windowsx.h>
17
18 #define NDEBUG
19 #include <debug.h>
20
21 #include "guiterm.h"
22 #include "conwnd.h"
23 #include "resource.h"
24
25 /* GLOBALS ********************************************************************/
26
27 // #define PM_CREATE_CONSOLE (WM_APP + 1)
28 // #define PM_DESTROY_CONSOLE (WM_APP + 2)
29
30 // See guiterm.c
31 #define CONGUI_MIN_WIDTH 10
32 #define CONGUI_MIN_HEIGHT 10
33 #define CONGUI_UPDATE_TIME 0
34 #define CONGUI_UPDATE_TIMER 1
35
36 #define CURSOR_BLINK_TIME 500
37
38
39 /**************************************************************\
40 \** Define the Console Leader Process for the console window **/
41 #define GWLP_CONWND_ALLOC (2 * sizeof(LONG_PTR))
42 #define GWLP_CONSOLE_LEADER_PID 0
43 #define GWLP_CONSOLE_LEADER_TID 4
44
45 VOID
46 SetConWndConsoleLeaderCID(IN PGUI_CONSOLE_DATA GuiData)
47 {
48 PCONSOLE_PROCESS_DATA ProcessData;
49 CLIENT_ID ConsoleLeaderCID;
50
51 ProcessData = ConSrvGetConsoleLeaderProcess(GuiData->Console);
52 ConsoleLeaderCID = ProcessData->Process->ClientId;
53 SetWindowLongPtrW(GuiData->hWindow, GWLP_CONSOLE_LEADER_PID,
54 (LONG_PTR)(ConsoleLeaderCID.UniqueProcess));
55 SetWindowLongPtrW(GuiData->hWindow, GWLP_CONSOLE_LEADER_TID,
56 (LONG_PTR)(ConsoleLeaderCID.UniqueThread));
57 }
58 /**************************************************************/
59
60 HICON ghDefaultIcon = NULL;
61 HICON ghDefaultIconSm = NULL;
62 HCURSOR ghDefaultCursor = NULL;
63
64 typedef struct _GUICONSOLE_MENUITEM
65 {
66 UINT uID;
67 const struct _GUICONSOLE_MENUITEM *SubMenu;
68 WORD wCmdID;
69 } GUICONSOLE_MENUITEM, *PGUICONSOLE_MENUITEM;
70
71 static const GUICONSOLE_MENUITEM GuiConsoleEditMenuItems[] =
72 {
73 { IDS_MARK, NULL, ID_SYSTEM_EDIT_MARK },
74 { IDS_COPY, NULL, ID_SYSTEM_EDIT_COPY },
75 { IDS_PASTE, NULL, ID_SYSTEM_EDIT_PASTE },
76 { IDS_SELECTALL, NULL, ID_SYSTEM_EDIT_SELECTALL },
77 { IDS_SCROLL, NULL, ID_SYSTEM_EDIT_SCROLL },
78 { IDS_FIND, NULL, ID_SYSTEM_EDIT_FIND },
79
80 { 0, NULL, 0 } /* End of list */
81 };
82
83 static const GUICONSOLE_MENUITEM GuiConsoleMainMenuItems[] =
84 {
85 { IDS_EDIT, GuiConsoleEditMenuItems, 0 },
86 { IDS_DEFAULTS, NULL, ID_SYSTEM_DEFAULTS },
87 { IDS_PROPERTIES, NULL, ID_SYSTEM_PROPERTIES },
88
89 { 0, NULL, 0 } /* End of list */
90 };
91
92 /*
93 * Default 16-color palette for foreground and background
94 * (corresponding flags in comments).
95 */
96 const COLORREF s_Colors[16] =
97 {
98 RGB(0, 0, 0), // (Black)
99 RGB(0, 0, 128), // BLUE
100 RGB(0, 128, 0), // GREEN
101 RGB(0, 128, 128), // BLUE | GREEN
102 RGB(128, 0, 0), // RED
103 RGB(128, 0, 128), // BLUE | RED
104 RGB(128, 128, 0), // GREEN | RED
105 RGB(192, 192, 192), // BLUE | GREEN | RED
106
107 RGB(128, 128, 128), // (Grey) INTENSITY
108 RGB(0, 0, 255), // BLUE | INTENSITY
109 RGB(0, 255, 0), // GREEN | INTENSITY
110 RGB(0, 255, 255), // BLUE | GREEN | INTENSITY
111 RGB(255, 0, 0), // RED | INTENSITY
112 RGB(255, 0, 255), // BLUE | RED | INTENSITY
113 RGB(255, 255, 0), // GREEN | RED | INTENSITY
114 RGB(255, 255, 255) // BLUE | GREEN | RED | INTENSITY
115 };
116
117 /* FUNCTIONS ******************************************************************/
118
119 static LRESULT CALLBACK
120 ConWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
121
122 BOOLEAN
123 RegisterConWndClass(IN HINSTANCE hInstance)
124 {
125 WNDCLASSEXW WndClass;
126 ATOM WndClassAtom;
127
128 ghDefaultIcon = LoadImageW(hInstance,
129 MAKEINTRESOURCEW(IDI_TERMINAL),
130 IMAGE_ICON,
131 GetSystemMetrics(SM_CXICON),
132 GetSystemMetrics(SM_CYICON),
133 LR_SHARED);
134 ghDefaultIconSm = LoadImageW(hInstance,
135 MAKEINTRESOURCEW(IDI_TERMINAL),
136 IMAGE_ICON,
137 GetSystemMetrics(SM_CXSMICON),
138 GetSystemMetrics(SM_CYSMICON),
139 LR_SHARED);
140 ghDefaultCursor = LoadCursorW(NULL, IDC_ARROW);
141
142 WndClass.cbSize = sizeof(WNDCLASSEXW);
143 WndClass.lpszClassName = GUI_CONWND_CLASS;
144 WndClass.lpfnWndProc = ConWndProc;
145 WndClass.style = CS_DBLCLKS /* | CS_HREDRAW | CS_VREDRAW */;
146 WndClass.hInstance = hInstance;
147 WndClass.hIcon = ghDefaultIcon;
148 WndClass.hIconSm = ghDefaultIconSm;
149 WndClass.hCursor = ghDefaultCursor;
150 WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // The color of a terminal when it is switched off.
151 WndClass.lpszMenuName = NULL;
152 WndClass.cbClsExtra = 0;
153 WndClass.cbWndExtra = GWLP_CONWND_ALLOC;
154
155 WndClassAtom = RegisterClassExW(&WndClass);
156 if (WndClassAtom == 0)
157 {
158 DPRINT1("Failed to register GUI console class\n");
159 }
160 else
161 {
162 NtUserConsoleControl(GuiConsoleWndClassAtom, &WndClassAtom, sizeof(ATOM));
163 }
164
165 return (WndClassAtom != 0);
166 }
167
168 BOOLEAN
169 UnRegisterConWndClass(HINSTANCE hInstance)
170 {
171 return !!UnregisterClassW(GUI_CONWND_CLASS, hInstance);
172 }
173
174
175
176 static VOID
177 GetScreenBufferSizeUnits(IN PCONSOLE_SCREEN_BUFFER Buffer,
178 IN PGUI_CONSOLE_DATA GuiData,
179 OUT PUINT WidthUnit,
180 OUT PUINT HeightUnit)
181 {
182 if (Buffer == NULL || GuiData == NULL ||
183 WidthUnit == NULL || HeightUnit == NULL)
184 {
185 return;
186 }
187
188 if (GetType(Buffer) == TEXTMODE_BUFFER)
189 {
190 *WidthUnit = GuiData->CharWidth ;
191 *HeightUnit = GuiData->CharHeight;
192 }
193 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
194 {
195 *WidthUnit = 1;
196 *HeightUnit = 1;
197 }
198 }
199
200 static VOID
201 AppendMenuItems(HMENU hMenu,
202 const GUICONSOLE_MENUITEM *Items)
203 {
204 UINT i = 0;
205 WCHAR szMenuString[255];
206 HMENU hSubMenu;
207
208 do
209 {
210 if (Items[i].uID != (UINT)-1)
211 {
212 if (LoadStringW(ConSrvDllInstance,
213 Items[i].uID,
214 szMenuString,
215 ARRAYSIZE(szMenuString)) > 0)
216 {
217 if (Items[i].SubMenu != NULL)
218 {
219 hSubMenu = CreatePopupMenu();
220 if (hSubMenu != NULL)
221 {
222 AppendMenuItems(hSubMenu, Items[i].SubMenu);
223
224 if (!AppendMenuW(hMenu,
225 MF_STRING | MF_POPUP,
226 (UINT_PTR)hSubMenu,
227 szMenuString))
228 {
229 DestroyMenu(hSubMenu);
230 }
231 }
232 }
233 else
234 {
235 AppendMenuW(hMenu,
236 MF_STRING,
237 Items[i].wCmdID,
238 szMenuString);
239 }
240 }
241 }
242 else
243 {
244 AppendMenuW(hMenu,
245 MF_SEPARATOR,
246 0,
247 NULL);
248 }
249 i++;
250 } while (!(Items[i].uID == 0 && Items[i].SubMenu == NULL && Items[i].wCmdID == 0));
251 }
252
253 //static
254 VOID
255 CreateSysMenu(HWND hWnd)
256 {
257 MENUITEMINFOW mii;
258 WCHAR szMenuStringBack[255];
259 WCHAR *ptrTab;
260 HMENU hMenu = GetSystemMenu(hWnd, FALSE);
261 if (hMenu != NULL)
262 {
263 mii.cbSize = sizeof(mii);
264 mii.fMask = MIIM_STRING;
265 mii.dwTypeData = szMenuStringBack;
266 mii.cch = sizeof(szMenuStringBack)/sizeof(WCHAR);
267
268 GetMenuItemInfoW(hMenu, SC_CLOSE, FALSE, &mii);
269
270 ptrTab = wcschr(szMenuStringBack, '\t');
271 if (ptrTab)
272 {
273 *ptrTab = '\0';
274 mii.cch = wcslen(szMenuStringBack);
275
276 SetMenuItemInfoW(hMenu, SC_CLOSE, FALSE, &mii);
277 }
278
279 AppendMenuItems(hMenu, GuiConsoleMainMenuItems);
280 DrawMenuBar(hWnd);
281 }
282 }
283
284 static VOID
285 SendMenuEvent(PCONSRV_CONSOLE Console, UINT CmdId)
286 {
287 INPUT_RECORD er;
288
289 DPRINT("Menu item ID: %d\n", CmdId);
290
291 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
292
293 er.EventType = MENU_EVENT;
294 er.Event.MenuEvent.dwCommandId = CmdId;
295 ConioProcessInputEvent(Console, &er);
296
297 LeaveCriticalSection(&Console->Lock);
298 }
299
300 static VOID
301 Copy(PGUI_CONSOLE_DATA GuiData);
302 static VOID
303 Paste(PGUI_CONSOLE_DATA GuiData);
304 static VOID
305 UpdateSelection(PGUI_CONSOLE_DATA GuiData,
306 PCOORD SelectionAnchor OPTIONAL,
307 PCOORD coord);
308
309 static VOID
310 Mark(PGUI_CONSOLE_DATA GuiData)
311 {
312 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
313
314 /* Clear the old selection */
315 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
316
317 /* Restart a new selection */
318 GuiData->dwSelectionCursor = ActiveBuffer->ViewOrigin;
319 UpdateSelection(GuiData,
320 &GuiData->dwSelectionCursor,
321 &GuiData->dwSelectionCursor);
322 }
323
324 static VOID
325 SelectAll(PGUI_CONSOLE_DATA GuiData)
326 {
327 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
328 COORD SelectionAnchor;
329
330 /* Clear the old selection */
331 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
332
333 /*
334 * The selection area extends to the whole screen buffer's width.
335 */
336 SelectionAnchor.X = SelectionAnchor.Y = 0;
337 GuiData->dwSelectionCursor.X = ActiveBuffer->ScreenBufferSize.X - 1;
338
339 /*
340 * Determine whether the selection must extend to just some part
341 * (for text-mode screen buffers) or to all of the screen buffer's
342 * height (for graphics ones).
343 */
344 if (GetType(ActiveBuffer) == TEXTMODE_BUFFER)
345 {
346 /*
347 * We select all the characters from the first line
348 * to the line where the cursor is positioned.
349 */
350 GuiData->dwSelectionCursor.Y = ActiveBuffer->CursorPosition.Y;
351 }
352 else /* if (GetType(ActiveBuffer) == GRAPHICS_BUFFER) */
353 {
354 /*
355 * We select all the screen buffer area.
356 */
357 GuiData->dwSelectionCursor.Y = ActiveBuffer->ScreenBufferSize.Y - 1;
358 }
359
360 /* Restart a new selection */
361 GuiData->Selection.dwFlags |= CONSOLE_MOUSE_SELECTION;
362 UpdateSelection(GuiData, &SelectionAnchor, &GuiData->dwSelectionCursor);
363 }
364
365 static LRESULT
366 OnCommand(PGUI_CONSOLE_DATA GuiData, WPARAM wParam, LPARAM lParam)
367 {
368 LRESULT Ret = TRUE;
369 PCONSRV_CONSOLE Console = GuiData->Console;
370
371 /*
372 * In case the selected menu item belongs to the user-reserved menu id range,
373 * send to him a menu event and return directly. The user must handle those
374 * reserved menu commands...
375 */
376 if (GuiData->CmdIdLow <= (UINT)wParam && (UINT)wParam <= GuiData->CmdIdHigh)
377 {
378 SendMenuEvent(Console, (UINT)wParam);
379 goto Quit;
380 }
381
382 /* ... otherwise, perform actions. */
383 switch (wParam)
384 {
385 case ID_SYSTEM_EDIT_MARK:
386 Mark(GuiData);
387 break;
388
389 case ID_SYSTEM_EDIT_COPY:
390 Copy(GuiData);
391 break;
392
393 case ID_SYSTEM_EDIT_PASTE:
394 Paste(GuiData);
395 break;
396
397 case ID_SYSTEM_EDIT_SELECTALL:
398 SelectAll(GuiData);
399 break;
400
401 case ID_SYSTEM_EDIT_SCROLL:
402 DPRINT1("Scrolling is not handled yet\n");
403 break;
404
405 case ID_SYSTEM_EDIT_FIND:
406 DPRINT1("Finding is not handled yet\n");
407 break;
408
409 case ID_SYSTEM_DEFAULTS:
410 GuiConsoleShowConsoleProperties(GuiData, TRUE);
411 break;
412
413 case ID_SYSTEM_PROPERTIES:
414 GuiConsoleShowConsoleProperties(GuiData, FALSE);
415 break;
416
417 default:
418 Ret = FALSE;
419 break;
420 }
421
422 Quit:
423 if (!Ret)
424 Ret = DefWindowProcW(GuiData->hWindow, WM_SYSCOMMAND, wParam, lParam);
425
426 return Ret;
427 }
428
429 static PGUI_CONSOLE_DATA
430 GuiGetGuiData(HWND hWnd)
431 {
432 /* This function ensures that the console pointer is not NULL */
433 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
434 return ( ((GuiData == NULL) || (GuiData->hWindow == hWnd && GuiData->Console != NULL)) ? GuiData : NULL );
435 }
436
437 static VOID
438 ResizeConWnd(PGUI_CONSOLE_DATA GuiData, DWORD WidthUnit, DWORD HeightUnit)
439 {
440 PCONSOLE_SCREEN_BUFFER Buff = GuiData->ActiveBuffer;
441 SCROLLINFO sInfo;
442
443 DWORD Width, Height;
444
445 Width = Buff->ViewSize.X * WidthUnit +
446 2 * (GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE));
447 Height = Buff->ViewSize.Y * HeightUnit +
448 2 * (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE)) + GetSystemMetrics(SM_CYCAPTION);
449
450 /* Set scrollbar sizes */
451 sInfo.cbSize = sizeof(SCROLLINFO);
452 sInfo.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
453 sInfo.nMin = 0;
454 if (Buff->ScreenBufferSize.Y > Buff->ViewSize.Y)
455 {
456 sInfo.nMax = Buff->ScreenBufferSize.Y - 1;
457 sInfo.nPage = Buff->ViewSize.Y;
458 sInfo.nPos = Buff->ViewOrigin.Y;
459 SetScrollInfo(GuiData->hWindow, SB_VERT, &sInfo, TRUE);
460 Width += GetSystemMetrics(SM_CXVSCROLL);
461 ShowScrollBar(GuiData->hWindow, SB_VERT, TRUE);
462 }
463 else
464 {
465 ShowScrollBar(GuiData->hWindow, SB_VERT, FALSE);
466 }
467
468 if (Buff->ScreenBufferSize.X > Buff->ViewSize.X)
469 {
470 sInfo.nMax = Buff->ScreenBufferSize.X - 1;
471 sInfo.nPage = Buff->ViewSize.X;
472 sInfo.nPos = Buff->ViewOrigin.X;
473 SetScrollInfo(GuiData->hWindow, SB_HORZ, &sInfo, TRUE);
474 Height += GetSystemMetrics(SM_CYHSCROLL);
475 ShowScrollBar(GuiData->hWindow, SB_HORZ, TRUE);
476 }
477 else
478 {
479 ShowScrollBar(GuiData->hWindow, SB_HORZ, FALSE);
480 }
481
482 /* Resize the window */
483 SetWindowPos(GuiData->hWindow, NULL, 0, 0, Width, Height,
484 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOCOPYBITS);
485 // NOTE: The SWP_NOCOPYBITS flag can be replaced by a subsequent call
486 // to: InvalidateRect(GuiData->hWindow, NULL, TRUE);
487 }
488
489
490 VOID
491 DeleteFonts(PGUI_CONSOLE_DATA GuiData)
492 {
493 ULONG i;
494 for (i = 0; i < ARRAYSIZE(GuiData->Font); ++i)
495 {
496 if (GuiData->Font[i] != NULL) DeleteObject(GuiData->Font[i]);
497 GuiData->Font[i] = NULL;
498 }
499 }
500
501 static HFONT
502 CreateDerivedFont(HFONT OrgFont,
503 // COORD FontSize,
504 ULONG FontWeight,
505 // BOOLEAN bItalic,
506 BOOLEAN bUnderline,
507 BOOLEAN bStrikeOut)
508 {
509 LOGFONT lf;
510
511 /* Initialize the LOGFONT structure */
512 RtlZeroMemory(&lf, sizeof(lf));
513
514 /* Retrieve the details of the current font */
515 if (GetObject(OrgFont, sizeof(lf), &lf) == 0)
516 return NULL;
517
518 /* Change the font attributes */
519 // lf.lfHeight = FontSize.Y;
520 // lf.lfWidth = FontSize.X;
521 lf.lfWeight = FontWeight;
522 // lf.lfItalic = bItalic;
523 lf.lfUnderline = bUnderline;
524 lf.lfStrikeOut = bStrikeOut;
525
526 /* Build a new font */
527 return CreateFontIndirect(&lf);
528 }
529
530 BOOL
531 InitFonts(PGUI_CONSOLE_DATA GuiData,
532 LPWSTR FaceName, // Points to a WCHAR array of LF_FACESIZE elements.
533 ULONG FontFamily,
534 COORD FontSize,
535 ULONG FontWeight)
536 {
537 HDC hDC;
538 HFONT OldFont, NewFont;
539 TEXTMETRICW Metrics;
540 SIZE CharSize;
541
542 hDC = GetDC(GuiData->hWindow);
543
544 /*
545 * Initialize a new NORMAL font and get its metrics.
546 */
547
548 FontSize.Y = FontSize.Y > 0 ? -MulDiv(FontSize.Y, GetDeviceCaps(hDC, LOGPIXELSY), 72)
549 : FontSize.Y;
550
551 NewFont = CreateFontW(FontSize.Y,
552 FontSize.X,
553 0,
554 TA_BASELINE,
555 FontWeight,
556 FALSE,
557 FALSE,
558 FALSE,
559 OEM_CHARSET,
560 OUT_DEFAULT_PRECIS,
561 CLIP_DEFAULT_PRECIS,
562 DEFAULT_QUALITY,
563 FIXED_PITCH | FontFamily,
564 FaceName);
565 if (NewFont == NULL)
566 {
567 DPRINT1("InitFonts: CreateFontW failed\n");
568 ReleaseDC(GuiData->hWindow, hDC);
569 return FALSE;
570 }
571
572 OldFont = SelectObject(hDC, NewFont);
573 if (OldFont == NULL)
574 {
575 DPRINT1("InitFonts: SelectObject failed\n");
576 ReleaseDC(GuiData->hWindow, hDC);
577 DeleteObject(NewFont);
578 return FALSE;
579 }
580
581 if (!GetTextMetricsW(hDC, &Metrics))
582 {
583 DPRINT1("InitFonts: GetTextMetrics failed\n");
584 SelectObject(hDC, OldFont);
585 ReleaseDC(GuiData->hWindow, hDC);
586 DeleteObject(NewFont);
587 return FALSE;
588 }
589 GuiData->CharWidth = Metrics.tmMaxCharWidth;
590 GuiData->CharHeight = Metrics.tmHeight + Metrics.tmExternalLeading;
591
592 /* Measure real char width more precisely if possible */
593 if (GetTextExtentPoint32W(hDC, L"R", 1, &CharSize))
594 GuiData->CharWidth = CharSize.cx;
595
596 SelectObject(hDC, OldFont);
597 ReleaseDC(GuiData->hWindow, hDC);
598
599 /*
600 * Initialization succeeded.
601 */
602 // Delete all the old fonts first.
603 DeleteFonts(GuiData);
604 GuiData->Font[FONT_NORMAL] = NewFont;
605
606 /*
607 * Now build the other fonts (bold, underlined, mixed).
608 */
609 GuiData->Font[FONT_BOLD] =
610 CreateDerivedFont(GuiData->Font[FONT_NORMAL],
611 FontWeight < FW_BOLD ? FW_BOLD : FontWeight,
612 FALSE,
613 FALSE);
614 GuiData->Font[FONT_UNDERLINE] =
615 CreateDerivedFont(GuiData->Font[FONT_NORMAL],
616 FontWeight,
617 TRUE,
618 FALSE);
619 GuiData->Font[FONT_BOLD | FONT_UNDERLINE] =
620 CreateDerivedFont(GuiData->Font[FONT_NORMAL],
621 FontWeight < FW_BOLD ? FW_BOLD : FontWeight,
622 TRUE,
623 FALSE);
624
625 /*
626 * Save the settings.
627 */
628 if (FaceName != GuiData->GuiInfo.FaceName)
629 {
630 wcsncpy(GuiData->GuiInfo.FaceName, FaceName, LF_FACESIZE);
631 GuiData->GuiInfo.FaceName[LF_FACESIZE - 1] = UNICODE_NULL;
632 }
633 GuiData->GuiInfo.FontFamily = FontFamily;
634 GuiData->GuiInfo.FontSize = FontSize;
635 GuiData->GuiInfo.FontWeight = FontWeight;
636
637 return TRUE;
638 }
639
640
641 static BOOL
642 OnNcCreate(HWND hWnd, LPCREATESTRUCTW Create)
643 {
644 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA)Create->lpCreateParams;
645 PCONSRV_CONSOLE Console;
646
647 if (NULL == GuiData)
648 {
649 DPRINT1("GuiConsoleNcCreate: No GUI data\n");
650 return FALSE;
651 }
652
653 Console = GuiData->Console;
654
655 GuiData->hWindow = hWnd;
656
657 /* Initialize the fonts */
658 if (!InitFonts(GuiData,
659 GuiData->GuiInfo.FaceName,
660 GuiData->GuiInfo.FontFamily,
661 GuiData->GuiInfo.FontSize,
662 GuiData->GuiInfo.FontWeight))
663 {
664 DPRINT1("GuiConsoleNcCreate: InitFonts failed\n");
665 GuiData->hWindow = NULL;
666 NtSetEvent(GuiData->hGuiInitEvent, NULL);
667 return FALSE;
668 }
669
670 /* Initialize the terminal framebuffer */
671 GuiData->hMemDC = CreateCompatibleDC(NULL);
672 GuiData->hBitmap = NULL;
673 GuiData->hSysPalette = NULL; /* Original system palette */
674
675 /* Update the icons of the window */
676 if (GuiData->hIcon != ghDefaultIcon)
677 {
678 DefWindowProcW(GuiData->hWindow, WM_SETICON, ICON_BIG , (LPARAM)GuiData->hIcon );
679 DefWindowProcW(GuiData->hWindow, WM_SETICON, ICON_SMALL, (LPARAM)GuiData->hIconSm);
680 }
681
682 // FIXME: Keep these instructions here ? ///////////////////////////////////
683 Console->ActiveBuffer->CursorBlinkOn = TRUE;
684 Console->ActiveBuffer->ForceCursorOff = FALSE;
685 ////////////////////////////////////////////////////////////////////////////
686
687 SetWindowLongPtrW(GuiData->hWindow, GWLP_USERDATA, (DWORD_PTR)GuiData);
688
689 if (GuiData->IsWindowVisible)
690 {
691 SetTimer(GuiData->hWindow, CONGUI_UPDATE_TIMER, CONGUI_UPDATE_TIME, NULL);
692 }
693
694 // FIXME: HACK: Potential HACK for CORE-8129; see revision 63595.
695 //CreateSysMenu(GuiData->hWindow);
696
697 DPRINT("OnNcCreate - setting start event\n");
698 NtSetEvent(GuiData->hGuiInitEvent, NULL);
699
700 return (BOOL)DefWindowProcW(GuiData->hWindow, WM_NCCREATE, 0, (LPARAM)Create);
701 }
702
703
704 BOOL
705 EnterFullScreen(PGUI_CONSOLE_DATA GuiData);
706 VOID
707 LeaveFullScreen(PGUI_CONSOLE_DATA GuiData);
708 VOID
709 SwitchFullScreen(PGUI_CONSOLE_DATA GuiData, BOOL FullScreen);
710 VOID
711 GuiConsoleSwitchFullScreen(PGUI_CONSOLE_DATA GuiData);
712
713 static VOID
714 OnActivate(PGUI_CONSOLE_DATA GuiData, WPARAM wParam)
715 {
716 WORD ActivationState = LOWORD(wParam);
717
718 DPRINT("WM_ACTIVATE - ActivationState = %d\n", ActivationState);
719
720 if ( ActivationState == WA_ACTIVE ||
721 ActivationState == WA_CLICKACTIVE )
722 {
723 if (GuiData->GuiInfo.FullScreen)
724 {
725 EnterFullScreen(GuiData);
726 // // PostMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_RESTORE, 0);
727 // SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_RESTORE, 0);
728 }
729 }
730 else // if (ActivationState == WA_INACTIVE)
731 {
732 if (GuiData->GuiInfo.FullScreen)
733 {
734 SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
735 LeaveFullScreen(GuiData);
736 // // PostMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
737 // SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
738 }
739 }
740
741 /*
742 * Ignore the next mouse signal when we are going to be enabled again via
743 * the mouse, in order to prevent, e.g. when we are in Edit mode, erroneous
744 * mouse actions from the user that could spoil text selection or copy/pastes.
745 */
746 if (ActivationState == WA_CLICKACTIVE)
747 GuiData->IgnoreNextMouseSignal = TRUE;
748 }
749
750 static VOID
751 OnFocus(PGUI_CONSOLE_DATA GuiData, BOOL SetFocus)
752 {
753 PCONSRV_CONSOLE Console = GuiData->Console;
754 INPUT_RECORD er;
755
756 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
757
758 er.EventType = FOCUS_EVENT;
759 er.Event.FocusEvent.bSetFocus = SetFocus;
760 ConioProcessInputEvent(Console, &er);
761
762 LeaveCriticalSection(&Console->Lock);
763
764 if (SetFocus)
765 DPRINT("TODO: Create console caret\n");
766 else
767 DPRINT("TODO: Destroy console caret\n");
768 }
769
770 static VOID
771 SmallRectToRect(PGUI_CONSOLE_DATA GuiData, PRECT Rect, PSMALL_RECT SmallRect)
772 {
773 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
774 UINT WidthUnit, HeightUnit;
775
776 GetScreenBufferSizeUnits(Buffer, GuiData, &WidthUnit, &HeightUnit);
777
778 Rect->left = (SmallRect->Left - Buffer->ViewOrigin.X) * WidthUnit ;
779 Rect->top = (SmallRect->Top - Buffer->ViewOrigin.Y) * HeightUnit;
780 Rect->right = (SmallRect->Right + 1 - Buffer->ViewOrigin.X) * WidthUnit ;
781 Rect->bottom = (SmallRect->Bottom + 1 - Buffer->ViewOrigin.Y) * HeightUnit;
782 }
783
784 VOID
785 GetSelectionBeginEnd(PCOORD Begin, PCOORD End,
786 PCOORD SelectionAnchor,
787 PSMALL_RECT SmallRect)
788 {
789 if (Begin == NULL || End == NULL) return;
790
791 *Begin = *SelectionAnchor;
792 End->X = (SelectionAnchor->X == SmallRect->Left) ? SmallRect->Right
793 /* Case X != Left, must be == Right */ : SmallRect->Left;
794 End->Y = (SelectionAnchor->Y == SmallRect->Top ) ? SmallRect->Bottom
795 /* Case Y != Top, must be == Bottom */ : SmallRect->Top;
796
797 /* Exchange Begin / End if Begin > End lexicographically */
798 if (Begin->Y > End->Y || (Begin->Y == End->Y && Begin->X > End->X))
799 {
800 End->X = _InterlockedExchange16(&Begin->X, End->X);
801 End->Y = _InterlockedExchange16(&Begin->Y, End->Y);
802 }
803 }
804
805 static HRGN
806 CreateSelectionRgn(PGUI_CONSOLE_DATA GuiData,
807 BOOL LineSelection,
808 PCOORD SelectionAnchor,
809 PSMALL_RECT SmallRect)
810 {
811 if (!LineSelection)
812 {
813 RECT rect;
814 SmallRectToRect(GuiData, &rect, SmallRect);
815 return CreateRectRgnIndirect(&rect);
816 }
817 else
818 {
819 HRGN SelRgn;
820 COORD Begin, End;
821
822 GetSelectionBeginEnd(&Begin, &End, SelectionAnchor, SmallRect);
823
824 if (Begin.Y == End.Y)
825 {
826 SMALL_RECT sr;
827 RECT r ;
828
829 sr.Left = Begin.X;
830 sr.Top = Begin.Y;
831 sr.Right = End.X;
832 sr.Bottom = End.Y;
833
834 // Debug thingie to see whether I can put this corner case
835 // together with the previous one.
836 if (SmallRect->Left != sr.Left ||
837 SmallRect->Top != sr.Top ||
838 SmallRect->Right != sr.Right ||
839 SmallRect->Bottom != sr.Bottom)
840 {
841 DPRINT1("\n"
842 "SmallRect = (%d, %d, %d, %d)\n"
843 "sr = (%d, %d, %d, %d)\n"
844 "\n",
845 SmallRect->Left, SmallRect->Top, SmallRect->Right, SmallRect->Bottom,
846 sr.Left, sr.Top, sr.Right, sr.Bottom);
847 }
848
849 SmallRectToRect(GuiData, &r, &sr);
850 SelRgn = CreateRectRgnIndirect(&r);
851 }
852 else
853 {
854 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
855
856 HRGN rg1, rg2, rg3;
857 SMALL_RECT sr1, sr2, sr3;
858 RECT r1 , r2 , r3 ;
859
860 sr1.Left = Begin.X;
861 sr1.Top = Begin.Y;
862 sr1.Right = ActiveBuffer->ScreenBufferSize.X - 1;
863 sr1.Bottom = Begin.Y;
864
865 sr2.Left = 0;
866 sr2.Top = Begin.Y + 1;
867 sr2.Right = ActiveBuffer->ScreenBufferSize.X - 1;
868 sr2.Bottom = End.Y - 1;
869
870 sr3.Left = 0;
871 sr3.Top = End.Y;
872 sr3.Right = End.X;
873 sr3.Bottom = End.Y;
874
875 SmallRectToRect(GuiData, &r1, &sr1);
876 SmallRectToRect(GuiData, &r2, &sr2);
877 SmallRectToRect(GuiData, &r3, &sr3);
878
879 rg1 = CreateRectRgnIndirect(&r1);
880 rg2 = CreateRectRgnIndirect(&r2);
881 rg3 = CreateRectRgnIndirect(&r3);
882
883 CombineRgn(rg1, rg1, rg2, RGN_XOR);
884 CombineRgn(rg1, rg1, rg3, RGN_XOR);
885 DeleteObject(rg3);
886 DeleteObject(rg2);
887
888 SelRgn = rg1;
889 }
890
891 return SelRgn;
892 }
893 }
894
895 static VOID
896 PaintSelectionRect(PGUI_CONSOLE_DATA GuiData, PPAINTSTRUCT pps)
897 {
898 HRGN rgnPaint = CreateRectRgnIndirect(&pps->rcPaint);
899 HRGN rgnSel = CreateSelectionRgn(GuiData, GuiData->LineSelection,
900 &GuiData->Selection.dwSelectionAnchor,
901 &GuiData->Selection.srSelection);
902
903 /* Invert the selection */
904
905 int ErrorCode = CombineRgn(rgnPaint, rgnPaint, rgnSel, RGN_AND);
906 if (ErrorCode != ERROR && ErrorCode != NULLREGION)
907 {
908 InvertRgn(pps->hdc, rgnPaint);
909 }
910
911 DeleteObject(rgnSel);
912 DeleteObject(rgnPaint);
913 }
914
915 static VOID
916 UpdateSelection(PGUI_CONSOLE_DATA GuiData,
917 PCOORD SelectionAnchor OPTIONAL,
918 PCOORD coord)
919 {
920 PCONSRV_CONSOLE Console = GuiData->Console;
921 HRGN oldRgn = CreateSelectionRgn(GuiData, GuiData->LineSelection,
922 &GuiData->Selection.dwSelectionAnchor,
923 &GuiData->Selection.srSelection);
924
925 /* Update the anchor if needed (use the old one if NULL) */
926 if (SelectionAnchor)
927 GuiData->Selection.dwSelectionAnchor = *SelectionAnchor;
928
929 if (coord != NULL)
930 {
931 SMALL_RECT rc;
932 HRGN newRgn;
933
934 /*
935 * Pressing the Control key while selecting text, allows us to enter
936 * into line-selection mode, the selection mode of *nix terminals.
937 */
938 BOOL OldLineSel = GuiData->LineSelection;
939 GuiData->LineSelection = !!(GetKeyState(VK_CONTROL) & 0x8000);
940
941 /* Exchange left/top with right/bottom if required */
942 rc.Left = min(GuiData->Selection.dwSelectionAnchor.X, coord->X);
943 rc.Top = min(GuiData->Selection.dwSelectionAnchor.Y, coord->Y);
944 rc.Right = max(GuiData->Selection.dwSelectionAnchor.X, coord->X);
945 rc.Bottom = max(GuiData->Selection.dwSelectionAnchor.Y, coord->Y);
946
947 newRgn = CreateSelectionRgn(GuiData, GuiData->LineSelection,
948 &GuiData->Selection.dwSelectionAnchor,
949 &rc);
950
951 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
952 {
953 if (OldLineSel != GuiData->LineSelection ||
954 memcmp(&rc, &GuiData->Selection.srSelection, sizeof(SMALL_RECT)) != 0)
955 {
956 /* Calculate the region that needs to be updated */
957 if (oldRgn && newRgn && CombineRgn(newRgn, newRgn, oldRgn, RGN_XOR) != ERROR)
958 {
959 InvalidateRgn(GuiData->hWindow, newRgn, FALSE);
960 }
961 }
962 }
963 else
964 {
965 InvalidateRgn(GuiData->hWindow, newRgn, FALSE);
966 }
967
968 DeleteObject(newRgn);
969
970 GuiData->Selection.dwFlags |= CONSOLE_SELECTION_NOT_EMPTY;
971 GuiData->Selection.srSelection = rc;
972 GuiData->dwSelectionCursor = *coord;
973
974 if ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) == 0)
975 {
976 LPWSTR SelTypeStr = NULL , WindowTitle = NULL;
977 SIZE_T SelTypeStrLength = 0, Length = 0;
978
979 /* Clear the old selection */
980 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
981 {
982 InvalidateRgn(GuiData->hWindow, oldRgn, FALSE);
983 }
984
985 /*
986 * When passing a zero-length buffer size, LoadString(...) returns
987 * a read-only pointer buffer to the program's resource string.
988 */
989 SelTypeStrLength =
990 LoadStringW(ConSrvDllInstance,
991 (GuiData->Selection.dwFlags & CONSOLE_MOUSE_SELECTION)
992 ? IDS_SELECT_TITLE : IDS_MARK_TITLE,
993 (LPWSTR)&SelTypeStr, 0);
994
995 /*
996 * Prepend the selection type string to the current console title
997 * if we succeeded in retrieving a valid localized string.
998 */
999 if (SelTypeStr)
1000 {
1001 // 3 for " - " and 1 for NULL
1002 Length = Console->Title.Length + (SelTypeStrLength + 3 + 1) * sizeof(WCHAR);
1003 WindowTitle = ConsoleAllocHeap(0, Length);
1004
1005 wcsncpy(WindowTitle, SelTypeStr, SelTypeStrLength);
1006 WindowTitle[SelTypeStrLength] = L'\0';
1007 wcscat(WindowTitle, L" - ");
1008 wcscat(WindowTitle, Console->Title.Buffer);
1009
1010 SetWindowTextW(GuiData->hWindow, WindowTitle);
1011 ConsoleFreeHeap(WindowTitle);
1012 }
1013
1014 GuiData->Selection.dwFlags |= CONSOLE_SELECTION_IN_PROGRESS;
1015 ConioPause(Console, PAUSED_FROM_SELECTION);
1016 }
1017 }
1018 else
1019 {
1020 /* Clear the selection */
1021 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
1022 {
1023 InvalidateRgn(GuiData->hWindow, oldRgn, FALSE);
1024 }
1025
1026 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
1027 ConioUnpause(Console, PAUSED_FROM_SELECTION);
1028
1029 /* Restore the console title */
1030 SetWindowTextW(GuiData->hWindow, Console->Title.Buffer);
1031 }
1032
1033 DeleteObject(oldRgn);
1034 }
1035
1036
1037 VOID
1038 GuiPaintTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
1039 PGUI_CONSOLE_DATA GuiData,
1040 PRECT rcView,
1041 PRECT rcFramebuffer);
1042 VOID
1043 GuiPaintGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
1044 PGUI_CONSOLE_DATA GuiData,
1045 PRECT rcView,
1046 PRECT rcFramebuffer);
1047
1048 static VOID
1049 OnPaint(PGUI_CONSOLE_DATA GuiData)
1050 {
1051 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
1052 PAINTSTRUCT ps;
1053 RECT rcPaint;
1054
1055 /* Do nothing if the window is hidden */
1056 if (!GuiData->IsWindowVisible) return;
1057
1058 BeginPaint(GuiData->hWindow, &ps);
1059 if (ps.hdc != NULL &&
1060 ps.rcPaint.left < ps.rcPaint.right &&
1061 ps.rcPaint.top < ps.rcPaint.bottom)
1062 {
1063 EnterCriticalSection(&GuiData->Lock);
1064
1065 /* Compose the current screen-buffer on-memory */
1066 if (GetType(ActiveBuffer) == TEXTMODE_BUFFER)
1067 {
1068 GuiPaintTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)ActiveBuffer,
1069 GuiData, &ps.rcPaint, &rcPaint);
1070 }
1071 else /* if (GetType(ActiveBuffer) == GRAPHICS_BUFFER) */
1072 {
1073 GuiPaintGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)ActiveBuffer,
1074 GuiData, &ps.rcPaint, &rcPaint);
1075 }
1076
1077 /* Send it to screen */
1078 BitBlt(ps.hdc,
1079 ps.rcPaint.left,
1080 ps.rcPaint.top,
1081 rcPaint.right - rcPaint.left,
1082 rcPaint.bottom - rcPaint.top,
1083 GuiData->hMemDC,
1084 rcPaint.left,
1085 rcPaint.top,
1086 SRCCOPY);
1087
1088 /* Draw the selection region if needed */
1089 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
1090 {
1091 PaintSelectionRect(GuiData, &ps);
1092 }
1093
1094 LeaveCriticalSection(&GuiData->Lock);
1095 }
1096 EndPaint(GuiData->hWindow, &ps);
1097
1098 return;
1099 }
1100
1101 static VOID
1102 OnPaletteChanged(PGUI_CONSOLE_DATA GuiData)
1103 {
1104 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
1105
1106 /* Do nothing if the window is hidden */
1107 if (!GuiData->IsWindowVisible) return;
1108
1109 // See WM_PALETTECHANGED message
1110 // if ((HWND)wParam == hWnd) break;
1111
1112 // if (GetType(ActiveBuffer) == GRAPHICS_BUFFER)
1113 if (ActiveBuffer->PaletteHandle)
1114 {
1115 DPRINT("WM_PALETTECHANGED changing palette\n");
1116
1117 /* Specify the use of the system palette for the framebuffer */
1118 SetSystemPaletteUse(GuiData->hMemDC, ActiveBuffer->PaletteUsage);
1119
1120 /* Realize the (logical) palette */
1121 RealizePalette(GuiData->hMemDC);
1122 }
1123 }
1124
1125 static BOOL
1126 IsSystemKey(WORD VirtualKeyCode)
1127 {
1128 switch (VirtualKeyCode)
1129 {
1130 /* From MSDN, "Virtual-Key Codes" */
1131 case VK_RETURN:
1132 case VK_SHIFT:
1133 case VK_CONTROL:
1134 case VK_MENU:
1135 case VK_PAUSE:
1136 case VK_CAPITAL:
1137 case VK_ESCAPE:
1138 case VK_LWIN:
1139 case VK_RWIN:
1140 case VK_NUMLOCK:
1141 case VK_SCROLL:
1142 return TRUE;
1143 default:
1144 return FALSE;
1145 }
1146 }
1147
1148 static VOID
1149 OnKey(PGUI_CONSOLE_DATA GuiData, UINT msg, WPARAM wParam, LPARAM lParam)
1150 {
1151 PCONSRV_CONSOLE Console = GuiData->Console;
1152 PCONSOLE_SCREEN_BUFFER ActiveBuffer;
1153
1154 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
1155
1156 ActiveBuffer = GuiData->ActiveBuffer;
1157
1158 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS)
1159 {
1160 WORD VirtualKeyCode = LOWORD(wParam);
1161
1162 if (msg != WM_KEYDOWN) goto Quit;
1163
1164 if (VirtualKeyCode == VK_RETURN)
1165 {
1166 /* Copy (and clear) selection if ENTER is pressed */
1167 Copy(GuiData);
1168 goto Quit;
1169 }
1170 else if ( VirtualKeyCode == VK_ESCAPE ||
1171 (VirtualKeyCode == 'C' && (GetKeyState(VK_CONTROL) & 0x8000)) )
1172 {
1173 /* Cancel selection if ESC or Ctrl-C are pressed */
1174 UpdateSelection(GuiData, NULL, NULL);
1175 goto Quit;
1176 }
1177
1178 if ((GuiData->Selection.dwFlags & CONSOLE_MOUSE_SELECTION) == 0)
1179 {
1180 /* Keyboard selection mode */
1181 BOOL Interpreted = FALSE;
1182 BOOL MajPressed = !!(GetKeyState(VK_SHIFT) & 0x8000);
1183
1184 switch (VirtualKeyCode)
1185 {
1186 case VK_LEFT:
1187 {
1188 Interpreted = TRUE;
1189 if (GuiData->dwSelectionCursor.X > 0)
1190 GuiData->dwSelectionCursor.X--;
1191
1192 break;
1193 }
1194
1195 case VK_RIGHT:
1196 {
1197 Interpreted = TRUE;
1198 if (GuiData->dwSelectionCursor.X < ActiveBuffer->ScreenBufferSize.X - 1)
1199 GuiData->dwSelectionCursor.X++;
1200
1201 break;
1202 }
1203
1204 case VK_UP:
1205 {
1206 Interpreted = TRUE;
1207 if (GuiData->dwSelectionCursor.Y > 0)
1208 GuiData->dwSelectionCursor.Y--;
1209
1210 break;
1211 }
1212
1213 case VK_DOWN:
1214 {
1215 Interpreted = TRUE;
1216 if (GuiData->dwSelectionCursor.Y < ActiveBuffer->ScreenBufferSize.Y - 1)
1217 GuiData->dwSelectionCursor.Y++;
1218
1219 break;
1220 }
1221
1222 case VK_HOME:
1223 {
1224 Interpreted = TRUE;
1225 GuiData->dwSelectionCursor.X = 0;
1226 GuiData->dwSelectionCursor.Y = 0;
1227 break;
1228 }
1229
1230 case VK_END:
1231 {
1232 Interpreted = TRUE;
1233 GuiData->dwSelectionCursor.Y = ActiveBuffer->ScreenBufferSize.Y - 1;
1234 break;
1235 }
1236
1237 case VK_PRIOR:
1238 {
1239 Interpreted = TRUE;
1240 GuiData->dwSelectionCursor.Y -= ActiveBuffer->ViewSize.Y;
1241 if (GuiData->dwSelectionCursor.Y < 0)
1242 GuiData->dwSelectionCursor.Y = 0;
1243
1244 break;
1245 }
1246
1247 case VK_NEXT:
1248 {
1249 Interpreted = TRUE;
1250 GuiData->dwSelectionCursor.Y += ActiveBuffer->ViewSize.Y;
1251 if (GuiData->dwSelectionCursor.Y >= ActiveBuffer->ScreenBufferSize.Y)
1252 GuiData->dwSelectionCursor.Y = ActiveBuffer->ScreenBufferSize.Y - 1;
1253
1254 break;
1255 }
1256
1257 default:
1258 break;
1259 }
1260
1261 if (Interpreted)
1262 {
1263 UpdateSelection(GuiData,
1264 !MajPressed ? &GuiData->dwSelectionCursor : NULL,
1265 &GuiData->dwSelectionCursor);
1266 }
1267 else if (!IsSystemKey(VirtualKeyCode))
1268 {
1269 /* Emit an error beep sound */
1270 SendNotifyMessage(GuiData->hWindow, PM_CONSOLE_BEEP, 0, 0);
1271 }
1272
1273 goto Quit;
1274 }
1275 else
1276 {
1277 /* Mouse selection mode */
1278
1279 if (!IsSystemKey(VirtualKeyCode))
1280 {
1281 /* Clear the selection and send the key into the input buffer */
1282 UpdateSelection(GuiData, NULL, NULL);
1283 }
1284 else
1285 {
1286 goto Quit;
1287 }
1288 }
1289 }
1290
1291 if ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) == 0)
1292 {
1293 MSG Message;
1294
1295 Message.hwnd = GuiData->hWindow;
1296 Message.message = msg;
1297 Message.wParam = wParam;
1298 Message.lParam = lParam;
1299
1300 ConioProcessKey(Console, &Message);
1301 }
1302
1303 Quit:
1304 LeaveCriticalSection(&Console->Lock);
1305 }
1306
1307
1308 // FIXME: Remove after fixing OnTimer
1309 VOID
1310 InvalidateCell(PGUI_CONSOLE_DATA GuiData,
1311 SHORT x, SHORT y);
1312
1313 static VOID
1314 OnTimer(PGUI_CONSOLE_DATA GuiData)
1315 {
1316 PCONSRV_CONSOLE Console = GuiData->Console;
1317 PCONSOLE_SCREEN_BUFFER Buff;
1318
1319 /* Do nothing if the window is hidden */
1320 if (!GuiData->IsWindowVisible) return;
1321
1322 SetTimer(GuiData->hWindow, CONGUI_UPDATE_TIMER, CURSOR_BLINK_TIME, NULL);
1323
1324 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
1325
1326 Buff = GuiData->ActiveBuffer;
1327
1328 if (GetType(Buff) == TEXTMODE_BUFFER)
1329 {
1330 InvalidateCell(GuiData, Buff->CursorPosition.X, Buff->CursorPosition.Y);
1331 Buff->CursorBlinkOn = !Buff->CursorBlinkOn;
1332
1333 if ((GuiData->OldCursor.x != Buff->CursorPosition.X) ||
1334 (GuiData->OldCursor.y != Buff->CursorPosition.Y))
1335 {
1336 SCROLLINFO xScroll;
1337 int OldScrollX = -1, OldScrollY = -1;
1338 int NewScrollX = -1, NewScrollY = -1;
1339
1340 xScroll.cbSize = sizeof(SCROLLINFO);
1341 xScroll.fMask = SIF_POS;
1342 // Capture the original position of the scroll bars and save them.
1343 if (GetScrollInfo(GuiData->hWindow, SB_HORZ, &xScroll)) OldScrollX = xScroll.nPos;
1344 if (GetScrollInfo(GuiData->hWindow, SB_VERT, &xScroll)) OldScrollY = xScroll.nPos;
1345
1346 // If we successfully got the info for the horizontal scrollbar
1347 if (OldScrollX >= 0)
1348 {
1349 if ((Buff->CursorPosition.X < Buff->ViewOrigin.X) ||
1350 (Buff->CursorPosition.X >= (Buff->ViewOrigin.X + Buff->ViewSize.X)))
1351 {
1352 // Handle the horizontal scroll bar
1353 if (Buff->CursorPosition.X >= Buff->ViewSize.X)
1354 NewScrollX = Buff->CursorPosition.X - Buff->ViewSize.X + 1;
1355 else
1356 NewScrollX = 0;
1357 }
1358 else
1359 {
1360 NewScrollX = OldScrollX;
1361 }
1362 }
1363 // If we successfully got the info for the vertical scrollbar
1364 if (OldScrollY >= 0)
1365 {
1366 if ((Buff->CursorPosition.Y < Buff->ViewOrigin.Y) ||
1367 (Buff->CursorPosition.Y >= (Buff->ViewOrigin.Y + Buff->ViewSize.Y)))
1368 {
1369 // Handle the vertical scroll bar
1370 if (Buff->CursorPosition.Y >= Buff->ViewSize.Y)
1371 NewScrollY = Buff->CursorPosition.Y - Buff->ViewSize.Y + 1;
1372 else
1373 NewScrollY = 0;
1374 }
1375 else
1376 {
1377 NewScrollY = OldScrollY;
1378 }
1379 }
1380
1381 // Adjust scroll bars and refresh the window if the cursor has moved outside the visible area
1382 // NOTE: OldScroll# and NewScroll# will both be -1 (initial value) if the info for the respective scrollbar
1383 // was not obtained successfully in the previous steps. This means their difference is 0 (no scrolling)
1384 // and their associated scrollbar is left alone.
1385 if ((OldScrollX != NewScrollX) || (OldScrollY != NewScrollY))
1386 {
1387 Buff->ViewOrigin.X = NewScrollX;
1388 Buff->ViewOrigin.Y = NewScrollY;
1389 ScrollWindowEx(GuiData->hWindow,
1390 (OldScrollX - NewScrollX) * GuiData->CharWidth,
1391 (OldScrollY - NewScrollY) * GuiData->CharHeight,
1392 NULL,
1393 NULL,
1394 NULL,
1395 NULL,
1396 SW_INVALIDATE);
1397 if (NewScrollX >= 0)
1398 {
1399 xScroll.nPos = NewScrollX;
1400 SetScrollInfo(GuiData->hWindow, SB_HORZ, &xScroll, TRUE);
1401 }
1402 if (NewScrollY >= 0)
1403 {
1404 xScroll.nPos = NewScrollY;
1405 SetScrollInfo(GuiData->hWindow, SB_VERT, &xScroll, TRUE);
1406 }
1407 UpdateWindow(GuiData->hWindow);
1408 // InvalidateRect(GuiData->hWindow, NULL, FALSE);
1409 GuiData->OldCursor.x = Buff->CursorPosition.X;
1410 GuiData->OldCursor.y = Buff->CursorPosition.Y;
1411 }
1412 }
1413 }
1414 else /* if (GetType(Buff) == GRAPHICS_BUFFER) */
1415 {
1416 }
1417
1418 LeaveCriticalSection(&Console->Lock);
1419 }
1420
1421 static BOOL
1422 OnClose(PGUI_CONSOLE_DATA GuiData)
1423 {
1424 PCONSRV_CONSOLE Console = GuiData->Console;
1425
1426 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE))
1427 return TRUE;
1428
1429 // TODO: Prompt for termination ? (Warn the user about possible apps running in this console)
1430
1431 /*
1432 * FIXME: Windows will wait up to 5 seconds for the thread to exit.
1433 * We shouldn't wait here, though, since the console lock is entered.
1434 * A copy of the thread list probably needs to be made.
1435 */
1436 ConSrvConsoleProcessCtrlEvent(Console, 0, CTRL_CLOSE_EVENT);
1437
1438 LeaveCriticalSection(&Console->Lock);
1439 return FALSE;
1440 }
1441
1442 static LRESULT
1443 OnNcDestroy(HWND hWnd)
1444 {
1445 PGUI_CONSOLE_DATA GuiData = GuiGetGuiData(hWnd);
1446
1447 if (GuiData->IsWindowVisible)
1448 {
1449 KillTimer(hWnd, CONGUI_UPDATE_TIMER);
1450 }
1451
1452 GetSystemMenu(hWnd, TRUE);
1453
1454 if (GuiData)
1455 {
1456 /* Free the terminal framebuffer */
1457 if (GuiData->hMemDC ) DeleteDC(GuiData->hMemDC);
1458 if (GuiData->hBitmap) DeleteObject(GuiData->hBitmap);
1459 // if (GuiData->hSysPalette) DeleteObject(GuiData->hSysPalette);
1460 DeleteFonts(GuiData);
1461 }
1462
1463 /* Free the GuiData registration */
1464 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (DWORD_PTR)NULL);
1465
1466 return DefWindowProcW(hWnd, WM_NCDESTROY, 0, 0);
1467 }
1468
1469 static COORD
1470 PointToCoord(PGUI_CONSOLE_DATA GuiData, LPARAM lParam)
1471 {
1472 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1473 COORD Coord;
1474 UINT WidthUnit, HeightUnit;
1475
1476 GetScreenBufferSizeUnits(Buffer, GuiData, &WidthUnit, &HeightUnit);
1477
1478 Coord.X = Buffer->ViewOrigin.X + ((SHORT)LOWORD(lParam) / (int)WidthUnit );
1479 Coord.Y = Buffer->ViewOrigin.Y + ((SHORT)HIWORD(lParam) / (int)HeightUnit);
1480
1481 /* Clip coordinate to ensure it's inside buffer */
1482 if (Coord.X < 0)
1483 Coord.X = 0;
1484 else if (Coord.X >= Buffer->ScreenBufferSize.X)
1485 Coord.X = Buffer->ScreenBufferSize.X - 1;
1486
1487 if (Coord.Y < 0)
1488 Coord.Y = 0;
1489 else if (Coord.Y >= Buffer->ScreenBufferSize.Y)
1490 Coord.Y = Buffer->ScreenBufferSize.Y - 1;
1491
1492 return Coord;
1493 }
1494
1495 static LRESULT
1496 OnMouse(PGUI_CONSOLE_DATA GuiData, UINT msg, WPARAM wParam, LPARAM lParam)
1497 {
1498 BOOL Err = FALSE;
1499 PCONSRV_CONSOLE Console = GuiData->Console;
1500
1501 // FIXME: It's here that we need to check whether we has focus or not
1502 // and whether we are in edit mode or not, to know if we need to deal
1503 // with the mouse, or not.
1504
1505 if (GuiData->IgnoreNextMouseSignal)
1506 {
1507 if (msg != WM_LBUTTONDOWN &&
1508 msg != WM_MBUTTONDOWN &&
1509 msg != WM_RBUTTONDOWN)
1510 {
1511 /*
1512 * If this mouse signal is not a button-down action
1513 * then it is the last signal being ignored.
1514 */
1515 GuiData->IgnoreNextMouseSignal = FALSE;
1516 }
1517 else
1518 {
1519 /*
1520 * This mouse signal is a button-down action.
1521 * Ignore it and perform default action.
1522 */
1523 Err = TRUE;
1524 }
1525 goto Quit;
1526 }
1527
1528 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE))
1529 {
1530 Err = TRUE;
1531 goto Quit;
1532 }
1533
1534 if ( (GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) ||
1535 (Console->QuickEdit) )
1536 {
1537 switch (msg)
1538 {
1539 case WM_LBUTTONDOWN:
1540 {
1541 /* Clear the old selection */
1542 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
1543
1544 /* Restart a new selection */
1545 GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1546 SetCapture(GuiData->hWindow);
1547 GuiData->Selection.dwFlags |= CONSOLE_MOUSE_SELECTION | CONSOLE_MOUSE_DOWN;
1548 UpdateSelection(GuiData,
1549 &GuiData->dwSelectionCursor,
1550 &GuiData->dwSelectionCursor);
1551
1552 break;
1553 }
1554
1555 case WM_LBUTTONUP:
1556 {
1557 if (!(GuiData->Selection.dwFlags & CONSOLE_MOUSE_DOWN)) break;
1558
1559 // GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1560 GuiData->Selection.dwFlags &= ~CONSOLE_MOUSE_DOWN;
1561 // UpdateSelection(GuiData, NULL, &GuiData->dwSelectionCursor);
1562 ReleaseCapture();
1563
1564 break;
1565 }
1566
1567 case WM_LBUTTONDBLCLK:
1568 {
1569 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1570
1571 if (GetType(Buffer) == TEXTMODE_BUFFER)
1572 {
1573 #define IS_WORD_SEP(c) \
1574 ((c) == L'\0' || (c) == L' ' || (c) == L'\t' || (c) == L'\r' || (c) == L'\n')
1575
1576 PTEXTMODE_SCREEN_BUFFER TextBuffer = (PTEXTMODE_SCREEN_BUFFER)Buffer;
1577 COORD cL, cR;
1578 PCHAR_INFO ptrL, ptrR;
1579
1580 /* Starting point */
1581 cL = cR = PointToCoord(GuiData, lParam);
1582 ptrL = ptrR = ConioCoordToPointer(TextBuffer, cL.X, cL.Y);
1583
1584 /* Enlarge the selection by checking for whitespace */
1585 while ((0 < cL.X) && !IS_WORD_SEP(ptrL->Char.UnicodeChar)
1586 && !IS_WORD_SEP((ptrL-1)->Char.UnicodeChar))
1587 {
1588 --cL.X;
1589 --ptrL;
1590 }
1591 while ((cR.X < TextBuffer->ScreenBufferSize.X - 1) &&
1592 !IS_WORD_SEP(ptrR->Char.UnicodeChar) &&
1593 !IS_WORD_SEP((ptrR+1)->Char.UnicodeChar))
1594 {
1595 ++cR.X;
1596 ++ptrR;
1597 }
1598
1599 /*
1600 * Update the selection started with the single
1601 * left-click that preceded this double-click.
1602 */
1603 GuiData->Selection.dwFlags |= CONSOLE_MOUSE_SELECTION | CONSOLE_MOUSE_DOWN;
1604 UpdateSelection(GuiData, &cL, &cR);
1605
1606 /* Ignore the next mouse move signal */
1607 GuiData->IgnoreNextMouseSignal = TRUE;
1608 }
1609
1610 break;
1611 }
1612
1613 case WM_RBUTTONDOWN:
1614 case WM_RBUTTONDBLCLK:
1615 {
1616 if (!(GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY))
1617 {
1618 Paste(GuiData);
1619 }
1620 else
1621 {
1622 Copy(GuiData);
1623 }
1624
1625 /* Ignore the next mouse move signal */
1626 GuiData->IgnoreNextMouseSignal = TRUE;
1627 break;
1628 }
1629
1630 case WM_MOUSEMOVE:
1631 {
1632 if (!(wParam & MK_LBUTTON)) break;
1633 if (!(GuiData->Selection.dwFlags & CONSOLE_MOUSE_DOWN)) break;
1634
1635 // TODO: Scroll buffer to bring SelectionCursor into view
1636 GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1637 UpdateSelection(GuiData, NULL, &GuiData->dwSelectionCursor);
1638
1639 break;
1640 }
1641
1642 default:
1643 Err = FALSE; // TRUE;
1644 break;
1645 }
1646 }
1647 else if (Console->InputBuffer.Mode & ENABLE_MOUSE_INPUT)
1648 {
1649 INPUT_RECORD er;
1650 WORD wKeyState = GET_KEYSTATE_WPARAM(wParam);
1651 DWORD dwButtonState = 0;
1652 DWORD dwControlKeyState = 0;
1653 DWORD dwEventFlags = 0;
1654
1655 switch (msg)
1656 {
1657 case WM_LBUTTONDOWN:
1658 SetCapture(GuiData->hWindow);
1659 dwButtonState = FROM_LEFT_1ST_BUTTON_PRESSED;
1660 dwEventFlags = 0;
1661 break;
1662
1663 case WM_MBUTTONDOWN:
1664 SetCapture(GuiData->hWindow);
1665 dwButtonState = FROM_LEFT_2ND_BUTTON_PRESSED;
1666 dwEventFlags = 0;
1667 break;
1668
1669 case WM_RBUTTONDOWN:
1670 SetCapture(GuiData->hWindow);
1671 dwButtonState = RIGHTMOST_BUTTON_PRESSED;
1672 dwEventFlags = 0;
1673 break;
1674
1675 case WM_LBUTTONUP:
1676 ReleaseCapture();
1677 dwButtonState = 0;
1678 dwEventFlags = 0;
1679 break;
1680
1681 case WM_MBUTTONUP:
1682 ReleaseCapture();
1683 dwButtonState = 0;
1684 dwEventFlags = 0;
1685 break;
1686
1687 case WM_RBUTTONUP:
1688 ReleaseCapture();
1689 dwButtonState = 0;
1690 dwEventFlags = 0;
1691 break;
1692
1693 case WM_LBUTTONDBLCLK:
1694 dwButtonState = FROM_LEFT_1ST_BUTTON_PRESSED;
1695 dwEventFlags = DOUBLE_CLICK;
1696 break;
1697
1698 case WM_MBUTTONDBLCLK:
1699 dwButtonState = FROM_LEFT_2ND_BUTTON_PRESSED;
1700 dwEventFlags = DOUBLE_CLICK;
1701 break;
1702
1703 case WM_RBUTTONDBLCLK:
1704 dwButtonState = RIGHTMOST_BUTTON_PRESSED;
1705 dwEventFlags = DOUBLE_CLICK;
1706 break;
1707
1708 case WM_MOUSEMOVE:
1709 dwButtonState = 0;
1710 dwEventFlags = MOUSE_MOVED;
1711 break;
1712
1713 case WM_MOUSEWHEEL:
1714 dwButtonState = GET_WHEEL_DELTA_WPARAM(wParam) << 16;
1715 dwEventFlags = MOUSE_WHEELED;
1716 break;
1717
1718 case WM_MOUSEHWHEEL:
1719 dwButtonState = GET_WHEEL_DELTA_WPARAM(wParam) << 16;
1720 dwEventFlags = MOUSE_HWHEELED;
1721 break;
1722
1723 default:
1724 Err = TRUE;
1725 break;
1726 }
1727
1728 /*
1729 * HACK FOR CORE-8394: Ignore the next mouse move signal
1730 * just after mouse down click actions.
1731 */
1732 switch (msg)
1733 {
1734 case WM_LBUTTONDOWN:
1735 case WM_MBUTTONDOWN:
1736 case WM_RBUTTONDOWN:
1737 GuiData->IgnoreNextMouseSignal = TRUE;
1738 default:
1739 break;
1740 }
1741
1742 if (!Err)
1743 {
1744 if (wKeyState & MK_LBUTTON)
1745 dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1746 if (wKeyState & MK_MBUTTON)
1747 dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1748 if (wKeyState & MK_RBUTTON)
1749 dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1750
1751 if (GetKeyState(VK_RMENU) & 0x8000)
1752 dwControlKeyState |= RIGHT_ALT_PRESSED;
1753 if (GetKeyState(VK_LMENU) & 0x8000)
1754 dwControlKeyState |= LEFT_ALT_PRESSED;
1755 if (GetKeyState(VK_RCONTROL) & 0x8000)
1756 dwControlKeyState |= RIGHT_CTRL_PRESSED;
1757 if (GetKeyState(VK_LCONTROL) & 0x8000)
1758 dwControlKeyState |= LEFT_CTRL_PRESSED;
1759 if (GetKeyState(VK_SHIFT) & 0x8000)
1760 dwControlKeyState |= SHIFT_PRESSED;
1761 if (GetKeyState(VK_NUMLOCK) & 0x0001)
1762 dwControlKeyState |= NUMLOCK_ON;
1763 if (GetKeyState(VK_SCROLL) & 0x0001)
1764 dwControlKeyState |= SCROLLLOCK_ON;
1765 if (GetKeyState(VK_CAPITAL) & 0x0001)
1766 dwControlKeyState |= CAPSLOCK_ON;
1767 /* See WM_CHAR MSDN documentation for instance */
1768 if (lParam & 0x01000000)
1769 dwControlKeyState |= ENHANCED_KEY;
1770
1771 er.EventType = MOUSE_EVENT;
1772 er.Event.MouseEvent.dwMousePosition = PointToCoord(GuiData, lParam);
1773 er.Event.MouseEvent.dwButtonState = dwButtonState;
1774 er.Event.MouseEvent.dwControlKeyState = dwControlKeyState;
1775 er.Event.MouseEvent.dwEventFlags = dwEventFlags;
1776
1777 ConioProcessInputEvent(Console, &er);
1778 }
1779 }
1780 else
1781 {
1782 Err = TRUE;
1783 }
1784
1785 LeaveCriticalSection(&Console->Lock);
1786
1787 Quit:
1788 if (Err)
1789 return DefWindowProcW(GuiData->hWindow, msg, wParam, lParam);
1790 else
1791 return 0;
1792 }
1793
1794 VOID
1795 GuiCopyFromTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
1796 PGUI_CONSOLE_DATA GuiData);
1797 VOID
1798 GuiCopyFromGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
1799 PGUI_CONSOLE_DATA GuiData);
1800
1801 static VOID
1802 Copy(PGUI_CONSOLE_DATA GuiData)
1803 {
1804 if (OpenClipboard(GuiData->hWindow))
1805 {
1806 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1807
1808 if (GetType(Buffer) == TEXTMODE_BUFFER)
1809 {
1810 GuiCopyFromTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)Buffer, GuiData);
1811 }
1812 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
1813 {
1814 GuiCopyFromGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)Buffer, GuiData);
1815 }
1816
1817 CloseClipboard();
1818 }
1819
1820 /* Clear the selection */
1821 UpdateSelection(GuiData, NULL, NULL);
1822 }
1823
1824 VOID
1825 GuiPasteToTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
1826 PGUI_CONSOLE_DATA GuiData);
1827 VOID
1828 GuiPasteToGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
1829 PGUI_CONSOLE_DATA GuiData);
1830
1831 static VOID
1832 Paste(PGUI_CONSOLE_DATA GuiData)
1833 {
1834 if (OpenClipboard(GuiData->hWindow))
1835 {
1836 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1837
1838 if (GetType(Buffer) == TEXTMODE_BUFFER)
1839 {
1840 GuiPasteToTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)Buffer, GuiData);
1841 }
1842 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
1843 {
1844 GuiPasteToGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)Buffer, GuiData);
1845 }
1846
1847 CloseClipboard();
1848 }
1849 }
1850
1851 static VOID
1852 OnGetMinMaxInfo(PGUI_CONSOLE_DATA GuiData, PMINMAXINFO minMaxInfo)
1853 {
1854 PCONSRV_CONSOLE Console = GuiData->Console;
1855 PCONSOLE_SCREEN_BUFFER ActiveBuffer;
1856 DWORD windx, windy;
1857 UINT WidthUnit, HeightUnit;
1858
1859 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
1860
1861 ActiveBuffer = GuiData->ActiveBuffer;
1862
1863 GetScreenBufferSizeUnits(ActiveBuffer, GuiData, &WidthUnit, &HeightUnit);
1864
1865 windx = CONGUI_MIN_WIDTH * WidthUnit + 2 * (GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE));
1866 windy = CONGUI_MIN_HEIGHT * HeightUnit + 2 * (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE)) + GetSystemMetrics(SM_CYCAPTION);
1867
1868 minMaxInfo->ptMinTrackSize.x = windx;
1869 minMaxInfo->ptMinTrackSize.y = windy;
1870
1871 windx = (ActiveBuffer->ScreenBufferSize.X) * WidthUnit + 2 * (GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE));
1872 windy = (ActiveBuffer->ScreenBufferSize.Y) * HeightUnit + 2 * (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE)) + GetSystemMetrics(SM_CYCAPTION);
1873
1874 if (ActiveBuffer->ViewSize.X < ActiveBuffer->ScreenBufferSize.X) windy += GetSystemMetrics(SM_CYHSCROLL); // window currently has a horizontal scrollbar
1875 if (ActiveBuffer->ViewSize.Y < ActiveBuffer->ScreenBufferSize.Y) windx += GetSystemMetrics(SM_CXVSCROLL); // window currently has a vertical scrollbar
1876
1877 minMaxInfo->ptMaxTrackSize.x = windx;
1878 minMaxInfo->ptMaxTrackSize.y = windy;
1879
1880 LeaveCriticalSection(&Console->Lock);
1881 }
1882
1883 static VOID
1884 OnSize(PGUI_CONSOLE_DATA GuiData, WPARAM wParam, LPARAM lParam)
1885 {
1886 PCONSRV_CONSOLE Console = GuiData->Console;
1887
1888 /* Do nothing if the window is hidden */
1889 if (!GuiData->IsWindowVisible) return;
1890
1891 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return;
1892
1893 if ((GuiData->WindowSizeLock == FALSE) &&
1894 (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED))
1895 {
1896 PCONSOLE_SCREEN_BUFFER Buff = GuiData->ActiveBuffer;
1897 DWORD windx, windy, charx, chary;
1898 UINT WidthUnit, HeightUnit;
1899
1900 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
1901
1902 GuiData->WindowSizeLock = TRUE;
1903
1904 windx = LOWORD(lParam);
1905 windy = HIWORD(lParam);
1906
1907 // Compensate for existing scroll bars (because lParam values do not accommodate scroll bar)
1908 if (Buff->ViewSize.X < Buff->ScreenBufferSize.X) windy += GetSystemMetrics(SM_CYHSCROLL); // window currently has a horizontal scrollbar
1909 if (Buff->ViewSize.Y < Buff->ScreenBufferSize.Y) windx += GetSystemMetrics(SM_CXVSCROLL); // window currently has a vertical scrollbar
1910
1911 charx = windx / (int)WidthUnit ;
1912 chary = windy / (int)HeightUnit;
1913
1914 // Character alignment (round size up or down)
1915 if ((windx % WidthUnit ) >= (WidthUnit / 2)) ++charx;
1916 if ((windy % HeightUnit) >= (HeightUnit / 2)) ++chary;
1917
1918 // Compensate for added scroll bars in new window
1919 if (charx < (DWORD)Buff->ScreenBufferSize.X) windy -= GetSystemMetrics(SM_CYHSCROLL); // new window will have a horizontal scroll bar
1920 if (chary < (DWORD)Buff->ScreenBufferSize.Y) windx -= GetSystemMetrics(SM_CXVSCROLL); // new window will have a vertical scroll bar
1921
1922 charx = windx / (int)WidthUnit ;
1923 chary = windy / (int)HeightUnit;
1924
1925 // Character alignment (round size up or down)
1926 if ((windx % WidthUnit ) >= (WidthUnit / 2)) ++charx;
1927 if ((windy % HeightUnit) >= (HeightUnit / 2)) ++chary;
1928
1929 // Resize window
1930 if ((charx != Buff->ViewSize.X) || (chary != Buff->ViewSize.Y))
1931 {
1932 Buff->ViewSize.X = (charx <= (DWORD)Buff->ScreenBufferSize.X) ? charx : Buff->ScreenBufferSize.X;
1933 Buff->ViewSize.Y = (chary <= (DWORD)Buff->ScreenBufferSize.Y) ? chary : Buff->ScreenBufferSize.Y;
1934 }
1935
1936 ResizeConWnd(GuiData, WidthUnit, HeightUnit);
1937
1938 // Adjust the start of the visible area if we are attempting to show nonexistent areas
1939 if ((Buff->ScreenBufferSize.X - Buff->ViewOrigin.X) < Buff->ViewSize.X) Buff->ViewOrigin.X = Buff->ScreenBufferSize.X - Buff->ViewSize.X;
1940 if ((Buff->ScreenBufferSize.Y - Buff->ViewOrigin.Y) < Buff->ViewSize.Y) Buff->ViewOrigin.Y = Buff->ScreenBufferSize.Y - Buff->ViewSize.Y;
1941 InvalidateRect(GuiData->hWindow, NULL, TRUE);
1942
1943 GuiData->WindowSizeLock = FALSE;
1944 }
1945
1946 LeaveCriticalSection(&Console->Lock);
1947 }
1948
1949 static VOID
1950 OnMove(PGUI_CONSOLE_DATA GuiData)
1951 {
1952 RECT rcWnd;
1953
1954 // TODO: Simplify the code.
1955 // See: GuiConsoleNotifyWndProc() PM_CREATE_CONSOLE.
1956
1957 /* Retrieve our real position */
1958 GetWindowRect(GuiData->hWindow, &rcWnd);
1959 GuiData->GuiInfo.WindowOrigin.x = rcWnd.left;
1960 GuiData->GuiInfo.WindowOrigin.y = rcWnd.top;
1961 }
1962
1963 /*
1964 // HACK: This functionality is standard for general scrollbars. Don't add it by hand.
1965
1966 VOID
1967 GuiConsoleHandleScrollbarMenu(VOID)
1968 {
1969 HMENU hMenu;
1970
1971 hMenu = CreatePopupMenu();
1972 if (hMenu == NULL)
1973 {
1974 DPRINT("CreatePopupMenu failed\n");
1975 return;
1976 }
1977
1978 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1979 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1980 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1981 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1982 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1983 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1984 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1985 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1986 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1987 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1988 }
1989 */
1990
1991 static LRESULT
1992 OnScroll(PGUI_CONSOLE_DATA GuiData, UINT uMsg, WPARAM wParam)
1993 {
1994 PCONSRV_CONSOLE Console = GuiData->Console;
1995 PCONSOLE_SCREEN_BUFFER Buff;
1996 SCROLLINFO sInfo;
1997 int fnBar;
1998 int old_pos, Maximum;
1999 PSHORT pShowXY;
2000
2001 if (!ConDrvValidateConsoleUnsafe((PCONSOLE)Console, CONSOLE_RUNNING, TRUE)) return 0;
2002
2003 Buff = GuiData->ActiveBuffer;
2004
2005 if (uMsg == WM_HSCROLL)
2006 {
2007 fnBar = SB_HORZ;
2008 Maximum = Buff->ScreenBufferSize.X - Buff->ViewSize.X;
2009 pShowXY = &Buff->ViewOrigin.X;
2010 }
2011 else
2012 {
2013 fnBar = SB_VERT;
2014 Maximum = Buff->ScreenBufferSize.Y - Buff->ViewSize.Y;
2015 pShowXY = &Buff->ViewOrigin.Y;
2016 }
2017
2018 /* set scrollbar sizes */
2019 sInfo.cbSize = sizeof(SCROLLINFO);
2020 sInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE | SIF_TRACKPOS;
2021
2022 if (!GetScrollInfo(GuiData->hWindow, fnBar, &sInfo)) goto Quit;
2023
2024 old_pos = sInfo.nPos;
2025
2026 switch (LOWORD(wParam))
2027 {
2028 case SB_LINELEFT:
2029 sInfo.nPos -= 1;
2030 break;
2031
2032 case SB_LINERIGHT:
2033 sInfo.nPos += 1;
2034 break;
2035
2036 case SB_PAGELEFT:
2037 sInfo.nPos -= sInfo.nPage;
2038 break;
2039
2040 case SB_PAGERIGHT:
2041 sInfo.nPos += sInfo.nPage;
2042 break;
2043
2044 case SB_THUMBTRACK:
2045 sInfo.nPos = sInfo.nTrackPos;
2046 ConioPause(Console, PAUSED_FROM_SCROLLBAR);
2047 break;
2048
2049 case SB_THUMBPOSITION:
2050 ConioUnpause(Console, PAUSED_FROM_SCROLLBAR);
2051 break;
2052
2053 case SB_TOP:
2054 sInfo.nPos = sInfo.nMin;
2055 break;
2056
2057 case SB_BOTTOM:
2058 sInfo.nPos = sInfo.nMax;
2059 break;
2060
2061 default:
2062 break;
2063 }
2064
2065 sInfo.nPos = max(sInfo.nPos, 0);
2066 sInfo.nPos = min(sInfo.nPos, Maximum);
2067
2068 if (old_pos != sInfo.nPos)
2069 {
2070 USHORT OldX = Buff->ViewOrigin.X;
2071 USHORT OldY = Buff->ViewOrigin.Y;
2072 UINT WidthUnit, HeightUnit;
2073
2074 *pShowXY = sInfo.nPos;
2075
2076 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
2077
2078 ScrollWindowEx(GuiData->hWindow,
2079 (OldX - Buff->ViewOrigin.X) * WidthUnit ,
2080 (OldY - Buff->ViewOrigin.Y) * HeightUnit,
2081 NULL,
2082 NULL,
2083 NULL,
2084 NULL,
2085 SW_INVALIDATE);
2086
2087 sInfo.fMask = SIF_POS;
2088 SetScrollInfo(GuiData->hWindow, fnBar, &sInfo, TRUE);
2089
2090 UpdateWindow(GuiData->hWindow);
2091 // InvalidateRect(GuiData->hWindow, NULL, FALSE);
2092 }
2093
2094 Quit:
2095 LeaveCriticalSection(&Console->Lock);
2096 return 0;
2097 }
2098
2099
2100 static LRESULT CALLBACK
2101 ConWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2102 {
2103 LRESULT Result = 0;
2104 PGUI_CONSOLE_DATA GuiData = NULL;
2105 PCONSRV_CONSOLE Console = NULL;
2106
2107 /*
2108 * - If it's the first time we create a window for the terminal,
2109 * just initialize it and return.
2110 *
2111 * - If we are destroying the window, just do it and return.
2112 */
2113 if (msg == WM_NCCREATE)
2114 {
2115 return (LRESULT)OnNcCreate(hWnd, (LPCREATESTRUCTW)lParam);
2116 }
2117 else if (msg == WM_NCDESTROY)
2118 {
2119 return OnNcDestroy(hWnd);
2120 }
2121
2122 /*
2123 * Now the terminal window is initialized.
2124 * Get the terminal data via the window's data.
2125 * If there is no data, just go away.
2126 */
2127 GuiData = GuiGetGuiData(hWnd);
2128 if (GuiData == NULL) return DefWindowProcW(hWnd, msg, wParam, lParam);
2129
2130 // TEMPORARY HACK until all of the functions can deal with a NULL GuiData->ActiveBuffer ...
2131 if (GuiData->ActiveBuffer == NULL) return DefWindowProcW(hWnd, msg, wParam, lParam);
2132
2133 /*
2134 * Just retrieve a pointer to the console in case somebody needs it.
2135 * It is not NULL because it was checked in GuiGetGuiData.
2136 * Each helper function which needs the console has to validate and lock it.
2137 */
2138 Console = GuiData->Console;
2139
2140 /* We have a console, start message dispatching */
2141 switch (msg)
2142 {
2143 case WM_ACTIVATE:
2144 OnActivate(GuiData, wParam);
2145 break;
2146
2147 case WM_CLOSE:
2148 if (OnClose(GuiData)) goto Default;
2149 break;
2150
2151 case WM_PAINT:
2152 OnPaint(GuiData);
2153 break;
2154
2155 case WM_TIMER:
2156 OnTimer(GuiData);
2157 break;
2158
2159 case WM_PALETTECHANGED:
2160 {
2161 DPRINT("WM_PALETTECHANGED called\n");
2162
2163 /*
2164 * Protects against infinite loops:
2165 * "... A window that receives this message must not realize
2166 * its palette, unless it determines that wParam does not contain
2167 * its own window handle." (WM_PALETTECHANGED description - MSDN)
2168 *
2169 * This message is sent to all windows, including the one that
2170 * changed the system palette and caused this message to be sent.
2171 * The wParam of this message contains the handle of the window
2172 * that caused the system palette to change. To avoid an infinite
2173 * loop, care must be taken to check that the wParam of this message
2174 * does not match the window's handle.
2175 */
2176 if ((HWND)wParam == hWnd) break;
2177
2178 DPRINT("WM_PALETTECHANGED ok\n");
2179 OnPaletteChanged(GuiData);
2180 DPRINT("WM_PALETTECHANGED quit\n");
2181 break;
2182 }
2183
2184 case WM_KEYDOWN:
2185 case WM_KEYUP:
2186 case WM_CHAR:
2187 case WM_DEADCHAR:
2188 case WM_SYSKEYDOWN:
2189 case WM_SYSKEYUP:
2190 case WM_SYSCHAR:
2191 case WM_SYSDEADCHAR:
2192 {
2193 /* Detect Alt-Enter presses and switch back and forth to fullscreen mode */
2194 if (msg == WM_SYSKEYDOWN && (HIWORD(lParam) & KF_ALTDOWN) && wParam == VK_RETURN)
2195 {
2196 /* Switch only at first Alt-Enter press, and ignore subsequent key repetitions */
2197 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
2198 GuiConsoleSwitchFullScreen(GuiData);
2199
2200 break;
2201 }
2202 /* Detect Alt-Esc/Space/Tab presses defer to DefWindowProc */
2203 if ( (HIWORD(lParam) & KF_ALTDOWN) && (wParam == VK_ESCAPE || wParam == VK_SPACE || wParam == VK_TAB))
2204 {
2205 return DefWindowProcW(hWnd, msg, wParam, lParam);
2206 }
2207
2208 OnKey(GuiData, msg, wParam, lParam);
2209 break;
2210 }
2211
2212 case WM_SETCURSOR:
2213 {
2214 /* Do nothing if the window is hidden */
2215 if (!GuiData->IsWindowVisible) goto Default;
2216
2217 /*
2218 * The message was sent because we are manually triggering a change.
2219 * Check whether the mouse is indeed present on this console window
2220 * and take appropriate decisions.
2221 */
2222 if (wParam == -1 && lParam == -1)
2223 {
2224 POINT mouseCoords;
2225 HWND hWndHit;
2226
2227 /* Get the placement of the mouse */
2228 GetCursorPos(&mouseCoords);
2229
2230 /* On which window is placed the mouse ? */
2231 hWndHit = WindowFromPoint(mouseCoords);
2232
2233 /* It's our window. Perform the hit-test to be used later on. */
2234 if (hWndHit == hWnd)
2235 {
2236 wParam = (WPARAM)hWnd;
2237 lParam = DefWindowProcW(hWndHit, WM_NCHITTEST, 0,
2238 MAKELPARAM(mouseCoords.x, mouseCoords.y));
2239 }
2240 }
2241
2242 /* Set the mouse cursor only when we are in the client area */
2243 if ((HWND)wParam == hWnd && LOWORD(lParam) == HTCLIENT)
2244 {
2245 if (GuiData->MouseCursorRefCount >= 0)
2246 {
2247 /* Show the cursor */
2248 SetCursor(GuiData->hCursor);
2249 }
2250 else
2251 {
2252 /* Hide the cursor if the reference count is negative */
2253 SetCursor(NULL);
2254 }
2255 return TRUE;
2256 }
2257 else
2258 {
2259 goto Default;
2260 }
2261 }
2262
2263 case WM_LBUTTONDOWN:
2264 case WM_MBUTTONDOWN:
2265 case WM_RBUTTONDOWN:
2266 case WM_LBUTTONUP:
2267 case WM_MBUTTONUP:
2268 case WM_RBUTTONUP:
2269 case WM_LBUTTONDBLCLK:
2270 case WM_MBUTTONDBLCLK:
2271 case WM_RBUTTONDBLCLK:
2272 case WM_MOUSEMOVE:
2273 case WM_MOUSEWHEEL:
2274 case WM_MOUSEHWHEEL:
2275 {
2276 Result = OnMouse(GuiData, msg, wParam, lParam);
2277 break;
2278 }
2279
2280 case WM_HSCROLL:
2281 case WM_VSCROLL:
2282 {
2283 Result = OnScroll(GuiData, msg, wParam);
2284 break;
2285 }
2286
2287 case WM_CONTEXTMENU:
2288 {
2289 /* Do nothing if the window is hidden */
2290 if (!GuiData->IsWindowVisible) break;
2291
2292 if (DefWindowProcW(hWnd /*GuiData->hWindow*/, WM_NCHITTEST, 0, lParam) == HTCLIENT)
2293 {
2294 HMENU hMenu = CreatePopupMenu();
2295 if (hMenu != NULL)
2296 {
2297 AppendMenuItems(hMenu, GuiConsoleEditMenuItems);
2298 TrackPopupMenuEx(hMenu,
2299 TPM_RIGHTBUTTON,
2300 GET_X_LPARAM(lParam),
2301 GET_Y_LPARAM(lParam),
2302 hWnd,
2303 NULL);
2304 DestroyMenu(hMenu);
2305 }
2306 break;
2307 }
2308 else
2309 {
2310 goto Default;
2311 }
2312 }
2313
2314 case WM_INITMENU:
2315 {
2316 HMENU hMenu = (HMENU)wParam;
2317 if (hMenu != NULL)
2318 {
2319 /* Enable or disable the Close menu item */
2320 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND |
2321 (GuiData->IsCloseButtonEnabled ? MF_ENABLED : MF_GRAYED));
2322
2323 /* Enable or disable the Copy and Paste items */
2324 EnableMenuItem(hMenu, ID_SYSTEM_EDIT_COPY , MF_BYCOMMAND |
2325 ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) &&
2326 (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY) ? MF_ENABLED : MF_GRAYED));
2327 // FIXME: Following whether the active screen buffer is text-mode
2328 // or graphics-mode, search for CF_UNICODETEXT or CF_BITMAP formats.
2329 EnableMenuItem(hMenu, ID_SYSTEM_EDIT_PASTE, MF_BYCOMMAND |
2330 (!(GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) &&
2331 IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
2332 }
2333
2334 SendMenuEvent(Console, WM_INITMENU);
2335 break;
2336 }
2337
2338 case WM_MENUSELECT:
2339 {
2340 if (HIWORD(wParam) == 0xFFFF) // Allow all the menu flags
2341 {
2342 SendMenuEvent(Console, WM_MENUSELECT);
2343 }
2344 break;
2345 }
2346
2347 case WM_COMMAND:
2348 case WM_SYSCOMMAND:
2349 {
2350 Result = OnCommand(GuiData, wParam, lParam);
2351 break;
2352 }
2353
2354 case WM_SETFOCUS:
2355 case WM_KILLFOCUS:
2356 OnFocus(GuiData, (msg == WM_SETFOCUS));
2357 break;
2358
2359 case WM_GETMINMAXINFO:
2360 OnGetMinMaxInfo(GuiData, (PMINMAXINFO)lParam);
2361 break;
2362
2363 case WM_MOVE:
2364 OnMove(GuiData);
2365 break;
2366
2367 #if 0 // This code is here to prepare & control dynamic console SB resizing.
2368 case WM_SIZING:
2369 {
2370 PRECT dragRect = (PRECT)lParam;
2371 switch (wParam)
2372 {
2373 case WMSZ_LEFT:
2374 DPRINT1("WMSZ_LEFT\n");
2375 break;
2376 case WMSZ_RIGHT:
2377 DPRINT1("WMSZ_RIGHT\n");
2378 break;
2379 case WMSZ_TOP:
2380 DPRINT1("WMSZ_TOP\n");
2381 break;
2382 case WMSZ_TOPLEFT:
2383 DPRINT1("WMSZ_TOPLEFT\n");
2384 break;
2385 case WMSZ_TOPRIGHT:
2386 DPRINT1("WMSZ_TOPRIGHT\n");
2387 break;
2388 case WMSZ_BOTTOM:
2389 DPRINT1("WMSZ_BOTTOM\n");
2390 break;
2391 case WMSZ_BOTTOMLEFT:
2392 DPRINT1("WMSZ_BOTTOMLEFT\n");
2393 break;
2394 case WMSZ_BOTTOMRIGHT:
2395 DPRINT1("WMSZ_BOTTOMRIGHT\n");
2396 break;
2397 default:
2398 DPRINT1("wParam = %d\n", wParam);
2399 break;
2400 }
2401 DPRINT1("dragRect = {.left = %d ; .top = %d ; .right = %d ; .bottom = %d}\n",
2402 dragRect->left, dragRect->top, dragRect->right, dragRect->bottom);
2403 break;
2404 }
2405 #endif
2406
2407 case WM_SIZE:
2408 OnSize(GuiData, wParam, lParam);
2409 break;
2410
2411 case PM_RESIZE_TERMINAL:
2412 {
2413 PCONSOLE_SCREEN_BUFFER Buff = GuiData->ActiveBuffer;
2414 HDC hDC;
2415 HBITMAP hnew, hold;
2416
2417 DWORD Width, Height;
2418 UINT WidthUnit, HeightUnit;
2419
2420 /* Do nothing if the window is hidden */
2421 if (!GuiData->IsWindowVisible) break;
2422
2423 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
2424
2425 Width = Buff->ScreenBufferSize.X * WidthUnit ;
2426 Height = Buff->ScreenBufferSize.Y * HeightUnit;
2427
2428 /* Recreate the framebuffer */
2429 hDC = GetDC(GuiData->hWindow);
2430 hnew = CreateCompatibleBitmap(hDC, Width, Height);
2431 ReleaseDC(GuiData->hWindow, hDC);
2432 hold = SelectObject(GuiData->hMemDC, hnew);
2433 if (GuiData->hBitmap)
2434 {
2435 if (hold == GuiData->hBitmap) DeleteObject(GuiData->hBitmap);
2436 }
2437 GuiData->hBitmap = hnew;
2438
2439 /* Resize the window to the user's values */
2440 GuiData->WindowSizeLock = TRUE;
2441 ResizeConWnd(GuiData, WidthUnit, HeightUnit);
2442 GuiData->WindowSizeLock = FALSE;
2443 break;
2444 }
2445
2446 /*
2447 * Undocumented message sent by Windows' console.dll for applying console info.
2448 * See http://www.catch22.net/sites/default/source/files/setconsoleinfo.c
2449 * and http://www.scn.rain.com/~neighorn/PDF/MSBugPaper.pdf
2450 * for more information.
2451 */
2452 case WM_SETCONSOLEINFO:
2453 {
2454 GuiApplyUserSettings(GuiData, (HANDLE)wParam);
2455 break;
2456 }
2457
2458 case PM_CONSOLE_BEEP:
2459 DPRINT1("Beep\n");
2460 Beep(800, 200);
2461 break;
2462
2463 // case PM_CONSOLE_SET_TITLE:
2464 // SetWindowTextW(GuiData->hWindow, GuiData->Console->Title.Buffer);
2465 // break;
2466
2467 default: Default:
2468 Result = DefWindowProcW(hWnd, msg, wParam, lParam);
2469 break;
2470 }
2471
2472 return Result;
2473 }
2474
2475 /* EOF */