[ConSrv]
[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 = ConDrvGetConsoleLeaderProcess(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 sizeof(szMenuString) / sizeof(szMenuString[0])) > 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(PCONSOLE Console, UINT CmdId)
286 {
287 INPUT_RECORD er;
288
289 DPRINT1("Menu item ID: %d\n", CmdId);
290
291 if (!ConDrvValidateConsoleUnsafe(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 PCONSOLE 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 static BOOL
490 OnNcCreate(HWND hWnd, LPCREATESTRUCTW Create)
491 {
492 PGUI_CONSOLE_DATA GuiData = (PGUI_CONSOLE_DATA)Create->lpCreateParams;
493 PCONSOLE Console;
494 HDC hDC;
495 HFONT OldFont;
496 TEXTMETRICW Metrics;
497 SIZE CharSize;
498
499 if (NULL == GuiData)
500 {
501 DPRINT1("GuiConsoleNcCreate: No GUI data\n");
502 return FALSE;
503 }
504
505 Console = GuiData->Console;
506
507 GuiData->hWindow = hWnd;
508
509 GuiData->Font = CreateFontW(LOWORD(GuiData->GuiInfo.FontSize),
510 0, // HIWORD(GuiData->GuiInfo.FontSize),
511 0,
512 TA_BASELINE,
513 GuiData->GuiInfo.FontWeight,
514 FALSE,
515 FALSE,
516 FALSE,
517 OEM_CHARSET,
518 OUT_DEFAULT_PRECIS,
519 CLIP_DEFAULT_PRECIS,
520 NONANTIALIASED_QUALITY,
521 FIXED_PITCH | GuiData->GuiInfo.FontFamily /* FF_DONTCARE */,
522 GuiData->GuiInfo.FaceName);
523
524 if (NULL == GuiData->Font)
525 {
526 DPRINT1("GuiConsoleNcCreate: CreateFont failed\n");
527 GuiData->hWindow = NULL;
528 SetEvent(GuiData->hGuiInitEvent);
529 return FALSE;
530 }
531 hDC = GetDC(GuiData->hWindow);
532 if (NULL == hDC)
533 {
534 DPRINT1("GuiConsoleNcCreate: GetDC failed\n");
535 DeleteObject(GuiData->Font);
536 GuiData->hWindow = NULL;
537 SetEvent(GuiData->hGuiInitEvent);
538 return FALSE;
539 }
540 OldFont = SelectObject(hDC, GuiData->Font);
541 if (NULL == OldFont)
542 {
543 DPRINT1("GuiConsoleNcCreate: SelectObject failed\n");
544 ReleaseDC(GuiData->hWindow, hDC);
545 DeleteObject(GuiData->Font);
546 GuiData->hWindow = NULL;
547 SetEvent(GuiData->hGuiInitEvent);
548 return FALSE;
549 }
550 if (!GetTextMetricsW(hDC, &Metrics))
551 {
552 DPRINT1("GuiConsoleNcCreate: GetTextMetrics failed\n");
553 SelectObject(hDC, OldFont);
554 ReleaseDC(GuiData->hWindow, hDC);
555 DeleteObject(GuiData->Font);
556 GuiData->hWindow = NULL;
557 SetEvent(GuiData->hGuiInitEvent);
558 return FALSE;
559 }
560 GuiData->CharWidth = Metrics.tmMaxCharWidth;
561 GuiData->CharHeight = Metrics.tmHeight + Metrics.tmExternalLeading;
562
563 /* Measure real char width more precisely if possible. */
564 if (GetTextExtentPoint32W(hDC, L"R", 1, &CharSize))
565 GuiData->CharWidth = CharSize.cx;
566
567 SelectObject(hDC, OldFont);
568
569 ReleaseDC(GuiData->hWindow, hDC);
570
571 /* Initialize the terminal framebuffer */
572 GuiData->hMemDC = CreateCompatibleDC(NULL);
573 GuiData->hBitmap = NULL;
574 GuiData->hSysPalette = NULL; /* Original system palette */
575
576 /* Update the icons of the window */
577 if (GuiData->hIcon != ghDefaultIcon)
578 {
579 DefWindowProcW(GuiData->hWindow, WM_SETICON, ICON_BIG , (LPARAM)GuiData->hIcon );
580 DefWindowProcW(GuiData->hWindow, WM_SETICON, ICON_SMALL, (LPARAM)GuiData->hIconSm);
581 }
582
583 // FIXME: Keep these instructions here ? ///////////////////////////////////
584 Console->ActiveBuffer->CursorBlinkOn = TRUE;
585 Console->ActiveBuffer->ForceCursorOff = FALSE;
586 ////////////////////////////////////////////////////////////////////////////
587
588 SetWindowLongPtrW(GuiData->hWindow, GWLP_USERDATA, (DWORD_PTR)GuiData);
589
590 SetTimer(GuiData->hWindow, CONGUI_UPDATE_TIMER, CONGUI_UPDATE_TIME, NULL);
591 //CreateSysMenu(GuiData->hWindow);
592
593 DPRINT("OnNcCreate - setting start event\n");
594 SetEvent(GuiData->hGuiInitEvent);
595
596 return (BOOL)DefWindowProcW(GuiData->hWindow, WM_NCCREATE, 0, (LPARAM)Create);
597 }
598
599
600 BOOL
601 EnterFullScreen(PGUI_CONSOLE_DATA GuiData);
602 VOID
603 LeaveFullScreen(PGUI_CONSOLE_DATA GuiData);
604 VOID
605 SwitchFullScreen(PGUI_CONSOLE_DATA GuiData, BOOL FullScreen);
606 VOID
607 GuiConsoleSwitchFullScreen(PGUI_CONSOLE_DATA GuiData);
608
609 static VOID
610 OnActivate(PGUI_CONSOLE_DATA GuiData, WPARAM wParam)
611 {
612 PCONSOLE Console = GuiData->Console;
613 WORD ActivationState = LOWORD(wParam);
614
615 DPRINT1("WM_ACTIVATE - ActivationState = %d\n");
616
617 if ( ActivationState == WA_ACTIVE ||
618 ActivationState == WA_CLICKACTIVE )
619 {
620 if (GuiData->GuiInfo.FullScreen)
621 {
622 EnterFullScreen(GuiData);
623 // // PostMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_RESTORE, 0);
624 // SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_RESTORE, 0);
625 }
626 }
627 else // if (ActivationState == WA_INACTIVE)
628 {
629 if (GuiData->GuiInfo.FullScreen)
630 {
631 SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
632 LeaveFullScreen(GuiData);
633 // // PostMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
634 // SendMessageW(GuiData->hWindow, WM_SYSCOMMAND, SC_MINIMIZE, 0);
635 }
636 }
637
638 /*
639 * When we are in QuickEdit mode, ignore the next mouse signal
640 * when we are going to be enabled again via the mouse, in order
641 * to prevent e.g. an erroneous right-click from the user which
642 * would have as an effect to paste some unwanted text...
643 */
644 if (Console->QuickEdit && (ActivationState == WA_CLICKACTIVE))
645 GuiData->IgnoreNextMouseSignal = TRUE;
646 }
647
648 static VOID
649 OnFocus(PGUI_CONSOLE_DATA GuiData, BOOL SetFocus)
650 {
651 PCONSOLE Console = GuiData->Console;
652 INPUT_RECORD er;
653
654 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return;
655
656 er.EventType = FOCUS_EVENT;
657 er.Event.FocusEvent.bSetFocus = SetFocus;
658 ConioProcessInputEvent(Console, &er);
659
660 LeaveCriticalSection(&Console->Lock);
661
662 if (SetFocus)
663 DPRINT1("TODO: Create console caret\n");
664 else
665 DPRINT1("TODO: Destroy console caret\n");
666 }
667
668 static VOID
669 SmallRectToRect(PGUI_CONSOLE_DATA GuiData, PRECT Rect, PSMALL_RECT SmallRect)
670 {
671 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
672 UINT WidthUnit, HeightUnit;
673
674 GetScreenBufferSizeUnits(Buffer, GuiData, &WidthUnit, &HeightUnit);
675
676 Rect->left = (SmallRect->Left - Buffer->ViewOrigin.X) * WidthUnit ;
677 Rect->top = (SmallRect->Top - Buffer->ViewOrigin.Y) * HeightUnit;
678 Rect->right = (SmallRect->Right + 1 - Buffer->ViewOrigin.X) * WidthUnit ;
679 Rect->bottom = (SmallRect->Bottom + 1 - Buffer->ViewOrigin.Y) * HeightUnit;
680 }
681
682 VOID
683 GetSelectionBeginEnd(PCOORD Begin, PCOORD End,
684 PCOORD SelectionAnchor,
685 PSMALL_RECT SmallRect)
686 {
687 if (Begin == NULL || End == NULL) return;
688
689 *Begin = *SelectionAnchor;
690 End->X = (SelectionAnchor->X == SmallRect->Left) ? SmallRect->Right
691 /* Case X != Left, must be == Right */ : SmallRect->Left;
692 End->Y = (SelectionAnchor->Y == SmallRect->Top ) ? SmallRect->Bottom
693 /* Case Y != Top, must be == Bottom */ : SmallRect->Top;
694
695 /* Exchange Begin / End if Begin > End lexicographically */
696 if (Begin->Y > End->Y || (Begin->Y == End->Y && Begin->X > End->X))
697 {
698 End->X = _InterlockedExchange16(&Begin->X, End->X);
699 End->Y = _InterlockedExchange16(&Begin->Y, End->Y);
700 }
701 }
702
703 static HRGN
704 CreateSelectionRgn(PGUI_CONSOLE_DATA GuiData,
705 BOOL LineSelection,
706 PCOORD SelectionAnchor,
707 PSMALL_RECT SmallRect)
708 {
709 if (!LineSelection)
710 {
711 RECT rect;
712 SmallRectToRect(GuiData, &rect, SmallRect);
713 return CreateRectRgnIndirect(&rect);
714 }
715 else
716 {
717 HRGN SelRgn;
718 COORD Begin, End;
719
720 GetSelectionBeginEnd(&Begin, &End, SelectionAnchor, SmallRect);
721
722 if (Begin.Y == End.Y)
723 {
724 SMALL_RECT sr;
725 RECT r ;
726
727 sr.Left = Begin.X;
728 sr.Top = Begin.Y;
729 sr.Right = End.X;
730 sr.Bottom = End.Y;
731
732 // Debug thingie to see whether I can put this corner case
733 // together with the previous one.
734 if (SmallRect->Left != sr.Left ||
735 SmallRect->Top != sr.Top ||
736 SmallRect->Right != sr.Right ||
737 SmallRect->Bottom != sr.Bottom)
738 {
739 DPRINT1("\n"
740 "SmallRect = (%d, %d, %d, %d)\n"
741 "sr = (%d, %d, %d, %d)\n"
742 "\n",
743 SmallRect->Left, SmallRect->Top, SmallRect->Right, SmallRect->Bottom,
744 sr.Left, sr.Top, sr.Right, sr.Bottom);
745 }
746
747 SmallRectToRect(GuiData, &r, &sr);
748 SelRgn = CreateRectRgnIndirect(&r);
749 }
750 else
751 {
752 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
753
754 HRGN rg1, rg2, rg3;
755 SMALL_RECT sr1, sr2, sr3;
756 RECT r1 , r2 , r3 ;
757
758 sr1.Left = Begin.X;
759 sr1.Top = Begin.Y;
760 sr1.Right = ActiveBuffer->ScreenBufferSize.X - 1;
761 sr1.Bottom = Begin.Y;
762
763 sr2.Left = 0;
764 sr2.Top = Begin.Y + 1;
765 sr2.Right = ActiveBuffer->ScreenBufferSize.X - 1;
766 sr2.Bottom = End.Y - 1;
767
768 sr3.Left = 0;
769 sr3.Top = End.Y;
770 sr3.Right = End.X;
771 sr3.Bottom = End.Y;
772
773 SmallRectToRect(GuiData, &r1, &sr1);
774 SmallRectToRect(GuiData, &r2, &sr2);
775 SmallRectToRect(GuiData, &r3, &sr3);
776
777 rg1 = CreateRectRgnIndirect(&r1);
778 rg2 = CreateRectRgnIndirect(&r2);
779 rg3 = CreateRectRgnIndirect(&r3);
780
781 CombineRgn(rg1, rg1, rg2, RGN_XOR);
782 CombineRgn(rg1, rg1, rg3, RGN_XOR);
783 DeleteObject(rg3);
784 DeleteObject(rg2);
785
786 SelRgn = rg1;
787 }
788
789 return SelRgn;
790 }
791 }
792
793 static VOID
794 PaintSelectionRect(PGUI_CONSOLE_DATA GuiData, PPAINTSTRUCT pps)
795 {
796 HRGN rgnPaint = CreateRectRgnIndirect(&pps->rcPaint);
797 HRGN rgnSel = CreateSelectionRgn(GuiData, GuiData->LineSelection,
798 &GuiData->Selection.dwSelectionAnchor,
799 &GuiData->Selection.srSelection);
800
801 /* Invert the selection */
802
803 int ErrorCode = CombineRgn(rgnPaint, rgnPaint, rgnSel, RGN_AND);
804 if (ErrorCode != ERROR && ErrorCode != NULLREGION)
805 {
806 InvertRgn(pps->hdc, rgnPaint);
807 }
808
809 DeleteObject(rgnSel);
810 DeleteObject(rgnPaint);
811 }
812
813 static VOID
814 UpdateSelection(PGUI_CONSOLE_DATA GuiData,
815 PCOORD SelectionAnchor OPTIONAL,
816 PCOORD coord)
817 {
818 PCONSOLE Console = GuiData->Console;
819 HRGN oldRgn = CreateSelectionRgn(GuiData, GuiData->LineSelection,
820 &GuiData->Selection.dwSelectionAnchor,
821 &GuiData->Selection.srSelection);
822
823 /* Update the anchor if needed (use the old one if NULL) */
824 if (SelectionAnchor)
825 GuiData->Selection.dwSelectionAnchor = *SelectionAnchor;
826
827 if (coord != NULL)
828 {
829 SMALL_RECT rc;
830 HRGN newRgn;
831
832 /*
833 * Pressing the Control key while selecting text, allows us to enter
834 * into line-selection mode, the selection mode of *nix terminals.
835 */
836 BOOL OldLineSel = GuiData->LineSelection;
837 GuiData->LineSelection = !!(GetKeyState(VK_CONTROL) & 0x8000);
838
839 /* Exchange left/top with right/bottom if required */
840 rc.Left = min(GuiData->Selection.dwSelectionAnchor.X, coord->X);
841 rc.Top = min(GuiData->Selection.dwSelectionAnchor.Y, coord->Y);
842 rc.Right = max(GuiData->Selection.dwSelectionAnchor.X, coord->X);
843 rc.Bottom = max(GuiData->Selection.dwSelectionAnchor.Y, coord->Y);
844
845 newRgn = CreateSelectionRgn(GuiData, GuiData->LineSelection,
846 &GuiData->Selection.dwSelectionAnchor,
847 &rc);
848
849 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
850 {
851 if (OldLineSel != GuiData->LineSelection ||
852 memcmp(&rc, &GuiData->Selection.srSelection, sizeof(SMALL_RECT)) != 0)
853 {
854 /* Calculate the region that needs to be updated */
855 if (oldRgn && newRgn && CombineRgn(newRgn, newRgn, oldRgn, RGN_XOR) != ERROR)
856 {
857 InvalidateRgn(GuiData->hWindow, newRgn, FALSE);
858 }
859 }
860 }
861 else
862 {
863 InvalidateRgn(GuiData->hWindow, newRgn, FALSE);
864 }
865
866 DeleteObject(newRgn);
867
868 GuiData->Selection.dwFlags |= CONSOLE_SELECTION_NOT_EMPTY;
869 GuiData->Selection.srSelection = rc;
870 GuiData->dwSelectionCursor = *coord;
871
872 if ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) == 0)
873 {
874 LPWSTR SelTypeStr = NULL , WindowTitle = NULL;
875 SIZE_T SelTypeStrLength = 0, Length = 0;
876
877 /* Clear the old selection */
878 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
879 {
880 InvalidateRgn(GuiData->hWindow, oldRgn, FALSE);
881 }
882
883 /*
884 * When passing a zero-length buffer size, LoadString(...) returns
885 * a read-only pointer buffer to the program's resource string.
886 */
887 SelTypeStrLength =
888 LoadStringW(ConSrvDllInstance,
889 (GuiData->Selection.dwFlags & CONSOLE_MOUSE_SELECTION)
890 ? IDS_SELECT_TITLE : IDS_MARK_TITLE,
891 (LPWSTR)&SelTypeStr, 0);
892
893 /*
894 * Prepend the selection type string to the current console title
895 * if we succeeded in retrieving a valid localized string.
896 */
897 if (SelTypeStr)
898 {
899 // 3 for " - " and 1 for NULL
900 Length = Console->Title.Length + (SelTypeStrLength + 3 + 1) * sizeof(WCHAR);
901 WindowTitle = ConsoleAllocHeap(0, Length);
902
903 wcsncpy(WindowTitle, SelTypeStr, SelTypeStrLength);
904 WindowTitle[SelTypeStrLength] = L'\0';
905 wcscat(WindowTitle, L" - ");
906 wcscat(WindowTitle, Console->Title.Buffer);
907
908 SetWindowText(GuiData->hWindow, WindowTitle);
909 ConsoleFreeHeap(WindowTitle);
910 }
911
912 GuiData->Selection.dwFlags |= CONSOLE_SELECTION_IN_PROGRESS;
913 ConioPause(Console, PAUSED_FROM_SELECTION);
914 }
915 }
916 else
917 {
918 /* Clear the selection */
919 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
920 {
921 InvalidateRgn(GuiData->hWindow, oldRgn, FALSE);
922 }
923
924 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
925 ConioUnpause(Console, PAUSED_FROM_SELECTION);
926
927 /* Restore the console title */
928 SetWindowText(GuiData->hWindow, Console->Title.Buffer);
929 }
930
931 DeleteObject(oldRgn);
932 }
933
934
935 VOID
936 GuiPaintTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
937 PGUI_CONSOLE_DATA GuiData,
938 PRECT rcView,
939 PRECT rcFramebuffer);
940 VOID
941 GuiPaintGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
942 PGUI_CONSOLE_DATA GuiData,
943 PRECT rcView,
944 PRECT rcFramebuffer);
945
946 static VOID
947 OnPaint(PGUI_CONSOLE_DATA GuiData)
948 {
949 PCONSOLE_SCREEN_BUFFER ActiveBuffer;
950 PAINTSTRUCT ps;
951 RECT rcPaint;
952
953 ActiveBuffer = GuiData->ActiveBuffer;
954
955 BeginPaint(GuiData->hWindow, &ps);
956 if (ps.hdc != NULL &&
957 ps.rcPaint.left < ps.rcPaint.right &&
958 ps.rcPaint.top < ps.rcPaint.bottom)
959 {
960 EnterCriticalSection(&GuiData->Lock);
961
962 /* Compose the current screen-buffer on-memory */
963 if (GetType(ActiveBuffer) == TEXTMODE_BUFFER)
964 {
965 GuiPaintTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)ActiveBuffer,
966 GuiData, &ps.rcPaint, &rcPaint);
967 }
968 else /* if (GetType(ActiveBuffer) == GRAPHICS_BUFFER) */
969 {
970 GuiPaintGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)ActiveBuffer,
971 GuiData, &ps.rcPaint, &rcPaint);
972 }
973
974 /* Send it to screen */
975 BitBlt(ps.hdc,
976 ps.rcPaint.left,
977 ps.rcPaint.top,
978 rcPaint.right - rcPaint.left,
979 rcPaint.bottom - rcPaint.top,
980 GuiData->hMemDC,
981 rcPaint.left,
982 rcPaint.top,
983 SRCCOPY);
984
985 /* Draw the selection region if needed */
986 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY)
987 {
988 PaintSelectionRect(GuiData, &ps);
989 }
990
991 LeaveCriticalSection(&GuiData->Lock);
992 }
993 EndPaint(GuiData->hWindow, &ps);
994
995 return;
996 }
997
998 static VOID
999 OnPaletteChanged(PGUI_CONSOLE_DATA GuiData)
1000 {
1001 PCONSOLE_SCREEN_BUFFER ActiveBuffer = GuiData->ActiveBuffer;
1002
1003 // See WM_PALETTECHANGED message
1004 // if ((HWND)wParam == hWnd) break;
1005
1006 // if (GetType(ActiveBuffer) == GRAPHICS_BUFFER)
1007 if (ActiveBuffer->PaletteHandle)
1008 {
1009 DPRINT("WM_PALETTECHANGED changing palette\n");
1010
1011 /* Specify the use of the system palette for the framebuffer */
1012 SetSystemPaletteUse(GuiData->hMemDC, ActiveBuffer->PaletteUsage);
1013
1014 /* Realize the (logical) palette */
1015 RealizePalette(GuiData->hMemDC);
1016 }
1017 }
1018
1019 static BOOL
1020 IsSystemKey(WORD VirtualKeyCode)
1021 {
1022 switch (VirtualKeyCode)
1023 {
1024 /* From MSDN, "Virtual-Key Codes" */
1025 case VK_RETURN:
1026 case VK_SHIFT:
1027 case VK_CONTROL:
1028 case VK_MENU:
1029 case VK_PAUSE:
1030 case VK_CAPITAL:
1031 case VK_ESCAPE:
1032 case VK_LWIN:
1033 case VK_RWIN:
1034 case VK_NUMLOCK:
1035 case VK_SCROLL:
1036 return TRUE;
1037 default:
1038 return FALSE;
1039 }
1040 }
1041
1042 static VOID
1043 OnKey(PGUI_CONSOLE_DATA GuiData, UINT msg, WPARAM wParam, LPARAM lParam)
1044 {
1045 PCONSOLE Console = GuiData->Console;
1046 PCONSOLE_SCREEN_BUFFER ActiveBuffer;
1047
1048 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return;
1049
1050 ActiveBuffer = GuiData->ActiveBuffer;
1051
1052 if (GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS)
1053 {
1054 WORD VirtualKeyCode = LOWORD(wParam);
1055
1056 if (msg != WM_KEYDOWN) goto Quit;
1057
1058 if (VirtualKeyCode == VK_RETURN)
1059 {
1060 /* Copy (and clear) selection if ENTER is pressed */
1061 Copy(GuiData);
1062 goto Quit;
1063 }
1064 else if ( VirtualKeyCode == VK_ESCAPE ||
1065 (VirtualKeyCode == 'C' && (GetKeyState(VK_CONTROL) & 0x8000)) )
1066 {
1067 /* Cancel selection if ESC or Ctrl-C are pressed */
1068 UpdateSelection(GuiData, NULL, NULL);
1069 goto Quit;
1070 }
1071
1072 if ((GuiData->Selection.dwFlags & CONSOLE_MOUSE_SELECTION) == 0)
1073 {
1074 /* Keyboard selection mode */
1075 BOOL Interpreted = FALSE;
1076 BOOL MajPressed = !!(GetKeyState(VK_SHIFT) & 0x8000);
1077
1078 switch (VirtualKeyCode)
1079 {
1080 case VK_LEFT:
1081 {
1082 Interpreted = TRUE;
1083 if (GuiData->dwSelectionCursor.X > 0)
1084 GuiData->dwSelectionCursor.X--;
1085
1086 break;
1087 }
1088
1089 case VK_RIGHT:
1090 {
1091 Interpreted = TRUE;
1092 if (GuiData->dwSelectionCursor.X < ActiveBuffer->ScreenBufferSize.X - 1)
1093 GuiData->dwSelectionCursor.X++;
1094
1095 break;
1096 }
1097
1098 case VK_UP:
1099 {
1100 Interpreted = TRUE;
1101 if (GuiData->dwSelectionCursor.Y > 0)
1102 GuiData->dwSelectionCursor.Y--;
1103
1104 break;
1105 }
1106
1107 case VK_DOWN:
1108 {
1109 Interpreted = TRUE;
1110 if (GuiData->dwSelectionCursor.Y < ActiveBuffer->ScreenBufferSize.Y - 1)
1111 GuiData->dwSelectionCursor.Y++;
1112
1113 break;
1114 }
1115
1116 case VK_HOME:
1117 {
1118 Interpreted = TRUE;
1119 GuiData->dwSelectionCursor.X = 0;
1120 GuiData->dwSelectionCursor.Y = 0;
1121 break;
1122 }
1123
1124 case VK_END:
1125 {
1126 Interpreted = TRUE;
1127 GuiData->dwSelectionCursor.Y = ActiveBuffer->ScreenBufferSize.Y - 1;
1128 break;
1129 }
1130
1131 case VK_PRIOR:
1132 {
1133 Interpreted = TRUE;
1134 GuiData->dwSelectionCursor.Y -= ActiveBuffer->ViewSize.Y;
1135 if (GuiData->dwSelectionCursor.Y < 0)
1136 GuiData->dwSelectionCursor.Y = 0;
1137
1138 break;
1139 }
1140
1141 case VK_NEXT:
1142 {
1143 Interpreted = TRUE;
1144 GuiData->dwSelectionCursor.Y += ActiveBuffer->ViewSize.Y;
1145 if (GuiData->dwSelectionCursor.Y >= ActiveBuffer->ScreenBufferSize.Y)
1146 GuiData->dwSelectionCursor.Y = ActiveBuffer->ScreenBufferSize.Y - 1;
1147
1148 break;
1149 }
1150
1151 default:
1152 break;
1153 }
1154
1155 if (Interpreted)
1156 {
1157 UpdateSelection(GuiData,
1158 !MajPressed ? &GuiData->dwSelectionCursor : NULL,
1159 &GuiData->dwSelectionCursor);
1160 }
1161 else if (!IsSystemKey(VirtualKeyCode))
1162 {
1163 /* Emit an error beep sound */
1164 SendNotifyMessage(GuiData->hWindow, PM_CONSOLE_BEEP, 0, 0);
1165 }
1166
1167 goto Quit;
1168 }
1169 else
1170 {
1171 /* Mouse selection mode */
1172
1173 if (!IsSystemKey(VirtualKeyCode))
1174 {
1175 /* Clear the selection and send the key into the input buffer */
1176 UpdateSelection(GuiData, NULL, NULL);
1177 }
1178 else
1179 {
1180 goto Quit;
1181 }
1182 }
1183 }
1184
1185 if ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) == 0)
1186 {
1187 MSG Message;
1188
1189 Message.hwnd = GuiData->hWindow;
1190 Message.message = msg;
1191 Message.wParam = wParam;
1192 Message.lParam = lParam;
1193
1194 ConioProcessKey(Console, &Message);
1195 }
1196
1197 Quit:
1198 LeaveCriticalSection(&Console->Lock);
1199 }
1200
1201
1202 // FIXME: Remove after fixing OnTimer
1203 VOID
1204 InvalidateCell(PGUI_CONSOLE_DATA GuiData,
1205 SHORT x, SHORT y);
1206
1207 static VOID
1208 OnTimer(PGUI_CONSOLE_DATA GuiData)
1209 {
1210 PCONSOLE Console = GuiData->Console;
1211 PCONSOLE_SCREEN_BUFFER Buff;
1212
1213 SetTimer(GuiData->hWindow, CONGUI_UPDATE_TIMER, CURSOR_BLINK_TIME, NULL);
1214
1215 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return;
1216
1217 Buff = GuiData->ActiveBuffer;
1218
1219 if (GetType(Buff) == TEXTMODE_BUFFER)
1220 {
1221 InvalidateCell(GuiData, Buff->CursorPosition.X, Buff->CursorPosition.Y);
1222 Buff->CursorBlinkOn = !Buff->CursorBlinkOn;
1223
1224 if ((GuiData->OldCursor.x != Buff->CursorPosition.X) ||
1225 (GuiData->OldCursor.y != Buff->CursorPosition.Y))
1226 {
1227 SCROLLINFO xScroll;
1228 int OldScrollX = -1, OldScrollY = -1;
1229 int NewScrollX = -1, NewScrollY = -1;
1230
1231 xScroll.cbSize = sizeof(SCROLLINFO);
1232 xScroll.fMask = SIF_POS;
1233 // Capture the original position of the scroll bars and save them.
1234 if (GetScrollInfo(GuiData->hWindow, SB_HORZ, &xScroll)) OldScrollX = xScroll.nPos;
1235 if (GetScrollInfo(GuiData->hWindow, SB_VERT, &xScroll)) OldScrollY = xScroll.nPos;
1236
1237 // If we successfully got the info for the horizontal scrollbar
1238 if (OldScrollX >= 0)
1239 {
1240 if ((Buff->CursorPosition.X < Buff->ViewOrigin.X) ||
1241 (Buff->CursorPosition.X >= (Buff->ViewOrigin.X + Buff->ViewSize.X)))
1242 {
1243 // Handle the horizontal scroll bar
1244 if (Buff->CursorPosition.X >= Buff->ViewSize.X)
1245 NewScrollX = Buff->CursorPosition.X - Buff->ViewSize.X + 1;
1246 else
1247 NewScrollX = 0;
1248 }
1249 else
1250 {
1251 NewScrollX = OldScrollX;
1252 }
1253 }
1254 // If we successfully got the info for the vertical scrollbar
1255 if (OldScrollY >= 0)
1256 {
1257 if ((Buff->CursorPosition.Y < Buff->ViewOrigin.Y) ||
1258 (Buff->CursorPosition.Y >= (Buff->ViewOrigin.Y + Buff->ViewSize.Y)))
1259 {
1260 // Handle the vertical scroll bar
1261 if (Buff->CursorPosition.Y >= Buff->ViewSize.Y)
1262 NewScrollY = Buff->CursorPosition.Y - Buff->ViewSize.Y + 1;
1263 else
1264 NewScrollY = 0;
1265 }
1266 else
1267 {
1268 NewScrollY = OldScrollY;
1269 }
1270 }
1271
1272 // Adjust scroll bars and refresh the window if the cursor has moved outside the visible area
1273 // NOTE: OldScroll# and NewScroll# will both be -1 (initial value) if the info for the respective scrollbar
1274 // was not obtained successfully in the previous steps. This means their difference is 0 (no scrolling)
1275 // and their associated scrollbar is left alone.
1276 if ((OldScrollX != NewScrollX) || (OldScrollY != NewScrollY))
1277 {
1278 Buff->ViewOrigin.X = NewScrollX;
1279 Buff->ViewOrigin.Y = NewScrollY;
1280 ScrollWindowEx(GuiData->hWindow,
1281 (OldScrollX - NewScrollX) * GuiData->CharWidth,
1282 (OldScrollY - NewScrollY) * GuiData->CharHeight,
1283 NULL,
1284 NULL,
1285 NULL,
1286 NULL,
1287 SW_INVALIDATE);
1288 if (NewScrollX >= 0)
1289 {
1290 xScroll.nPos = NewScrollX;
1291 SetScrollInfo(GuiData->hWindow, SB_HORZ, &xScroll, TRUE);
1292 }
1293 if (NewScrollY >= 0)
1294 {
1295 xScroll.nPos = NewScrollY;
1296 SetScrollInfo(GuiData->hWindow, SB_VERT, &xScroll, TRUE);
1297 }
1298 UpdateWindow(GuiData->hWindow);
1299 // InvalidateRect(GuiData->hWindow, NULL, FALSE);
1300 GuiData->OldCursor.x = Buff->CursorPosition.X;
1301 GuiData->OldCursor.y = Buff->CursorPosition.Y;
1302 }
1303 }
1304 }
1305 else /* if (GetType(Buff) == GRAPHICS_BUFFER) */
1306 {
1307 }
1308
1309 LeaveCriticalSection(&Console->Lock);
1310 }
1311
1312 static BOOL
1313 OnClose(PGUI_CONSOLE_DATA GuiData)
1314 {
1315 PCONSOLE Console = GuiData->Console;
1316
1317 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE))
1318 return TRUE;
1319
1320 // TODO: Prompt for termination ? (Warn the user about possible apps running in this console)
1321
1322 /*
1323 * FIXME: Windows will wait up to 5 seconds for the thread to exit.
1324 * We shouldn't wait here, though, since the console lock is entered.
1325 * A copy of the thread list probably needs to be made.
1326 */
1327 ConDrvConsoleProcessCtrlEvent(Console, 0, CTRL_CLOSE_EVENT);
1328
1329 LeaveCriticalSection(&Console->Lock);
1330 return FALSE;
1331 }
1332
1333 static LRESULT
1334 OnNcDestroy(HWND hWnd)
1335 {
1336 PGUI_CONSOLE_DATA GuiData = GuiGetGuiData(hWnd);
1337
1338 KillTimer(hWnd, CONGUI_UPDATE_TIMER);
1339 GetSystemMenu(hWnd, TRUE);
1340
1341 if (GuiData)
1342 {
1343 /* Free the terminal framebuffer */
1344 if (GuiData->hMemDC ) DeleteDC(GuiData->hMemDC);
1345 if (GuiData->hBitmap) DeleteObject(GuiData->hBitmap);
1346 // if (GuiData->hSysPalette) DeleteObject(GuiData->hSysPalette);
1347 if (GuiData->Font) DeleteObject(GuiData->Font);
1348 }
1349
1350 /* Free the GuiData registration */
1351 SetWindowLongPtrW(hWnd, GWLP_USERDATA, (DWORD_PTR)NULL);
1352
1353 return DefWindowProcW(hWnd, WM_NCDESTROY, 0, 0);
1354 }
1355
1356 static COORD
1357 PointToCoord(PGUI_CONSOLE_DATA GuiData, LPARAM lParam)
1358 {
1359 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1360 COORD Coord;
1361 UINT WidthUnit, HeightUnit;
1362
1363 GetScreenBufferSizeUnits(Buffer, GuiData, &WidthUnit, &HeightUnit);
1364
1365 Coord.X = Buffer->ViewOrigin.X + ((SHORT)LOWORD(lParam) / (int)WidthUnit );
1366 Coord.Y = Buffer->ViewOrigin.Y + ((SHORT)HIWORD(lParam) / (int)HeightUnit);
1367
1368 /* Clip coordinate to ensure it's inside buffer */
1369 if (Coord.X < 0)
1370 Coord.X = 0;
1371 else if (Coord.X >= Buffer->ScreenBufferSize.X)
1372 Coord.X = Buffer->ScreenBufferSize.X - 1;
1373
1374 if (Coord.Y < 0)
1375 Coord.Y = 0;
1376 else if (Coord.Y >= Buffer->ScreenBufferSize.Y)
1377 Coord.Y = Buffer->ScreenBufferSize.Y - 1;
1378
1379 return Coord;
1380 }
1381
1382 static LRESULT
1383 OnMouse(PGUI_CONSOLE_DATA GuiData, UINT msg, WPARAM wParam, LPARAM lParam)
1384 {
1385 BOOL Err = FALSE;
1386 PCONSOLE Console = GuiData->Console;
1387
1388 if (GuiData->IgnoreNextMouseSignal)
1389 {
1390 if (msg != WM_LBUTTONDOWN &&
1391 msg != WM_MBUTTONDOWN &&
1392 msg != WM_RBUTTONDOWN &&
1393 msg != WM_MOUSEMOVE)
1394 {
1395 /*
1396 * If this mouse signal is not a button-down action or a move,
1397 * then it is the last signal being ignored.
1398 */
1399 GuiData->IgnoreNextMouseSignal = FALSE;
1400 }
1401 else
1402 {
1403 /*
1404 * This mouse signal is a button-down action or a move.
1405 * Ignore it and perform default action.
1406 */
1407 Err = TRUE;
1408 }
1409 goto Quit;
1410 }
1411
1412 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE))
1413 {
1414 Err = TRUE;
1415 goto Quit;
1416 }
1417
1418 if ( (GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) ||
1419 (Console->QuickEdit) )
1420 {
1421 switch (msg)
1422 {
1423 case WM_LBUTTONDOWN:
1424 {
1425 /* Clear the old selection */
1426 GuiData->Selection.dwFlags = CONSOLE_NO_SELECTION;
1427
1428 /* Restart a new selection */
1429 GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1430 SetCapture(GuiData->hWindow);
1431 GuiData->Selection.dwFlags |= CONSOLE_MOUSE_SELECTION | CONSOLE_MOUSE_DOWN;
1432 UpdateSelection(GuiData,
1433 &GuiData->dwSelectionCursor,
1434 &GuiData->dwSelectionCursor);
1435
1436 break;
1437 }
1438
1439 case WM_LBUTTONUP:
1440 {
1441 if (!(GuiData->Selection.dwFlags & CONSOLE_MOUSE_DOWN)) break;
1442
1443 // GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1444 GuiData->Selection.dwFlags &= ~CONSOLE_MOUSE_DOWN;
1445 // UpdateSelection(GuiData, NULL, &GuiData->dwSelectionCursor);
1446 ReleaseCapture();
1447
1448 break;
1449 }
1450
1451 case WM_LBUTTONDBLCLK:
1452 {
1453 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1454
1455 if (GetType(Buffer) == TEXTMODE_BUFFER)
1456 {
1457 #define IS_WORD_SEP(c) \
1458 ((c) == L'\0' || (c) == L' ' || (c) == L'\t' || (c) == L'\r' || (c) == L'\n')
1459
1460 PTEXTMODE_SCREEN_BUFFER TextBuffer = (PTEXTMODE_SCREEN_BUFFER)Buffer;
1461 COORD cL, cR;
1462 PCHAR_INFO ptrL, ptrR;
1463
1464 /* Starting point */
1465 cL = cR = PointToCoord(GuiData, lParam);
1466 ptrL = ptrR = ConioCoordToPointer(TextBuffer, cL.X, cL.Y);
1467
1468 /* Enlarge the selection by checking for whitespace */
1469 while ((0 < cL.X) && !IS_WORD_SEP(ptrL->Char.UnicodeChar)
1470 && !IS_WORD_SEP((ptrL-1)->Char.UnicodeChar))
1471 {
1472 --cL.X;
1473 --ptrL;
1474 }
1475 while ((cR.X < TextBuffer->ScreenBufferSize.X - 1) &&
1476 !IS_WORD_SEP(ptrR->Char.UnicodeChar) &&
1477 !IS_WORD_SEP((ptrR+1)->Char.UnicodeChar))
1478 {
1479 ++cR.X;
1480 ++ptrR;
1481 }
1482
1483 /*
1484 * Update the selection started with the single
1485 * left-click that preceded this double-click.
1486 */
1487 GuiData->Selection.dwFlags |= CONSOLE_MOUSE_SELECTION | CONSOLE_MOUSE_DOWN;
1488 UpdateSelection(GuiData, &cL, &cR);
1489
1490 /* Ignore the next mouse move signal */
1491 GuiData->IgnoreNextMouseSignal = TRUE;
1492 }
1493
1494 break;
1495 }
1496
1497 case WM_RBUTTONDOWN:
1498 case WM_RBUTTONDBLCLK:
1499 {
1500 if (!(GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY))
1501 {
1502 Paste(GuiData);
1503 }
1504 else
1505 {
1506 Copy(GuiData);
1507 }
1508
1509 /* Ignore the next mouse move signal */
1510 GuiData->IgnoreNextMouseSignal = TRUE;
1511 break;
1512 }
1513
1514 case WM_MOUSEMOVE:
1515 {
1516 if (!(wParam & MK_LBUTTON)) break;
1517 if (!(GuiData->Selection.dwFlags & CONSOLE_MOUSE_DOWN)) break;
1518
1519 // TODO: Scroll buffer to bring SelectionCursor into view
1520 GuiData->dwSelectionCursor = PointToCoord(GuiData, lParam);
1521 UpdateSelection(GuiData, NULL, &GuiData->dwSelectionCursor);
1522
1523 break;
1524 }
1525
1526 default:
1527 Err = FALSE; // TRUE;
1528 break;
1529 }
1530 }
1531 else if (Console->InputBuffer.Mode & ENABLE_MOUSE_INPUT)
1532 {
1533 INPUT_RECORD er;
1534 WORD wKeyState = GET_KEYSTATE_WPARAM(wParam);
1535 DWORD dwButtonState = 0;
1536 DWORD dwControlKeyState = 0;
1537 DWORD dwEventFlags = 0;
1538
1539 switch (msg)
1540 {
1541 case WM_LBUTTONDOWN:
1542 SetCapture(GuiData->hWindow);
1543 dwButtonState = FROM_LEFT_1ST_BUTTON_PRESSED;
1544 dwEventFlags = 0;
1545 break;
1546
1547 case WM_MBUTTONDOWN:
1548 SetCapture(GuiData->hWindow);
1549 dwButtonState = FROM_LEFT_2ND_BUTTON_PRESSED;
1550 dwEventFlags = 0;
1551 break;
1552
1553 case WM_RBUTTONDOWN:
1554 SetCapture(GuiData->hWindow);
1555 dwButtonState = RIGHTMOST_BUTTON_PRESSED;
1556 dwEventFlags = 0;
1557 break;
1558
1559 case WM_LBUTTONUP:
1560 ReleaseCapture();
1561 dwButtonState = 0;
1562 dwEventFlags = 0;
1563 break;
1564
1565 case WM_MBUTTONUP:
1566 ReleaseCapture();
1567 dwButtonState = 0;
1568 dwEventFlags = 0;
1569 break;
1570
1571 case WM_RBUTTONUP:
1572 ReleaseCapture();
1573 dwButtonState = 0;
1574 dwEventFlags = 0;
1575 break;
1576
1577 case WM_LBUTTONDBLCLK:
1578 dwButtonState = FROM_LEFT_1ST_BUTTON_PRESSED;
1579 dwEventFlags = DOUBLE_CLICK;
1580 break;
1581
1582 case WM_MBUTTONDBLCLK:
1583 dwButtonState = FROM_LEFT_2ND_BUTTON_PRESSED;
1584 dwEventFlags = DOUBLE_CLICK;
1585 break;
1586
1587 case WM_RBUTTONDBLCLK:
1588 dwButtonState = RIGHTMOST_BUTTON_PRESSED;
1589 dwEventFlags = DOUBLE_CLICK;
1590 break;
1591
1592 case WM_MOUSEMOVE:
1593 dwButtonState = 0;
1594 dwEventFlags = MOUSE_MOVED;
1595 break;
1596
1597 case WM_MOUSEWHEEL:
1598 dwButtonState = GET_WHEEL_DELTA_WPARAM(wParam) << 16;
1599 dwEventFlags = MOUSE_WHEELED;
1600 break;
1601
1602 case WM_MOUSEHWHEEL:
1603 dwButtonState = GET_WHEEL_DELTA_WPARAM(wParam) << 16;
1604 dwEventFlags = MOUSE_HWHEELED;
1605 break;
1606
1607 default:
1608 Err = TRUE;
1609 break;
1610 }
1611
1612 if (!Err)
1613 {
1614 if (wKeyState & MK_LBUTTON)
1615 dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1616 if (wKeyState & MK_MBUTTON)
1617 dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1618 if (wKeyState & MK_RBUTTON)
1619 dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1620
1621 if (GetKeyState(VK_RMENU) & 0x8000)
1622 dwControlKeyState |= RIGHT_ALT_PRESSED;
1623 if (GetKeyState(VK_LMENU) & 0x8000)
1624 dwControlKeyState |= LEFT_ALT_PRESSED;
1625 if (GetKeyState(VK_RCONTROL) & 0x8000)
1626 dwControlKeyState |= RIGHT_CTRL_PRESSED;
1627 if (GetKeyState(VK_LCONTROL) & 0x8000)
1628 dwControlKeyState |= LEFT_CTRL_PRESSED;
1629 if (GetKeyState(VK_SHIFT) & 0x8000)
1630 dwControlKeyState |= SHIFT_PRESSED;
1631 if (GetKeyState(VK_NUMLOCK) & 0x0001)
1632 dwControlKeyState |= NUMLOCK_ON;
1633 if (GetKeyState(VK_SCROLL) & 0x0001)
1634 dwControlKeyState |= SCROLLLOCK_ON;
1635 if (GetKeyState(VK_CAPITAL) & 0x0001)
1636 dwControlKeyState |= CAPSLOCK_ON;
1637 /* See WM_CHAR MSDN documentation for instance */
1638 if (lParam & 0x01000000)
1639 dwControlKeyState |= ENHANCED_KEY;
1640
1641 er.EventType = MOUSE_EVENT;
1642 er.Event.MouseEvent.dwMousePosition = PointToCoord(GuiData, lParam);
1643 er.Event.MouseEvent.dwButtonState = dwButtonState;
1644 er.Event.MouseEvent.dwControlKeyState = dwControlKeyState;
1645 er.Event.MouseEvent.dwEventFlags = dwEventFlags;
1646
1647 ConioProcessInputEvent(Console, &er);
1648 }
1649 }
1650 else
1651 {
1652 Err = TRUE;
1653 }
1654
1655 LeaveCriticalSection(&Console->Lock);
1656
1657 Quit:
1658 if (Err)
1659 return DefWindowProcW(GuiData->hWindow, msg, wParam, lParam);
1660 else
1661 return 0;
1662 }
1663
1664 VOID
1665 GuiCopyFromTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
1666 PGUI_CONSOLE_DATA GuiData);
1667 VOID
1668 GuiCopyFromGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
1669 PGUI_CONSOLE_DATA GuiData);
1670
1671 static VOID
1672 Copy(PGUI_CONSOLE_DATA GuiData)
1673 {
1674 if (OpenClipboard(GuiData->hWindow) == TRUE)
1675 {
1676 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1677
1678 if (GetType(Buffer) == TEXTMODE_BUFFER)
1679 {
1680 GuiCopyFromTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)Buffer, GuiData);
1681 }
1682 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
1683 {
1684 GuiCopyFromGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)Buffer, GuiData);
1685 }
1686
1687 CloseClipboard();
1688 }
1689
1690 /* Clear the selection */
1691 UpdateSelection(GuiData, NULL, NULL);
1692 }
1693
1694 VOID
1695 GuiPasteToTextModeBuffer(PTEXTMODE_SCREEN_BUFFER Buffer,
1696 PGUI_CONSOLE_DATA GuiData);
1697 VOID
1698 GuiPasteToGraphicsBuffer(PGRAPHICS_SCREEN_BUFFER Buffer,
1699 PGUI_CONSOLE_DATA GuiData);
1700
1701 static VOID
1702 Paste(PGUI_CONSOLE_DATA GuiData)
1703 {
1704 if (OpenClipboard(GuiData->hWindow) == TRUE)
1705 {
1706 PCONSOLE_SCREEN_BUFFER Buffer = GuiData->ActiveBuffer;
1707
1708 if (GetType(Buffer) == TEXTMODE_BUFFER)
1709 {
1710 GuiPasteToTextModeBuffer((PTEXTMODE_SCREEN_BUFFER)Buffer, GuiData);
1711 }
1712 else /* if (GetType(Buffer) == GRAPHICS_BUFFER) */
1713 {
1714 GuiPasteToGraphicsBuffer((PGRAPHICS_SCREEN_BUFFER)Buffer, GuiData);
1715 }
1716
1717 CloseClipboard();
1718 }
1719 }
1720
1721 static VOID
1722 OnGetMinMaxInfo(PGUI_CONSOLE_DATA GuiData, PMINMAXINFO minMaxInfo)
1723 {
1724 PCONSOLE Console = GuiData->Console;
1725 PCONSOLE_SCREEN_BUFFER ActiveBuffer;
1726 DWORD windx, windy;
1727 UINT WidthUnit, HeightUnit;
1728
1729 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return;
1730
1731 ActiveBuffer = GuiData->ActiveBuffer;
1732
1733 GetScreenBufferSizeUnits(ActiveBuffer, GuiData, &WidthUnit, &HeightUnit);
1734
1735 windx = CONGUI_MIN_WIDTH * WidthUnit + 2 * (GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE));
1736 windy = CONGUI_MIN_HEIGHT * HeightUnit + 2 * (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE)) + GetSystemMetrics(SM_CYCAPTION);
1737
1738 minMaxInfo->ptMinTrackSize.x = windx;
1739 minMaxInfo->ptMinTrackSize.y = windy;
1740
1741 windx = (ActiveBuffer->ScreenBufferSize.X) * WidthUnit + 2 * (GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXEDGE));
1742 windy = (ActiveBuffer->ScreenBufferSize.Y) * HeightUnit + 2 * (GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYEDGE)) + GetSystemMetrics(SM_CYCAPTION);
1743
1744 if (ActiveBuffer->ViewSize.X < ActiveBuffer->ScreenBufferSize.X) windy += GetSystemMetrics(SM_CYHSCROLL); // window currently has a horizontal scrollbar
1745 if (ActiveBuffer->ViewSize.Y < ActiveBuffer->ScreenBufferSize.Y) windx += GetSystemMetrics(SM_CXVSCROLL); // window currently has a vertical scrollbar
1746
1747 minMaxInfo->ptMaxTrackSize.x = windx;
1748 minMaxInfo->ptMaxTrackSize.y = windy;
1749
1750 LeaveCriticalSection(&Console->Lock);
1751 }
1752
1753 static VOID
1754 OnSize(PGUI_CONSOLE_DATA GuiData, WPARAM wParam, LPARAM lParam)
1755 {
1756 PCONSOLE Console = GuiData->Console;
1757
1758 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return;
1759
1760 if ((GuiData->WindowSizeLock == FALSE) &&
1761 (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED))
1762 {
1763 PCONSOLE_SCREEN_BUFFER Buff = GuiData->ActiveBuffer;
1764 DWORD windx, windy, charx, chary;
1765 UINT WidthUnit, HeightUnit;
1766
1767 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
1768
1769 GuiData->WindowSizeLock = TRUE;
1770
1771 windx = LOWORD(lParam);
1772 windy = HIWORD(lParam);
1773
1774 // Compensate for existing scroll bars (because lParam values do not accommodate scroll bar)
1775 if (Buff->ViewSize.X < Buff->ScreenBufferSize.X) windy += GetSystemMetrics(SM_CYHSCROLL); // window currently has a horizontal scrollbar
1776 if (Buff->ViewSize.Y < Buff->ScreenBufferSize.Y) windx += GetSystemMetrics(SM_CXVSCROLL); // window currently has a vertical scrollbar
1777
1778 charx = windx / (int)WidthUnit ;
1779 chary = windy / (int)HeightUnit;
1780
1781 // Character alignment (round size up or down)
1782 if ((windx % WidthUnit ) >= (WidthUnit / 2)) ++charx;
1783 if ((windy % HeightUnit) >= (HeightUnit / 2)) ++chary;
1784
1785 // Compensate for added scroll bars in new window
1786 if (charx < Buff->ScreenBufferSize.X) windy -= GetSystemMetrics(SM_CYHSCROLL); // new window will have a horizontal scroll bar
1787 if (chary < Buff->ScreenBufferSize.Y) windx -= GetSystemMetrics(SM_CXVSCROLL); // new window will have a vertical scroll bar
1788
1789 charx = windx / (int)WidthUnit ;
1790 chary = windy / (int)HeightUnit;
1791
1792 // Character alignment (round size up or down)
1793 if ((windx % WidthUnit ) >= (WidthUnit / 2)) ++charx;
1794 if ((windy % HeightUnit) >= (HeightUnit / 2)) ++chary;
1795
1796 // Resize window
1797 if ((charx != Buff->ViewSize.X) || (chary != Buff->ViewSize.Y))
1798 {
1799 Buff->ViewSize.X = (charx <= Buff->ScreenBufferSize.X) ? charx : Buff->ScreenBufferSize.X;
1800 Buff->ViewSize.Y = (chary <= Buff->ScreenBufferSize.Y) ? chary : Buff->ScreenBufferSize.Y;
1801 }
1802
1803 ResizeConWnd(GuiData, WidthUnit, HeightUnit);
1804
1805 // Adjust the start of the visible area if we are attempting to show nonexistent areas
1806 if ((Buff->ScreenBufferSize.X - Buff->ViewOrigin.X) < Buff->ViewSize.X) Buff->ViewOrigin.X = Buff->ScreenBufferSize.X - Buff->ViewSize.X;
1807 if ((Buff->ScreenBufferSize.Y - Buff->ViewOrigin.Y) < Buff->ViewSize.Y) Buff->ViewOrigin.Y = Buff->ScreenBufferSize.Y - Buff->ViewSize.Y;
1808 InvalidateRect(GuiData->hWindow, NULL, TRUE);
1809
1810 GuiData->WindowSizeLock = FALSE;
1811 }
1812
1813 LeaveCriticalSection(&Console->Lock);
1814 }
1815
1816 static VOID
1817 OnMove(PGUI_CONSOLE_DATA GuiData)
1818 {
1819 RECT rcWnd;
1820
1821 // TODO: Simplify the code.
1822 // See: GuiConsoleNotifyWndProc() PM_CREATE_CONSOLE.
1823
1824 /* Retrieve our real position */
1825 GetWindowRect(GuiData->hWindow, &rcWnd);
1826 GuiData->GuiInfo.WindowOrigin.x = rcWnd.left;
1827 GuiData->GuiInfo.WindowOrigin.y = rcWnd.top;
1828 }
1829
1830 /*
1831 // HACK: This functionality is standard for general scrollbars. Don't add it by hand.
1832
1833 VOID
1834 FASTCALL
1835 GuiConsoleHandleScrollbarMenu(VOID)
1836 {
1837 HMENU hMenu;
1838
1839 hMenu = CreatePopupMenu();
1840 if (hMenu == NULL)
1841 {
1842 DPRINT("CreatePopupMenu failed\n");
1843 return;
1844 }
1845
1846 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLHERE);
1847 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1848 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLTOP);
1849 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLBOTTOM);
1850 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1851 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_UP);
1852 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLPAGE_DOWN);
1853 //InsertItem(hMenu, MFT_SEPARATOR, MIIM_FTYPE, 0, NULL, -1);
1854 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLUP);
1855 //InsertItem(hMenu, MIIM_STRING, MIIM_ID | MIIM_FTYPE | MIIM_STRING, 0, NULL, IDS_SCROLLDOWN);
1856 }
1857 */
1858
1859 static LRESULT
1860 OnScroll(PGUI_CONSOLE_DATA GuiData, UINT uMsg, WPARAM wParam)
1861 {
1862 PCONSOLE Console = GuiData->Console;
1863 PCONSOLE_SCREEN_BUFFER Buff;
1864 SCROLLINFO sInfo;
1865 int fnBar;
1866 int old_pos, Maximum;
1867 PSHORT pShowXY;
1868
1869 if (!ConDrvValidateConsoleUnsafe(Console, CONSOLE_RUNNING, TRUE)) return 0;
1870
1871 Buff = GuiData->ActiveBuffer;
1872
1873 if (uMsg == WM_HSCROLL)
1874 {
1875 fnBar = SB_HORZ;
1876 Maximum = Buff->ScreenBufferSize.X - Buff->ViewSize.X;
1877 pShowXY = &Buff->ViewOrigin.X;
1878 }
1879 else
1880 {
1881 fnBar = SB_VERT;
1882 Maximum = Buff->ScreenBufferSize.Y - Buff->ViewSize.Y;
1883 pShowXY = &Buff->ViewOrigin.Y;
1884 }
1885
1886 /* set scrollbar sizes */
1887 sInfo.cbSize = sizeof(SCROLLINFO);
1888 sInfo.fMask = SIF_RANGE | SIF_POS | SIF_PAGE | SIF_TRACKPOS;
1889
1890 if (!GetScrollInfo(GuiData->hWindow, fnBar, &sInfo)) goto Quit;
1891
1892 old_pos = sInfo.nPos;
1893
1894 switch (LOWORD(wParam))
1895 {
1896 case SB_LINELEFT:
1897 sInfo.nPos -= 1;
1898 break;
1899
1900 case SB_LINERIGHT:
1901 sInfo.nPos += 1;
1902 break;
1903
1904 case SB_PAGELEFT:
1905 sInfo.nPos -= sInfo.nPage;
1906 break;
1907
1908 case SB_PAGERIGHT:
1909 sInfo.nPos += sInfo.nPage;
1910 break;
1911
1912 case SB_THUMBTRACK:
1913 sInfo.nPos = sInfo.nTrackPos;
1914 ConioPause(Console, PAUSED_FROM_SCROLLBAR);
1915 break;
1916
1917 case SB_THUMBPOSITION:
1918 ConioUnpause(Console, PAUSED_FROM_SCROLLBAR);
1919 break;
1920
1921 case SB_TOP:
1922 sInfo.nPos = sInfo.nMin;
1923 break;
1924
1925 case SB_BOTTOM:
1926 sInfo.nPos = sInfo.nMax;
1927 break;
1928
1929 default:
1930 break;
1931 }
1932
1933 sInfo.nPos = max(sInfo.nPos, 0);
1934 sInfo.nPos = min(sInfo.nPos, Maximum);
1935
1936 if (old_pos != sInfo.nPos)
1937 {
1938 USHORT OldX = Buff->ViewOrigin.X;
1939 USHORT OldY = Buff->ViewOrigin.Y;
1940 UINT WidthUnit, HeightUnit;
1941
1942 *pShowXY = sInfo.nPos;
1943
1944 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
1945
1946 ScrollWindowEx(GuiData->hWindow,
1947 (OldX - Buff->ViewOrigin.X) * WidthUnit ,
1948 (OldY - Buff->ViewOrigin.Y) * HeightUnit,
1949 NULL,
1950 NULL,
1951 NULL,
1952 NULL,
1953 SW_INVALIDATE);
1954
1955 sInfo.fMask = SIF_POS;
1956 SetScrollInfo(GuiData->hWindow, fnBar, &sInfo, TRUE);
1957
1958 UpdateWindow(GuiData->hWindow);
1959 // InvalidateRect(GuiData->hWindow, NULL, FALSE);
1960 }
1961
1962 Quit:
1963 LeaveCriticalSection(&Console->Lock);
1964 return 0;
1965 }
1966
1967
1968 static LRESULT CALLBACK
1969 ConWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1970 {
1971 LRESULT Result = 0;
1972 PGUI_CONSOLE_DATA GuiData = NULL;
1973 PCONSOLE Console = NULL;
1974
1975 /*
1976 * - If it's the first time we create a window for the terminal,
1977 * just initialize it and return.
1978 *
1979 * - If we are destroying the window, just do it and return.
1980 */
1981 if (msg == WM_NCCREATE)
1982 {
1983 return (LRESULT)OnNcCreate(hWnd, (LPCREATESTRUCTW)lParam);
1984 }
1985 else if (msg == WM_NCDESTROY)
1986 {
1987 return OnNcDestroy(hWnd);
1988 }
1989
1990 /*
1991 * Now the terminal window is initialized.
1992 * Get the terminal data via the window's data.
1993 * If there is no data, just go away.
1994 */
1995 GuiData = GuiGetGuiData(hWnd);
1996 if (GuiData == NULL) return DefWindowProcW(hWnd, msg, wParam, lParam);
1997
1998 // TEMPORARY HACK until all of the functions can deal with a NULL GuiData->ActiveBuffer ...
1999 if (GuiData->ActiveBuffer == NULL) return DefWindowProcW(hWnd, msg, wParam, lParam);
2000
2001 /*
2002 * Just retrieve a pointer to the console in case somebody needs it.
2003 * It is not NULL because it was checked in GuiGetGuiData.
2004 * Each helper function which needs the console has to validate and lock it.
2005 */
2006 Console = GuiData->Console;
2007
2008 /* We have a console, start message dispatching */
2009 switch (msg)
2010 {
2011 case WM_ACTIVATE:
2012 OnActivate(GuiData, wParam);
2013 break;
2014
2015 case WM_CLOSE:
2016 if (OnClose(GuiData)) goto Default;
2017 break;
2018
2019 case WM_PAINT:
2020 OnPaint(GuiData);
2021 break;
2022
2023 case WM_TIMER:
2024 OnTimer(GuiData);
2025 break;
2026
2027 case WM_PALETTECHANGED:
2028 {
2029 DPRINT("WM_PALETTECHANGED called\n");
2030
2031 /*
2032 * Protects against infinite loops:
2033 * "... A window that receives this message must not realize
2034 * its palette, unless it determines that wParam does not contain
2035 * its own window handle." (WM_PALETTECHANGED description - MSDN)
2036 *
2037 * This message is sent to all windows, including the one that
2038 * changed the system palette and caused this message to be sent.
2039 * The wParam of this message contains the handle of the window
2040 * that caused the system palette to change. To avoid an infinite
2041 * loop, care must be taken to check that the wParam of this message
2042 * does not match the window's handle.
2043 */
2044 if ((HWND)wParam == hWnd) break;
2045
2046 DPRINT("WM_PALETTECHANGED ok\n");
2047 OnPaletteChanged(GuiData);
2048 DPRINT("WM_PALETTECHANGED quit\n");
2049 break;
2050 }
2051
2052 case WM_KEYDOWN:
2053 case WM_KEYUP:
2054 case WM_CHAR:
2055 case WM_DEADCHAR:
2056 case WM_SYSKEYDOWN:
2057 case WM_SYSKEYUP:
2058 case WM_SYSCHAR:
2059 case WM_SYSDEADCHAR:
2060 {
2061 /* Detect Alt-Enter presses and switch back and forth to fullscreen mode */
2062 if (msg == WM_SYSKEYDOWN && (HIWORD(lParam) & KF_ALTDOWN) && wParam == VK_RETURN)
2063 {
2064 /* Switch only at first Alt-Enter press, and ignore subsequent key repetitions */
2065 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
2066 GuiConsoleSwitchFullScreen(GuiData);
2067
2068 break;
2069 }
2070 /* Detect Alt-Esc/Space/Tab presses defer to DefWindowProc */
2071 if ( (HIWORD(lParam) & KF_ALTDOWN) && (wParam == VK_ESCAPE || wParam == VK_SPACE || wParam == VK_TAB))
2072 {
2073 return DefWindowProcW(hWnd, msg, wParam, lParam);
2074 }
2075
2076 OnKey(GuiData, msg, wParam, lParam);
2077 break;
2078 }
2079
2080 case WM_SETCURSOR:
2081 {
2082 /*
2083 * The message was sent because we are manually triggering a change.
2084 * Check whether the mouse is indeed present on this console window
2085 * and take appropriate decisions.
2086 */
2087 if (wParam == -1 && lParam == -1)
2088 {
2089 POINT mouseCoords;
2090 HWND hWndHit;
2091
2092 /* Get the placement of the mouse */
2093 GetCursorPos(&mouseCoords);
2094
2095 /* On which window is placed the mouse ? */
2096 hWndHit = WindowFromPoint(mouseCoords);
2097
2098 /* It's our window. Perform the hit-test to be used later on. */
2099 if (hWndHit == hWnd)
2100 {
2101 wParam = (WPARAM)hWnd;
2102 lParam = DefWindowProcW(hWndHit, WM_NCHITTEST, 0,
2103 MAKELPARAM(mouseCoords.x, mouseCoords.y));
2104 }
2105 }
2106
2107 /* Set the mouse cursor only when we are in the client area */
2108 if ((HWND)wParam == hWnd && LOWORD(lParam) == HTCLIENT)
2109 {
2110 if (GuiData->MouseCursorRefCount >= 0)
2111 {
2112 /* Show the cursor */
2113 SetCursor(GuiData->hCursor);
2114 }
2115 else
2116 {
2117 /* Hide the cursor if the reference count is negative */
2118 SetCursor(NULL);
2119 }
2120 return TRUE;
2121 }
2122 else
2123 {
2124 goto Default;
2125 }
2126 }
2127
2128 case WM_LBUTTONDOWN:
2129 case WM_MBUTTONDOWN:
2130 case WM_RBUTTONDOWN:
2131 case WM_LBUTTONUP:
2132 case WM_MBUTTONUP:
2133 case WM_RBUTTONUP:
2134 case WM_LBUTTONDBLCLK:
2135 case WM_MBUTTONDBLCLK:
2136 case WM_RBUTTONDBLCLK:
2137 case WM_MOUSEMOVE:
2138 case WM_MOUSEWHEEL:
2139 case WM_MOUSEHWHEEL:
2140 {
2141 Result = OnMouse(GuiData, msg, wParam, lParam);
2142 break;
2143 }
2144
2145 case WM_HSCROLL:
2146 case WM_VSCROLL:
2147 {
2148 Result = OnScroll(GuiData, msg, wParam);
2149 break;
2150 }
2151
2152 case WM_CONTEXTMENU:
2153 {
2154 if (DefWindowProcW(hWnd /*GuiData->hWindow*/, WM_NCHITTEST, 0, lParam) == HTCLIENT)
2155 {
2156 HMENU hMenu = CreatePopupMenu();
2157 if (hMenu != NULL)
2158 {
2159 AppendMenuItems(hMenu, GuiConsoleEditMenuItems);
2160 TrackPopupMenuEx(hMenu,
2161 TPM_RIGHTBUTTON,
2162 GET_X_LPARAM(lParam),
2163 GET_Y_LPARAM(lParam),
2164 hWnd,
2165 NULL);
2166 DestroyMenu(hMenu);
2167 }
2168 break;
2169 }
2170 else
2171 {
2172 goto Default;
2173 }
2174 }
2175
2176 case WM_INITMENU:
2177 {
2178 HMENU hMenu = (HMENU)wParam;
2179 if (hMenu != NULL)
2180 {
2181 /* Enable or disable the Close menu item */
2182 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND |
2183 (GuiData->IsCloseButtonEnabled ? MF_ENABLED : MF_GRAYED));
2184
2185 /* Enable or disable the Copy and Paste items */
2186 EnableMenuItem(hMenu, ID_SYSTEM_EDIT_COPY , MF_BYCOMMAND |
2187 ((GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) &&
2188 (GuiData->Selection.dwFlags & CONSOLE_SELECTION_NOT_EMPTY) ? MF_ENABLED : MF_GRAYED));
2189 // FIXME: Following whether the active screen buffer is text-mode
2190 // or graphics-mode, search for CF_UNICODETEXT or CF_BITMAP formats.
2191 EnableMenuItem(hMenu, ID_SYSTEM_EDIT_PASTE, MF_BYCOMMAND |
2192 (!(GuiData->Selection.dwFlags & CONSOLE_SELECTION_IN_PROGRESS) &&
2193 IsClipboardFormatAvailable(CF_UNICODETEXT) ? MF_ENABLED : MF_GRAYED));
2194 }
2195
2196 SendMenuEvent(Console, WM_INITMENU);
2197 break;
2198 }
2199
2200 case WM_MENUSELECT:
2201 {
2202 if (HIWORD(wParam) == 0xFFFF) // Allow all the menu flags
2203 {
2204 SendMenuEvent(Console, WM_MENUSELECT);
2205 }
2206 break;
2207 }
2208
2209 case WM_COMMAND:
2210 case WM_SYSCOMMAND:
2211 {
2212 Result = OnCommand(GuiData, wParam, lParam);
2213 break;
2214 }
2215
2216 case WM_SETFOCUS:
2217 case WM_KILLFOCUS:
2218 OnFocus(GuiData, (msg == WM_SETFOCUS));
2219 break;
2220
2221 case WM_GETMINMAXINFO:
2222 OnGetMinMaxInfo(GuiData, (PMINMAXINFO)lParam);
2223 break;
2224
2225 case WM_MOVE:
2226 OnMove(GuiData);
2227 break;
2228
2229 #if 0 // This code is here to prepare & control dynamic console SB resizing.
2230 case WM_SIZING:
2231 {
2232 PRECT dragRect = (PRECT)lParam;
2233 switch (wParam)
2234 {
2235 case WMSZ_LEFT:
2236 DPRINT1("WMSZ_LEFT\n");
2237 break;
2238 case WMSZ_RIGHT:
2239 DPRINT1("WMSZ_RIGHT\n");
2240 break;
2241 case WMSZ_TOP:
2242 DPRINT1("WMSZ_TOP\n");
2243 break;
2244 case WMSZ_TOPLEFT:
2245 DPRINT1("WMSZ_TOPLEFT\n");
2246 break;
2247 case WMSZ_TOPRIGHT:
2248 DPRINT1("WMSZ_TOPRIGHT\n");
2249 break;
2250 case WMSZ_BOTTOM:
2251 DPRINT1("WMSZ_BOTTOM\n");
2252 break;
2253 case WMSZ_BOTTOMLEFT:
2254 DPRINT1("WMSZ_BOTTOMLEFT\n");
2255 break;
2256 case WMSZ_BOTTOMRIGHT:
2257 DPRINT1("WMSZ_BOTTOMRIGHT\n");
2258 break;
2259 default:
2260 DPRINT1("wParam = %d\n", wParam);
2261 break;
2262 }
2263 DPRINT1("dragRect = {.left = %d ; .top = %d ; .right = %d ; .bottom = %d}\n",
2264 dragRect->left, dragRect->top, dragRect->right, dragRect->bottom);
2265 break;
2266 }
2267 #endif
2268
2269 case WM_SIZE:
2270 OnSize(GuiData, wParam, lParam);
2271 break;
2272
2273 case PM_RESIZE_TERMINAL:
2274 {
2275 PCONSOLE_SCREEN_BUFFER Buff = GuiData->ActiveBuffer;
2276 HDC hDC;
2277 HBITMAP hnew, hold;
2278
2279 DWORD Width, Height;
2280 UINT WidthUnit, HeightUnit;
2281
2282 GetScreenBufferSizeUnits(Buff, GuiData, &WidthUnit, &HeightUnit);
2283
2284 Width = Buff->ScreenBufferSize.X * WidthUnit ;
2285 Height = Buff->ScreenBufferSize.Y * HeightUnit;
2286
2287 /* Recreate the framebuffer */
2288 hDC = GetDC(GuiData->hWindow);
2289 hnew = CreateCompatibleBitmap(hDC, Width, Height);
2290 ReleaseDC(GuiData->hWindow, hDC);
2291 hold = SelectObject(GuiData->hMemDC, hnew);
2292 if (GuiData->hBitmap)
2293 {
2294 if (hold == GuiData->hBitmap) DeleteObject(GuiData->hBitmap);
2295 }
2296 GuiData->hBitmap = hnew;
2297
2298 /* Resize the window to the user's values */
2299 GuiData->WindowSizeLock = TRUE;
2300 ResizeConWnd(GuiData, WidthUnit, HeightUnit);
2301 GuiData->WindowSizeLock = FALSE;
2302 break;
2303 }
2304
2305 case PM_APPLY_CONSOLE_INFO:
2306 {
2307 GuiApplyUserSettings(GuiData, (HANDLE)wParam, (BOOL)lParam);
2308 break;
2309 }
2310
2311 case PM_CONSOLE_BEEP:
2312 DPRINT1("Beep !!\n");
2313 Beep(800, 200);
2314 break;
2315
2316 // case PM_CONSOLE_SET_TITLE:
2317 // SetWindowText(GuiData->hWindow, GuiData->Console->Title.Buffer);
2318 // break;
2319
2320 default: Default:
2321 Result = DefWindowProcW(hWnd, msg, wParam, lParam);
2322 break;
2323 }
2324
2325 return Result;
2326 }
2327
2328 /* EOF */