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