1 namespace gui::drivers;
12 #define _WIN32_WINNT 0x0502
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 const uint16 className[] = L"Ecere Application";
113 static HINSTANCE hInstance;
115 static WPARAM lastBits;
116 static LPARAM lastRes;
118 static DEVMODE devMode;
119 #ifndef ECERE_NODINPUT
120 static HWND acquiredWindow = null;
122 static HCURSOR systemCursors[SystemCursor];
123 static bool fullScreenMode;
124 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
125 static DWORD hiResTimer;
126 // static HWND topWindow;
128 static HWND startBar;
130 #ifndef ECERE_NODINPUT
132 static HMODULE dInputDll = null;
133 static BOOL * (WINAPI * directInputCreate)(HINSTANCE, DWORD, IDirectInput **, void *);
134 static IDirectInput * dInput = null;
135 static IDirectInputDevice * directMouse = null;
136 #ifndef ECERE_NOJOYSTICK
137 static int numJoysticks = 0;
138 static IDirectInputDevice2 * directJoysticks[NUMJOY] = {null,null,null,null};
143 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
144 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
145 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
146 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
147 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
148 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
149 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
150 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
151 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
152 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
153 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
154 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
155 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
156 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
160 static Box lastMonitorAreas[32];
161 static Box monitorAreas[32];
164 static bool EnumerateMonitors(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
166 MONITORINFO info = { 0 };
167 info.cbSize = sizeof(MONITORINFO);
168 GetMonitorInfo(hMonitor, &info);
169 monitorAreas[monitor] = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
174 static bool externalDisplayChange;
176 static int taskBarState;
177 static WINDOWPLACEMENT taskBarPlacement;
178 static bool activateApp;
180 static SystemCursor lastCursor = (SystemCursor)-1;
182 static void SmartShowWindow(HWND windowHandle, WindowState state, bool doActivate)
185 if(state == maximized)
186 showCmd = doActivate ? SW_SHOWMAXIMIZED : SW_MAXIMIZE;
187 else if(state == minimized)
188 showCmd = SW_MINIMIZE;
190 showCmd = doActivate ? SW_SHOWNORMAL : SW_SHOWNOACTIVATE;
191 ShowWindow(windowHandle, showCmd);
194 void AeroSnapPosition(Window window, int x, int y, int w, int h)
196 int oldX = window.absPosition.x;
198 // To prevent having a window < minClientSize with Aero Snap, because we don't receive a WM_SIZING!
199 // The sensible implementation of this is in WM_SIZING
203 window.GetDecorationsSize(&ew, &eh);
211 gw = Max(gw, window.minSize.w);
212 gh = Max(gh, window.minSize.h);
213 gw = Min(gw, window.maxSize.w);
214 gh = Min(gh, window.maxSize.h);
216 if(!window.OnResizing(&gw, &gh))
218 gw = window.clientSize.w;
219 gh = window.clientSize.h;
222 gw = Max(gw, window.skinMinSize.w);
223 gh = Max(gh, window.skinMinSize.h);
228 if(w != gw || h != gh)
231 // Adjust x position if we resized from top or bottom left corner
239 if(window.state == minimized && !window.nativeDecorations && !window.hasMinimize)
241 guiApp.interfaceDriver.PositionRootWindow(window, x, y, w, h, move, true);
244 window.ExternalPosition(x, y, w, h);
247 void Win32UpdateStyle(Window window)
249 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
250 if(window.showInTaskBar)
251 style |= WS_EX_APPWINDOW;
253 style &= ~WS_EX_APPWINDOW;
255 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style);
258 class Win32Interface : Interface
260 class_property(name) = "Win32";
262 void ::RepositionDesktop(bool updateChildren)
265 static double lastTime = 0, time;
268 //static Size lastScreen;
269 //static Point lastScreenPos;
270 static double lastAutoHideCheck = 0;
271 int newTaskBarState = taskBarState;
272 HMONITOR primaryMonitor = 0;
275 if(time - lastTime < 0.1) return;
278 // Every sec, check for the auto hide property
279 if(time - lastAutoHideCheck > 1)
281 APPBARDATA appBarData = { 0 };
282 newTaskBarState = (int)SHAppBarMessage(ABM_GETSTATE, &appBarData);
283 lastAutoHideCheck = time;
287 EnumDisplayMonitors(null, null, EnumerateMonitors, 0);
288 for(c = 0; c<monitor; c++)
290 if(monitorAreas[c].left != lastMonitorAreas[c].left ||
291 monitorAreas[c].top != lastMonitorAreas[c].top ||
292 monitorAreas[c].right != lastMonitorAreas[c].right ||
293 monitorAreas[c].bottom != lastMonitorAreas[c].bottom)
299 memcpy(lastMonitorAreas, monitorAreas, sizeof(monitorAreas));
301 guiApp.virtualScreen =
303 GetSystemMetrics(SM_CXVIRTUALSCREEN),
304 GetSystemMetrics(SM_CYVIRTUALSCREEN)
306 guiApp.virtualScreenPos =
308 x = GetSystemMetrics(SM_XVIRTUALSCREEN),
309 y = GetSystemMetrics(SM_YVIRTUALSCREEN)
313 HMONITOR monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
316 MONITORINFO info = { 0 };
317 info.cbSize = sizeof(MONITORINFO);
318 primaryMonitor = monitor;
319 GetMonitorInfo(monitor, &info);
320 x = info.rcMonitor.left;
321 y = info.rcMonitor.top;
322 w = info.rcMonitor.right - info.rcWork.left;
323 h = info.rcMonitor.bottom - info.rcWork.top;
328 WINDOWPLACEMENT placement = { 0 };
329 placement.length = sizeof(WINDOWPLACEMENT);
331 if(!GetWindowPlacement(startBar, &placement))
333 startBar = FindWindowA("Shell_TrayWnd", null);
334 GetWindowPlacement(startBar, &placement);
337 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
338 if(primaryMonitor == taskBarMonitor)
340 if(placement.rcNormalPosition.top <= 0 && placement.rcNormalPosition.bottom >= h - 1)
342 if(placement.rcNormalPosition.left <= 0)
344 x = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.right;
348 w = (taskBarState & ABS_AUTOHIDE) ? (w - 1) : Min(placement.rcNormalPosition.left, w);
350 else if(placement.rcNormalPosition.left <= 0 && placement.rcNormalPosition.right >= w - 1)
352 if(placement.rcNormalPosition.top <= 0)
354 y = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.bottom;
358 h = (taskBarState & ABS_AUTOHIDE) ? (h - 1) : Min(placement.rcNormalPosition.top, h);
364 placement.rcNormalPosition.left != taskBarPlacement.rcNormalPosition.left ||
365 placement.rcNormalPosition.top != taskBarPlacement.rcNormalPosition.top ||
366 placement.rcNormalPosition.right != taskBarPlacement.rcNormalPosition.right ||
367 placement.rcNormalPosition.bottom != taskBarPlacement.rcNormalPosition.bottom ||
368 newTaskBarState != taskBarState)
370 taskBarState = newTaskBarState;
371 guiApp.desktop.size.w = 0;
376 taskBarPlacement = placement;
377 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
380 //lastScreen = guiApp.virtualScreen;
381 //lastScreenPos = guiApp.virtualScreenPos;
385 /****************************************************************************
386 /// PRIVATE UTILITY FUNCTIONS /////////////
387 ****************************************************************************/
388 // --- Keyboard Input ---
389 bool ::ProcessKeyMessage(Window window, DWORD msg, WPARAM wParam, LPARAM lParam, unichar ch)
395 bool frenchShift = (ch < 0x10000) ? (((VkKeyScan((uint16)ch) >> 8) & 6) == 6) : false;
397 if(msg == WM_CHAR || msg == WM_DEADCHAR)
402 if(msg == WM_MOUSEWHEEL)
403 code = (((short) HIWORD(wParam)) < 0) ? wheelDown : wheelUp;
406 key = (byte)((lParam & 0xFF0000)>>16);
407 if(lParam & 0x1000000)
409 key = Interface::GetExtendedKey(key);
419 if(msg == WM_MOUSEWHEEL)
421 if(::GetAsyncKeyState(VK_SHIFT) & 0x80000)
423 if(::GetAsyncKeyState(VK_CONTROL) & 0x80000)
425 if(::GetAsyncKeyState(VK_MENU) & 0x80000)
428 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, 0);
432 if(key != leftShift && key != rightShift && ::GetKeyState(VK_SHIFT) & 0x80000)
434 if(key != leftControl && key != rightControl && ::GetKeyState(VK_CONTROL) & 0x80000 && !frenchShift)
436 if(key != leftAlt && key != rightAlt && ::GetKeyState(VK_MENU) & 0x80000 && !frenchShift)
439 byte ch = Interface::TranslateKeykey, code.shift);
440 if(::GetKeyState(VK_CAPITAL))
444 if(msg == WM_KEYUP || msg == WM_SYSKEYUP)
446 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
450 if(!(lParam & 0x40000000))
455 Class * keyClass = eSystem_FindClass(__ecereModule, "Key");
456 code.OnGetString(string, null, null);
457 eSystem_Logf("%s\n", string);
461 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
464 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code,ch);
471 // --- Window procedure ---
472 DWORD CALLBACK ::ApplicationWindow(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
475 Window window = (Window)GetWindowLongPtr(windowHandle, GWLP_USERDATA);
477 Window window = (Window)GetWindowLong(windowHandle, GWL_USERDATA);
479 static Point lastPos;
486 case WM_QUERYNEWPALETTE:
487 case WM_PALETTECHANGED:
489 window.display.RestorePalette();
490 UpdateWindow(windowHandle);
494 if(!fullScreenMode && !guiApp.IsModeSwitching())
498 Window modalRoot = window.FindModal();
500 HWND modalWindow = modalRoot ? modalRoot.windowHandle : null;
502 if(window.creationActivation == flash || window.hasMinimize || window.borderStyle != sizableThin)
504 FLASHWINFO flashInfo = { 0 };
505 flashInfo.cbSize = sizeof(FLASHWINFO);
506 flashInfo.hwnd = window.windowHandle;
507 flashInfo.uCount = 0;
508 flashInfo.dwFlags = FLASHW_STOP;
509 FlashWindowEx((void *)&flashInfo);
512 if(modalWindow && modalWindow != windowHandle)
513 modalRoot.ExternalActivate(true, true, window, null);
515 window.ExternalActivate(true, true, window, null);
522 foreground = GetForegroundWindow();
523 if(foreground == windowHandle && lParam)
524 foreground = (HWND)lParam;
526 GetWindowThreadProcessId(foreground, &id);
529 windowLong = (void*)GetWindowLongPtr(foreground, GWLP_WNDPROC);
531 windowLong = (void*)GetWindowLong(foreground, GWL_WNDPROC);
533 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
534 // The != ApplicationWindow check is for not recognizing the Console window as an Ecere Window
535 // That check causes a problem with the OpenGL driver which seems to popup a window of a different class
536 if(window.displaySystem && window.displaySystem.driver == class(OpenGLDisplayDriver))
537 windowLong = (void *)ApplicationWindow;
539 if(id != GetCurrentProcessId() || windowLong != (void *)ApplicationWindow)
540 window.ExternalActivate(false, true, window, null);
541 // DefWindowProc for WM_NCACTIVATE draws the decorations, make sure it's drawn in the right state
542 return (uint)DefWindowProc(windowHandle, msg, window.active, lParam);
547 for(window = guiApp.desktop.firstChild; window; window = window.next)
548 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
549 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
553 return (uint)DefWindowProc(windowHandle, msg, window ? window.active : wParam, lParam);
556 if(!guiApp.IsModeSwitching())
560 HDC hdc = GetDC(windowHandle);
563 guiApp.SetAppFocus(false);
564 ShowWindow(windowHandle, SW_MINIMIZE);
565 ChangeDisplaySettings(null,0);
566 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
570 ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
571 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
573 window.display.RestorePalette();
574 ShowWindow(windowHandle, SW_SHOWNORMAL);
575 guiApp.SetAppFocus(true);
577 window.display.Resize(window.clientSize.w, window.clientSize.h);
580 ReleaseDC(windowHandle, hdc);
586 if(wParam && !guiApp.desktop.active /*&& lParam != GetCurrentThreadID()*/)
590 for(window = guiApp.desktop.firstChild; window; window = window.next)
591 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
592 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
595 guiApp.SetAppFocus((bool)wParam);
599 guiApp.SetAppFocus((bool)wParam);
604 if(!window.alphaBlend || window.display.pixelFormat != pixelFormat888)
608 #define ACCESS_ITEM(l, id) \
609 ((FastItem)(((id) == -1) ? null : (((byte *)((l).items)) + (id) * (l).itemSize)))
612 #define ACCESS_ITEM(l, id) \
615 BoxItem item = window.dirtyArea.count ? (BoxItem)ACCESS_ITEM(window.dirtyArea, window.dirtyArea.first) : null;
617 BeginPaint(windowHandle, &ps);
619 // Prevent flickering if we're going to update anyways
621 printf(" Paint message (%d, %d)-(%d, %d)\n",
622 item ? item.box.left : 0,
623 item ? item.box.top : 0,
624 item ? item.box.right : 0,
625 item ? item.box.bottom : 0);
627 // Causes redraw bug...
628 if(!window.manageDisplay || !item ||
631 item.box.right < window.size.w - 1 ||
632 item.box.bottom < window.size.h - 1)
634 Box box { ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-1, ps.rcPaint.bottom-1 };
635 window.UpdateDirty(box);
637 // TODO: Fix precompiler with
638 // window.UpdateDirty((Box *)&ps.rcPaint);
639 EndPaint(windowHandle, &ps);
643 BeginPaint(windowHandle, &ps);
644 EndPaint(windowHandle, &ps);
648 case WM_DISPLAYCHANGE:
650 if(!guiApp.fullScreenMode && (lastBits != wParam || lastRes != lParam))
653 HWND foregroundWindow = GetForegroundWindow();
654 int w = LOWORD(lParam);
655 int h = HIWORD(lParam);
657 GetWindowRect(foregroundWindow, &rect);
658 if(rect.right == w && rect.bottom == h)
664 externalDisplayChange = true;
665 if(guiApp.desktop.DisplayModeChanged())
668 if(!window.style.hidden)
669 SmartShowWindow(window.windowHandle, (window.nativeDecorations && window.state == maximized) ? maximized : normal, false);
670 window.FigureCaption(caption);
671 SetRootWindowCaption(window, caption);
673 externalDisplayChange = false;
686 DWORD min = 0, max = 0;
688 if(msg != WM_CHAR && window.composing)
693 case WM_SYSKEYDOWN: min = max = WM_SYSCHAR; break;
694 case WM_KEYDOWN: min = max = WM_CHAR; break;
695 case WM_SYSKEYUP: min = WM_SYSCHAR; max = WM_SYSDEADCHAR; break;
696 case WM_KEYUP: min = WM_CHAR; max = WM_DEADCHAR; break;
699 // This is very annoying.
700 // PeekMessage somehow invokes this function directly... don't know why.
701 // Only crashes when running through debug mode in visual studio
703 if(msg == WM_CHAR || msg == WM_DEADCHAR || PeekMessage(&charMsg, windowHandle, min, max, PM_REMOVE))
705 ch = (msg == WM_CHAR || msg == WM_DEADCHAR) ? (unichar)wParam : (unichar)charMsg.wParam;
706 // TOCHECK: What is this for again? Fixing some obscure activation status?
707 // -- I believe this was somehow allowing 'unmaximizing', but was causing problems
708 // as there was no way to prevent AltEnter from doing so (e.g. when it is used for a node property)
709 // Worked around by fixing ProcessHotKeys to properly check for sysButtons in parent.parent when sys buttons
710 // are placed inside a menu bar for a document
712 if(msg == WM_SYSKEYDOWN && ch == 13)
714 ShowWindow(window.windowHandle, window.state == maximized ? SW_MAXIMIZE : SW_SHOWNORMAL);
715 // This last line been commented out for a long time:
716 // window.ExternalActivate(true, true, window, null);
719 if(msg == WM_SYSKEYUP || msg == WM_KEYUP)
721 if(!ProcessKeyMessage(window, WM_KEYDOWN, 0x40000000, 0, ch))
727 ProcessKeyMessage(window, msg, wParam, lParam, ch);
728 if(window.composing && msg == WM_CHAR)
730 COMPOSITIONFORM form;
731 Window caretOwner = guiApp.caretOwner;
734 HIMC ctx = ImmGetContext(windowHandle);
735 form.dwStyle = CFS_POINT;
736 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
737 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.x + caretOwner.absPosition.y - window.absPosition.y + 2;
738 window.composing = true;
739 ImmSetCompositionWindow(ctx, &form);
740 ImmReleaseContext(windowHandle, ctx);
743 if(msg != WM_MOUSEWHEEL)
747 case WM_IME_STARTCOMPOSITION:
749 COMPOSITIONFORM form;
750 Window caretOwner = guiApp.caretOwner;
753 HIMC ctx = ImmGetContext(windowHandle);
754 form.dwStyle = CFS_POINT;
755 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
756 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y + 2;
758 form.rcArea.left = window.caretPos.x;
759 form.rcArea.top = 250;
760 form.rcArea.right = 500;
761 form.rcArea.bottom = 300;
763 window.composing = true;
764 DefWindowProc(windowHandle, msg, wParam, lParam);
765 ImmSetCompositionWindow(ctx, &form);
766 ImmSetStatusWindowPos(ctx, &form.ptCurrentPos);
768 FontResource res = caretOwner.font;
769 LOGFONT font = { 0 };
771 int pixels = GetDeviceCaps(hdc, LOGPIXELSY);
773 font.lfHeight = -(int)((float)res.size * pixels / 72 + 0.5);
774 font.lfWeight = res.flags.bold ? FW_BOLD : FW_NORMAL;
775 font.lfItalic = res.flags.italic ? TRUE : FALSE,
776 font.lfUnderline = res.flags.underline ? TRUE : FALSE;
777 font.lfCharSet = DEFAULT_CHARSET;
778 font.lfOutPrecision = OUT_DEFAULT_PRECIS;
779 font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
780 font.lfQuality = DEFAULT_QUALITY;
781 font.lfPitchAndFamily = (byte)(DEFAULT_PITCH|FF_DONTCARE); // TODO: Fix compiler 0 | 0 to produce byte, not int
782 UTF8toUTF16Buffer(res.faceName, font.lfFaceName, LF_FACESIZE);
784 ImmSetCompositionFont(ctx, &font);
785 ImmReleaseContext(windowHandle, ctx);
791 case WM_IME_ENDCOMPOSITION:
792 window.composing = false;
793 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
794 /*case WM_IME_COMPOSITION:
795 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
797 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
799 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
801 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
803 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
805 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
806 case WM_IME_SETCONTEXT:
807 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
809 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
811 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
812 case WM_IME_COMPOSITIONFULL:
813 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
816 if(window.nativeDecorations)
817 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
827 case WM_LBUTTONDBLCLK:
829 case WM_RBUTTONDBLCLK:
831 case WM_MBUTTONDBLCLK:
833 x = window.absPosition.x;
834 y = window.absPosition.y;
835 /*case WM_NCLBUTTONUP:
838 case WM_NCLBUTTONDOWN:
839 case WM_NCLBUTTONDBLCLK:
840 case WM_NCRBUTTONDOWN:
841 case WM_NCRBUTTONDBLCLK:
842 case WM_NCMBUTTONDOWN:
843 case WM_NCMBUTTONDBLCLK:*/
847 bool consequential = false;
849 x += (short)LOWORD(lParam);
850 y += (short)HIWORD(lParam);
852 if(window.nativeDecorations && msg != WM_NCMOUSEMOVE)
854 x += window.clientStart.x;
855 y += window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
858 if(::GetKeyState(VK_SHIFT) & 0x80000) code.shift = true;
859 if(::GetKeyState(VK_CONTROL) & 0x80000) code.ctrl = true;
860 if(::GetKeyState(VK_MENU) & 0x80000) code.alt = true;
861 if(wParam & MK_LBUTTON) code.left = true;
862 if(wParam & MK_MBUTTON) code.middle = true;
863 if(wParam & MK_RBUTTON) code.right = true;
865 if(msg == WM_MOUSEMOVE)
867 if(lastPos.x == x && lastPos.y == y)
868 consequential = true;
878 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove, x,y,&code, consequential, false);
880 //case WM_NCLBUTTONDBLCLK:
881 case WM_LBUTTONDBLCLK:
882 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick, x,y,&code, false, true))
884 //case WM_NCLBUTTONDOWN:
886 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown,x,y,&code, false,
887 /*(msg == WM_LBUTTONDBLCLK) ? false: */true);
889 //case WM_NCLBUTTONUP:
890 case WM_LBUTTONUP: window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp, x,y,&code, false, false);break;
891 //case WM_NCMBUTTONDBLCLK:
892 case WM_MBUTTONDBLCLK:
893 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick, x,y,&code, false, false))
895 //case WM_NCMBUTTONDOWN:
897 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown, x,y,&code, false,
898 (msg == WM_LBUTTONDBLCLK) ? false: true);
900 //case WM_NCMBUTTONUP:
902 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp, x,y,&code, false, false);
904 //case WM_NCRBUTTONDBLCLK:
905 case WM_RBUTTONDBLCLK:
906 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick, x,y,&code, false, true))
908 //case WM_NCRBUTTONDOWN:
910 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown, x,y,&code, false,
911 (msg == WM_LBUTTONDBLCLK) ? false: true);
913 //case WM_NCRBUTTONUP:
915 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp, x,y,&code, false, false);
919 if(msg == WM_NCMOUSEMOVE)
920 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
925 if(window.nativeDecorations)
927 if(DefWindowProc(windowHandle, msg, wParam, lParam))
930 // SetCursor(systemCursors[0]);
931 SetCursor((lastCursor == (SystemCursor)-1) ? null : systemCursors[lastCursor]);
934 case WM_EXITMENULOOP:
935 case WM_EXITSIZEMOVE:
936 // We had some DirectInput stuff in here
938 case WM_ENTERMENULOOP:
939 case WM_ENTERSIZEMOVE:
940 // We had some DirectInput stuff in here
951 GetWindowRect(windowHandle, &rcWindow);
953 x = rcWindow.left - desktopX;
954 y = rcWindow.top - desktopY;
955 w = rcWindow.right - rcWindow.left;
956 h = rcWindow.bottom - rcWindow.top;
958 AeroSnapPosition(window, x, y, w, h);
965 RECT * rect = (RECT *)lParam;
969 window.GetDecorationsSize(&ew, &eh);
971 w = rect->right - rect->left;
972 h = rect->bottom - rect->top;
980 w = Max(w, window.minSize.w);
981 h = Max(h, window.minSize.h);
982 w = Min(w, window.maxSize.w);
983 h = Min(h, window.maxSize.h);
985 if(!window.OnResizing(&w, &h))
987 w = window.clientSize.w;
988 h = window.clientSize.h;
991 w = Max(w, window.skinMinSize.w);
992 h = Max(h, window.skinMinSize.h);
997 if(wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT)
998 rect->left = rect->right - w;
1000 rect->right = rect->left + w;
1002 if(wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOP || wParam == WMSZ_TOPRIGHT)
1003 rect->top = rect->bottom - h;
1005 rect->bottom = rect->top + h;
1011 if(window.nativeDecorations)
1015 GetWindowRect(windowHandle, &rcWindow);
1017 if(wParam == SIZE_MAXIMIZED && window.state != maximized)
1018 window.state = maximized;
1019 else if(wParam == SIZE_MINIMIZED && window.state != minimized)
1020 window.state = minimized;
1021 else if(wParam == SIZE_RESTORED && window.state != normal && window.visible)
1022 window.state = normal;
1024 x = rcWindow.left - desktopX;
1025 y = rcWindow.top - desktopY;
1026 w = rcWindow.right - rcWindow.left;
1027 h = rcWindow.bottom - rcWindow.top;
1029 AeroSnapPosition(window, x, y, w, h);
1030 if(!guiApp.modeSwitching)
1031 window.UpdateVisual(null);
1034 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
1038 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
1042 return (uint)DefMDIChildProc(windowHandle, msg, wParam, lParam);
1043 // return DefWindowProc(windowHandle, msg, wParam, lParam);
1047 // --- DirectInput ---
1049 #ifndef ECERE_NODINPUT
1051 #ifndef ECERE_NOJOYSTICK
1052 bool CALLBACK ::JoystickCallback( const DIDEVICEINSTANCE* pdidInstance, void * context )
1054 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_Joystick,
1055 (IDirectInputDevice **)&directJoysticks[numJoysticks], null ))
1057 return DIENUM_CONTINUE;
1060 bool CALLBACK ::JoystickAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, void * context )
1063 IDirectInputDevice2 * curJoy = (IDirectInputDevice2 *)context;
1065 diprg.diph.dwSize = sizeof(DIPROPRANGE);
1066 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1067 diprg.diph.dwHow = DIPH_BYOFFSET;
1068 diprg.diph.dwObj = pdidoi->dwOfs;
1071 if(curJoy->lpVtbl->SetProperty(curJoy, DIPROP_RANGE, &diprg.diph))
1073 return DIENUM_CONTINUE;
1077 void ::TerminateDirectInput()
1082 directMouse->lpVtbl->Unacquire(directMouse);
1083 directMouse->lpVtbl->Release(directMouse);
1086 #ifndef ECERE_NOJOYSTICK
1087 for(j=0; j<numJoysticks; j++)
1089 if (directJoysticks[j])
1091 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1092 directJoysticks[j]->lpVtbl->Release(directJoysticks[j]);
1093 directJoysticks[j] = null;
1100 dInput->lpVtbl->Release(dInput);
1104 directInputCreate = null;
1108 FreeLibrary(dInputDll);
1113 bool ::InitDirectInput()
1115 bool result = false;
1117 dInputDll = LoadLibraryA("dinput.dll");
1120 if((directInputCreate = (void *)GetProcAddress(dInputDll, "DirectInputCreateA")))
1122 if(!directInputCreate( hInstance, DIRECTINPUT_VERSION, &dInput, null ))
1127 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_SysMouse, &directMouse, null ))
1128 directMouse->lpVtbl->SetDataFormat(directMouse, &c_dfDIMouse );
1130 #ifndef ECERE_NOJOYSTICK
1132 dInput->lpVtbl->EnumDevices(dInput, DIDEVTYPE_JOYSTICK, JoystickCallback, null, DIEDFL_ATTACHEDONLY );
1133 for(j=0; j<NUMJOY; j++)
1134 if(directJoysticks[j])
1135 if(!directJoysticks[j]->lpVtbl->SetDataFormat(directJoysticks[j], &c_dfDIJoystick ))
1136 directJoysticks[j]->lpVtbl->EnumObjects(directJoysticks[j], JoystickAxesCallback, directJoysticks[j], DIDFT_AXIS );
1145 void ::AcquireDirectInput(HWND windowHandle, bool state)
1147 if((state && !acquiredWindow) || (!state && acquiredWindow == windowHandle))
1154 directMouse->lpVtbl->SetCooperativeLevel(directMouse, /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1155 directMouse->lpVtbl->Acquire(directMouse);
1158 directMouse->lpVtbl->Unacquire(directMouse);
1160 #ifndef ECERE_NOJOYSTICK
1161 for(j = 0; j<NUMJOY; j++)
1162 if(directJoysticks[j])
1166 directJoysticks[j]->lpVtbl->SetCooperativeLevel(directJoysticks[j], /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1167 directJoysticks[j]->lpVtbl->Acquire(directJoysticks[j]);
1170 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1173 acquiredWindow = state ? windowHandle : null;
1178 /****************************************************************************
1179 /// INTEGRATION FUNCTIONALITY /////////////
1180 ****************************************************************************/
1181 bool eisWIN32ProcessKey(Window window, DWORD msg, DWORD wParam, DWORD lParam, byte ch)
1183 return ProcessKeyMessage(window, msg, wParam, lParam, ch);
1186 HWND eisWIN32GetWindowHandle(Window window)
1188 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;
1191 /****************************************************************************
1192 /// DRIVER IMPLEMENTATION /////////////
1193 ****************************************************************************/
1196 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, uint64 dwUser, uint64 dw1, uint64 dw2)
1198 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1201 guiApp.SignalEvent();
1204 // --- User Interface System ---
1209 CS_DBLCLKS, ApplicationWindow, 0L, 0L, 0,
1210 LoadIcon(null, IDI_APPLICATION),
1217 lastRes = MAKELPARAM(GetSystemMetrics(SM_CYSCREEN), GetSystemMetrics(SM_CXSCREEN));
1218 lastBits = (WPARAM)GetDeviceCaps(hdc, BITSPIXEL);
1222 wcl.hInstance = hInstance = GetModuleHandle(null);
1223 RegisterClass(&wcl);
1225 // Move that in reposition desktop
1226 startBar = FindWindowA("Shell_TrayWnd", null);
1228 systemCursors[arrow] = LoadCursor(null, IDC_ARROW);
1229 systemCursors[iBeam] = LoadCursor(null, IDC_IBEAM);
1230 systemCursors[cross] = LoadCursor(null, IDC_CROSS);
1231 systemCursors[moving] = LoadCursor(null, IDC_SIZEALL);
1232 systemCursors[sizeNESW] = LoadCursor(null, IDC_SIZENESW);
1233 systemCursors[sizeNS ] = LoadCursor(null, IDC_SIZENS);
1234 systemCursors[sizeNWSE] = LoadCursor(null, IDC_SIZENWSE);
1235 systemCursors[sizeWE ] = LoadCursor(null, IDC_SIZEWE);
1236 systemCursors[hand ] = LoadCursor(null, IDC_HAND);
1238 SetTimer(null, 0, (DWORD)(1000.0 / 18.2), null);
1239 //SetTimer(null, 0, 1, null);
1243 hiResTimer = timeSetEvent(
1246 TimerProc,0,TIME_PERIODIC);
1249 hiResTimer = timeSetEvent(
1252 TimerProc,0,TIME_PERIODIC);
1256 topWindow = CreateWindowEx(0, className, "",WS_POPUP,0,0,1,1,HWND_DESKTOP,
1257 null, hInstance, null);
1264 if(hiResTimer) timeKillEvent(hiResTimer);
1265 #ifndef ECERE_NODINPUT
1266 TerminateDirectInput();
1270 void SetTimerResolution(uint hertz)
1272 if(hiResTimer) timeKillEvent(hiResTimer);
1274 hiResTimer = timeSetEvent(1000 / hertz, 1000 / hertz, TimerProc, 0, TIME_PERIODIC);
1277 bool ProcessInput(bool processAll)
1281 RepositionDesktop(true);
1282 if(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
1284 while(PeekMessage(&msg,0,0,0,PM_REMOVE))
1286 TranslateMessage(&msg);
1287 DispatchMessage(&msg);
1288 // printf("%d\n", msg.message);
1289 if(!processAll || msg.message == WM_TIMER) break;
1298 MsgWaitForMultipleObjects(1, (void **)guiApp.semaphore, FALSE, (uint)(1000 / 18.2) /*INFINITE*/, QS_ALLINPUT);
1301 void Lock(Window window)
1306 void Unlock(Window window)
1311 const char ** GraphicsDrivers(int * numDrivers)
1313 static const char *graphicsDrivers[] = { "GDI", "DirectDraw", "OpenGL", "Direct3D", "Direct3D8", "Direct3D9" };
1314 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1315 return (const char **)graphicsDrivers;
1318 void GetCurrentMode(bool * fullScreen, Resolution * resolution, PixelFormat * colorDepth, int * refreshRate)
1320 *fullScreen = fullScreenMode;
1324 for(c = 0; c<Resolution::enumSize; c++)
1325 if(GetResolutionWidth(c) == devMode.dmPelsWidth && GetResolutionHeight(c) == devMode.dmPelsHeight)
1330 switch(devMode.dmBitsPerPel)
1332 case 8: *colorDepth = pixelFormat8; break;
1333 case 16: *colorDepth = pixelFormat555; break;
1334 default: *colorDepth = pixelFormat888; break;
1336 * refreshRate = devMode.dmDisplayFrequency;
1340 void EnsureFullScreen(bool *fullScreen)
1345 bool ScreenMode(bool fullScreen, Resolution resolution, PixelFormat colorDepth, int refreshRate, bool * textMode)
1350 fullScreenMode = fullScreen;
1354 FillBytes(&devMode, 0, sizeof(DEVMODE));
1355 devMode.dmSize = (uint16)sizeof(devMode);
1356 devMode.dmFields |=DM_BITSPERPEL;
1357 devMode.dmFields |=DM_PELSWIDTH|DM_PELSHEIGHT;
1358 devMode.dmFields |= DM_DISPLAYFREQUENCY;
1359 devMode.dmBitsPerPel = colorDepth ? GetDepthBits(colorDepth) : GetDeviceCaps(hdc, BITSPIXEL);
1360 devMode.dmPelsWidth = resolution ? GetResolutionWidth(resolution) : GetSystemMetrics(SM_CXSCREEN);
1361 devMode.dmPelsHeight = resolution ? GetResolutionHeight(resolution) : GetSystemMetrics(SM_CYSCREEN);
1362 devMode.dmDisplayFrequency = refreshRate ? refreshRate : GetDeviceCaps(hdc, VREFRESH);
1363 if(ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1367 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
1368 guiApp.SetDesktopPosition(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), false);
1373 static bool firstTime = true;
1375 ChangeDisplaySettings(null, 0);
1377 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
1378 desktopX = desktopY = desktopW = desktopH = 0;
1380 RepositionDesktop(false);
1387 // --- Window Creation ---
1388 void * CreateRootWindow(Window window)
1391 uint16 * text = UTF8toUTF16(window.text, null);
1394 windowHandle = CreateWindowEx(0, className, text,
1396 0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
1398 null, hInstance, null);
1399 ShowWindow(windowHandle, SW_SHOWNORMAL);
1401 else if(window.systemParent)
1402 windowHandle = CreateWindowEx(0, className, text,
1403 WS_CHILD,0,0,1,1, window.systemParent, null, hInstance, null);
1408 HWND parentWindow = null; //HWND_DESKTOP; // we get different behaviors with desktop...
1409 Window master = window.master, rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1410 if(window.style.stayOnTop)
1411 exStyle |= WS_EX_TOPMOST;
1413 if(rootWindow && (window._isModal || window.style.interim || (window.style.thin && !window.style.showInTaskBar)))
1414 parentWindow = rootWindow.is3D ? rootWindow.parent.windowHandle : rootWindow.windowHandle;
1416 if(window.alphaBlend)
1417 // if(window.background.a < 255) //&& window.style & ES_REDRAW) Not needed anymore?
1418 exStyle |= WS_EX_LAYERED; // | WS_EX_TRANSPARENT;
1420 // Toolwindow will disappear if they don't have AppWindow set
1421 if(window.style.showInTaskBar) // Took this out as a way to prevent sticky notes from showing in task bar... ( added flag to Communicator ) || (!parentWindow && window.style.thin))
1423 exStyle |= WS_EX_APPWINDOW;
1424 parentWindow = null;
1427 if(window.style.thin)
1428 exStyle |= WS_EX_TOOLWINDOW;
1430 if(window.windowHandle)
1431 windowHandle = window.windowHandle;
1434 if(window.nativeDecorations)
1436 BorderBits borderStyle = window.borderStyle; // FIXME!
1437 style = WS_OVERLAPPED;
1438 if(borderStyle.fixed)
1439 style |= WS_CAPTION;
1441 style |= WS_SYSMENU;
1442 if(borderStyle.sizable)
1443 style |= WS_THICKFRAME;
1444 if(window.hasMinimize)
1445 style |= WS_MINIMIZEBOX;
1446 if(window.hasMaximize)
1447 style |= WS_MAXIMIZEBOX;
1449 windowHandle = CreateWindowEx(
1452 style | (window.systemParent ? WS_CHILD :
1453 (WS_POPUP | (window.style.hasMinimize ? WS_MINIMIZEBOX : 0))),
1454 0,0,1,1, parentWindow, null, hInstance, null);
1456 if(exStyle & WS_EX_LAYERED)
1457 SetLayeredWindowAttributes(windowHandle, 0, 255 /*A(window.background)*/, LWA_ALPHA);
1463 SetWindowLongPtr(windowHandle, GWLP_USERDATA, (int64)window);
1465 SetWindowLong(windowHandle, GWL_USERDATA, (DWORD)window);
1468 return windowHandle;
1471 void DestroyRootWindow(Window window)
1474 int c, lockCount = guiApp.lockMutex.lockCount;
1475 for(c = 0; c < lockCount; c++)
1476 guiApp.lockMutex.Release();
1478 oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1480 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1482 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1484 DestroyIcon(oldIcon);
1486 ShowWindow(window.windowHandle, SW_HIDE);
1489 SetWindowLongPtr(window.windowHandle, GWLP_USERDATA, (int64)null);
1491 SetWindowLong(window.windowHandle, GWL_USERDATA, 0);
1493 DestroyWindow(window.windowHandle);
1495 for(c = 0; c < lockCount; c++)
1496 guiApp.lockMutex.Wait();
1498 window.windowHandle = null;
1501 // -- Window manipulation ---
1503 void SetRootWindowCaption(Window window, const char * name)
1505 uint16 * text = UTF8toUTF16(name, null);
1507 SetWindowText(window.windowHandle, text);
1512 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
1514 int flags = SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS;
1516 if(!window.systemParent && !fullScreenMode)
1522 if(!move) flags |= SWP_NOMOVE;
1523 if(!resize) flags |= SWP_NOSIZE;
1525 /*if(move && resize)
1527 flags |= SWP_NOMOVE;
1528 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1529 flags &=~SWP_NOMOVE;
1530 flags |= SWP_NOSIZE;
1532 if(!window.nativeDecorations || window.state != maximized || !window.visible || guiApp.modeSwitching)
1533 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1536 void OrderRootWindow(Window window, bool topMost)
1538 SetWindowPos(window.windowHandle, topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0,
1539 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
1542 void SetRootWindowColor(Window window)
1544 if(window.alphaBlend && window.display.pixelFormat == pixelFormat888)
1546 /*if(A(window.background) == 255)
1548 if((style & WS_EX_LAYERED) == WS_EX_LAYERED)
1549 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style &~WS_EX_LAYERED);
1553 #ifndef ECERE_NOBLENDING
1554 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1555 if((style & WS_EX_LAYERED) != WS_EX_LAYERED)
1556 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style | WS_EX_LAYERED);
1558 // SetLayeredWindowAttributes(window.windowHandle, 0, 255 /*Max(A(window.background),1)*/, LWA_ALPHA);
1563 void OffsetWindow(Window window, int * x, int * y)
1565 if(window.systemParent)
1567 POINT point = {*x,*y};
1568 ClientToScreen(GetParent(window.windowHandle), &point);
1574 void UpdateRootWindow(Window window)
1576 UpdateWindow(window.windowHandle);
1579 void SetRootWindowState(Window window, WindowState state, bool visible)
1583 WindowState curState = window.state;
1584 *&window.state = state;
1589 SmartShowWindow(window.windowHandle, window.nativeDecorations ? state : normal, (window.active || window.creationActivation == activate) && !externalDisplayChange);
1592 ShowWindow(window.windowHandle, SW_MINIMIZE);
1595 *&window.state = curState;
1599 ShowWindow(window.windowHandle, SW_HIDE);
1603 void ActivateRootWindow(Window window)
1605 if(!externalDisplayChange)
1606 SetForegroundWindow(window.windowHandle);
1609 void FlashRootWindow(Window window)
1611 HWND hwnd = window.windowHandle;
1612 FLASHWINFO flashInfo = { 0 };
1613 Window master = window.master, rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1614 if(!window.style.showInTaskBar && rootWindow && (window._isModal || window.style.interim))
1615 hwnd = rootWindow.windowHandle;
1617 flashInfo.cbSize = sizeof(FLASHWINFO);
1618 flashInfo.hwnd = hwnd;
1619 flashInfo.uCount = 1;
1620 flashInfo.dwFlags = FLASHW_TRAY; // FLASHW_ALL;
1622 FlashWindowEx((void *)&flashInfo);
1626 // --- Mouse-based window movement ---
1627 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
1629 if(!fullScreenMode && !window.systemParent)
1630 // Commented out for Chess game because AI thread takes over the moving of main window... Still required?
1631 //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
1632 ; //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
1635 SetWindowPos(window.windowHandle, HWND_TOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1636 mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
1637 if(!window.stayOnTop)
1638 SetWindowPos(window.windowHandle, HWND_NOTOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1642 void StopMoving(Window window)
1644 if(!fullScreenMode && !window.systemParent)
1645 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
1648 // -- Mouse manipulation ---
1650 void GetMousePosition(int *x, int *y)
1653 GetCursorPos(&point);
1658 void SetMousePosition(int x, int y)
1663 void SetMouseRange(Window window, Box box)
1665 ClipCursor((RECT *) box);
1668 void SetMouseCapture(Window window)
1671 SetCapture(window.windowHandle);
1676 // -- Mouse cursor ---
1678 void SetMouseCursor(Window window, SystemCursor cursor)
1680 if(lastCursor != cursor)
1682 lastCursor = cursor;
1683 SetCursor((cursor == (SystemCursor)-1) ? null : systemCursors[cursor]);
1689 void SetCaret(int x, int y, int size)
1694 // --- Clipboard manipulation ---
1696 void ClearClipboard()
1698 if(OpenClipboard(null))
1705 bool AllocateClipboard(ClipBoard clipBoard, uint size)
1707 bool result = false;
1708 clipBoard.text = new byte[size];
1713 bool SaveClipboard(ClipBoard clipBoard)
1715 bool result = false;
1719 uint16 * u16text = UTF8toUTF16(clipBoard.text, &wordCount);
1721 clipBoard.handle = GlobalAlloc(GHND | GMEM_DDESHARE, wordCount * 2);
1722 if(clipBoard.handle)
1724 uint16 * text = GlobalLock(clipBoard.handle);
1727 memcpy(text, u16text, wordCount * 2);
1728 GlobalUnlock(clipBoard.handle);
1729 if(OpenClipboard(null))
1732 if(SetClipboardData(CF_UNICODETEXT, clipBoard.handle))
1740 GlobalFree(clipBoard.handle);
1747 bool LoadClipboard(ClipBoard clipBoard)
1749 bool result = false;
1750 if(OpenClipboard(null))
1752 if((clipBoard.handle = GetClipboardData(CF_UNICODETEXT)))
1754 uint16 * u16text = GlobalLock(clipBoard.handle);
1757 clipBoard.text = UTF16toUTF8(u16text);
1759 GlobalUnlock(clipBoard.handle);
1767 void UnloadClipboard(ClipBoard clipBoard)
1769 delete clipBoard.text;
1772 // --- State based input ---
1774 bool AcquireInput(Window window, bool state)
1776 #ifndef ECERE_NODINPUT
1777 if(dInput || InitDirectInput())
1779 AcquireDirectInput(window.windowHandle, state);
1786 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
1788 bool result = false;
1789 #ifndef ECERE_NODINPUT
1790 DIMOUSESTATE dims = {0};
1792 if(acquiredWindow && directMouse)
1794 if(directMouse->lpVtbl->GetDeviceState(directMouse, sizeof(DIMOUSESTATE), &dims ))
1796 FillBytes(&dims, 0, sizeof(dims));
1797 directMouse->lpVtbl->Acquire(directMouse);
1801 /* We don't really want this...
1804 if(GetAsyncKeyState(VK_LBUTTON))
1805 dims.rgbButtons[0] |= 0x80;
1806 if(GetAsyncKeyState(VK_RBUTTON))
1807 dims.rgbButtons[1] |= 0x80;
1808 if(GetAsyncKeyState(VK_MBUTTON))
1809 dims.rgbButtons[2] |= 0x80;
1818 Window window = (Window)GetWindowLongPtr(acquiredWindow, GWLP_USERDATA);
1820 Window window = (Window)GetWindowLong(acquiredWindow, GWL_USERDATA);
1822 ProcessKeyMessage(window, WM_MOUSEWHEEL, ((uint16)(short)dims.lZ) << 16, 0, 0);
1826 *buttons = MouseButtons {
1827 left = (dims.rgbButtons[0] & 0x80) ? true : false,
1828 right = (dims.rgbButtons[1] & 0x80) ? true : false,
1829 middle = (dims.rgbButtons[2] & 0x80) ? true : false };
1836 bool GetJoystickState(int device, Joystick joystick)
1838 bool result = false;
1839 #ifndef ECERE_NODINPUT
1840 if(joystick != null)
1842 DIJOYSTATE dijs = {0};
1843 #ifndef ECERE_NOJOYSTICK
1844 if(acquiredWindow && device < numJoysticks)
1846 if(directJoysticks[device])
1848 directJoysticks[device]->lpVtbl->Poll(directJoysticks[device]);
1849 if(directJoysticks[device]->lpVtbl->GetDeviceState(directJoysticks[device], sizeof(DIJOYSTATE), &dijs ))
1850 directJoysticks[device]->lpVtbl->Acquire(directJoysticks[device]);
1855 joystick.x = dijs.lX;
1856 joystick.y = dijs.lY;
1857 joystick.z = dijs.lZ;
1858 joystick.rx = dijs.lRx;
1859 joystick.ry = dijs.lRy;
1860 joystick.rz = dijs.lRz;
1862 ((dijs.rgbButtons[0] & 0x80) ? JOY_BUTTON1 : 0)
1863 | ((dijs.rgbButtons[1] & 0x80) ? JOY_BUTTON2 : 0)
1864 | ((dijs.rgbButtons[2] & 0x80) ? JOY_BUTTON3 : 0)
1865 | ((dijs.rgbButtons[3] & 0x80) ? JOY_BUTTON4 : 0);
1871 bool GetKeyState(Key key)
1873 bool keyState = false;
1874 if(key < 256 || key == alt || key == shift || key == control)
1878 ks = GetAsyncKeyState(VK_MENU);
1879 else if(key == control)
1880 ks = GetAsyncKeyState(VK_CONTROL);
1881 else if(key == shift)
1882 ks = GetAsyncKeyState(VK_SHIFT);
1883 else if(key2VK[key])
1884 ks = GetAsyncKeyState(key2VK[key]);
1885 keyState = (ks & 0x80000) ? true : false;
1887 else if(key == capsState)
1888 keyState = (::GetKeyState(VK_CAPITAL) & 0x00000001) != 0;
1889 else if(key == numState)
1890 keyState = (::GetKeyState(VK_NUMLOCK) & 0x00000001) != 0;
1891 else if(key == scrollState)
1892 keyState = (::GetKeyState(VK_SCROLL) & 0x00000001) != 0;
1896 bool SetIcon(Window window, BitmapResource resource)
1899 HICON oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1901 // Dialogs Inherit master's icon if none set
1902 if(!window.style.showInTaskBar && window.hasClose)
1904 Window master = window.master;
1905 while(master && !resource)
1907 Window rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
1908 if(rootWindow && rootWindow.icon)
1909 resource = rootWindow.icon;
1911 master = master.master;
1915 // WARNING -- putting this here as it is right after CreateRootWindow
1916 // Take out Layered flag if we're not in 24 bit
1918 if(window.alphaBlend && window.display.pixelFormat != pixelFormat888)
1920 #ifndef ECERE_NOBLENDING
1921 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1922 style &= ~WS_EX_LAYERED;
1923 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style);
1929 if(oldIcon && oldIcon != (HICON)GetClassLongPtr(window.windowHandle, GCLP_HICON))
1931 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1934 DestroyIcon(oldIcon);
1940 if(bitmap.Load(resource.fileName, null, null))
1943 PixelFormat format = window.display.pixelFormat;
1944 int bits = GetDepthBits(format);
1947 bitmap.Convert(null, pixelFormat888, null);
1948 and.Allocate(null, (bitmap.width+7/8), bitmap.height, 0, pixelFormat8, false);
1950 blend = bits == 32 || bitmap.pixelFormat != pixelFormat888;
1953 byte * picture = and.picture;
1956 uint size = bitmap.height * bitmap.width;
1961 while(m < 8 && c < size)
1964 mask |= blend ? (!((ColorAlpha *)bitmap.picture)[c].a) : (((ColorAlpha *)bitmap.picture)[c].a <= 192);
1968 picture[b++] = mask;
1973 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1974 if(blend ? (!color.a) : (color.a <= 192))
1976 color.color = { 0, 0, 0 };
1977 ((ColorAlpha *)bitmap.picture)[c] = color;
1982 if(bits == 15) { bits = 16; format = pixelFormat565; };
1983 bitmap.Convert(null, format, null);
1985 icon = CreateIcon(hInstance, bitmap.width, bitmap.height, 1, (byte)bits, and.picture, bitmap.picture);
1990 SendMessage(window.windowHandle, WM_SETICON, ICON_BIG, (LPARAM)icon);
1991 SendMessage(window.windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)icon);
1995 void ::GetScreenArea(Window window, Box box)
1997 HMONITOR monitor = MonitorFromWindow(window.windowHandle, MONITOR_DEFAULTTONEAREST);
1998 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
2001 MONITORINFO info = { 0 };
2002 info.cbSize = sizeof(MONITORINFO);
2003 GetMonitorInfo(monitor, &info);
2004 // box = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
2005 box = { info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.right-1, info.rcMonitor.bottom-1 };
2007 if(taskBarMonitor == monitor)
2009 if(taskBarPlacement.rcNormalPosition.top <= box.top && taskBarPlacement.rcNormalPosition.bottom >= box.bottom)
2011 if(taskBarPlacement.rcNormalPosition.left <= box.left)
2013 if(taskBarState & ABS_AUTOHIDE)
2016 box.left = taskBarPlacement.rcNormalPosition.right;
2018 else if(taskBarState & ABS_AUTOHIDE)
2021 box.right = taskBarPlacement.rcNormalPosition.left;
2023 else if(taskBarPlacement.rcNormalPosition.left <= box.left && taskBarPlacement.rcNormalPosition.right >= box.right)
2025 if(taskBarPlacement.rcNormalPosition.top <= box.top)
2027 if(taskBarState & ABS_AUTOHIDE)
2030 box.top = taskBarPlacement.rcNormalPosition.bottom;
2032 else if(taskBarState & ABS_AUTOHIDE)
2035 box.bottom = taskBarPlacement.rcNormalPosition.top;
2039 box.left -= desktopX;
2040 box.top -= desktopY;
2041 box.right -= desktopX;
2042 box.bottom -= desktopY;