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_CONTROL,'A','S',
106 'D','F','G','H','J','K','L',VK_SEMI,VK_QUOTE,VK_TILDE,VK_SHIFT,VK_BACK_SLASH,'Z','X','C','V',
107 'B','N','M',VK_COMMA,VK_PERIOD,VK_DIVIDE,VK_SHIFT,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,0,0,0,0,0,VK_HOME,VK_UP,VK_PRIOR,VK_LEFT,VK_RIGHT,VK_END,VK_DOWN,VK_NEXT,VK_INSERT,VK_DELETE,
111 0,0,0,VK_SHIFT,VK_MENU,VK_CONTROL
113 static char className[] = "ECERE Application";
114 static HINSTANCE hInstance;
116 static DEVMODE devMode;
117 static HWND acquiredWindow = null;
118 static HCURSOR systemCursors[SystemCursor];
119 static bool fullScreenMode;
120 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
121 static DWORD hiResTimer;
122 // static HWND topWindow;
124 static HWND startBar;
126 #ifndef ECERE_NODINPUT
128 static HMODULE dInputDll = null;
129 static BOOL * (WINAPI * directInputCreate)(HINSTANCE, DWORD, IDirectInput **, void *);
130 static IDirectInput * dInput = null;
131 static IDirectInputDevice * directMouse = null;
132 #ifndef ECERE_NOJOYSTICK
133 static int numJoysticks = 0;
134 static IDirectInputDevice2 * directJoysticks[NUMJOY] = {null,null,null,null};
139 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
140 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
141 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
142 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
143 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
144 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
145 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
146 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
147 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
148 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
149 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
150 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
151 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
152 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
156 static Box lastMonitorAreas[32];
157 static Box monitorAreas[32];
160 static bool EnumerateMonitors(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
162 MONITORINFO info = { 0 };
163 info.cbSize = sizeof(MONITORINFO);
164 GetMonitorInfo(hMonitor, &info);
165 monitorAreas[monitor] = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
170 static bool externalDisplayChange;
172 static int taskBarState;
173 static WINDOWPLACEMENT taskBarPlacement;
174 static bool activateApp;
176 static SystemCursor lastCursor = (SystemCursor)-1;
178 void AeroSnapPosition(Window window, int x, int y, int w, int h)
180 int oldX = window.absPosition.x;
182 // To prevent having a window < minClientSize with Aero Snap, because we don't receive a WM_SIZING!
183 // The sensible implementation of this is in WM_SIZING
187 window.GetDecorationsSize(&ew, &eh);
195 gw = Max(gw, window.minSize.w);
196 gh = Max(gh, window.minSize.h);
197 gw = Min(gw, window.maxSize.w);
198 gh = Min(gh, window.maxSize.h);
200 if(!window.OnResizing(&gw, &gh))
202 gw = window.clientSize.w;
203 gh = window.clientSize.h;
206 gw = Max(gw, window.skinMinSize.w);
207 gh = Max(gh, window.skinMinSize.h);
212 if(w != gw || h != gh)
215 // Adjust x position if we resized from top or bottom left corner
223 guiApp.interfaceDriver.PositionRootWindow(window, x, y, w, h, move, true);
226 window.ExternalPosition(x, y, w, h);
229 class Win32Interface : Interface
231 class_property(name) = "Win32";
233 void ::RepositionDesktop(bool updateChildren)
236 static double lastTime = 0, time;
239 static Size lastScreen;
240 static Point lastScreenPos;
241 static WINDOWPLACEMENT lastPlacement;
243 static double lastAutoHideCheck = 0;
244 int newTaskBarState = taskBarState;
245 HMONITOR primaryMonitor;
248 if(time - lastTime < 0.1) return;
251 // Every sec, check for the auto hide property
252 if(time - lastAutoHideCheck > 1)
254 APPBARDATA appBarData = { 0 };
255 newTaskBarState = (int)SHAppBarMessage(ABM_GETSTATE, &appBarData);
256 lastAutoHideCheck = time;
260 EnumDisplayMonitors(null, null, EnumerateMonitors, 0);
261 for(c = 0; c<monitor; c++)
263 if(monitorAreas[c].left != lastMonitorAreas[c].left ||
264 monitorAreas[c].top != lastMonitorAreas[c].top ||
265 monitorAreas[c].right != lastMonitorAreas[c].right ||
266 monitorAreas[c].bottom != lastMonitorAreas[c].bottom)
272 memcpy(lastMonitorAreas, monitorAreas, sizeof(monitorAreas));
274 guiApp.virtualScreen =
276 GetSystemMetrics(SM_CXVIRTUALSCREEN),
277 GetSystemMetrics(SM_CYVIRTUALSCREEN)
279 guiApp.virtualScreenPos =
281 x = GetSystemMetrics(SM_XVIRTUALSCREEN),
282 y = GetSystemMetrics(SM_YVIRTUALSCREEN)
286 HMONITOR monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
289 MONITORINFO info = { 0 };
290 info.cbSize = sizeof(MONITORINFO);
291 primaryMonitor = monitor;
292 GetMonitorInfo(monitor, &info);
293 x = info.rcMonitor.left;
294 y = info.rcMonitor.top;
295 w = info.rcMonitor.right - info.rcWork.left;
296 h = info.rcMonitor.bottom - info.rcWork.top;
301 WINDOWPLACEMENT placement = { 0 };
302 placement.length = sizeof(WINDOWPLACEMENT);
304 if(!GetWindowPlacement(startBar, &placement))
306 startBar = FindWindowA("Shell_TrayWnd", null);
307 GetWindowPlacement(startBar, &placement);
310 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
311 if(primaryMonitor == taskBarMonitor)
313 if(placement.rcNormalPosition.top <= 0 && placement.rcNormalPosition.bottom >= h - 1)
315 if(placement.rcNormalPosition.left <= 0)
317 x = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.right;
321 w = (taskBarState & ABS_AUTOHIDE) ? (w - 1) : Min(placement.rcNormalPosition.left, w);
323 else if(placement.rcNormalPosition.left <= 0 && placement.rcNormalPosition.right >= w - 1)
325 if(placement.rcNormalPosition.top <= 0)
327 y = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.bottom;
331 h = (taskBarState & ABS_AUTOHIDE) ? (h - 1) : Min(placement.rcNormalPosition.top, h);
337 placement.rcNormalPosition.left != taskBarPlacement.rcNormalPosition.left ||
338 placement.rcNormalPosition.top != taskBarPlacement.rcNormalPosition.top ||
339 placement.rcNormalPosition.right != taskBarPlacement.rcNormalPosition.right ||
340 placement.rcNormalPosition.bottom != taskBarPlacement.rcNormalPosition.bottom ||
341 newTaskBarState != taskBarState)
343 taskBarState = newTaskBarState;
344 guiApp.desktop.size.w = 0;
349 taskBarPlacement = placement;
350 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
353 lastScreen = guiApp.virtualScreen;
354 lastScreenPos = guiApp.virtualScreenPos;
358 /****************************************************************************
359 /// PRIVATE UTILITY FUNCTIONS /////////////
360 ****************************************************************************/
361 // --- Keyboard Input ---
362 bool ::ProcessKeyMessage(Window window, DWORD msg, WPARAM wParam, LPARAM lParam, unichar ch)
368 bool frenchShift = (ch < 0x10000) ? (((VkKeyScan((uint16)ch) >> 8) & 6) == 6) : false;
370 if(msg == WM_CHAR || msg == WM_DEADCHAR)
375 if(msg == WM_MOUSEWHEEL)
376 code = (((short) HIWORD(wParam)) < 0) ? wheelDown : wheelUp;
379 key = (byte)((lParam & 0xFF0000)>>16);
380 if(lParam & 0x1000000)
382 key = Interface::GetExtendedKey(key);
392 if(key != leftShift && key != rightShift && ::GetKeyState(VK_SHIFT) & 0x80000)
394 if(key != leftControl && key != rightControl && ::GetKeyState(VK_CONTROL) & 0x80000 && !frenchShift)
396 if(key != leftAlt && key != rightAlt && ::GetKeyState(VK_MENU) & 0x80000 && !frenchShift)
399 if(msg == WM_MOUSEWHEEL)
401 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, 0);
406 byte ch = Interface::TranslateKeykey, code.shift);
407 if(::GetKeyState(VK_CAPITAL))
411 if(msg == WM_KEYUP || msg == WM_SYSKEYUP)
413 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
417 if(!(lParam & 0x40000000))
422 Class * keyClass = eSystem_FindClass(__ecereModule, "Key");
423 code.OnGetString(string, null, null);
424 eSystem_Logf("%s\n", string);
428 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
431 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code,ch);
438 // --- Window procedure ---
439 DWORD CALLBACK ::ApplicationWindow(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
442 Window window = (Window)GetWindowLongPtr(windowHandle, GWLP_USERDATA);
444 Window window = (Window)GetWindowLong(windowHandle, GWL_USERDATA);
446 static Point lastPos;
453 case WM_QUERYNEWPALETTE:
454 case WM_PALETTECHANGED:
456 window.display.RestorePalette();
457 UpdateWindow(windowHandle);
461 if(!fullScreenMode && !guiApp.IsModeSwitching())
465 Window modalRoot = window.FindModal();
467 HWND modalWindow = modalRoot ? modalRoot.windowHandle : null;
469 FLASHWINFO flashInfo = { 0 };
470 flashInfo.cbSize = sizeof(FLASHWINFO);
471 flashInfo.hwnd = window.windowHandle;
472 flashInfo.uCount = 0;
473 flashInfo.dwFlags = FLASHW_STOP;
474 FlashWindowEx((void *)&flashInfo);
476 if(modalWindow && modalWindow != windowHandle)
477 modalRoot.ExternalActivate(true, true, window, null);
479 window.ExternalActivate(true, true, window, null);
486 foreground = GetForegroundWindow();
487 if(foreground == windowHandle && lParam)
488 foreground = (HWND)lParam;
490 GetWindowThreadProcessId(foreground, &id);
493 windowLong = (void*)GetWindowLongPtr(foreground, GWLP_WNDPROC);
495 windowLong = (void*)GetWindowLong(foreground, GWL_WNDPROC);
497 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
498 // The != ApplicationWindow check is for not recognizing the Console window as an Ecere Window
499 // That check causes a problem with the OpenGL driver which seems to popup a window of a different class
500 if(window.displaySystem && window.displaySystem.driver == class(OpenGLDisplayDriver))
501 windowLong = (void *)ApplicationWindow;
503 if(id != GetCurrentProcessId() || windowLong != (void *)ApplicationWindow)
504 window.ExternalActivate(false, true, window, null);
505 // DefWindowProc for WM_NCACTIVATE draws the decorations, make sure it's drawn in the right state
506 return (uint)DefWindowProc(windowHandle, msg, window.active, lParam);
511 for(window = guiApp.desktop.firstChild; window; window = window.next)
512 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
513 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
517 return (uint)DefWindowProc(windowHandle, msg, window ? window.active : wParam, lParam);
520 if(!guiApp.IsModeSwitching())
524 HDC hdc = GetDC(windowHandle);
527 guiApp.SetAppFocus(false);
528 ShowWindow(windowHandle, SW_MINIMIZE);
529 ChangeDisplaySettings(null,0);
530 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
534 ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
535 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
537 window.display.RestorePalette();
538 ShowWindow(windowHandle, SW_SHOWNORMAL);
539 guiApp.SetAppFocus(true);
541 window.display.Resize(window.clientSize.w, window.clientSize.h);
544 ReleaseDC(windowHandle, hdc);
550 if(wParam && !guiApp.desktop.active /*&& lParam != GetCurrentThreadID()*/)
554 for(window = guiApp.desktop.firstChild; window; window = window.next)
555 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
556 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
559 guiApp.SetAppFocus((bool)wParam);
563 guiApp.SetAppFocus((bool)wParam);
567 if(!window.alphaBlend || window.display.pixelFormat != pixelFormat888)
572 #define ACCESS_ITEM(l, id) \
573 ((FastItem)(((id) == -1) ? null : (((byte *)((l).items)) + (id) * (l).itemSize)))
576 #define ACCESS_ITEM(l, id) \
579 BoxItem item = window.dirtyArea.count ? (BoxItem)ACCESS_ITEM(window.dirtyArea, window.dirtyArea.first) : null;
581 BeginPaint(windowHandle, &ps);
583 // Prevent flickering if we're going to update anyways
585 printf(" Paint message (%d, %d)-(%d, %d)\n",
586 item ? item.box.left : 0,
587 item ? item.box.top : 0,
588 item ? item.box.right : 0,
589 item ? item.box.bottom : 0);
591 // Causes redraw bug...
592 if(!window.manageDisplay || !item ||
595 item.box.right < window.size.w - 1 ||
596 item.box.bottom < window.size.h - 1)
598 Box box { ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-1, ps.rcPaint.bottom-1 };
599 window.UpdateDirty(box);
601 // TODO: Fix precompiler with
602 // window.UpdateDirty((Box *)&ps.rcPaint);
603 EndPaint(windowHandle, &ps);
607 case WM_DISPLAYCHANGE:
609 static int lastBits = 0;
610 static int lastRes = 0;
611 if(lastBits != wParam || lastRes != lParam)
613 lastBits = (int)wParam;
614 lastRes = (int)lParam;
616 externalDisplayChange = true;
617 if(guiApp.desktop.DisplayModeChanged())
620 if(!window.style.hidden)
621 ShowWindow(windowHandle, SW_SHOWNOACTIVATE /*SW_SHOWNORMAL*/);
622 window.FigureCaption(caption);
623 SetRootWindowCaption(window, caption);
625 externalDisplayChange = false;
640 if(msg != WM_CHAR && window.composing)
645 case WM_SYSKEYDOWN: min = max = WM_SYSCHAR; break;
646 case WM_KEYDOWN: min = max = WM_CHAR; break;
647 case WM_SYSKEYUP: min = WM_SYSCHAR; max = WM_SYSDEADCHAR; break;
648 case WM_KEYUP: min = WM_CHAR; max = WM_DEADCHAR; break;
651 // This is very annoying.
652 // PeekMessage somehow invokes this function directly... don't know why.
653 // Only crashes when running through debug mode in visual studio
655 if(msg == WM_CHAR || msg == WM_DEADCHAR || PeekMessage(&charMsg, windowHandle, min, max, PM_REMOVE))
657 ch = (msg == WM_CHAR || msg == WM_DEADCHAR) ? (unichar)wParam : (unichar)charMsg.wParam;
658 // TOCHECK: What is this for again? Fixing some obscure activation status?
659 // -- I believe this was somehow allowing 'unmaximizing', but was causing problems
660 // as there was no way to prevent AltEnter from doing so (e.g. when it is used for a node property)
661 // Worked around by fixing ProcessHotKeys to properly check for sysButtons in parent.parent when sys buttons
662 // are placed inside a menu bar for a document
664 if(msg == WM_SYSKEYDOWN && ch == 13)
666 ShowWindow(window.windowHandle, window.state == maximized ? SW_MAXIMIZE : SW_SHOWNORMAL);
667 // This last line been commented out for a long time:
668 // window.ExternalActivate(true, true, window, null);
671 if(msg == WM_SYSKEYUP || msg == WM_KEYUP)
673 if(!ProcessKeyMessage(window, WM_KEYDOWN, 0x40000000, 0, ch))
679 ProcessKeyMessage(window, msg, wParam, lParam, ch);
680 if(window.composing && msg == WM_CHAR)
682 COMPOSITIONFORM form;
683 Window caretOwner = guiApp.caretOwner;
686 HIMC ctx = ImmGetContext(windowHandle);
687 form.dwStyle = CFS_POINT;
688 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
689 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.x + caretOwner.absPosition.y - window.absPosition.y + 2;
690 window.composing = true;
691 ImmSetCompositionWindow(ctx, &form);
692 ImmReleaseContext(windowHandle, ctx);
695 if(msg != WM_MOUSEWHEEL)
699 case WM_IME_STARTCOMPOSITION:
701 COMPOSITIONFORM form;
702 Window caretOwner = guiApp.caretOwner;
705 HIMC ctx = ImmGetContext(windowHandle);
706 form.dwStyle = CFS_POINT;
707 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
708 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y + 2;
710 form.rcArea.left = window.caretPos.x;
711 form.rcArea.top = 250;
712 form.rcArea.right = 500;
713 form.rcArea.bottom = 300;
715 window.composing = true;
716 DefWindowProc(windowHandle, msg, wParam, lParam);
717 ImmSetCompositionWindow(ctx, &form);
718 ImmSetStatusWindowPos(ctx, &form.ptCurrentPos);
720 FontResource res = caretOwner.font;
721 LOGFONT font = { 0 };
723 int pixels = GetDeviceCaps(hdc, LOGPIXELSY);
725 font.lfHeight = -(int)((float)res.size * pixels / 72 + 0.5);
726 font.lfWeight = res.flags.bold ? FW_BOLD : FW_NORMAL;
727 font.lfItalic = res.flags.italic ? TRUE : FALSE,
728 font.lfUnderline = res.flags.underline ? TRUE : FALSE;
729 font.lfCharSet = DEFAULT_CHARSET;
730 font.lfOutPrecision = OUT_DEFAULT_PRECIS;
731 font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
732 font.lfQuality = DEFAULT_QUALITY;
733 font.lfPitchAndFamily = (byte)(DEFAULT_PITCH|FF_DONTCARE); // TODO: Fix compiler 0 | 0 to produce byte, not int
734 UTF8toUTF16Buffer(res.faceName, font.lfFaceName, LF_FACESIZE);
736 ImmSetCompositionFont(ctx, &font);
737 ImmReleaseContext(windowHandle, ctx);
743 case WM_IME_ENDCOMPOSITION:
744 window.composing = false;
745 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
746 /*case WM_IME_COMPOSITION:
747 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
749 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
751 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
753 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
755 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
757 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
758 case WM_IME_SETCONTEXT:
759 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
761 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
763 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
764 case WM_IME_COMPOSITIONFULL:
765 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
768 if(window.nativeDecorations)
769 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
779 case WM_LBUTTONDBLCLK:
781 case WM_RBUTTONDBLCLK:
783 case WM_MBUTTONDBLCLK:
785 x = window.absPosition.x;
786 y = window.absPosition.y;
787 /*case WM_NCLBUTTONUP:
790 case WM_NCLBUTTONDOWN:
791 case WM_NCLBUTTONDBLCLK:
792 case WM_NCRBUTTONDOWN:
793 case WM_NCRBUTTONDBLCLK:
794 case WM_NCMBUTTONDOWN:
795 case WM_NCMBUTTONDBLCLK:*/
799 bool consequential = false;
801 x += (short)LOWORD(lParam);
802 y += (short)HIWORD(lParam);
804 if(window.nativeDecorations && msg != WM_NCMOUSEMOVE)
806 x += window.clientStart.x;
807 y += window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
810 if(::GetKeyState(VK_SHIFT) & 0x80000) code.shift = true;
811 if(::GetKeyState(VK_CONTROL) & 0x80000) code.ctrl = true;
812 if(::GetKeyState(VK_MENU) & 0x80000) code.alt = true;
813 if(wParam & MK_LBUTTON) code.left = true;
814 if(wParam & MK_MBUTTON) code.middle = true;
815 if(wParam & MK_RBUTTON) code.right = true;
817 if(msg == WM_MOUSEMOVE)
819 if(lastPos.x == x && lastPos.y == y)
820 consequential = true;
830 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove, x,y,&code, consequential, false);
832 //case WM_NCLBUTTONDBLCLK:
833 case WM_LBUTTONDBLCLK:
834 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick, x,y,&code, false, true))
836 //case WM_NCLBUTTONDOWN:
838 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown,x,y,&code, false,
839 /*(msg == WM_LBUTTONDBLCLK) ? false: */true);
841 //case WM_NCLBUTTONUP:
842 case WM_LBUTTONUP: window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp, x,y,&code, false, false);break;
843 //case WM_NCMBUTTONDBLCLK:
844 case WM_MBUTTONDBLCLK:
845 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick, x,y,&code, false, false))
847 //case WM_NCMBUTTONDOWN:
849 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown, x,y,&code, false,
850 (msg == WM_LBUTTONDBLCLK) ? false: true);
852 //case WM_NCMBUTTONUP:
854 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp, x,y,&code, false, false);
856 //case WM_NCRBUTTONDBLCLK:
857 case WM_RBUTTONDBLCLK:
858 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick, x,y,&code, false, true))
860 //case WM_NCRBUTTONDOWN:
862 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown, x,y,&code, false,
863 (msg == WM_LBUTTONDBLCLK) ? false: true);
865 //case WM_NCRBUTTONUP:
867 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp, x,y,&code, false, false);
871 if(msg == WM_NCMOUSEMOVE)
872 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
877 if(window.nativeDecorations)
879 if(DefWindowProc(windowHandle, msg, wParam, lParam))
882 // SetCursor(systemCursors[0]);
883 SetCursor((lastCursor == (SystemCursor)-1) ? null : systemCursors[lastCursor]);
886 case WM_EXITMENULOOP:
887 case WM_EXITSIZEMOVE:
888 // We had some DirectInput stuff in here
890 case WM_ENTERMENULOOP:
891 case WM_ENTERSIZEMOVE:
892 // We had some DirectInput stuff in here
902 WINDOWPLACEMENT placement = { 0 };
904 placement.length = sizeof(WINDOWPLACEMENT);
905 GetWindowRect(windowHandle, &rcWindow);
906 GetWindowPlacement(windowHandle, &placement);
908 x = rcWindow.left - desktopX;
909 y = rcWindow.top - desktopY;
910 w = rcWindow.right - rcWindow.left;
911 h = rcWindow.bottom - rcWindow.top;
913 AeroSnapPosition(window, x, y, w, h);
920 RECT * rect = (RECT *)lParam;
924 window.GetDecorationsSize(&ew, &eh);
926 w = rect->right - rect->left;
927 h = rect->bottom - rect->top;
935 w = Max(w, window.minSize.w);
936 h = Max(h, window.minSize.h);
937 w = Min(w, window.maxSize.w);
938 h = Min(h, window.maxSize.h);
940 if(!window.OnResizing(&w, &h))
942 w = window.clientSize.w;
943 h = window.clientSize.h;
946 w = Max(w, window.skinMinSize.w);
947 h = Max(h, window.skinMinSize.h);
952 if(wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT)
953 rect->left = rect->right - w;
955 rect->right = rect->left + w;
957 if(wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOP || wParam == WMSZ_TOPRIGHT)
958 rect->top = rect->bottom - h;
960 rect->bottom = rect->top + h;
966 if(window.nativeDecorations)
970 GetWindowRect(windowHandle, &rcWindow);
972 if(wParam == SIZE_MAXIMIZED && window.state != maximized)
973 window.state = maximized;
974 else if(wParam == SIZE_MINIMIZED && window.state != minimized)
975 window.state = minimized;
976 else if(wParam == SIZE_RESTORED && window.state != normal && window.visible)
977 window.state = normal;
979 x = rcWindow.left - desktopX;
980 y = rcWindow.top - desktopY;
981 w = rcWindow.right - rcWindow.left;
982 h = rcWindow.bottom - rcWindow.top;
984 AeroSnapPosition(window, x, y, w, h);
985 if(!guiApp.modeSwitching)
986 window.UpdateVisual(null);
989 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
993 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
997 return (uint)DefMDIChildProc(windowHandle, msg, wParam, lParam);
998 // return DefWindowProc(windowHandle, msg, wParam, lParam);
1002 // --- DirectInput ---
1004 #ifndef ECERE_NODINPUT
1006 #ifndef ECERE_NOJOYSTICK
1007 bool CALLBACK ::JoystickCallback( const DIDEVICEINSTANCE* pdidInstance, void * context )
1009 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_Joystick,
1010 (IDirectInputDevice **)&directJoysticks[numJoysticks], null ))
1012 return DIENUM_CONTINUE;
1015 bool CALLBACK ::JoystickAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, void * context )
1018 IDirectInputDevice2 * curJoy = (IDirectInputDevice2 *)context;
1020 diprg.diph.dwSize = sizeof(DIPROPRANGE);
1021 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1022 diprg.diph.dwHow = DIPH_BYOFFSET;
1023 diprg.diph.dwObj = pdidoi->dwOfs;
1026 if(curJoy->lpVtbl->SetProperty(curJoy, DIPROP_RANGE, &diprg.diph))
1028 return DIENUM_CONTINUE;
1032 void ::TerminateDirectInput()
1037 directMouse->lpVtbl->Unacquire(directMouse);
1038 directMouse->lpVtbl->Release(directMouse);
1041 #ifndef ECERE_NOJOYSTICK
1042 for(j=0; j<numJoysticks; j++)
1044 if (directJoysticks[j])
1046 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1047 directJoysticks[j]->lpVtbl->Release(directJoysticks[j]);
1048 directJoysticks[j] = null;
1055 dInput->lpVtbl->Release(dInput);
1059 directInputCreate = null;
1063 FreeLibrary(dInputDll);
1068 bool ::InitDirectInput()
1070 bool result = false;
1072 dInputDll = LoadLibraryA("dinput.dll");
1075 if((directInputCreate = (void *)GetProcAddress(dInputDll, "DirectInputCreateA")))
1077 if(!directInputCreate( hInstance, DIRECTINPUT_VERSION, &dInput, null ))
1082 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_SysMouse, &directMouse, null ))
1083 directMouse->lpVtbl->SetDataFormat(directMouse, &c_dfDIMouse );
1085 #ifndef ECERE_NOJOYSTICK
1087 dInput->lpVtbl->EnumDevices(dInput, DIDEVTYPE_JOYSTICK, JoystickCallback, null, DIEDFL_ATTACHEDONLY );
1088 for(j=0; j<NUMJOY; j++)
1089 if(directJoysticks[j])
1090 if(!directJoysticks[j]->lpVtbl->SetDataFormat(directJoysticks[j], &c_dfDIJoystick ))
1091 directJoysticks[j]->lpVtbl->EnumObjects(directJoysticks[j], JoystickAxesCallback, directJoysticks[j], DIDFT_AXIS );
1100 void ::AcquireDirectInput(HWND windowHandle, bool state)
1104 if((state && !acquiredWindow) || (!state && acquiredWindow == windowHandle))
1111 directMouse->lpVtbl->SetCooperativeLevel(directMouse, /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1112 directMouse->lpVtbl->Acquire(directMouse);
1115 directMouse->lpVtbl->Unacquire(directMouse);
1117 #ifndef ECERE_NOJOYSTICK
1118 for(j = 0; j<NUMJOY; j++)
1119 if(directJoysticks[j])
1123 directJoysticks[j]->lpVtbl->SetCooperativeLevel(directJoysticks[j], /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1124 directJoysticks[j]->lpVtbl->Acquire(directJoysticks[j]);
1127 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1130 acquiredWindow = state ? windowHandle : null;
1135 /****************************************************************************
1136 /// INTEGRATION FUNCTIONALITY /////////////
1137 ****************************************************************************/
1138 bool eisWIN32ProcessKey(Window window, DWORD msg, DWORD wParam, DWORD lParam, byte ch)
1140 return ProcessKeyMessage(window, msg, wParam, lParam, ch);
1143 HWND eisWIN32GetWindowHandle(Window window)
1145 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;
1148 /****************************************************************************
1149 /// DRIVER IMPLEMENTATION /////////////
1150 ****************************************************************************/
1153 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, uint64 dwUser, uint64 dw1, uint64 dw2)
1155 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1158 guiApp.SignalEvent();
1161 // --- User Interface System ---
1166 CS_DBLCLKS, ApplicationWindow, 0L, 0L, 0,
1167 LoadIcon(null, IDI_APPLICATION),
1174 wcl.hInstance = hInstance = GetModuleHandle(null);
1175 RegisterClass(&wcl);
1177 // Move that in reposition desktop
1178 startBar = FindWindowA("Shell_TrayWnd", null);
1180 systemCursors[arrow] = LoadCursor(null, IDC_ARROW);
1181 systemCursors[iBeam] = LoadCursor(null, IDC_IBEAM);
1182 systemCursors[cross] = LoadCursor(null, IDC_CROSS);
1183 systemCursors[moving] = LoadCursor(null, IDC_SIZEALL);
1184 systemCursors[sizeNESW] = LoadCursor(null, IDC_SIZENESW);
1185 systemCursors[sizeNS ] = LoadCursor(null, IDC_SIZENS);
1186 systemCursors[sizeNWSE] = LoadCursor(null, IDC_SIZENWSE);
1187 systemCursors[sizeWE ] = LoadCursor(null, IDC_SIZEWE);
1188 systemCursors[hand ] = LoadCursor(null, IDC_HAND);
1190 SetTimer(null, 0, (DWORD)(1000.0 / 18.2), null);
1191 //SetTimer(null, 0, 1, null);
1195 hiResTimer = timeSetEvent(
1198 TimerProc,0,TIME_PERIODIC);
1201 hiResTimer = timeSetEvent(
1204 TimerProc,0,TIME_PERIODIC);
1208 topWindow = CreateWindowEx(0, className, "",WS_POPUP,0,0,1,1,HWND_DESKTOP,
1209 null, hInstance, null);
1216 if(hiResTimer) timeKillEvent(hiResTimer);
1217 #ifndef ECERE_NODINPUT
1218 TerminateDirectInput();
1222 void SetTimerResolution(uint hertz)
1224 if(hiResTimer) timeKillEvent(hiResTimer);
1226 hiResTimer = timeSetEvent(1000 / hertz, 1000 / hertz, TimerProc, 0, TIME_PERIODIC);
1229 bool ProcessInput(bool processAll)
1233 RepositionDesktop(true);
1234 if(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
1236 while(PeekMessage(&msg,0,0,0,PM_REMOVE))
1238 TranslateMessage(&msg);
1239 DispatchMessage(&msg);
1240 // printf("%d\n", msg.message);
1241 if(!processAll || msg.message == WM_TIMER) break;
1250 MsgWaitForMultipleObjects(1, (void **)guiApp.semaphore, FALSE, (uint)(1000 / 18.2) /*INFINITE*/, QS_ALLINPUT);
1253 void Lock(Window window)
1258 void Unlock(Window window)
1263 char ** GraphicsDrivers(int * numDrivers)
1265 static char *graphicsDrivers[] = { "GDI", "DirectDraw", "OpenGL", "Direct3D", "Direct3D8", "Direct3D9" };
1266 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1267 return (char **)graphicsDrivers;
1270 void GetCurrentMode(bool * fullScreen, Resolution * resolution, PixelFormat * colorDepth, int * refreshRate)
1272 *fullScreen = fullScreenMode;
1276 for(c = 0; c<Resolution::enumSize; c++)
1277 if(GetResolutionWidth(c) == devMode.dmPelsWidth && GetResolutionHeight(c) == devMode.dmPelsHeight)
1282 switch(devMode.dmBitsPerPel)
1284 case 8: *colorDepth = pixelFormat8; break;
1285 case 16: *colorDepth = pixelFormat555; break;
1286 default: *colorDepth = pixelFormat888; break;
1288 * refreshRate = devMode.dmDisplayFrequency;
1292 void EnsureFullScreen(bool *fullScreen)
1297 bool ScreenMode(bool fullScreen, Resolution resolution, PixelFormat colorDepth, int refreshRate, bool * textMode)
1302 fullScreenMode = fullScreen;
1306 FillBytes(&devMode, 0, sizeof(DEVMODE));
1307 devMode.dmSize = (uint16)sizeof(devMode);
1308 devMode.dmFields |=DM_BITSPERPEL;
1309 devMode.dmFields |=DM_PELSWIDTH|DM_PELSHEIGHT;
1310 devMode.dmFields |= DM_DISPLAYFREQUENCY;
1311 devMode.dmBitsPerPel = colorDepth ? GetDepthBits(colorDepth) : GetDeviceCaps(hdc, BITSPIXEL);
1312 devMode.dmPelsWidth = resolution ? GetResolutionWidth(resolution) : GetSystemMetrics(SM_CXSCREEN);
1313 devMode.dmPelsHeight = resolution ? GetResolutionHeight(resolution) : GetSystemMetrics(SM_CYSCREEN);
1314 devMode.dmDisplayFrequency = refreshRate ? refreshRate : GetDeviceCaps(hdc, VREFRESH);
1315 if(ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1319 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
1320 guiApp.SetDesktopPosition(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), false);
1325 static bool firstTime = true;
1327 ChangeDisplaySettings(null, 0);
1329 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
1330 desktopX = desktopY = desktopW = desktopH = 0;
1332 RepositionDesktop(false);
1339 // --- Window Creation ---
1340 void * CreateRootWindow(Window window)
1343 uint16 * text = UTF8toUTF16(window.text, null);
1346 windowHandle = CreateWindowEx(0, className, text,
1348 0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
1350 null, hInstance, null);
1351 ShowWindow(windowHandle, SW_SHOWNORMAL);
1353 else if(window.systemParent)
1354 windowHandle = CreateWindowEx(0, className, text,
1355 WS_CHILD,0,0,1,1, window.systemParent, null, hInstance, null);
1360 HWND parentWindow = null; //HWND_DESKTOP; // we get different behaviors with desktop...
1361 Window master = window.master, rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1362 if(window.style.stayOnTop)
1363 exStyle |= WS_EX_TOPMOST;
1365 if(rootWindow && (window._isModal || window.style.interim))
1366 parentWindow = rootWindow.is3D ? rootWindow.parent.windowHandle : rootWindow.windowHandle;
1368 if(window.alphaBlend)
1369 // if(window.background.a < 255) //&& window.style & ES_REDRAW) Not needed anymore?
1370 exStyle |= WS_EX_LAYERED; // | WS_EX_TRANSPARENT;
1372 // Toolwindow will disappear if they don't have AppWindow set
1373 if(window.style.showInTaskBar || (!parentWindow && window.style.thin))
1375 exStyle |= WS_EX_APPWINDOW;
1376 parentWindow = null;
1379 if(window.style.thin)
1380 exStyle |= WS_EX_TOOLWINDOW;
1382 if(window.windowHandle)
1383 windowHandle = window.windowHandle;
1386 if(window.nativeDecorations)
1388 BorderBits borderStyle = window.borderStyle; // FIXME!
1389 style = WS_OVERLAPPED;
1390 if(borderStyle.fixed)
1391 style |= WS_CAPTION;
1393 style |= WS_SYSMENU;
1394 if(borderStyle.sizable)
1395 style |= WS_THICKFRAME;
1396 if(window.hasMinimize)
1397 style |= WS_MINIMIZEBOX;
1398 if(window.hasMaximize)
1399 style |= WS_MAXIMIZEBOX;
1401 windowHandle = CreateWindowEx(
1404 style | (window.systemParent ? WS_CHILD :
1405 (WS_POPUP | (window.style.hasMinimize ? WS_MINIMIZEBOX : 0))),
1406 0,0,1,1, parentWindow, null, hInstance, null);
1408 if(exStyle & WS_EX_LAYERED)
1409 SetLayeredWindowAttributes(windowHandle, 0, 255 /*A(window.background)*/, LWA_ALPHA);
1415 SetWindowLongPtr(windowHandle, GWLP_USERDATA, (int64)window);
1417 SetWindowLong(windowHandle, GWL_USERDATA, (DWORD)window);
1420 return windowHandle;
1423 void DestroyRootWindow(Window window)
1426 int c, lockCount = guiApp.lockMutex.lockCount;
1427 for(c = 0; c < lockCount; c++)
1428 guiApp.lockMutex.Release();
1430 oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1432 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1434 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1436 DestroyIcon(oldIcon);
1438 ShowWindow(window.windowHandle, SW_HIDE);
1441 SetWindowLongPtr(window.windowHandle, GWLP_USERDATA, (int64)null);
1443 SetWindowLong(window.windowHandle, GWL_USERDATA, 0);
1445 DestroyWindow(window.windowHandle);
1447 for(c = 0; c < lockCount; c++)
1448 guiApp.lockMutex.Wait();
1450 window.windowHandle = null;
1453 // -- Window manipulation ---
1455 void SetRootWindowCaption(Window window, char * name)
1457 uint16 * text = UTF8toUTF16(name, null);
1459 SetWindowText(window.windowHandle, text);
1464 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
1466 int flags = SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS;
1468 if(!window.systemParent && !fullScreenMode)
1474 if(!move) flags |= SWP_NOMOVE;
1475 if(!resize) flags |= SWP_NOSIZE;
1477 /*if(move && resize)
1479 flags |= SWP_NOMOVE;
1480 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1481 flags &=~SWP_NOMOVE;
1482 flags |= SWP_NOSIZE;
1484 if(!window.nativeDecorations || window.state != maximized || !window.visible || guiApp.modeSwitching)
1485 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1488 void OrderRootWindow(Window window, bool topMost)
1490 SetWindowPos(window.windowHandle, topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0,
1491 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
1494 void SetRootWindowColor(Window window)
1496 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1497 if(window.alphaBlend && window.display.pixelFormat == pixelFormat888)
1499 /*if(A(window.background) == 255)
1501 if((style & WS_EX_LAYERED) == WS_EX_LAYERED)
1502 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style &~WS_EX_LAYERED);
1506 #ifndef ECERE_NOBLENDING
1507 if((style & WS_EX_LAYERED) != WS_EX_LAYERED)
1508 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style | WS_EX_LAYERED);
1510 // SetLayeredWindowAttributes(window.windowHandle, 0, 255 /*Max(A(window.background),1)*/, LWA_ALPHA);
1515 void OffsetWindow(Window window, int * x, int * y)
1517 if(window.systemParent)
1519 POINT point = {*x,*y};
1520 ClientToScreen(GetParent(window.windowHandle), &point);
1526 void UpdateRootWindow(Window window)
1528 UpdateWindow(window.windowHandle);
1531 void SetRootWindowState(Window window, WindowState state, bool visible)
1539 ShowWindow(window.windowHandle, ((window.active || window.creationActivation == activate) && !externalDisplayChange) ?
1540 ((window.nativeDecorations && state == maximized) ? SW_MAXIMIZE : SW_SHOWNORMAL) : SW_SHOWNOACTIVATE);
1543 ShowWindow(window.windowHandle, SW_MINIMIZE);
1549 ShowWindow(window.windowHandle, SW_HIDE);
1553 void ActivateRootWindow(Window window)
1555 if(!externalDisplayChange)
1556 SetForegroundWindow(window.windowHandle);
1559 void FlashRootWindow(Window window)
1561 FLASHWINFO flashInfo = { 0 };
1562 flashInfo.cbSize = sizeof(FLASHWINFO);
1563 flashInfo.hwnd = window.windowHandle;
1564 flashInfo.uCount = 1;
1565 flashInfo.dwFlags = FLASHW_TRAY; // FLASHW_ALL;
1567 FlashWindowEx((void *)&flashInfo);
1571 // --- Mouse-based window movement ---
1572 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
1574 if(!fullScreenMode && !window.systemParent)
1575 // Commented out for Chess game because AI thread takes over the moving of main window... Still required?
1576 //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
1577 ; //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
1580 SetWindowPos(window.windowHandle, HWND_TOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1581 mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
1582 SetWindowPos(window.windowHandle, HWND_NOTOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1586 void StopMoving(Window window)
1588 if(!fullScreenMode && !window.systemParent)
1589 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
1592 // -- Mouse manipulation ---
1594 void GetMousePosition(int *x, int *y)
1597 GetCursorPos(&point);
1602 void SetMousePosition(int x, int y)
1607 void SetMouseRange(Window window, Box box)
1609 ClipCursor((RECT *) box);
1612 void SetMouseCapture(Window window)
1615 SetCapture(window.windowHandle);
1620 // -- Mouse cursor ---
1622 void SetMouseCursor(SystemCursor cursor)
1624 if(lastCursor != cursor)
1626 lastCursor = cursor;
1627 SetCursor((cursor == (SystemCursor)-1) ? null : systemCursors[cursor]);
1633 void SetCaret(int x, int y, int size)
1638 // --- Clipboard manipulation ---
1640 void ClearClipboard()
1642 if(OpenClipboard(null))
1649 bool AllocateClipboard(ClipBoard clipBoard, uint size)
1651 bool result = false;
1652 clipBoard.text = new byte[size];
1657 bool SaveClipboard(ClipBoard clipBoard)
1659 bool result = false;
1663 uint16 * u16text = UTF8toUTF16(clipBoard.text, &wordCount);
1665 clipBoard.handle = GlobalAlloc(GHND | GMEM_DDESHARE, wordCount * 2);
1666 if(clipBoard.handle)
1668 uint16 * text = GlobalLock(clipBoard.handle);
1671 memcpy(text, u16text, wordCount * 2);
1672 GlobalUnlock(clipBoard.handle);
1673 if(OpenClipboard(null))
1676 if(SetClipboardData(CF_UNICODETEXT, clipBoard.handle))
1684 GlobalFree(clipBoard.handle);
1691 bool LoadClipboard(ClipBoard clipBoard)
1693 bool result = false;
1694 if(OpenClipboard(null))
1696 if((clipBoard.handle = GetClipboardData(CF_UNICODETEXT)))
1698 uint16 * u16text = GlobalLock(clipBoard.handle);
1701 clipBoard.text = UTF16toUTF8(u16text);
1703 GlobalUnlock(clipBoard.handle);
1711 void UnloadClipboard(ClipBoard clipBoard)
1713 delete clipBoard.text;
1716 // --- State based input ---
1718 bool AcquireInput(Window window, bool state)
1720 #ifndef ECERE_NODINPUT
1721 if(dInput || InitDirectInput())
1723 AcquireDirectInput(window.windowHandle, state);
1730 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
1732 bool result = false;
1733 #ifndef ECERE_NODINPUT
1734 DIMOUSESTATE dims = {0};
1736 if(acquiredWindow && directMouse)
1738 if(directMouse->lpVtbl->GetDeviceState(directMouse, sizeof(DIMOUSESTATE), &dims ))
1740 FillBytes(&dims, 0, sizeof(dims));
1741 directMouse->lpVtbl->Acquire(directMouse);
1745 /* We don't really want this...
1748 if(GetAsyncKeyState(VK_LBUTTON))
1749 dims.rgbButtons[0] |= 0x80;
1750 if(GetAsyncKeyState(VK_RBUTTON))
1751 dims.rgbButtons[1] |= 0x80;
1752 if(GetAsyncKeyState(VK_MBUTTON))
1753 dims.rgbButtons[2] |= 0x80;
1761 *buttons = MouseButtons {
1762 left = (dims.rgbButtons[0] & 0x80) ? true : false,
1763 right = (dims.rgbButtons[1] & 0x80) ? true : false,
1764 middle = (dims.rgbButtons[2] & 0x80) ? true : false };
1771 bool GetJoystickState(int device, Joystick joystick)
1773 bool result = false;
1774 #ifndef ECERE_NODINPUT
1775 if(joystick != null)
1777 DIJOYSTATE dijs = {0};
1778 #ifndef ECERE_NOJOYSTICK
1779 if(acquiredWindow && device < numJoysticks)
1781 if(directJoysticks[device])
1783 directJoysticks[device]->lpVtbl->Poll(directJoysticks[device]);
1784 if(directJoysticks[device]->lpVtbl->GetDeviceState(directJoysticks[device], sizeof(DIJOYSTATE), &dijs ))
1785 directJoysticks[device]->lpVtbl->Acquire(directJoysticks[device]);
1790 joystick.x = dijs.lX;
1791 joystick.y = dijs.lY;
1792 joystick.z = dijs.lZ;
1793 joystick.rx = dijs.lRx;
1794 joystick.ry = dijs.lRy;
1795 joystick.rz = dijs.lRz;
1797 ((dijs.rgbButtons[0] & 0x80) ? JOY_BUTTON1 : 0)
1798 | ((dijs.rgbButtons[1] & 0x80) ? JOY_BUTTON2 : 0)
1799 | ((dijs.rgbButtons[2] & 0x80) ? JOY_BUTTON3 : 0)
1800 | ((dijs.rgbButtons[3] & 0x80) ? JOY_BUTTON4 : 0);
1806 bool GetKeyState(Key key)
1808 bool keyState = false;
1812 keyState = GetAsyncKeyState(key2VK[key]);
1813 keyState = (keyState & 0x80000) ? true : false;
1815 else if(key == capsState)
1816 keyState = ::GetKeyState(VK_CAPITAL) & 0x00000001;
1817 else if(key == numState)
1818 keyState = ::GetKeyState(VK_NUMLOCK) & 0x00000001;
1819 else if(key == scrollState)
1820 keyState = ::GetKeyState(VK_SCROLL) & 0x00000001;
1824 bool SetIcon(Window window, BitmapResource resource)
1827 HICON oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1829 // Dialogs Inherit master's icon if none set
1830 if(!window.style.showInTaskBar && window.hasClose)
1832 Window master = window.master;
1833 while(master && !resource)
1835 Window rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1836 if(rootWindow && rootWindow.icon)
1837 resource = rootWindow.icon;
1839 master = master.master;
1843 // WARNING -- putting this here as it is right after CreateRootWindow
1844 // Take out Layered flag if we're not in 24 bit
1846 if(window.alphaBlend && window.display.pixelFormat != pixelFormat888)
1848 #ifndef ECERE_NOBLENDING
1849 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1850 style &= ~WS_EX_LAYERED;
1851 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style);
1857 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1859 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1862 DestroyIcon(oldIcon);
1868 if(bitmap.Load(resource.fileName, null, null))
1871 PixelFormat format = window.display.pixelFormat;
1872 int bits = GetDepthBits(format);
1875 bitmap.Convert(null, pixelFormat888, null);
1876 and.Allocate(null, (bitmap.width+7/8), bitmap.height, 0, pixelFormat8, false);
1878 blend = bits == 32 || bitmap.pixelFormat != pixelFormat888;
1881 byte * picture = and.picture;
1884 uint size = bitmap.height * bitmap.width;
1889 while(m < 8 && c < size)
1892 mask |= blend ? (!((ColorAlpha *)bitmap.picture)[c].a) : (((ColorAlpha *)bitmap.picture)[c].a <= 192);
1896 picture[b++] = mask;
1901 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1902 if(blend ? (!color.a) : (color.a <= 192))
1904 color.color = { 0, 0, 0 };
1905 ((ColorAlpha *)bitmap.picture)[c] = color;
1910 if(bits == 15) { bits = 16; format = pixelFormat565; };
1911 bitmap.Convert(null, format, null);
1913 icon = CreateIcon(hInstance, bitmap.width, bitmap.height, 1, (byte)bits, and.picture, bitmap.picture);
1918 SendMessage(window.windowHandle, WM_SETICON, ICON_BIG, (LPARAM)icon);
1919 SendMessage(window.windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)icon);
1923 void ::GetScreenArea(Window window, Box box)
1925 HMONITOR monitor = MonitorFromWindow(window.windowHandle, MONITOR_DEFAULTTONEAREST);
1926 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
1929 MONITORINFO info = { 0 };
1930 info.cbSize = sizeof(MONITORINFO);
1931 GetMonitorInfo(monitor, &info);
1932 // box = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
1933 box = { info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.right-1, info.rcMonitor.bottom-1 };
1935 if(taskBarMonitor == monitor)
1937 if(taskBarPlacement.rcNormalPosition.top <= box.top && taskBarPlacement.rcNormalPosition.bottom >= box.bottom)
1939 if(taskBarPlacement.rcNormalPosition.left <= box.left)
1941 if(taskBarState & ABS_AUTOHIDE)
1944 box.left = taskBarPlacement.rcNormalPosition.right;
1946 else if(taskBarState & ABS_AUTOHIDE)
1949 box.right = taskBarPlacement.rcNormalPosition.left;
1951 else if(taskBarPlacement.rcNormalPosition.left <= box.left && taskBarPlacement.rcNormalPosition.right >= box.right)
1953 if(taskBarPlacement.rcNormalPosition.top <= box.top)
1955 if(taskBarState & ABS_AUTOHIDE)
1958 box.top = taskBarPlacement.rcNormalPosition.bottom;
1960 else if(taskBarState & ABS_AUTOHIDE)
1963 box.bottom = taskBarPlacement.rcNormalPosition.top;
1967 box.left -= desktopX;
1968 box.top -= desktopY;
1969 box.right -= desktopX;
1970 box.bottom -= desktopY;