1 namespace gui::drivers;
12 #define _WIN32_WINNT 0x0501
19 #define WIN32_LEAN_AND_MEAN
20 #define Method _Method
23 #define String _String
25 #define Platform _Platform
31 #if defined(ECERE_VANILLA)
32 #define ECERE_NOJOYSTICK
35 #ifndef ECERE_NODINPUT
36 #ifndef ECERE_NOJOYSTICK
37 #define DIRECTINPUT_VERSION 0x700
40 #define DIRECTINPUT_VERSION 0x300
50 #define VK_EQUALS 0xBB
53 #define VK_PERIOD 0xBE
56 #define VK_LBRACKET 0xDB
57 #define VK_BACK_SLASH 0xDC
58 #define VK_RBRACKET 0xDD
62 #define WM_MOUSEWHEEL 0x20A
66 #define IDC_HAND (char *)0x7F89
71 #define LWA_COLORKEY 0x00000001
72 #define LWA_ALPHA 0x00000002
74 #define WS_EX_LAYERED 0x00080000
79 SetLayeredWindowAttributes(
97 #define ECERE_NODINPUT
98 #define ECERE_NOJOYSTICK
99 #define ECERE_NOBLENDING
102 static byte key2VK[256] =
104 0,VK_ESCAPE,'1','2','3','4','5','6','7','8','9','0',VK_MINUS,VK_EQUALS,VK_BACK,VK_TAB,
105 'Q','W','E','R','T','Y','U','I','O','P',VK_LBRACKET,VK_RBRACKET,VK_RETURN,VK_LCONTROL,'A','S',
106 'D','F','G','H','J','K','L',VK_SEMI,VK_QUOTE,VK_TILDE,VK_LSHIFT,VK_BACK_SLASH,'Z','X','C','V',
107 'B','N','M',VK_COMMA,VK_PERIOD,VK_DIVIDE,VK_RSHIFT,VK_MULTIPLY,VK_LMENU,VK_SPACE,VK_CAPITAL,VK_F1,VK_F2,VK_F3,VK_F4,VK_F5,
108 VK_F6,VK_F7,VK_F8,VK_F9,VK_F10,VK_NUMLOCK,VK_SCROLL,VK_NUMPAD7,VK_NUMPAD8,VK_NUMPAD9,VK_SUBTRACT,VK_NUMPAD4,VK_NUMPAD5,VK_NUMPAD6,VK_ADD,VK_NUMPAD1,
109 VK_NUMPAD2,VK_NUMPAD3,VK_NUMPAD0,VK_DELETE,0,0,0,VK_F11,VK_F12,0,0,0,0,0,0,0,
110 0,VK_RCONTROL,0,0,VK_RMENU,0,VK_HOME,VK_UP,VK_PRIOR,VK_LEFT,VK_RIGHT,VK_END,VK_DOWN,VK_NEXT,VK_INSERT,VK_DELETE
112 static uint16 className[] = L"Ecere Application";
113 static HINSTANCE hInstance;
115 static DEVMODE devMode;
116 static HWND acquiredWindow = null;
117 static HCURSOR systemCursors[SystemCursor];
118 static bool fullScreenMode;
119 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
120 static DWORD hiResTimer;
121 // static HWND topWindow;
123 static HWND startBar;
125 #ifndef ECERE_NODINPUT
127 static HMODULE dInputDll = null;
128 static BOOL * (WINAPI * directInputCreate)(HINSTANCE, DWORD, IDirectInput **, void *);
129 static IDirectInput * dInput = null;
130 static IDirectInputDevice * directMouse = null;
131 #ifndef ECERE_NOJOYSTICK
132 static int numJoysticks = 0;
133 static IDirectInputDevice2 * directJoysticks[NUMJOY] = {null,null,null,null};
138 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
139 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
140 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
141 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
142 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
143 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
144 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
145 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
146 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
147 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
148 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
149 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
150 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
151 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
155 static Box lastMonitorAreas[32];
156 static Box monitorAreas[32];
159 static bool EnumerateMonitors(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
161 MONITORINFO info = { 0 };
162 info.cbSize = sizeof(MONITORINFO);
163 GetMonitorInfo(hMonitor, &info);
164 monitorAreas[monitor] = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
169 static bool externalDisplayChange;
171 static int taskBarState;
172 static WINDOWPLACEMENT taskBarPlacement;
173 static bool activateApp;
175 static SystemCursor lastCursor = (SystemCursor)-1;
177 void AeroSnapPosition(Window window, int x, int y, int w, int h)
179 int oldX = window.absPosition.x;
181 // To prevent having a window < minClientSize with Aero Snap, because we don't receive a WM_SIZING!
182 // The sensible implementation of this is in WM_SIZING
186 window.GetDecorationsSize(&ew, &eh);
194 gw = Max(gw, window.minSize.w);
195 gh = Max(gh, window.minSize.h);
196 gw = Min(gw, window.maxSize.w);
197 gh = Min(gh, window.maxSize.h);
199 if(!window.OnResizing(&gw, &gh))
201 gw = window.clientSize.w;
202 gh = window.clientSize.h;
205 gw = Max(gw, window.skinMinSize.w);
206 gh = Max(gh, window.skinMinSize.h);
211 if(w != gw || h != gh)
214 // Adjust x position if we resized from top or bottom left corner
222 guiApp.interfaceDriver.PositionRootWindow(window, x, y, w, h, move, true);
225 window.ExternalPosition(x, y, w, h);
228 class Win32Interface : Interface
230 class_property(name) = "Win32";
232 void ::RepositionDesktop(bool updateChildren)
235 static double lastTime = 0, time;
238 //static Size lastScreen;
239 //static Point lastScreenPos;
240 static double lastAutoHideCheck = 0;
241 int newTaskBarState = taskBarState;
242 HMONITOR primaryMonitor;
245 if(time - lastTime < 0.1) return;
248 // Every sec, check for the auto hide property
249 if(time - lastAutoHideCheck > 1)
251 APPBARDATA appBarData = { 0 };
252 newTaskBarState = (int)SHAppBarMessage(ABM_GETSTATE, &appBarData);
253 lastAutoHideCheck = time;
257 EnumDisplayMonitors(null, null, EnumerateMonitors, 0);
258 for(c = 0; c<monitor; c++)
260 if(monitorAreas[c].left != lastMonitorAreas[c].left ||
261 monitorAreas[c].top != lastMonitorAreas[c].top ||
262 monitorAreas[c].right != lastMonitorAreas[c].right ||
263 monitorAreas[c].bottom != lastMonitorAreas[c].bottom)
269 memcpy(lastMonitorAreas, monitorAreas, sizeof(monitorAreas));
271 guiApp.virtualScreen =
273 GetSystemMetrics(SM_CXVIRTUALSCREEN),
274 GetSystemMetrics(SM_CYVIRTUALSCREEN)
276 guiApp.virtualScreenPos =
278 x = GetSystemMetrics(SM_XVIRTUALSCREEN),
279 y = GetSystemMetrics(SM_YVIRTUALSCREEN)
283 HMONITOR monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
286 MONITORINFO info = { 0 };
287 info.cbSize = sizeof(MONITORINFO);
288 primaryMonitor = monitor;
289 GetMonitorInfo(monitor, &info);
290 x = info.rcMonitor.left;
291 y = info.rcMonitor.top;
292 w = info.rcMonitor.right - info.rcWork.left;
293 h = info.rcMonitor.bottom - info.rcWork.top;
298 WINDOWPLACEMENT placement = { 0 };
299 placement.length = sizeof(WINDOWPLACEMENT);
301 if(!GetWindowPlacement(startBar, &placement))
303 startBar = FindWindowA("Shell_TrayWnd", null);
304 GetWindowPlacement(startBar, &placement);
307 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
308 if(primaryMonitor == taskBarMonitor)
310 if(placement.rcNormalPosition.top <= 0 && placement.rcNormalPosition.bottom >= h - 1)
312 if(placement.rcNormalPosition.left <= 0)
314 x = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.right;
318 w = (taskBarState & ABS_AUTOHIDE) ? (w - 1) : Min(placement.rcNormalPosition.left, w);
320 else if(placement.rcNormalPosition.left <= 0 && placement.rcNormalPosition.right >= w - 1)
322 if(placement.rcNormalPosition.top <= 0)
324 y = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.bottom;
328 h = (taskBarState & ABS_AUTOHIDE) ? (h - 1) : Min(placement.rcNormalPosition.top, h);
334 placement.rcNormalPosition.left != taskBarPlacement.rcNormalPosition.left ||
335 placement.rcNormalPosition.top != taskBarPlacement.rcNormalPosition.top ||
336 placement.rcNormalPosition.right != taskBarPlacement.rcNormalPosition.right ||
337 placement.rcNormalPosition.bottom != taskBarPlacement.rcNormalPosition.bottom ||
338 newTaskBarState != taskBarState)
340 taskBarState = newTaskBarState;
341 guiApp.desktop.size.w = 0;
346 taskBarPlacement = placement;
347 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
350 //lastScreen = guiApp.virtualScreen;
351 //lastScreenPos = guiApp.virtualScreenPos;
355 /****************************************************************************
356 /// PRIVATE UTILITY FUNCTIONS /////////////
357 ****************************************************************************/
358 // --- Keyboard Input ---
359 bool ::ProcessKeyMessage(Window window, DWORD msg, WPARAM wParam, LPARAM lParam, unichar ch)
365 bool frenchShift = (ch < 0x10000) ? (((VkKeyScan((uint16)ch) >> 8) & 6) == 6) : false;
367 if(msg == WM_CHAR || msg == WM_DEADCHAR)
372 if(msg == WM_MOUSEWHEEL)
373 code = (((short) HIWORD(wParam)) < 0) ? wheelDown : wheelUp;
376 key = (byte)((lParam & 0xFF0000)>>16);
377 if(lParam & 0x1000000)
379 key = Interface::GetExtendedKey(key);
389 if(msg == WM_MOUSEWHEEL)
391 if(::GetAsyncKeyState(VK_SHIFT) & 0x80000)
393 if(::GetAsyncKeyState(VK_CONTROL) & 0x80000)
395 if(::GetAsyncKeyState(VK_MENU) & 0x80000)
398 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, 0);
402 if(key != leftShift && key != rightShift && ::GetKeyState(VK_SHIFT) & 0x80000)
404 if(key != leftControl && key != rightControl && ::GetKeyState(VK_CONTROL) & 0x80000 && !frenchShift)
406 if(key != leftAlt && key != rightAlt && ::GetKeyState(VK_MENU) & 0x80000 && !frenchShift)
409 byte ch = Interface::TranslateKeykey, code.shift);
410 if(::GetKeyState(VK_CAPITAL))
414 if(msg == WM_KEYUP || msg == WM_SYSKEYUP)
416 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
420 if(!(lParam & 0x40000000))
425 Class * keyClass = eSystem_FindClass(__ecereModule, "Key");
426 code.OnGetString(string, null, null);
427 eSystem_Logf("%s\n", string);
431 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
434 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code,ch);
441 // --- Window procedure ---
442 DWORD CALLBACK ::ApplicationWindow(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
445 Window window = (Window)GetWindowLongPtr(windowHandle, GWLP_USERDATA);
447 Window window = (Window)GetWindowLong(windowHandle, GWL_USERDATA);
449 static Point lastPos;
456 case WM_QUERYNEWPALETTE:
457 case WM_PALETTECHANGED:
459 window.display.RestorePalette();
460 UpdateWindow(windowHandle);
464 if(!fullScreenMode && !guiApp.IsModeSwitching())
468 Window modalRoot = window.FindModal();
470 HWND modalWindow = modalRoot ? modalRoot.windowHandle : null;
472 FLASHWINFO flashInfo = { 0 };
473 flashInfo.cbSize = sizeof(FLASHWINFO);
474 flashInfo.hwnd = window.windowHandle;
475 flashInfo.uCount = 0;
476 flashInfo.dwFlags = FLASHW_STOP;
477 FlashWindowEx((void *)&flashInfo);
479 if(modalWindow && modalWindow != windowHandle)
480 modalRoot.ExternalActivate(true, true, window, null);
482 window.ExternalActivate(true, true, window, null);
489 foreground = GetForegroundWindow();
490 if(foreground == windowHandle && lParam)
491 foreground = (HWND)lParam;
493 GetWindowThreadProcessId(foreground, &id);
496 windowLong = (void*)GetWindowLongPtr(foreground, GWLP_WNDPROC);
498 windowLong = (void*)GetWindowLong(foreground, GWL_WNDPROC);
500 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
501 // The != ApplicationWindow check is for not recognizing the Console window as an Ecere Window
502 // That check causes a problem with the OpenGL driver which seems to popup a window of a different class
503 if(window.displaySystem && window.displaySystem.driver == class(OpenGLDisplayDriver))
504 windowLong = (void *)ApplicationWindow;
506 if(id != GetCurrentProcessId() || windowLong != (void *)ApplicationWindow)
507 window.ExternalActivate(false, true, window, null);
508 // DefWindowProc for WM_NCACTIVATE draws the decorations, make sure it's drawn in the right state
509 return (uint)DefWindowProc(windowHandle, msg, window.active, lParam);
514 for(window = guiApp.desktop.firstChild; window; window = window.next)
515 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
516 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
520 return (uint)DefWindowProc(windowHandle, msg, window ? window.active : wParam, lParam);
523 if(!guiApp.IsModeSwitching())
527 HDC hdc = GetDC(windowHandle);
530 guiApp.SetAppFocus(false);
531 ShowWindow(windowHandle, SW_MINIMIZE);
532 ChangeDisplaySettings(null,0);
533 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
537 ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
538 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
540 window.display.RestorePalette();
541 ShowWindow(windowHandle, SW_SHOWNORMAL);
542 guiApp.SetAppFocus(true);
544 window.display.Resize(window.clientSize.w, window.clientSize.h);
547 ReleaseDC(windowHandle, hdc);
553 if(wParam && !guiApp.desktop.active /*&& lParam != GetCurrentThreadID()*/)
557 for(window = guiApp.desktop.firstChild; window; window = window.next)
558 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
559 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
562 guiApp.SetAppFocus((bool)wParam);
566 guiApp.SetAppFocus((bool)wParam);
570 if(!window.alphaBlend || window.display.pixelFormat != pixelFormat888)
575 #define ACCESS_ITEM(l, id) \
576 ((FastItem)(((id) == -1) ? null : (((byte *)((l).items)) + (id) * (l).itemSize)))
579 #define ACCESS_ITEM(l, id) \
582 BoxItem item = window.dirtyArea.count ? (BoxItem)ACCESS_ITEM(window.dirtyArea, window.dirtyArea.first) : null;
584 BeginPaint(windowHandle, &ps);
586 // Prevent flickering if we're going to update anyways
588 printf(" Paint message (%d, %d)-(%d, %d)\n",
589 item ? item.box.left : 0,
590 item ? item.box.top : 0,
591 item ? item.box.right : 0,
592 item ? item.box.bottom : 0);
594 // Causes redraw bug...
595 if(!window.manageDisplay || !item ||
598 item.box.right < window.size.w - 1 ||
599 item.box.bottom < window.size.h - 1)
601 Box box { ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-1, ps.rcPaint.bottom-1 };
602 window.UpdateDirty(box);
604 // TODO: Fix precompiler with
605 // window.UpdateDirty((Box *)&ps.rcPaint);
606 EndPaint(windowHandle, &ps);
610 case WM_DISPLAYCHANGE:
612 static int lastBits = 0;
613 static int lastRes = 0;
614 if(lastBits != wParam || lastRes != lParam)
616 lastBits = (int)wParam;
617 lastRes = (int)lParam;
619 externalDisplayChange = true;
620 if(guiApp.desktop.DisplayModeChanged())
623 if(!window.style.hidden)
624 ShowWindow(windowHandle, SW_SHOWNOACTIVATE /*SW_SHOWNORMAL*/);
625 window.FigureCaption(caption);
626 SetRootWindowCaption(window, caption);
628 externalDisplayChange = false;
643 if(msg != WM_CHAR && window.composing)
648 case WM_SYSKEYDOWN: min = max = WM_SYSCHAR; break;
649 case WM_KEYDOWN: min = max = WM_CHAR; break;
650 case WM_SYSKEYUP: min = WM_SYSCHAR; max = WM_SYSDEADCHAR; break;
651 case WM_KEYUP: min = WM_CHAR; max = WM_DEADCHAR; break;
654 // This is very annoying.
655 // PeekMessage somehow invokes this function directly... don't know why.
656 // Only crashes when running through debug mode in visual studio
658 if(msg == WM_CHAR || msg == WM_DEADCHAR || PeekMessage(&charMsg, windowHandle, min, max, PM_REMOVE))
660 ch = (msg == WM_CHAR || msg == WM_DEADCHAR) ? (unichar)wParam : (unichar)charMsg.wParam;
661 // TOCHECK: What is this for again? Fixing some obscure activation status?
662 // -- I believe this was somehow allowing 'unmaximizing', but was causing problems
663 // as there was no way to prevent AltEnter from doing so (e.g. when it is used for a node property)
664 // Worked around by fixing ProcessHotKeys to properly check for sysButtons in parent.parent when sys buttons
665 // are placed inside a menu bar for a document
667 if(msg == WM_SYSKEYDOWN && ch == 13)
669 ShowWindow(window.windowHandle, window.state == maximized ? SW_MAXIMIZE : SW_SHOWNORMAL);
670 // This last line been commented out for a long time:
671 // window.ExternalActivate(true, true, window, null);
674 if(msg == WM_SYSKEYUP || msg == WM_KEYUP)
676 if(!ProcessKeyMessage(window, WM_KEYDOWN, 0x40000000, 0, ch))
682 ProcessKeyMessage(window, msg, wParam, lParam, ch);
683 if(window.composing && msg == WM_CHAR)
685 COMPOSITIONFORM form;
686 Window caretOwner = guiApp.caretOwner;
689 HIMC ctx = ImmGetContext(windowHandle);
690 form.dwStyle = CFS_POINT;
691 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
692 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.x + caretOwner.absPosition.y - window.absPosition.y + 2;
693 window.composing = true;
694 ImmSetCompositionWindow(ctx, &form);
695 ImmReleaseContext(windowHandle, ctx);
698 if(msg != WM_MOUSEWHEEL)
702 case WM_IME_STARTCOMPOSITION:
704 COMPOSITIONFORM form;
705 Window caretOwner = guiApp.caretOwner;
708 HIMC ctx = ImmGetContext(windowHandle);
709 form.dwStyle = CFS_POINT;
710 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
711 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y + 2;
713 form.rcArea.left = window.caretPos.x;
714 form.rcArea.top = 250;
715 form.rcArea.right = 500;
716 form.rcArea.bottom = 300;
718 window.composing = true;
719 DefWindowProc(windowHandle, msg, wParam, lParam);
720 ImmSetCompositionWindow(ctx, &form);
721 ImmSetStatusWindowPos(ctx, &form.ptCurrentPos);
723 FontResource res = caretOwner.font;
724 LOGFONT font = { 0 };
726 int pixels = GetDeviceCaps(hdc, LOGPIXELSY);
728 font.lfHeight = -(int)((float)res.size * pixels / 72 + 0.5);
729 font.lfWeight = res.flags.bold ? FW_BOLD : FW_NORMAL;
730 font.lfItalic = res.flags.italic ? TRUE : FALSE,
731 font.lfUnderline = res.flags.underline ? TRUE : FALSE;
732 font.lfCharSet = DEFAULT_CHARSET;
733 font.lfOutPrecision = OUT_DEFAULT_PRECIS;
734 font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
735 font.lfQuality = DEFAULT_QUALITY;
736 font.lfPitchAndFamily = (byte)(DEFAULT_PITCH|FF_DONTCARE); // TODO: Fix compiler 0 | 0 to produce byte, not int
737 UTF8toUTF16Buffer(res.faceName, font.lfFaceName, LF_FACESIZE);
739 ImmSetCompositionFont(ctx, &font);
740 ImmReleaseContext(windowHandle, ctx);
746 case WM_IME_ENDCOMPOSITION:
747 window.composing = false;
748 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
749 /*case WM_IME_COMPOSITION:
750 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
752 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
754 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
756 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
758 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
760 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
761 case WM_IME_SETCONTEXT:
762 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
764 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
766 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
767 case WM_IME_COMPOSITIONFULL:
768 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
771 if(window.nativeDecorations)
772 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
782 case WM_LBUTTONDBLCLK:
784 case WM_RBUTTONDBLCLK:
786 case WM_MBUTTONDBLCLK:
788 x = window.absPosition.x;
789 y = window.absPosition.y;
790 /*case WM_NCLBUTTONUP:
793 case WM_NCLBUTTONDOWN:
794 case WM_NCLBUTTONDBLCLK:
795 case WM_NCRBUTTONDOWN:
796 case WM_NCRBUTTONDBLCLK:
797 case WM_NCMBUTTONDOWN:
798 case WM_NCMBUTTONDBLCLK:*/
802 bool consequential = false;
804 x += (short)LOWORD(lParam);
805 y += (short)HIWORD(lParam);
807 if(window.nativeDecorations && msg != WM_NCMOUSEMOVE)
809 x += window.clientStart.x;
810 y += window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
813 if(::GetKeyState(VK_SHIFT) & 0x80000) code.shift = true;
814 if(::GetKeyState(VK_CONTROL) & 0x80000) code.ctrl = true;
815 if(::GetKeyState(VK_MENU) & 0x80000) code.alt = true;
816 if(wParam & MK_LBUTTON) code.left = true;
817 if(wParam & MK_MBUTTON) code.middle = true;
818 if(wParam & MK_RBUTTON) code.right = true;
820 if(msg == WM_MOUSEMOVE)
822 if(lastPos.x == x && lastPos.y == y)
823 consequential = true;
833 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove, x,y,&code, consequential, false);
835 //case WM_NCLBUTTONDBLCLK:
836 case WM_LBUTTONDBLCLK:
837 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick, x,y,&code, false, true))
839 //case WM_NCLBUTTONDOWN:
841 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown,x,y,&code, false,
842 /*(msg == WM_LBUTTONDBLCLK) ? false: */true);
844 //case WM_NCLBUTTONUP:
845 case WM_LBUTTONUP: window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp, x,y,&code, false, false);break;
846 //case WM_NCMBUTTONDBLCLK:
847 case WM_MBUTTONDBLCLK:
848 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick, x,y,&code, false, false))
850 //case WM_NCMBUTTONDOWN:
852 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown, x,y,&code, false,
853 (msg == WM_LBUTTONDBLCLK) ? false: true);
855 //case WM_NCMBUTTONUP:
857 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp, x,y,&code, false, false);
859 //case WM_NCRBUTTONDBLCLK:
860 case WM_RBUTTONDBLCLK:
861 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick, x,y,&code, false, true))
863 //case WM_NCRBUTTONDOWN:
865 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown, x,y,&code, false,
866 (msg == WM_LBUTTONDBLCLK) ? false: true);
868 //case WM_NCRBUTTONUP:
870 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp, x,y,&code, false, false);
874 if(msg == WM_NCMOUSEMOVE)
875 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
880 if(window.nativeDecorations)
882 if(DefWindowProc(windowHandle, msg, wParam, lParam))
885 // SetCursor(systemCursors[0]);
886 SetCursor((lastCursor == (SystemCursor)-1) ? null : systemCursors[lastCursor]);
889 case WM_EXITMENULOOP:
890 case WM_EXITSIZEMOVE:
891 // We had some DirectInput stuff in here
893 case WM_ENTERMENULOOP:
894 case WM_ENTERSIZEMOVE:
895 // We had some DirectInput stuff in here
905 WINDOWPLACEMENT placement = { 0 };
907 placement.length = sizeof(WINDOWPLACEMENT);
908 GetWindowRect(windowHandle, &rcWindow);
909 GetWindowPlacement(windowHandle, &placement);
911 x = rcWindow.left - desktopX;
912 y = rcWindow.top - desktopY;
913 w = rcWindow.right - rcWindow.left;
914 h = rcWindow.bottom - rcWindow.top;
916 AeroSnapPosition(window, x, y, w, h);
923 RECT * rect = (RECT *)lParam;
927 window.GetDecorationsSize(&ew, &eh);
929 w = rect->right - rect->left;
930 h = rect->bottom - rect->top;
938 w = Max(w, window.minSize.w);
939 h = Max(h, window.minSize.h);
940 w = Min(w, window.maxSize.w);
941 h = Min(h, window.maxSize.h);
943 if(!window.OnResizing(&w, &h))
945 w = window.clientSize.w;
946 h = window.clientSize.h;
949 w = Max(w, window.skinMinSize.w);
950 h = Max(h, window.skinMinSize.h);
955 if(wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT)
956 rect->left = rect->right - w;
958 rect->right = rect->left + w;
960 if(wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOP || wParam == WMSZ_TOPRIGHT)
961 rect->top = rect->bottom - h;
963 rect->bottom = rect->top + h;
969 if(window.nativeDecorations)
973 GetWindowRect(windowHandle, &rcWindow);
975 if(wParam == SIZE_MAXIMIZED && window.state != maximized)
976 window.state = maximized;
977 else if(wParam == SIZE_MINIMIZED && window.state != minimized)
978 window.state = minimized;
979 else if(wParam == SIZE_RESTORED && window.state != normal && window.visible)
980 window.state = normal;
982 x = rcWindow.left - desktopX;
983 y = rcWindow.top - desktopY;
984 w = rcWindow.right - rcWindow.left;
985 h = rcWindow.bottom - rcWindow.top;
987 AeroSnapPosition(window, x, y, w, h);
988 if(!guiApp.modeSwitching)
989 window.UpdateVisual(null);
992 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
996 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
1000 return (uint)DefMDIChildProc(windowHandle, msg, wParam, lParam);
1001 // return DefWindowProc(windowHandle, msg, wParam, lParam);
1005 // --- DirectInput ---
1007 #ifndef ECERE_NODINPUT
1009 #ifndef ECERE_NOJOYSTICK
1010 bool CALLBACK ::JoystickCallback( const DIDEVICEINSTANCE* pdidInstance, void * context )
1012 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_Joystick,
1013 (IDirectInputDevice **)&directJoysticks[numJoysticks], null ))
1015 return DIENUM_CONTINUE;
1018 bool CALLBACK ::JoystickAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, void * context )
1021 IDirectInputDevice2 * curJoy = (IDirectInputDevice2 *)context;
1023 diprg.diph.dwSize = sizeof(DIPROPRANGE);
1024 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1025 diprg.diph.dwHow = DIPH_BYOFFSET;
1026 diprg.diph.dwObj = pdidoi->dwOfs;
1029 if(curJoy->lpVtbl->SetProperty(curJoy, DIPROP_RANGE, &diprg.diph))
1031 return DIENUM_CONTINUE;
1035 void ::TerminateDirectInput()
1040 directMouse->lpVtbl->Unacquire(directMouse);
1041 directMouse->lpVtbl->Release(directMouse);
1044 #ifndef ECERE_NOJOYSTICK
1045 for(j=0; j<numJoysticks; j++)
1047 if (directJoysticks[j])
1049 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1050 directJoysticks[j]->lpVtbl->Release(directJoysticks[j]);
1051 directJoysticks[j] = null;
1058 dInput->lpVtbl->Release(dInput);
1062 directInputCreate = null;
1066 FreeLibrary(dInputDll);
1071 bool ::InitDirectInput()
1073 bool result = false;
1075 dInputDll = LoadLibraryA("dinput.dll");
1078 if((directInputCreate = (void *)GetProcAddress(dInputDll, "DirectInputCreateA")))
1080 if(!directInputCreate( hInstance, DIRECTINPUT_VERSION, &dInput, null ))
1085 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_SysMouse, &directMouse, null ))
1086 directMouse->lpVtbl->SetDataFormat(directMouse, &c_dfDIMouse );
1088 #ifndef ECERE_NOJOYSTICK
1090 dInput->lpVtbl->EnumDevices(dInput, DIDEVTYPE_JOYSTICK, JoystickCallback, null, DIEDFL_ATTACHEDONLY );
1091 for(j=0; j<NUMJOY; j++)
1092 if(directJoysticks[j])
1093 if(!directJoysticks[j]->lpVtbl->SetDataFormat(directJoysticks[j], &c_dfDIJoystick ))
1094 directJoysticks[j]->lpVtbl->EnumObjects(directJoysticks[j], JoystickAxesCallback, directJoysticks[j], DIDFT_AXIS );
1103 void ::AcquireDirectInput(HWND windowHandle, bool state)
1105 if((state && !acquiredWindow) || (!state && acquiredWindow == windowHandle))
1112 directMouse->lpVtbl->SetCooperativeLevel(directMouse, /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1113 directMouse->lpVtbl->Acquire(directMouse);
1116 directMouse->lpVtbl->Unacquire(directMouse);
1118 #ifndef ECERE_NOJOYSTICK
1119 for(j = 0; j<NUMJOY; j++)
1120 if(directJoysticks[j])
1124 directJoysticks[j]->lpVtbl->SetCooperativeLevel(directJoysticks[j], /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1125 directJoysticks[j]->lpVtbl->Acquire(directJoysticks[j]);
1128 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1131 acquiredWindow = state ? windowHandle : null;
1136 /****************************************************************************
1137 /// INTEGRATION FUNCTIONALITY /////////////
1138 ****************************************************************************/
1139 bool eisWIN32ProcessKey(Window window, DWORD msg, DWORD wParam, DWORD lParam, byte ch)
1141 return ProcessKeyMessage(window, msg, wParam, lParam, ch);
1144 HWND eisWIN32GetWindowHandle(Window window)
1146 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;
1149 /****************************************************************************
1150 /// DRIVER IMPLEMENTATION /////////////
1151 ****************************************************************************/
1154 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, uint64 dwUser, uint64 dw1, uint64 dw2)
1156 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1159 guiApp.SignalEvent();
1162 // --- User Interface System ---
1167 CS_DBLCLKS, ApplicationWindow, 0L, 0L, 0,
1168 LoadIcon(null, IDI_APPLICATION),
1175 wcl.hInstance = hInstance = GetModuleHandle(null);
1176 RegisterClass(&wcl);
1178 // Move that in reposition desktop
1179 startBar = FindWindowA("Shell_TrayWnd", null);
1181 systemCursors[arrow] = LoadCursor(null, IDC_ARROW);
1182 systemCursors[iBeam] = LoadCursor(null, IDC_IBEAM);
1183 systemCursors[cross] = LoadCursor(null, IDC_CROSS);
1184 systemCursors[moving] = LoadCursor(null, IDC_SIZEALL);
1185 systemCursors[sizeNESW] = LoadCursor(null, IDC_SIZENESW);
1186 systemCursors[sizeNS ] = LoadCursor(null, IDC_SIZENS);
1187 systemCursors[sizeNWSE] = LoadCursor(null, IDC_SIZENWSE);
1188 systemCursors[sizeWE ] = LoadCursor(null, IDC_SIZEWE);
1189 systemCursors[hand ] = LoadCursor(null, IDC_HAND);
1191 SetTimer(null, 0, (DWORD)(1000.0 / 18.2), null);
1192 //SetTimer(null, 0, 1, null);
1196 hiResTimer = timeSetEvent(
1199 TimerProc,0,TIME_PERIODIC);
1202 hiResTimer = timeSetEvent(
1205 TimerProc,0,TIME_PERIODIC);
1209 topWindow = CreateWindowEx(0, className, "",WS_POPUP,0,0,1,1,HWND_DESKTOP,
1210 null, hInstance, null);
1217 if(hiResTimer) timeKillEvent(hiResTimer);
1218 #ifndef ECERE_NODINPUT
1219 TerminateDirectInput();
1223 void SetTimerResolution(uint hertz)
1225 if(hiResTimer) timeKillEvent(hiResTimer);
1227 hiResTimer = timeSetEvent(1000 / hertz, 1000 / hertz, TimerProc, 0, TIME_PERIODIC);
1230 bool ProcessInput(bool processAll)
1234 RepositionDesktop(true);
1235 if(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
1237 while(PeekMessage(&msg,0,0,0,PM_REMOVE))
1239 TranslateMessage(&msg);
1240 DispatchMessage(&msg);
1241 // printf("%d\n", msg.message);
1242 if(!processAll || msg.message == WM_TIMER) break;
1251 MsgWaitForMultipleObjects(1, (void **)guiApp.semaphore, FALSE, (uint)(1000 / 18.2) /*INFINITE*/, QS_ALLINPUT);
1254 void Lock(Window window)
1259 void Unlock(Window window)
1264 char ** GraphicsDrivers(int * numDrivers)
1266 static char *graphicsDrivers[] = { "GDI", "DirectDraw", "OpenGL", "Direct3D", "Direct3D8", "Direct3D9" };
1267 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1268 return (char **)graphicsDrivers;
1271 void GetCurrentMode(bool * fullScreen, Resolution * resolution, PixelFormat * colorDepth, int * refreshRate)
1273 *fullScreen = fullScreenMode;
1277 for(c = 0; c<Resolution::enumSize; c++)
1278 if(GetResolutionWidth(c) == devMode.dmPelsWidth && GetResolutionHeight(c) == devMode.dmPelsHeight)
1283 switch(devMode.dmBitsPerPel)
1285 case 8: *colorDepth = pixelFormat8; break;
1286 case 16: *colorDepth = pixelFormat555; break;
1287 default: *colorDepth = pixelFormat888; break;
1289 * refreshRate = devMode.dmDisplayFrequency;
1293 void EnsureFullScreen(bool *fullScreen)
1298 bool ScreenMode(bool fullScreen, Resolution resolution, PixelFormat colorDepth, int refreshRate, bool * textMode)
1303 fullScreenMode = fullScreen;
1307 FillBytes(&devMode, 0, sizeof(DEVMODE));
1308 devMode.dmSize = (uint16)sizeof(devMode);
1309 devMode.dmFields |=DM_BITSPERPEL;
1310 devMode.dmFields |=DM_PELSWIDTH|DM_PELSHEIGHT;
1311 devMode.dmFields |= DM_DISPLAYFREQUENCY;
1312 devMode.dmBitsPerPel = colorDepth ? GetDepthBits(colorDepth) : GetDeviceCaps(hdc, BITSPIXEL);
1313 devMode.dmPelsWidth = resolution ? GetResolutionWidth(resolution) : GetSystemMetrics(SM_CXSCREEN);
1314 devMode.dmPelsHeight = resolution ? GetResolutionHeight(resolution) : GetSystemMetrics(SM_CYSCREEN);
1315 devMode.dmDisplayFrequency = refreshRate ? refreshRate : GetDeviceCaps(hdc, VREFRESH);
1316 if(ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1320 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
1321 guiApp.SetDesktopPosition(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), false);
1326 static bool firstTime = true;
1328 ChangeDisplaySettings(null, 0);
1330 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
1331 desktopX = desktopY = desktopW = desktopH = 0;
1333 RepositionDesktop(false);
1340 // --- Window Creation ---
1341 void * CreateRootWindow(Window window)
1344 uint16 * text = UTF8toUTF16(window.text, null);
1347 windowHandle = CreateWindowEx(0, className, text,
1349 0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
1351 null, hInstance, null);
1352 ShowWindow(windowHandle, SW_SHOWNORMAL);
1354 else if(window.systemParent)
1355 windowHandle = CreateWindowEx(0, className, text,
1356 WS_CHILD,0,0,1,1, window.systemParent, null, hInstance, null);
1361 HWND parentWindow = null; //HWND_DESKTOP; // we get different behaviors with desktop...
1362 Window master = window.master, rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1363 if(window.style.stayOnTop)
1364 exStyle |= WS_EX_TOPMOST;
1366 if(rootWindow && (window._isModal || window.style.interim))
1367 parentWindow = rootWindow.is3D ? rootWindow.parent.windowHandle : rootWindow.windowHandle;
1369 if(window.alphaBlend)
1370 // if(window.background.a < 255) //&& window.style & ES_REDRAW) Not needed anymore?
1371 exStyle |= WS_EX_LAYERED; // | WS_EX_TRANSPARENT;
1373 // Toolwindow will disappear if they don't have AppWindow set
1374 if(window.style.showInTaskBar || (!parentWindow && window.style.thin))
1376 exStyle |= WS_EX_APPWINDOW;
1377 parentWindow = null;
1380 if(window.style.thin)
1381 exStyle |= WS_EX_TOOLWINDOW;
1383 if(window.windowHandle)
1384 windowHandle = window.windowHandle;
1387 if(window.nativeDecorations)
1389 BorderBits borderStyle = window.borderStyle; // FIXME!
1390 style = WS_OVERLAPPED;
1391 if(borderStyle.fixed)
1392 style |= WS_CAPTION;
1394 style |= WS_SYSMENU;
1395 if(borderStyle.sizable)
1396 style |= WS_THICKFRAME;
1397 if(window.hasMinimize)
1398 style |= WS_MINIMIZEBOX;
1399 if(window.hasMaximize)
1400 style |= WS_MAXIMIZEBOX;
1402 windowHandle = CreateWindowEx(
1405 style | (window.systemParent ? WS_CHILD :
1406 (WS_POPUP | (window.style.hasMinimize ? WS_MINIMIZEBOX : 0))),
1407 0,0,1,1, parentWindow, null, hInstance, null);
1409 if(exStyle & WS_EX_LAYERED)
1410 SetLayeredWindowAttributes(windowHandle, 0, 255 /*A(window.background)*/, LWA_ALPHA);
1416 SetWindowLongPtr(windowHandle, GWLP_USERDATA, (int64)window);
1418 SetWindowLong(windowHandle, GWL_USERDATA, (DWORD)window);
1421 return windowHandle;
1424 void DestroyRootWindow(Window window)
1427 int c, lockCount = guiApp.lockMutex.lockCount;
1428 for(c = 0; c < lockCount; c++)
1429 guiApp.lockMutex.Release();
1431 oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1433 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1435 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1437 DestroyIcon(oldIcon);
1439 ShowWindow(window.windowHandle, SW_HIDE);
1442 SetWindowLongPtr(window.windowHandle, GWLP_USERDATA, (int64)null);
1444 SetWindowLong(window.windowHandle, GWL_USERDATA, 0);
1446 DestroyWindow(window.windowHandle);
1448 for(c = 0; c < lockCount; c++)
1449 guiApp.lockMutex.Wait();
1451 window.windowHandle = null;
1454 // -- Window manipulation ---
1456 void SetRootWindowCaption(Window window, char * name)
1458 uint16 * text = UTF8toUTF16(name, null);
1460 SetWindowText(window.windowHandle, text);
1465 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
1467 int flags = SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS;
1469 if(!window.systemParent && !fullScreenMode)
1475 if(!move) flags |= SWP_NOMOVE;
1476 if(!resize) flags |= SWP_NOSIZE;
1478 /*if(move && resize)
1480 flags |= SWP_NOMOVE;
1481 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1482 flags &=~SWP_NOMOVE;
1483 flags |= SWP_NOSIZE;
1485 if(!window.nativeDecorations || window.state != maximized || !window.visible || guiApp.modeSwitching)
1486 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1489 void OrderRootWindow(Window window, bool topMost)
1491 SetWindowPos(window.windowHandle, topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0,
1492 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
1495 void SetRootWindowColor(Window window)
1497 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1498 if(window.alphaBlend && window.display.pixelFormat == pixelFormat888)
1500 /*if(A(window.background) == 255)
1502 if((style & WS_EX_LAYERED) == WS_EX_LAYERED)
1503 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style &~WS_EX_LAYERED);
1507 #ifndef ECERE_NOBLENDING
1508 if((style & WS_EX_LAYERED) != WS_EX_LAYERED)
1509 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style | WS_EX_LAYERED);
1511 // SetLayeredWindowAttributes(window.windowHandle, 0, 255 /*Max(A(window.background),1)*/, LWA_ALPHA);
1516 void OffsetWindow(Window window, int * x, int * y)
1518 if(window.systemParent)
1520 POINT point = {*x,*y};
1521 ClientToScreen(GetParent(window.windowHandle), &point);
1527 void UpdateRootWindow(Window window)
1529 UpdateWindow(window.windowHandle);
1532 void SetRootWindowState(Window window, WindowState state, bool visible)
1541 if((window.active || window.creationActivation == activate) && !externalDisplayChange)
1542 ShowWindow(window.windowHandle, (window.nativeDecorations && state == maximized) ? SW_MAXIMIZE : SW_SHOWNORMAL);
1545 WINDOWPLACEMENT plc = { 0 };
1546 GetWindowPlacement(window.windowHandle, &plc);
1547 plc.showCmd = (window.nativeDecorations && state == maximized) ? SW_MAXIMIZE : SW_SHOWNORMAL;
1548 ShowWindow(window.windowHandle, SW_SHOWNOACTIVATE);
1549 SetWindowPlacement(window.windowHandle, &plc);
1554 ShowWindow(window.windowHandle, SW_MINIMIZE);
1560 ShowWindow(window.windowHandle, SW_HIDE);
1564 void ActivateRootWindow(Window window)
1566 if(!externalDisplayChange)
1567 SetForegroundWindow(window.windowHandle);
1570 void FlashRootWindow(Window window)
1572 FLASHWINFO flashInfo = { 0 };
1573 flashInfo.cbSize = sizeof(FLASHWINFO);
1574 flashInfo.hwnd = window.windowHandle;
1575 flashInfo.uCount = 1;
1576 flashInfo.dwFlags = FLASHW_TRAY; // FLASHW_ALL;
1578 FlashWindowEx((void *)&flashInfo);
1582 // --- Mouse-based window movement ---
1583 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
1585 if(!fullScreenMode && !window.systemParent)
1586 // Commented out for Chess game because AI thread takes over the moving of main window... Still required?
1587 //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
1588 ; //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
1591 SetWindowPos(window.windowHandle, HWND_TOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1592 mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
1593 if(!window.stayOnTop)
1594 SetWindowPos(window.windowHandle, HWND_NOTOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1598 void StopMoving(Window window)
1600 if(!fullScreenMode && !window.systemParent)
1601 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
1604 // -- Mouse manipulation ---
1606 void GetMousePosition(int *x, int *y)
1609 GetCursorPos(&point);
1614 void SetMousePosition(int x, int y)
1619 void SetMouseRange(Window window, Box box)
1621 ClipCursor((RECT *) box);
1624 void SetMouseCapture(Window window)
1627 SetCapture(window.windowHandle);
1632 // -- Mouse cursor ---
1634 void SetMouseCursor(Window window, SystemCursor cursor)
1636 if(lastCursor != cursor)
1638 lastCursor = cursor;
1639 SetCursor((cursor == (SystemCursor)-1) ? null : systemCursors[cursor]);
1645 void SetCaret(int x, int y, int size)
1650 // --- Clipboard manipulation ---
1652 void ClearClipboard()
1654 if(OpenClipboard(null))
1661 bool AllocateClipboard(ClipBoard clipBoard, uint size)
1663 bool result = false;
1664 clipBoard.text = new byte[size];
1669 bool SaveClipboard(ClipBoard clipBoard)
1671 bool result = false;
1675 uint16 * u16text = UTF8toUTF16(clipBoard.text, &wordCount);
1677 clipBoard.handle = GlobalAlloc(GHND | GMEM_DDESHARE, wordCount * 2);
1678 if(clipBoard.handle)
1680 uint16 * text = GlobalLock(clipBoard.handle);
1683 memcpy(text, u16text, wordCount * 2);
1684 GlobalUnlock(clipBoard.handle);
1685 if(OpenClipboard(null))
1688 if(SetClipboardData(CF_UNICODETEXT, clipBoard.handle))
1696 GlobalFree(clipBoard.handle);
1703 bool LoadClipboard(ClipBoard clipBoard)
1705 bool result = false;
1706 if(OpenClipboard(null))
1708 if((clipBoard.handle = GetClipboardData(CF_UNICODETEXT)))
1710 uint16 * u16text = GlobalLock(clipBoard.handle);
1713 clipBoard.text = UTF16toUTF8(u16text);
1715 GlobalUnlock(clipBoard.handle);
1723 void UnloadClipboard(ClipBoard clipBoard)
1725 delete clipBoard.text;
1728 // --- State based input ---
1730 bool AcquireInput(Window window, bool state)
1732 #ifndef ECERE_NODINPUT
1733 if(dInput || InitDirectInput())
1735 AcquireDirectInput(window.windowHandle, state);
1742 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
1744 bool result = false;
1745 #ifndef ECERE_NODINPUT
1746 DIMOUSESTATE dims = {0};
1748 if(acquiredWindow && directMouse)
1750 if(directMouse->lpVtbl->GetDeviceState(directMouse, sizeof(DIMOUSESTATE), &dims ))
1752 FillBytes(&dims, 0, sizeof(dims));
1753 directMouse->lpVtbl->Acquire(directMouse);
1757 /* We don't really want this...
1760 if(GetAsyncKeyState(VK_LBUTTON))
1761 dims.rgbButtons[0] |= 0x80;
1762 if(GetAsyncKeyState(VK_RBUTTON))
1763 dims.rgbButtons[1] |= 0x80;
1764 if(GetAsyncKeyState(VK_MBUTTON))
1765 dims.rgbButtons[2] |= 0x80;
1774 Window window = (Window)GetWindowLongPtr(acquiredWindow, GWLP_USERDATA);
1776 Window window = (Window)GetWindowLong(acquiredWindow, GWL_USERDATA);
1778 ProcessKeyMessage(window, WM_MOUSEWHEEL, ((uint16)(short)dims.lZ) << 16, 0, 0);
1782 *buttons = MouseButtons {
1783 left = (dims.rgbButtons[0] & 0x80) ? true : false,
1784 right = (dims.rgbButtons[1] & 0x80) ? true : false,
1785 middle = (dims.rgbButtons[2] & 0x80) ? true : false };
1792 bool GetJoystickState(int device, Joystick joystick)
1794 bool result = false;
1795 #ifndef ECERE_NODINPUT
1796 if(joystick != null)
1798 DIJOYSTATE dijs = {0};
1799 #ifndef ECERE_NOJOYSTICK
1800 if(acquiredWindow && device < numJoysticks)
1802 if(directJoysticks[device])
1804 directJoysticks[device]->lpVtbl->Poll(directJoysticks[device]);
1805 if(directJoysticks[device]->lpVtbl->GetDeviceState(directJoysticks[device], sizeof(DIJOYSTATE), &dijs ))
1806 directJoysticks[device]->lpVtbl->Acquire(directJoysticks[device]);
1811 joystick.x = dijs.lX;
1812 joystick.y = dijs.lY;
1813 joystick.z = dijs.lZ;
1814 joystick.rx = dijs.lRx;
1815 joystick.ry = dijs.lRy;
1816 joystick.rz = dijs.lRz;
1818 ((dijs.rgbButtons[0] & 0x80) ? JOY_BUTTON1 : 0)
1819 | ((dijs.rgbButtons[1] & 0x80) ? JOY_BUTTON2 : 0)
1820 | ((dijs.rgbButtons[2] & 0x80) ? JOY_BUTTON3 : 0)
1821 | ((dijs.rgbButtons[3] & 0x80) ? JOY_BUTTON4 : 0);
1827 bool GetKeyState(Key key)
1829 bool keyState = false;
1830 if(key < 256 || key == alt || key == shift || key == control)
1834 ks = GetAsyncKeyState(VK_MENU);
1835 else if(key == control)
1836 ks = GetAsyncKeyState(VK_CONTROL);
1837 else if(key == shift)
1838 ks = GetAsyncKeyState(VK_SHIFT);
1839 else if(key2VK[key])
1840 ks = GetAsyncKeyState(key2VK[key]);
1841 keyState = (ks & 0x80000) ? true : false;
1843 else if(key == capsState)
1844 keyState = (::GetKeyState(VK_CAPITAL) & 0x00000001) != 0;
1845 else if(key == numState)
1846 keyState = (::GetKeyState(VK_NUMLOCK) & 0x00000001) != 0;
1847 else if(key == scrollState)
1848 keyState = (::GetKeyState(VK_SCROLL) & 0x00000001) != 0;
1852 bool SetIcon(Window window, BitmapResource resource)
1855 HICON oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1857 // Dialogs Inherit master's icon if none set
1858 if(!window.style.showInTaskBar && window.hasClose)
1860 Window master = window.master;
1861 while(master && !resource)
1863 Window rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1864 if(rootWindow && rootWindow.icon)
1865 resource = rootWindow.icon;
1867 master = master.master;
1871 // WARNING -- putting this here as it is right after CreateRootWindow
1872 // Take out Layered flag if we're not in 24 bit
1874 if(window.alphaBlend && window.display.pixelFormat != pixelFormat888)
1876 #ifndef ECERE_NOBLENDING
1877 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1878 style &= ~WS_EX_LAYERED;
1879 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style);
1885 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1887 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1890 DestroyIcon(oldIcon);
1896 if(bitmap.Load(resource.fileName, null, null))
1899 PixelFormat format = window.display.pixelFormat;
1900 int bits = GetDepthBits(format);
1903 bitmap.Convert(null, pixelFormat888, null);
1904 and.Allocate(null, (bitmap.width+7/8), bitmap.height, 0, pixelFormat8, false);
1906 blend = bits == 32 || bitmap.pixelFormat != pixelFormat888;
1909 byte * picture = and.picture;
1912 uint size = bitmap.height * bitmap.width;
1917 while(m < 8 && c < size)
1920 mask |= blend ? (!((ColorAlpha *)bitmap.picture)[c].a) : (((ColorAlpha *)bitmap.picture)[c].a <= 192);
1924 picture[b++] = mask;
1929 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1930 if(blend ? (!color.a) : (color.a <= 192))
1932 color.color = { 0, 0, 0 };
1933 ((ColorAlpha *)bitmap.picture)[c] = color;
1938 if(bits == 15) { bits = 16; format = pixelFormat565; };
1939 bitmap.Convert(null, format, null);
1941 icon = CreateIcon(hInstance, bitmap.width, bitmap.height, 1, (byte)bits, and.picture, bitmap.picture);
1946 SendMessage(window.windowHandle, WM_SETICON, ICON_BIG, (LPARAM)icon);
1947 SendMessage(window.windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)icon);
1951 void ::GetScreenArea(Window window, Box box)
1953 HMONITOR monitor = MonitorFromWindow(window.windowHandle, MONITOR_DEFAULTTONEAREST);
1954 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
1957 MONITORINFO info = { 0 };
1958 info.cbSize = sizeof(MONITORINFO);
1959 GetMonitorInfo(monitor, &info);
1960 // box = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
1961 box = { info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.right-1, info.rcMonitor.bottom-1 };
1963 if(taskBarMonitor == monitor)
1965 if(taskBarPlacement.rcNormalPosition.top <= box.top && taskBarPlacement.rcNormalPosition.bottom >= box.bottom)
1967 if(taskBarPlacement.rcNormalPosition.left <= box.left)
1969 if(taskBarState & ABS_AUTOHIDE)
1972 box.left = taskBarPlacement.rcNormalPosition.right;
1974 else if(taskBarState & ABS_AUTOHIDE)
1977 box.right = taskBarPlacement.rcNormalPosition.left;
1979 else if(taskBarPlacement.rcNormalPosition.left <= box.left && taskBarPlacement.rcNormalPosition.right >= box.right)
1981 if(taskBarPlacement.rcNormalPosition.top <= box.top)
1983 if(taskBarState & ABS_AUTOHIDE)
1986 box.top = taskBarPlacement.rcNormalPosition.bottom;
1988 else if(taskBarState & ABS_AUTOHIDE)
1991 box.bottom = taskBarPlacement.rcNormalPosition.top;
1995 box.left -= desktopX;
1996 box.top -= desktopY;
1997 box.right -= desktopX;
1998 box.bottom -= desktopY;