1 namespace gui::drivers;
10 #define _WIN32_WINNT 0x0500
17 #define WIN32_LEAN_AND_MEAN
18 #define Method _Method
25 #if defined(ECERE_VANILLA)
26 #define ECERE_NOJOYSTICK
29 #ifndef ECERE_NODINPUT
30 #ifndef ECERE_NOJOYSTICK
31 #define DIRECTINPUT_VERSION 0x700
34 #define DIRECTINPUT_VERSION 0x300
44 #define VK_EQUALS 0xBB
47 #define VK_PERIOD 0xBE
50 #define VK_LBRACKET 0xDB
51 #define VK_BACK_SLASH 0xDC
52 #define VK_RBRACKET 0xDD
56 #define WM_MOUSEWHEEL 0x20A
60 #define IDC_HAND (char *)0x7F89
65 #define LWA_COLORKEY 0x00000001
66 #define LWA_ALPHA 0x00000002
68 #define WS_EX_LAYERED 0x00080000
73 SetLayeredWindowAttributes(
88 #define ECERE_NODINPUT
89 #define ECERE_NOJOYSTICK
90 #define ECERE_NOBLENDING
93 static byte key2VK[256] =
95 0,VK_ESCAPE,'1','2','3','4','5','6','7','8','9','0',VK_MINUS,VK_EQUALS,VK_BACK,VK_TAB,
96 'Q','W','E','R','T','Y','U','I','O','P',VK_LBRACKET,VK_RBRACKET,VK_RETURN,VK_CONTROL,'A','S',
97 'D','F','G','H','J','K','L',VK_SEMI,VK_QUOTE,VK_TILDE,VK_SHIFT,VK_BACK_SLASH,'Z','X','C','V',
98 '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,
99 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,
100 VK_NUMPAD2,VK_NUMPAD3,VK_NUMPAD0,VK_DELETE,0,0,0,0,0,0,0,0,0,0,0,0,
101 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,
102 0,0,0,VK_SHIFT,VK_MENU,VK_CONTROL
104 static char className[] = "ECERE Application";
105 static HINSTANCE hInstance;
107 static DEVMODE devMode;
108 static HWND acquiredWindow = null;
109 static HCURSOR systemCursors[SystemCursor];
110 static bool fullScreenMode;
111 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
112 static DWORD hiResTimer;
113 static HWND topWindow;
115 static HWND startBar;
117 #ifndef ECERE_NODINPUT
119 static HMODULE dInputDll = null;
120 static BOOL * (WINAPI * directInputCreate)(HINSTANCE, DWORD, IDirectInput **, void *);
121 static IDirectInput * dInput = null;
122 static IDirectInputDevice * directMouse = null;
123 #ifndef ECERE_NOJOYSTICK
124 static int numJoysticks = 0;
125 static IDirectInputDevice2 * directJoysticks[NUMJOY] = {null,null,null,null};
130 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
131 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp;
132 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown;
133 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit;
134 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove;
135 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
136 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
137 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
138 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
139 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
140 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
141 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick;
142 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown;
143 extern int __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
147 static Box lastMonitorAreas[32];
148 static Box monitorAreas[32];
151 static bool EnumerateMonitors(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
153 MONITORINFO info = { 0 };
154 info.cbSize = sizeof(MONITORINFO);
155 GetMonitorInfo(hMonitor, &info);
156 monitorAreas[monitor] = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
161 static int taskBarState;
162 static WINDOWPLACEMENT taskBarPlacement;
163 static bool activateApp;
165 static SystemCursor lastCursor = (SystemCursor)-1;
167 class Win32Interface : Interface
169 class_property(name) = "Win32";
171 void ::RepositionDesktop(bool updateChildren)
174 static double lastTime = 0, time;
177 static Size lastScreen;
178 static Point lastScreenPos;
179 static WINDOWPLACEMENT lastPlacement;
181 static double lastAutoHideCheck = 0;
182 int newTaskBarState = taskBarState;
183 HMONITOR primaryMonitor;
186 if(time - lastTime < 0.1) return;
189 // Every sec, check for the auto hide property
190 if(time - lastAutoHideCheck > 1)
192 APPBARDATA appBarData = { 0 };
193 newTaskBarState = SHAppBarMessage(ABM_GETSTATE, &appBarData);
194 lastAutoHideCheck = time;
198 EnumDisplayMonitors(null, null, EnumerateMonitors, 0);
199 for(c = 0; c<monitor; c++)
201 if(monitorAreas[c].left != lastMonitorAreas[c].left ||
202 monitorAreas[c].top != lastMonitorAreas[c].top ||
203 monitorAreas[c].right != lastMonitorAreas[c].right ||
204 monitorAreas[c].bottom != lastMonitorAreas[c].bottom)
210 memcpy(lastMonitorAreas, monitorAreas, sizeof(monitorAreas));
212 guiApp.virtualScreen =
214 GetSystemMetrics(SM_CXVIRTUALSCREEN),
215 GetSystemMetrics(SM_CYVIRTUALSCREEN)
217 guiApp.virtualScreenPos =
219 x = GetSystemMetrics(SM_XVIRTUALSCREEN),
220 y = GetSystemMetrics(SM_YVIRTUALSCREEN)
224 HMONITOR monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
227 MONITORINFO info = { 0 };
228 info.cbSize = sizeof(MONITORINFO);
229 primaryMonitor = monitor;
230 GetMonitorInfo(monitor, &info);
231 x = info.rcMonitor.left;
232 y = info.rcMonitor.top;
233 w = info.rcMonitor.right - info.rcWork.left;
234 h = info.rcMonitor.bottom - info.rcWork.top;
239 WINDOWPLACEMENT placement = { 0 };
240 placement.length = sizeof(WINDOWPLACEMENT);
242 if(!GetWindowPlacement(startBar, &placement))
244 startBar = FindWindowA("Shell_TrayWnd", null);
245 GetWindowPlacement(startBar, &placement);
248 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
249 if(primaryMonitor == taskBarMonitor)
251 if(placement.rcNormalPosition.top <= 0 && placement.rcNormalPosition.bottom >= h - 1)
253 if(placement.rcNormalPosition.left <= 0)
255 x = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.right;
259 w = (taskBarState & ABS_AUTOHIDE) ? (w - 1) : Min(placement.rcNormalPosition.left, w);
261 else if(placement.rcNormalPosition.left <= 0 && placement.rcNormalPosition.right >= w - 1)
263 if(placement.rcNormalPosition.top <= 0)
265 y = (taskBarState & ABS_AUTOHIDE) ? 1 : placement.rcNormalPosition.bottom;
269 h = (taskBarState & ABS_AUTOHIDE) ? (h - 1) : Min(placement.rcNormalPosition.top, h);
275 placement.rcNormalPosition.left != taskBarPlacement.rcNormalPosition.left ||
276 placement.rcNormalPosition.top != taskBarPlacement.rcNormalPosition.top ||
277 placement.rcNormalPosition.right != taskBarPlacement.rcNormalPosition.right ||
278 placement.rcNormalPosition.bottom != taskBarPlacement.rcNormalPosition.bottom ||
279 newTaskBarState != taskBarState)
281 taskBarState = newTaskBarState;
282 guiApp.desktop.size.w = 0;
287 taskBarPlacement = placement;
288 guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
291 lastScreen = guiApp.virtualScreen;
292 lastScreenPos = guiApp.virtualScreenPos;
296 /****************************************************************************
297 /// PRIVATE UTILITY FUNCTIONS /////////////
298 ****************************************************************************/
299 // --- Keyboard Input ---
300 bool ::ProcessKeyMessage(Window window, DWORD msg, WPARAM wParam, LPARAM lParam, unichar ch)
306 bool frenchShift = (ch < 0x10000) ? (((VkKeyScan((uint16)ch) >> 8) & 6) == 6) : false;
308 if(msg == WM_CHAR || msg == WM_DEADCHAR)
313 if(msg == WM_MOUSEWHEEL)
314 code = (((short) HIWORD(wParam)) < 0) ? wheelDown : wheelUp;
317 key = (byte)((lParam & 0xFF0000)>>16);
318 if(lParam & 0x1000000)
320 key = Interface::GetExtendedKey(key);
330 if(key != leftShift && key != rightShift && ::GetKeyState(VK_SHIFT) & 0x80000)
332 if(key != leftControl && key != rightControl && ::GetKeyState(VK_CONTROL) & 0x80000 && !frenchShift)
334 if(key != leftAlt && key != rightAlt && ::GetKeyState(VK_MENU) & 0x80000 && !frenchShift)
337 if(msg == WM_MOUSEWHEEL)
339 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code, 0);
344 byte ch = Interface::TranslateKeykey, code.shift);
345 if(::GetKeyState(VK_CAPITAL))
349 if(msg == WM_KEYUP || msg == WM_SYSKEYUP)
351 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyUp, code, ch);
355 if(!(lParam & 0x40000000))
360 Class * keyClass = eSystem_FindClass(__ecereModule, "Key");
361 code.OnGetString(string, null, null);
362 eSystem_Logf("%s\n", string);
366 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyDown, code, ch);
369 result = window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, code,ch);
376 // --- Window procedure ---
377 DWORD CALLBACK ::ApplicationWindow(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
379 Window window = (Window)GetWindowLong(windowHandle, GWL_USERDATA);
380 static Point lastPos;
387 case WM_QUERYNEWPALETTE:
388 case WM_PALETTECHANGED:
390 window.display.RestorePalette();
391 UpdateWindow(windowHandle);
395 if(!fullScreenMode && !guiApp.IsModeSwitching())
399 Window modalRoot = window.FindModal();
401 HWND modalWindow = modalRoot ? modalRoot.windowHandle : null;
403 FLASHWINFO flashInfo = { 0 };
404 flashInfo.cbSize = sizeof(FLASHWINFO);
405 flashInfo.hwnd = window.windowHandle;
406 flashInfo.uCount = 0;
407 flashInfo.dwFlags = FLASHW_STOP;
408 FlashWindowEx((void *)&flashInfo);
410 if(modalWindow && modalWindow != windowHandle)
411 modalRoot.ExternalActivate(true, true, window, null);
413 window.ExternalActivate(true, true, window, null);
420 foreground = GetForegroundWindow();
421 if(foreground == windowHandle && lParam)
422 foreground = (HWND)lParam;
424 GetWindowThreadProcessId(foreground, &id);
426 windowLong = GetWindowLong(foreground, GWL_WNDPROC);
427 #if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
428 // The != ApplicationWindow check is for not recognizing the Console window as an Ecere Window
429 // That check causes a problem with the OpenGL driver which seems to popup a window of a different class
430 if(window.displaySystem && window.displaySystem.driver == class(OpenGLDisplayDriver))
431 windowLong = (uint)ApplicationWindow;
433 if(id != GetCurrentProcessId() || windowLong != (LPARAM)ApplicationWindow)
434 window.ExternalActivate(false, true, window, null);
435 // DefWindowProc for WM_NCACTIVATE draws the decorations, make sure it's drawn in the right state
436 return DefWindowProc(windowHandle, msg, window.active, lParam);
441 for(window = guiApp.desktop.firstChild; window; window = window.next)
442 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
443 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
447 return (uint)DefWindowProc(windowHandle, msg, window ? window.active : wParam, lParam);
450 if(!guiApp.IsModeSwitching())
454 HDC hdc = GetDC(windowHandle);
457 guiApp.SetAppFocus(false);
458 ShowWindow(windowHandle, SW_MINIMIZE);
459 ChangeDisplaySettings(null,0);
460 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
464 ChangeDisplaySettings(&devMode, CDS_FULLSCREEN);
465 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
467 window.display.RestorePalette();
468 ShowWindow(windowHandle, SW_SHOWNORMAL);
469 guiApp.SetAppFocus(true);
471 window.display.Resize(window.clientSize.w, window.clientSize.h);
474 ReleaseDC(windowHandle, hdc);
480 if(wParam && !guiApp.desktop.active /*&& lParam != GetCurrentThreadID()*/)
484 for(window = guiApp.desktop.firstChild; window; window = window.next)
485 SetWindowPos(window.windowHandle, window.style.stayOnTop ? HWND_TOPMOST : HWND_TOP, 0,0,0,0,
486 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE); //|SWP_NOREDRAW);
489 guiApp.SetAppFocus(wParam);
493 guiApp.SetAppFocus(wParam);
497 if(!window.alphaBlend || window.display.pixelFormat != pixelFormat888)
502 #define ACCESS_ITEM(l, id) \
503 ((FastItem)(((id) == -1) ? null : (((byte *)((l).items)) + (id) * (l).itemSize)))
506 #define ACCESS_ITEM(l, id) \
509 BoxItem item = window.dirtyArea.count ? (BoxItem)ACCESS_ITEM(window.dirtyArea, window.dirtyArea.first) : null;
511 BeginPaint(windowHandle, &ps);
513 // Prevent flickering if we're going to update anyways
515 printf(" Paint message (%d, %d)-(%d, %d)\n",
516 item ? item.box.left : 0,
517 item ? item.box.top : 0,
518 item ? item.box.right : 0,
519 item ? item.box.bottom : 0);
521 // Causes redraw bug...
522 if(!window.manageDisplay || !item ||
525 item.box.right < window.size.w - 1 ||
526 item.box.bottom < window.size.h - 1)
528 Box box { ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-1, ps.rcPaint.bottom-1 };
529 window.UpdateDirty(box);
531 // TODO: Fix precompiler with
532 // window.UpdateDirty((Box *)&ps.rcPaint);
533 EndPaint(windowHandle, &ps);
537 case WM_DISPLAYCHANGE:
539 static int lastBits = 0;
540 static int lastRes = 0;
541 if(lastBits != wParam || lastRes != lParam)
546 if(guiApp.desktop.DisplayModeChanged())
549 if(!window.style.hidden)
550 ShowWindow(windowHandle, SW_SHOWNOACTIVATE /*SW_SHOWNORMAL*/);
551 window.FigureCaption(caption);
552 SetRootWindowCaption(window, caption);
568 if(msg != WM_CHAR && window.composing)
573 case WM_SYSKEYDOWN: min = max = WM_SYSCHAR; break;
574 case WM_KEYDOWN: min = max = WM_CHAR; break;
575 case WM_SYSKEYUP: min = WM_SYSCHAR; max = WM_SYSDEADCHAR; break;
576 case WM_KEYUP: min = WM_CHAR; max = WM_DEADCHAR; break;
579 // This is very annoying.
580 // PeekMessage somehow invokes this function directly... don't know why.
581 // Only crashes when running through debug mode in visual studio
583 if(msg == WM_CHAR || msg == WM_DEADCHAR || PeekMessage(&charMsg, windowHandle, min, max, PM_REMOVE))
585 ch = (msg == WM_CHAR || msg == WM_DEADCHAR) ? wParam : (unichar)charMsg.wParam;
586 if(msg == WM_SYSKEYDOWN && ch == 13)
588 ShowWindow(window.windowHandle, SW_SHOWNORMAL);
589 // window.ExternalActivate(true, true, window, null);
591 if(msg == WM_SYSKEYUP || msg == WM_KEYUP)
593 if(!ProcessKeyMessage(window, WM_KEYDOWN, 0x40000000, 0, ch))
599 ProcessKeyMessage(window, msg, wParam, lParam, ch);
600 if(window.composing && msg == WM_CHAR)
602 COMPOSITIONFORM form;
603 Window caretOwner = guiApp.caretOwner;
606 HIMC ctx = ImmGetContext(windowHandle);
607 form.dwStyle = CFS_POINT;
608 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
609 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.x + caretOwner.absPosition.y - window.absPosition.y + 2;
610 window.composing = true;
611 ImmSetCompositionWindow(ctx, &form);
612 ImmReleaseContext(windowHandle, ctx);
615 if(msg != WM_MOUSEWHEEL)
619 case WM_IME_STARTCOMPOSITION:
621 COMPOSITIONFORM form;
622 Window caretOwner = guiApp.caretOwner;
625 HIMC ctx = ImmGetContext(windowHandle);
626 form.dwStyle = CFS_POINT;
627 form.ptCurrentPos.x = caretOwner.caretPos.x - caretOwner.scroll.x + caretOwner.absPosition.x - window.absPosition.x + 4;
628 form.ptCurrentPos.y = caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y + 2;
630 form.rcArea.left = window.caretPos.x;
631 form.rcArea.top = 250;
632 form.rcArea.right = 500;
633 form.rcArea.bottom = 300;
635 window.composing = true;
636 DefWindowProc(windowHandle, msg, wParam, lParam);
637 ImmSetCompositionWindow(ctx, &form);
638 ImmSetStatusWindowPos(ctx, &form.ptCurrentPos);
640 FontResource res = caretOwner.font;
641 LOGFONT font = { 0 };
643 int pixels = GetDeviceCaps(hdc, LOGPIXELSY);
645 font.lfHeight = -(int)((float)res.size * pixels / 72 + 0.5);
646 font.lfWeight = res.flags.bold ? FW_BOLD : FW_NORMAL;
647 font.lfItalic = res.flags.italic ? TRUE : FALSE,
648 font.lfUnderline = res.flags.underline ? TRUE : FALSE;
649 font.lfCharSet = DEFAULT_CHARSET;
650 font.lfOutPrecision = OUT_DEFAULT_PRECIS;
651 font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
652 font.lfQuality = DEFAULT_QUALITY;
653 font.lfPitchAndFamily = (byte)DEFAULT_PITCH|FF_DONTCARE; // TODO: Fix compiler 0 | 0 to produce byte, not int
654 UTF8toUTF16Buffer(res.faceName, font.lfFaceName, LF_FACESIZE);
656 ImmSetCompositionFont(ctx, &font);
657 ImmReleaseContext(windowHandle, ctx);
663 case WM_IME_ENDCOMPOSITION:
664 window.composing = false;
665 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
666 /*case WM_IME_COMPOSITION:
667 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
669 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
671 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
673 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
675 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
677 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
678 case WM_IME_SETCONTEXT:
679 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
681 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
683 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
684 case WM_IME_COMPOSITIONFULL:
685 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
688 if(window.nativeDecorations)
689 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
699 case WM_LBUTTONDBLCLK:
701 case WM_RBUTTONDBLCLK:
703 case WM_MBUTTONDBLCLK:
705 x = window.absPosition.x;
706 y = window.absPosition.y;
707 /*case WM_NCLBUTTONUP:
710 case WM_NCLBUTTONDOWN:
711 case WM_NCLBUTTONDBLCLK:
712 case WM_NCRBUTTONDOWN:
713 case WM_NCRBUTTONDBLCLK:
714 case WM_NCMBUTTONDOWN:
715 case WM_NCMBUTTONDBLCLK:
716 case WM_NCMOUSEMOVE:*/
719 bool consequential = false;
721 x += (short)LOWORD(lParam);
722 y += (short)HIWORD(lParam);
724 if(window.nativeDecorations)
726 x += window.clientStart.x;
727 y += window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
730 if(::GetKeyState(VK_SHIFT) & 0x80000) code.shift = true;
731 if(::GetKeyState(VK_CONTROL) & 0x80000) code.ctrl = true;
732 if(::GetKeyState(VK_MENU) & 0x80000) code.alt = true;
733 if(wParam & MK_LBUTTON) code.left = true;
734 if(wParam & MK_MBUTTON) code.middle = true;
735 if(wParam & MK_RBUTTON) code.right = true;
737 if(msg == WM_MOUSEMOVE)
739 if(lastPos.x == x && lastPos.y == y)
740 consequential = true;
748 //case WM_NCMOUSEMOVE:
750 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove, x,y,&code, consequential, false);
752 //case WM_NCLBUTTONDBLCLK:
753 case WM_LBUTTONDBLCLK:
754 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick, x,y,&code, false, true))
756 //case WM_NCLBUTTONDOWN:
758 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown,x,y,&code, false,
759 (msg == WM_LBUTTONDBLCLK) ? false: true);
761 //case WM_NCLBUTTONUP:
762 case WM_LBUTTONUP: window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp, x,y,&code, false, false);break;
763 //case WM_NCMBUTTONDBLCLK:
764 case WM_MBUTTONDBLCLK:
765 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick, x,y,&code, false, false))
767 //case WM_NCMBUTTONDOWN:
769 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown, x,y,&code, false,
770 (msg == WM_LBUTTONDBLCLK) ? false: true);
772 //case WM_NCMBUTTONUP:
774 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp, x,y,&code, false, false);
776 //case WM_NCRBUTTONDBLCLK:
777 case WM_RBUTTONDBLCLK:
778 if(!window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightDoubleClick, x,y,&code, false, true))
780 //case WM_NCRBUTTONDOWN:
782 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonDown, x,y,&code, false,
783 (msg == WM_LBUTTONDBLCLK) ? false: true);
785 //case WM_NCRBUTTONUP:
787 window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp, x,y,&code, false, false);
795 if(window.nativeDecorations)
797 if(DefWindowProc(windowHandle, msg, wParam, lParam))
800 // SetCursor(systemCursors[0]);
801 SetCursor((lastCursor == (SystemCursor)-1) ? null : systemCursors[lastCursor]);
804 case WM_EXITMENULOOP:
805 case WM_EXITSIZEMOVE:
806 // We had some DirectInput stuff in here
808 case WM_ENTERMENULOOP:
809 case WM_ENTERSIZEMOVE:
810 // We had some DirectInput stuff in here
820 WINDOWPLACEMENT placement = { 0 };
822 placement.length = sizeof(WINDOWPLACEMENT);
823 GetWindowRect(windowHandle, &rcWindow);
824 GetWindowPlacement(windowHandle, &placement);
827 if((placement.showCmd == SW_SHOWMAXIMIZED || placement.showCmd == SW_MAXIMIZE) && window.state != maximized)
828 window.state = maximized;
829 else if(placement.showCmd == SW_SHOWMINIMIZED && window.state != minimized)
830 window.state = minimized;
831 else if(placement.showCmd == SW_SHOWNORMAL && window.state != normal && window.visible)
832 window.state = normal;
834 window.ExternalPosition(x, y, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);
839 if(window.nativeDecorations)
841 int w = LOWORD(lParam);
842 int h = HIWORD(lParam);
844 WINDOWPLACEMENT placement = { 0 };
846 GetWindowRect(windowHandle, &rcWindow);
848 placement.length = sizeof(WINDOWPLACEMENT);
850 GetWindowPlacement(windowHandle, &placement);
851 if((placement.showCmd == SW_SHOWMAXIMIZED || placement.showCmd == SW_MAXIMIZE) && window.state != maximized)
852 window.state = maximized;
853 else if(placement.showCmd == SW_SHOWMINIMIZED && window.state != minimized)
854 window.state = minimized;
855 else if(placement.showCmd == SW_SHOWNORMAL && window.state != normal && window.visible)
856 window.state = normal;
860 w = rcWindow.right - rcWindow.left;
861 h = rcWindow.bottom - rcWindow.top;
862 window.ExternalPosition(x, y, w, h);
863 window.UpdateDisplay();
866 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
870 return (uint)DefWindowProc(windowHandle, msg, wParam, lParam);
874 return (uint)DefMDIChildProc(windowHandle, msg, wParam, lParam);
875 // return DefWindowProc(windowHandle, msg, wParam, lParam);
879 // --- DirectInput ---
881 #ifndef ECERE_NODINPUT
883 #ifndef ECERE_NOJOYSTICK
884 bool CALLBACK ::JoystickCallback( const DIDEVICEINSTANCE* pdidInstance, void * context )
886 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_Joystick,
887 (IDirectInputDevice **)&directJoysticks[numJoysticks], null ))
889 return DIENUM_CONTINUE;
892 bool CALLBACK ::JoystickAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, void * context )
895 IDirectInputDevice2 * curJoy = (IDirectInputDevice2 *)context;
897 diprg.diph.dwSize = sizeof(DIPROPRANGE);
898 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
899 diprg.diph.dwHow = DIPH_BYOFFSET;
900 diprg.diph.dwObj = pdidoi->dwOfs;
903 if(curJoy->lpVtbl->SetProperty(curJoy, DIPROP_RANGE, &diprg.diph))
905 return DIENUM_CONTINUE;
909 void ::TerminateDirectInput()
914 directMouse->lpVtbl->Unacquire(directMouse);
915 directMouse->lpVtbl->Release(directMouse);
918 #ifndef ECERE_NOJOYSTICK
919 for(j=0; j<numJoysticks; j++)
921 if (directJoysticks[j])
923 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
924 directJoysticks[j]->lpVtbl->Release(directJoysticks[j]);
925 directJoysticks[j] = null;
932 dInput->lpVtbl->Release(dInput);
936 directInputCreate = null;
940 FreeLibrary(dInputDll);
945 bool ::InitDirectInput()
949 dInputDll = LoadLibraryA("dinput.dll");
952 if((directInputCreate = (void *)GetProcAddress(dInputDll, "DirectInputCreateA")))
954 if(!directInputCreate( hInstance, DIRECTINPUT_VERSION, &dInput, null ))
959 if(!dInput->lpVtbl->CreateDevice(dInput, &GUID_SysMouse, &directMouse, null ))
960 directMouse->lpVtbl->SetDataFormat(directMouse, &c_dfDIMouse );
962 #ifndef ECERE_NOJOYSTICK
964 dInput->lpVtbl->EnumDevices(dInput, DIDEVTYPE_JOYSTICK, JoystickCallback, null, DIEDFL_ATTACHEDONLY );
965 for(j=0; j<NUMJOY; j++)
966 if(directJoysticks[j])
967 if(!directJoysticks[j]->lpVtbl->SetDataFormat(directJoysticks[j], &c_dfDIJoystick ))
968 directJoysticks[j]->lpVtbl->EnumObjects(directJoysticks[j], JoystickAxesCallback, directJoysticks[j], DIDFT_AXIS );
977 void ::AcquireDirectInput(HWND windowHandle, bool state)
981 if((state && !acquiredWindow) || (!state && acquiredWindow == windowHandle))
988 directMouse->lpVtbl->SetCooperativeLevel(directMouse, /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
989 directMouse->lpVtbl->Acquire(directMouse);
992 directMouse->lpVtbl->Unacquire(directMouse);
994 #ifndef ECERE_NOJOYSTICK
995 for(j = 0; j<NUMJOY; j++)
996 if(directJoysticks[j])
1000 directJoysticks[j]->lpVtbl->SetCooperativeLevel(directJoysticks[j], /*fullScreenMode ? */windowHandle /*: HWND_DESKTOP*/, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
1001 directJoysticks[j]->lpVtbl->Acquire(directJoysticks[j]);
1004 directJoysticks[j]->lpVtbl->Unacquire(directJoysticks[j]);
1007 acquiredWindow = state ? windowHandle : null;
1012 /****************************************************************************
1013 /// INTEGRATION FUNCTIONALITY /////////////
1014 ****************************************************************************/
1015 bool eisWIN32ProcessKey(Window window, DWORD msg, DWORD wParam, DWORD lParam, byte ch)
1017 return ProcessKeyMessage(window, msg, wParam, lParam, ch);
1020 HWND eisWIN32GetWindowHandle(Window window)
1022 return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;
1025 /****************************************************************************
1026 /// DRIVER IMPLEMENTATION /////////////
1027 ****************************************************************************/
1029 void CALLBACK ::TimerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
1031 guiApp.SignalEvent();
1034 // --- User Interface System ---
1039 CS_DBLCLKS, ApplicationWindow, 0L, 0L, 0,
1040 LoadIcon(null, IDI_APPLICATION),
1046 wcl.hInstance = hInstance = GetModuleHandle(null);
1047 RegisterClass(&wcl);
1049 // Move that in reposition desktop
1050 startBar = FindWindowA("Shell_TrayWnd", null);
1052 systemCursors[arrow] = LoadCursor(null, IDC_ARROW);
1053 systemCursors[iBeam] = LoadCursor(null, IDC_IBEAM);
1054 systemCursors[cross] = LoadCursor(null, IDC_CROSS);
1055 systemCursors[moving] = LoadCursor(null, IDC_SIZEALL);
1056 systemCursors[sizeNESW] = LoadCursor(null, IDC_SIZENESW);
1057 systemCursors[sizeNS ] = LoadCursor(null, IDC_SIZENS);
1058 systemCursors[sizeNWSE] = LoadCursor(null, IDC_SIZENWSE);
1059 systemCursors[sizeWE ] = LoadCursor(null, IDC_SIZEWE);
1060 systemCursors[hand ] = LoadCursor(null, IDC_HAND);
1062 SetTimer(null, 0, (DWORD)(1000.0 / 18.2), null);
1063 //SetTimer(null, 0, 1, null);
1067 hiResTimer = timeSetEvent(
1070 TimerProc,0,TIME_PERIODIC);
1073 hiResTimer = timeSetEvent(
1076 TimerProc,0,TIME_PERIODIC);
1080 topWindow = CreateWindowEx(0, className, "",WS_POPUP,0,0,1,1,HWND_DESKTOP,
1081 null, hInstance, null);
1088 if(hiResTimer) timeKillEvent(hiResTimer);
1089 #ifndef ECERE_NODINPUT
1090 TerminateDirectInput();
1094 void SetTimerResolution(uint hertz)
1096 if(hiResTimer) timeKillEvent(hiResTimer);
1098 hiResTimer = timeSetEvent(1000 / hertz, 1000 / hertz, TimerProc, 0, TIME_PERIODIC);
1101 bool ProcessInput(bool processAll)
1105 RepositionDesktop(true);
1106 if(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
1108 while(PeekMessage(&msg,0,0,0,PM_REMOVE))
1110 TranslateMessage(&msg);
1111 DispatchMessage(&msg);
1112 // printf("%d\n", msg.message);
1113 if(!processAll || msg.message == WM_TIMER) break;
1122 MsgWaitForMultipleObjects(1, (void **)guiApp.semaphore, FALSE, (uint)(1000 / 18.2) /*INFINITE*/, QS_ALLINPUT);
1125 void Lock(Window window)
1130 void Unlock(Window window)
1135 char ** GraphicsDrivers(int * numDrivers)
1137 static char *graphicsDrivers[] = { "GDI", "DirectDraw", "OpenGL", "Direct3D", "Direct3D8", "Direct3D9" };
1138 *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
1139 return (char **)graphicsDrivers;
1142 void GetCurrentMode(bool * fullScreen, Resolution * resolution, PixelFormat * colorDepth, int * refreshRate)
1144 *fullScreen = fullScreenMode;
1148 for(c = 0; c<Resolution::enumSize; c++)
1149 if(GetResolutionWidth(c) == devMode.dmPelsWidth && GetResolutionHeight(c) == devMode.dmPelsHeight)
1154 switch(devMode.dmBitsPerPel)
1156 case 8: *colorDepth = pixelFormat8; break;
1157 case 16: *colorDepth = pixelFormat555; break;
1158 default: *colorDepth = pixelFormat888; break;
1160 * refreshRate = devMode.dmDisplayFrequency;
1164 void EnsureFullScreen(bool *fullScreen)
1169 bool ScreenMode(bool fullScreen, Resolution resolution, PixelFormat colorDepth, int refreshRate, bool * textMode)
1174 fullScreenMode = fullScreen;
1178 FillBytes(&devMode, 0, sizeof(DEVMODE));
1179 devMode.dmSize = (uint16)sizeof(devMode);
1180 devMode.dmFields |=DM_BITSPERPEL;
1181 devMode.dmFields |=DM_PELSWIDTH|DM_PELSHEIGHT;
1182 devMode.dmFields |= DM_DISPLAYFREQUENCY;
1183 devMode.dmBitsPerPel = colorDepth ? GetDepthBits(colorDepth) : GetDeviceCaps(hdc, BITSPIXEL);
1184 devMode.dmPelsWidth = resolution ? GetResolutionWidth(resolution) : GetSystemMetrics(SM_CXSCREEN);
1185 devMode.dmPelsHeight = resolution ? GetResolutionHeight(resolution) : GetSystemMetrics(SM_CYSCREEN);
1186 devMode.dmDisplayFrequency = refreshRate ? refreshRate : GetDeviceCaps(hdc, VREFRESH);
1187 if(ChangeDisplaySettings(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1191 SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
1192 guiApp.SetDesktopPosition(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), false);
1197 static bool firstTime = true;
1199 ChangeDisplaySettings(null, 0);
1201 SetSystemPaletteUse(hdc, SYSPAL_STATIC);
1202 desktopX = desktopY = desktopW = desktopH = 0;
1204 RepositionDesktop(false);
1211 // --- Window Creation ---
1212 void * CreateRootWindow(Window window)
1215 uint16 * text = UTF8toUTF16(window.text, null);
1218 windowHandle = CreateWindowEx(0, className, text,
1220 0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
1222 null, hInstance, null);
1223 ShowWindow(windowHandle, SW_SHOWNORMAL);
1225 else if(window.systemParent)
1226 windowHandle = CreateWindowEx(0, className, text,
1227 WS_CHILD,0,0,1,1, window.systemParent, null, hInstance, null);
1232 HWND parentWindow = null; //HWND_DESKTOP; // we get different behaviors with desktop...
1233 if(window.style.stayOnTop)
1234 exStyle |= WS_EX_TOPMOST;
1236 // exStyle |= WS_EX_TOOLWINDOW;
1238 if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window.style.modal || window.style.interim))
1240 Window master = window.master;
1241 Window rootWindow = window.master.rootWindow;
1243 parentWindow = rootWindow.is3D ? rootWindow.parent.windowHandle : rootWindow.windowHandle;
1245 // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
1248 if(window.alphaBlend)
1249 // if(window.background.a < 255) //&& window.style & ES_REDRAW) Not needed anymore?
1250 exStyle |= WS_EX_LAYERED; // | WS_EX_TRANSPARENT;
1252 if(window.style.showInTaskBar)
1254 exStyle |= WS_EX_APPWINDOW;
1255 parentWindow = null;
1256 style |= WS_SYSMENU;
1258 else if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop)
1260 exStyle |= WS_EX_TOOLWINDOW;
1261 //exStyle |= WS_EX_APPWINDOW;
1262 style |= WS_SYSMENU;
1266 // exStyle |= WS_EX_APPWINDOW;
1267 style |= WS_SYSMENU;
1269 /*else if(parentWindow)
1270 exStyle |= WS_EX_TOOLWINDOW;*/
1271 /*if(window.interim)
1272 parentWindow = window.master.rootWindow.windowHandle;*/
1274 if(window.windowHandle)
1275 windowHandle = window.windowHandle;
1278 if(window.nativeDecorations)
1279 style |= WS_OVERLAPPEDWINDOW;
1280 windowHandle = CreateWindowEx(
1283 style | (window.systemParent ? WS_CHILD :
1284 (WS_POPUP | (window.style.hasMinimize ? WS_MINIMIZEBOX : 0))),
1285 0,0,1,1, parentWindow, null, hInstance, null);
1288 if(exStyle & WS_EX_LAYERED)
1289 SetLayeredWindowAttributes(windowHandle, 0, 255 /*A(window.background)*/, LWA_ALPHA);
1294 SetWindowLong(windowHandle, GWL_USERDATA, (DWORD)window);
1296 return windowHandle;
1299 void DestroyRootWindow(Window window)
1301 HICON oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1302 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1304 DestroyIcon(oldIcon);
1307 // TO FIX LOCK UP PROBLEMS... WOULD NEED TO UNLOCK ALL RECURSIONS ((GuiApplication)__thisModule.application).Unlock();
1308 ShowWindow(window.windowHandle, SW_HIDE);
1309 // TO FIX LOCK UP PROBLEMS... WOULD NEED TO LOCK ALL RECURSIONS((GuiApplication)__thisModule.application).Lock();
1311 SetWindowLong(window.windowHandle, GWL_USERDATA, 0);
1312 DestroyWindow(window.windowHandle);
1313 window.windowHandle = null;
1316 // -- Window manipulation ---
1318 void SetRootWindowCaption(Window window, char * name)
1320 uint16 * text = UTF8toUTF16(name, null);
1321 SetWindowText(window.windowHandle, text);
1325 void PositionRootWindow(Window window, int x, int y, int w, int h, bool move, bool resize)
1327 int flags = SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOCOPYBITS;
1329 if(!window.systemParent && !fullScreenMode)
1335 if(!move) flags |= SWP_NOMOVE;
1336 if(!resize) flags |= SWP_NOSIZE;
1338 /*if(move && resize)
1340 flags |= SWP_NOMOVE;
1341 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1342 flags &=~SWP_NOMOVE;
1343 flags |= SWP_NOSIZE;
1345 if(!window.nativeDecorations || window.state != maximized || !window.visible)
1346 SetWindowPos(window.windowHandle, null, x, y, w, h, flags);
1349 void OrderRootWindow(Window window, bool topMost)
1351 SetWindowPos(window.windowHandle, topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0,
1352 SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOSIZE|SWP_NOREDRAW);
1355 void SetRootWindowColor(Window window)
1357 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1358 if(window.alphaBlend && window.display.pixelFormat == pixelFormat888)
1360 /*if(A(window.background) == 255)
1362 if((style & WS_EX_LAYERED) == WS_EX_LAYERED)
1363 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style &~WS_EX_LAYERED);
1367 #ifndef ECERE_NOBLENDING
1368 if((style & WS_EX_LAYERED) != WS_EX_LAYERED)
1369 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style | WS_EX_LAYERED);
1371 // SetLayeredWindowAttributes(window.windowHandle, 0, 255 /*Max(A(window.background),1)*/, LWA_ALPHA);
1376 void OffsetWindow(Window window, int * x, int * y)
1378 if(window.systemParent)
1380 POINT point = {*x,*y};
1381 ClientToScreen(GetParent(window.windowHandle), &point);
1387 void UpdateRootWindow(Window window)
1389 UpdateWindow(window.windowHandle);
1392 void SetRootWindowState(Window window, WindowState state, bool visible)
1400 ShowWindow(window.windowHandle, (window.creationActivation == activate && !guiApp.modeSwitching) ?
1401 ((window.nativeDecorations && state == maximized) ? SW_MAXIMIZE : SW_SHOWNORMAL) : SW_SHOWNOACTIVATE);
1404 ShowWindow(window.windowHandle, SW_MINIMIZE);
1410 ShowWindow(window.windowHandle, SW_HIDE);
1414 void ActivateRootWindow(Window window)
1416 if(!guiApp.modeSwitching)
1417 SetForegroundWindow(window.windowHandle);
1420 void FlashRootWindow(Window window)
1422 FLASHWINFO flashInfo = { 0 };
1423 flashInfo.cbSize = sizeof(FLASHWINFO);
1424 flashInfo.hwnd = window.windowHandle;
1425 flashInfo.uCount = 1;
1426 flashInfo.dwFlags = FLASHW_TRAY; // FLASHW_ALL;
1427 FlashWindowEx((void *)&flashInfo);
1430 // --- Mouse-based window movement ---
1431 void StartMoving(Window window, int x, int y, bool fromKeyBoard)
1433 if(!fullScreenMode && !window.systemParent)
1434 // Commented out for Chess game because AI thread takes over the moving of main window... Still required?
1435 //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE);
1436 ; //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
1439 SetWindowPos(window.windowHandle, HWND_TOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1440 mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
1441 SetWindowPos(window.windowHandle, HWND_NOTOPMOST, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1445 void StopMoving(Window window)
1447 if(!fullScreenMode && !window.systemParent)
1448 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
1451 // -- Mouse manipulation ---
1453 void GetMousePosition(int *x, int *y)
1456 GetCursorPos(&point);
1461 void SetMousePosition(int x, int y)
1466 void SetMouseRange(Window window, Box box)
1468 ClipCursor((RECT *) box);
1471 void SetMouseCapture(Window window)
1474 SetCapture(window.windowHandle);
1479 // -- Mouse cursor ---
1481 void SetMouseCursor(SystemCursor cursor)
1483 if(lastCursor != cursor)
1485 lastCursor = cursor;
1486 SetCursor((cursor == (SystemCursor)-1) ? null : systemCursors[cursor]);
1492 void SetCaret(int x, int y, int size)
1497 // --- Clipboard manipulation ---
1499 void ClearClipboard()
1501 if(OpenClipboard(null))
1508 bool AllocateClipboard(ClipBoard clipBoard, uint size)
1510 bool result = false;
1511 clipBoard.text = new byte[size];
1516 bool SaveClipboard(ClipBoard clipBoard)
1518 bool result = false;
1522 uint16 * u16text = UTF8toUTF16(clipBoard.text, &wordCount);
1524 clipBoard.handle = GlobalAlloc(GHND | GMEM_DDESHARE, wordCount * 2);
1525 if(clipBoard.handle)
1527 uint16 * text = GlobalLock(clipBoard.handle);
1530 memcpy(text, u16text, wordCount * 2);
1531 GlobalUnlock(clipBoard.handle);
1532 if(OpenClipboard(null))
1535 if(SetClipboardData(CF_UNICODETEXT, clipBoard.handle))
1543 GlobalFree(clipBoard.handle);
1550 bool LoadClipboard(ClipBoard clipBoard)
1552 bool result = false;
1553 if(OpenClipboard(null))
1555 if(clipBoard.handle = GetClipboardData(CF_UNICODETEXT))
1557 uint16 * u16text = GlobalLock(clipBoard.handle);
1560 clipBoard.text = UTF16toUTF8(u16text);
1562 GlobalUnlock(clipBoard.handle);
1570 void UnloadClipboard(ClipBoard clipBoard)
1572 delete clipBoard.text;
1575 // --- State based input ---
1577 bool AcquireInput(Window window, bool state)
1579 #ifndef ECERE_NODINPUT
1580 if(dInput || InitDirectInput())
1582 AcquireDirectInput(window.windowHandle, state);
1589 bool GetMouseState(MouseButtons * buttons, int * x, int * y)
1591 bool result = false;
1592 #ifndef ECERE_NODINPUT
1593 DIMOUSESTATE dims = {0};
1595 if(acquiredWindow && directMouse)
1597 if(directMouse->lpVtbl->GetDeviceState(directMouse, sizeof(DIMOUSESTATE), &dims ))
1599 FillBytes(&dims, 0, sizeof(dims));
1600 directMouse->lpVtbl->Acquire(directMouse);
1604 /* We don't really want this...
1607 if(GetAsyncKeyState(VK_LBUTTON))
1608 dims.rgbButtons[0] |= 0x80;
1609 if(GetAsyncKeyState(VK_RBUTTON))
1610 dims.rgbButtons[1] |= 0x80;
1611 if(GetAsyncKeyState(VK_MBUTTON))
1612 dims.rgbButtons[2] |= 0x80;
1620 *buttons = MouseButtons {
1621 left = (dims.rgbButtons[0] & 0x80) ? true : false,
1622 right = (dims.rgbButtons[1] & 0x80) ? true : false,
1623 middle = (dims.rgbButtons[2] & 0x80) ? true : false };
1630 bool GetJoystickState(int device, Joystick joystick)
1632 bool result = false;
1633 #ifndef ECERE_NODINPUT
1634 if(joystick != null)
1636 DIJOYSTATE dijs = {0};
1637 #ifndef ECERE_NOJOYSTICK
1638 if(acquiredWindow && device < numJoysticks)
1640 if(directJoysticks[device])
1642 directJoysticks[device]->lpVtbl->Poll(directJoysticks[device]);
1643 if(directJoysticks[device]->lpVtbl->GetDeviceState(directJoysticks[device], sizeof(DIJOYSTATE), &dijs ))
1644 directJoysticks[device]->lpVtbl->Acquire(directJoysticks[device]);
1649 joystick.x = dijs.lX;
1650 joystick.y = dijs.lY;
1651 joystick.z = dijs.lZ;
1652 joystick.rx = dijs.lRx;
1653 joystick.ry = dijs.lRy;
1654 joystick.rz = dijs.lRz;
1656 ((dijs.rgbButtons[0] & 0x80) ? JOY_BUTTON1 : 0)
1657 | ((dijs.rgbButtons[1] & 0x80) ? JOY_BUTTON2 : 0)
1658 | ((dijs.rgbButtons[2] & 0x80) ? JOY_BUTTON3 : 0)
1659 | ((dijs.rgbButtons[3] & 0x80) ? JOY_BUTTON4 : 0);
1665 bool GetKeyState(Key key)
1667 bool keyState = false;
1671 keyState = GetAsyncKeyState(key2VK[key]);
1672 keyState = (keyState & 0x80000) ? true : false;
1674 else if(key == capsState)
1675 keyState = ::GetKeyState(VK_CAPITAL) & 0x00000001;
1676 else if(key == numState)
1677 keyState = ::GetKeyState(VK_NUMLOCK) & 0x00000001;
1678 else if(key == scrollState)
1679 keyState = ::GetKeyState(VK_SCROLL) & 0x00000001;
1683 bool SetIcon(Window window, BitmapResource resource)
1686 HICON oldIcon = (HICON)SendMessage(window.windowHandle, WM_GETICON, ICON_BIG, 0);
1688 // WARNING -- putting this here as it is right after CreateRootWindow
1689 // Take out Layered flag if we're not in 24 bit
1691 if(window.alphaBlend && window.display.pixelFormat != pixelFormat888)
1693 #ifndef ECERE_NOBLENDING
1694 DWORD style = GetWindowLong(window.windowHandle, GWL_EXSTYLE);
1695 style &= ~WS_EX_LAYERED;
1696 SetWindowLong(window.windowHandle, GWL_EXSTYLE, style);
1701 if(oldIcon && oldIcon != (HICON)GetClassLong(window.windowHandle, GCL_HICON))
1703 DestroyIcon(oldIcon);
1709 if(bitmap.Load(resource.fileName, null, null))
1713 PixelFormat format = window.display.pixelFormat;
1714 //int bits = 8<<GetColorDepthShifts(format);
1718 bits = GetDepthBits(format);
1720 bitmap.Convert(null, pixelFormat888, null);
1721 and.Allocate(null, (bitmap.width+7/8), bitmap.height, 0, pixelFormat8, false);
1723 blend = bits == 32 || bitmap.pixelFormat != pixelFormat888;
1726 byte * picture = and.picture;
1729 uint size = bitmap.height * bitmap.width;
1734 while(m < 8 && c < size)
1737 mask |= blend ? (!((ColorAlpha *)bitmap.picture)[c].a) : (((ColorAlpha *)bitmap.picture)[c].a <= 192);
1741 picture[b++] = mask;
1746 ColorAlpha color = ((ColorAlpha *)bitmap.picture)[c];
1747 if(blend ? (!color.a) : (color.a <= 192))
1749 color.color = { 0, 0, 0 };
1750 ((ColorAlpha *)bitmap.picture)[c] = color;
1755 if(bits == 15) { bits = 16; format = pixelFormat565; };
1756 bitmap.Convert(null, format, null);
1758 icon = CreateIcon(hInstance, bitmap.width, bitmap.height, 1, (byte)bits, and.picture, bitmap.picture);
1763 SendMessage(window.windowHandle, WM_SETICON, ICON_BIG, (LPARAM)icon);
1764 SendMessage(window.windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)icon);
1768 void ::GetScreenArea(Window window, Box box)
1770 HMONITOR monitor = MonitorFromWindow(window.windowHandle, MONITOR_DEFAULTTONEAREST);
1771 HMONITOR taskBarMonitor = MonitorFromWindow(startBar, MONITOR_DEFAULTTONEAREST);
1774 MONITORINFO info = { 0 };
1775 info.cbSize = sizeof(MONITORINFO);
1776 GetMonitorInfo(monitor, &info);
1777 // box = { info.rcWork.left, info.rcWork.top, info.rcWork.right, info.rcWork.bottom };
1778 box = { info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.right-1, info.rcMonitor.bottom-1 };
1780 if(taskBarMonitor == monitor)
1782 if(taskBarPlacement.rcNormalPosition.top <= box.top && taskBarPlacement.rcNormalPosition.bottom >= box.bottom)
1784 if(taskBarPlacement.rcNormalPosition.left <= box.left)
1786 if(taskBarState & ABS_AUTOHIDE)
1789 box.left = taskBarPlacement.rcNormalPosition.right;
1791 else if(taskBarState & ABS_AUTOHIDE)
1794 box.right = taskBarPlacement.rcNormalPosition.left;
1796 else if(taskBarPlacement.rcNormalPosition.left <= box.left && taskBarPlacement.rcNormalPosition.right >= box.right)
1798 if(taskBarPlacement.rcNormalPosition.top <= box.top)
1800 if(taskBarState & ABS_AUTOHIDE)
1803 box.top = taskBarPlacement.rcNormalPosition.bottom;
1805 else if(taskBarState & ABS_AUTOHIDE)
1808 box.bottom = taskBarPlacement.rcNormalPosition.top;
1812 box.left -= desktopX;
1813 box.top -= desktopY;
1814 box.right -= desktopX;
1815 box.bottom -= desktopY;