WinBFApp.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. #include <dsound.h>
  2. #include "WinBFApp.h"
  3. #include "DXRenderDevice.h"
  4. #include <signal.h>
  5. #include "../../util/BeefPerf.h"
  6. #include "DSoundManager.h"
  7. #include "DInputManager.h"
  8. #include <dwmapi.h>
  9. #pragma comment(lib, "dwmapi.lib")
  10. #include "util/AllocDebug.h"
  11. USING_NS_BF;
  12. int Beefy::WinBFMenu::mMenuCount = 0;
  13. int PrintStuff()
  14. {
  15. OutputDebugStringA("Hey!\n");
  16. return 123;
  17. }
  18. WinBFMenu::WinBFMenu()
  19. {
  20. mIsPlaceholder = false;
  21. mMenu = NULL;
  22. mParent = NULL;
  23. mMenuId = 0;
  24. }
  25. ///
  26. static HICON gMainIcon = NULL;
  27. static BOOL CALLBACK BFEnumResNameProc(
  28. HMODULE hModule,
  29. LPCWSTR lpszType,
  30. LPWSTR lpszName,
  31. LONG_PTR lParam
  32. )
  33. {
  34. gMainIcon = ::LoadIconW(hModule, lpszName);
  35. return FALSE;
  36. }
  37. struct AdjustedMonRect
  38. {
  39. int mMonCount;
  40. int mX;
  41. int mY;
  42. int mWidth;
  43. int mHeight;
  44. };
  45. static BOOL ClipToMonitor(HMONITOR mon, HDC hdc, LPRECT monRect, LPARAM userArg)
  46. {
  47. AdjustedMonRect* outRect = (AdjustedMonRect*)userArg;
  48. MONITORINFO monitorInfo = { sizeof(MONITORINFO) };
  49. if (::GetMonitorInfo(mon, &monitorInfo) == 0)
  50. return TRUE;
  51. outRect->mMonCount++;
  52. if (outRect->mX < monitorInfo.rcWork.left)
  53. outRect->mX = monitorInfo.rcWork.left;
  54. else if (outRect->mX + outRect->mWidth >= monitorInfo.rcWork.right)
  55. outRect->mX = BF_MAX((int)monitorInfo.rcWork.left, monitorInfo.rcWork.right - outRect->mWidth);
  56. if (outRect->mY < monitorInfo.rcWork.top)
  57. outRect->mY = monitorInfo.rcWork.top;
  58. else if (outRect->mY + outRect->mHeight >= monitorInfo.rcWork.bottom)
  59. outRect->mY = BF_MAX((int)monitorInfo.rcWork.top, monitorInfo.rcWork.bottom - outRect->mHeight);
  60. return TRUE;
  61. }
  62. static BOOL KeyboardLayoutHasAltGr(HKL layout)
  63. {
  64. BOOL hasAltGr = FALSE;
  65. int scancode;
  66. for (WORD i = 32; i < 256; ++i)
  67. {
  68. scancode = VkKeyScanEx((TCHAR)i, layout);
  69. if ((scancode != -1) && ((scancode & 0x600) == 0x600))
  70. {
  71. hasAltGr = TRUE;
  72. break;
  73. }
  74. }
  75. return hasAltGr;
  76. }
  77. WinBFWindow::WinBFWindow(BFWindow* parent, const StringImpl& title, int x, int y, int width, int height, int windowFlags)
  78. {
  79. //OutputDebugStrF("Wnd %p Create\n", this);
  80. HINSTANCE hInstance = GetModuleHandle(NULL);
  81. mMinWidth = 128;
  82. mMinHeight = 128;
  83. WNDCLASSW wc;
  84. wc.style = 0;
  85. wc.cbClsExtra = 0;
  86. wc.cbWndExtra = 0;
  87. wc.hbrBackground = NULL;
  88. //wc.hbrBackground = ::CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
  89. wc.hCursor = NULL;
  90. //wc.hIcon = (HICON) ::LoadImageA(hInstance, "MainIcon", IMAGE_ICON, 0, 0, 0);
  91. //wc.hIcon = (HICON) ::LoadImageA(hInstance, MAKEINTRESOURCEA(32512), IMAGE_ICON, 0, 0, 0);
  92. if (gMainIcon != NULL)
  93. {
  94. wc.hIcon = gMainIcon;
  95. }
  96. else
  97. {
  98. wc.hIcon = (HICON) ::LoadIconA(hInstance, "MainIcon");
  99. if (wc.hIcon == NULL)
  100. {
  101. EnumResourceNamesW(hInstance, (LPCWSTR)RT_GROUP_ICON, BFEnumResNameProc, 0);
  102. wc.hIcon = gMainIcon;
  103. }
  104. }
  105. wc.hInstance = hInstance;
  106. wc.lpfnWndProc = WindowProcStub;
  107. wc.lpszClassName = L"BFWindow";
  108. wc.lpszMenuName = NULL;
  109. RegisterClassW(&wc);
  110. int requestedX = x;
  111. int requestedY = y;
  112. int aWindowFlags = 0;
  113. bool hasDestAlpha = (windowFlags & BFWINDOW_DEST_ALPHA) != 0;
  114. if (windowFlags & BFWINDOW_MENU)
  115. {
  116. WinBFMenu* aMenu = new WinBFMenu();
  117. aMenu->mMenu = ::CreateMenu();
  118. mHMenuMap[aMenu->mMenu] = aMenu;
  119. mMenu = aMenu;
  120. }
  121. int windowFlagsEx = /*WS_EX_COMPOSITED |*/ WS_EX_LAYERED;
  122. if (windowFlags & BFWINDOW_TOOLWINDOW)
  123. windowFlagsEx |= WS_EX_TOOLWINDOW;
  124. if (windowFlags & BFWINDOW_BORDER)
  125. aWindowFlags |= WS_BORDER;
  126. if (windowFlags & BFWINDOW_THICKFRAME)
  127. aWindowFlags |= WS_THICKFRAME;
  128. if ((windowFlags & BFWINDOW_RESIZABLE) && (!hasDestAlpha))
  129. aWindowFlags |= WS_SIZEBOX;
  130. if (windowFlags & BFWINDOW_SYSMENU)
  131. aWindowFlags |= WS_SYSMENU;
  132. if (windowFlags & BFWINDOW_CAPTION)
  133. aWindowFlags |= WS_CAPTION;
  134. else
  135. aWindowFlags |= WS_POPUP;
  136. if (windowFlags & BFWINDOW_MINIMIZE)
  137. aWindowFlags |= WS_MINIMIZEBOX;
  138. if (windowFlags & BFWINDOW_MAXIMIZE)
  139. aWindowFlags |= WS_MAXIMIZEBOX;
  140. if ((windowFlags & BFWINDOW_TOPMOST) && (parent == NULL))
  141. windowFlagsEx |= WS_EX_TOPMOST;
  142. if ((windowFlags & BFWINDOW_ACCEPTFILES))
  143. windowFlagsEx |= WS_EX_ACCEPTFILES;
  144. if (windowFlags & BFWINDOW_CLIENT_SIZED)
  145. {
  146. RECT rect = {0, 0, width, height};
  147. AdjustWindowRectEx(&rect, aWindowFlags, mMenu != NULL, windowFlagsEx);
  148. x += rect.left;
  149. y += rect.top;
  150. width = rect.right - rect.left;
  151. height = rect.bottom - rect.top;
  152. }
  153. if (windowFlags & BFWINDOW_POPUP_POSITION)
  154. {
  155. AdjustedMonRect adjustRect = { 0, x, y, width, height };
  156. RECT wantRect = { x, y, x + width, y + height };
  157. EnumDisplayMonitors(NULL, &wantRect, ClipToMonitor, (LPARAM)&adjustRect);
  158. if (adjustRect.mMonCount == 0)
  159. EnumDisplayMonitors(NULL, NULL, ClipToMonitor, (LPARAM)&adjustRect);
  160. x = adjustRect.mX;
  161. y = adjustRect.mY;
  162. width = adjustRect.mWidth;
  163. height = adjustRect.mHeight;
  164. }
  165. mFlags = windowFlags;
  166. mMouseVisible = true;
  167. mParent = parent;
  168. HWND parentHWnd = NULL;
  169. if (parent != NULL)
  170. parentHWnd = ((WinBFWindow*) parent)->mHWnd;
  171. if (mMenu != NULL)
  172. {
  173. WinBFMenu* placeholderMenu = (WinBFMenu*) AddMenuItem(mMenu, 0, ": Placeholder Menu Item :", NULL, NULL, false, -1, false);
  174. placeholderMenu->mIsPlaceholder = true;
  175. }
  176. mHWnd = CreateWindowExW(windowFlagsEx, L"BFWindow", UTF8Decode(title).c_str(),
  177. aWindowFlags,
  178. x, y,
  179. width,
  180. height,
  181. parentHWnd,
  182. (mMenu != NULL) ? ((WinBFMenu*) mMenu)->mMenu : NULL,
  183. hInstance,
  184. 0);
  185. if ((windowFlags & BFWINDOW_ALPHA_MASK) == 0)
  186. SetLayeredWindowAttributes(mHWnd, 0, 255, 0);
  187. HWND relativeWindow = NULL;
  188. if ((windowFlags & BFWINDOW_TOPMOST) && (parent == NULL))
  189. relativeWindow = HWND_TOP;
  190. int showFlags = SWP_SHOWWINDOW;
  191. // if (windowFlags & BFWINDOW_SHOWMINIMIZED)
  192. // showFlags = SWP_
  193. mHasFocus = true;
  194. mSoftHasFocus = true;
  195. if (windowFlags & BFWINDOW_FAKEFOCUS)
  196. {
  197. showFlags |= SWP_NOACTIVATE;
  198. if (windowFlags & BFWINDOW_NO_ACTIVATE)
  199. {
  200. mHasFocus = false;
  201. mSoftHasFocus = false;
  202. }
  203. }
  204. else if (windowFlags & BFWINDOW_NO_ACTIVATE)
  205. {
  206. showFlags |= SWP_NOACTIVATE;
  207. mHasFocus = false;
  208. mSoftHasFocus = false;
  209. }
  210. if (windowFlags & (BFWINDOW_SHOWMINIMIZED | BFWINDOW_SHOWMAXIMIZED))
  211. {
  212. WINDOWPLACEMENT wndPlacement = { sizeof(WINDOWPLACEMENT), 0 };
  213. ::GetWindowPlacement(mHWnd, &wndPlacement);
  214. if (windowFlags & BFWINDOW_SHOWMINIMIZED)
  215. wndPlacement.showCmd = SW_SHOWMINIMIZED;
  216. else if (windowFlags & BFWINDOW_SHOWMAXIMIZED)
  217. wndPlacement.showCmd = SW_SHOWMAXIMIZED;
  218. else
  219. wndPlacement.showCmd = SW_SHOWNORMAL;
  220. wndPlacement.rcNormalPosition.left = x;
  221. wndPlacement.rcNormalPosition.top = y;
  222. wndPlacement.rcNormalPosition.right = x + width;
  223. wndPlacement.rcNormalPosition.bottom = y + height;
  224. ::SetWindowPlacement(mHWnd, &wndPlacement);
  225. }
  226. else
  227. {
  228. SetWindowPos(mHWnd, relativeWindow, x, y, width, height, showFlags);
  229. }
  230. SetTimer(mHWnd, 0, 10, NULL);
  231. mIsMouseInside = false;
  232. mRenderWindow = new DXRenderWindow((DXRenderDevice*) gBFApp->mRenderDevice, this, (windowFlags & BFWINDOW_FULLSCREEN) == 0);
  233. gBFApp->mRenderDevice->AddRenderWindow(mRenderWindow);
  234. SetWindowLongPtr(mHWnd, GWLP_USERDATA, (LONG_PTR)this);
  235. mIsMenuKeyHandled = false;
  236. mAlphaMaskBitmap = NULL;
  237. mAlphaMaskDC = NULL;
  238. mAlphaMaskPixels = NULL;
  239. mAlphaMaskWidth = 0;
  240. mAlphaMaskHeight = 0;
  241. mNeedsStateReset = false;
  242. mAwaitKeyReleases = false;
  243. mAwaitKeyReleasesEventTick = 0;
  244. mAwaitKeyReleasesCheckIdx = 0;
  245. mFocusLostTick = ::GetTickCount();
  246. if (windowFlags & BFWINDOW_DEST_ALPHA)
  247. {
  248. MARGINS dWMMargins = {-1, -1, -1, -1};
  249. DwmExtendFrameIntoClientArea(mHWnd, &dWMMargins);
  250. }
  251. if (windowFlags & BFWINDOW_MODAL)
  252. {
  253. EnableWindow(parentHWnd, FALSE);
  254. }
  255. if (parent != NULL)
  256. {
  257. auto winParent = (WinBFWindow*)parent;
  258. BF_ASSERT(winParent->mHWnd);
  259. parent->mChildren.push_back(this);
  260. }
  261. HKL layout = GetKeyboardLayout(0);
  262. mKeyLayoutHasAltGr = (KeyboardLayoutHasAltGr(layout) == TRUE);
  263. }
  264. WinBFWindow::~WinBFWindow()
  265. {
  266. //OutputDebugStrF("Wnd %p Destroyed\n", this);
  267. if (mHWnd != NULL)
  268. Destroy();
  269. }
  270. void* WinBFWindow::GetUnderlying()
  271. {
  272. return mHWnd;
  273. }
  274. void WinBFWindow::Destroy()
  275. {
  276. if (mAlphaMaskDC != NULL)
  277. DeleteDC(mAlphaMaskDC);
  278. mAlphaMaskDC = NULL;
  279. if (mAlphaMaskBitmap != NULL)
  280. DeleteObject(mAlphaMaskBitmap);
  281. mAlphaMaskBitmap = NULL;
  282. for (auto& menu : mMenuIDMap)
  283. {
  284. delete menu.mValue;
  285. }
  286. mMenuIDMap.Clear();
  287. ::DestroyWindow(mHWnd);
  288. BF_ASSERT(mHWnd == NULL);
  289. }
  290. bool WinBFWindow::TryClose()
  291. {
  292. SendMessage(mHWnd, WM_CLOSE, 0, 0);
  293. return mHWnd == NULL;
  294. }
  295. void WinBFWindow::SetTitle(const char* title)
  296. {
  297. SetWindowTextA(mHWnd, title);
  298. }
  299. void WinBFWindow::LostFocus(BFWindow* newFocus)
  300. {
  301. ///OutputDebugStrF("Lost focus\n");
  302. mFocusLostTick = ::GetTickCount();
  303. WinBFWindow* bfNewFocus = (WinBFWindow*)newFocus;
  304. mSoftHasFocus = false;
  305. for (int i = 0; i < KEYCODE_MAX; i++)
  306. {
  307. // Only transfer mode keys
  308. if (mIsKeyDown[i])
  309. {
  310. mIsKeyDown[i] = false;
  311. mKeyUpFunc(this, i);
  312. if ((newFocus != NULL) &&
  313. ((i == VK_SHIFT) || (i == VK_CONTROL) || (i == VK_MENU)))
  314. {
  315. newFocus->mIsKeyDown[i] = true;
  316. newFocus->mKeyDownFunc(newFocus, i, 0);
  317. }
  318. }
  319. }
  320. }
  321. void WinBFWindow::GotFocus()
  322. {
  323. DWORD tickNow = ::GetTickCount();
  324. //OutputDebugStrF("GotFocus since lost %d\n", tickNow - mFocusLostTick);
  325. if (tickNow - mFocusLostTick >= 1000)
  326. {
  327. mAwaitKeyReleases = true;
  328. mAwaitKeyReleasesCheckIdx = 0;
  329. mAwaitKeyReleasesEventTick = ::GetTickCount();
  330. }
  331. }
  332. void WinBFWindow::SetForeground()
  333. {
  334. bool hadFocus = mHasFocus;
  335. DWORD prevFocusLostTick = mFocusLostTick;
  336. if (mFlags & BFWINDOW_FAKEFOCUS)
  337. {
  338. mHasFocus = true;
  339. mSoftHasFocus = true;
  340. return;
  341. }
  342. ::SetFocus(mHWnd);
  343. ::SetForegroundWindow(mHWnd);
  344. //OutputDebugStrF("SetForeground %p %d %d %d\n", mHWnd, hadFocus, ::GetTickCount() - prevFocusLostTick, mAwaitKeyReleases);
  345. }
  346. static POINT gLastScreenMouseCoords = { -1, -1 };
  347. void WinBFWindow::RehupMouseOver(bool isMouseOver)
  348. {
  349. if ((!mIsMouseInside) && (isMouseOver))
  350. {
  351. TRACKMOUSEEVENT tme;
  352. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  353. tme.dwFlags = TME_LEAVE;
  354. tme.hwndTrack = mHWnd;
  355. TrackMouseEvent(&tme);
  356. mIsMouseInside = true;
  357. }
  358. if ((mIsMouseInside) && (!isMouseOver))
  359. {
  360. mIsMouseInside = false;
  361. mMouseLeaveFunc(this);
  362. }
  363. }
  364. bool WinBFWindow::CheckKeyReleases(bool isKeyDown)
  365. {
  366. if (!mHasFocus)
  367. GotFocus();
  368. if (!mAwaitKeyReleases)
  369. return true;
  370. // Time expired with no key presses
  371. if ((mAwaitKeyReleasesEventTick != 0) && (mAwaitKeyReleasesCheckIdx == 0) && (::GetTickCount() - mAwaitKeyReleasesEventTick > 150))
  372. {
  373. //OutputDebugStrF("CheckKeyReleases no initial key press\n");
  374. mAwaitKeyReleases = false;
  375. return true;
  376. }
  377. mAwaitKeyReleasesCheckIdx++;
  378. bool hasKeyDown = false;
  379. uint8 keysDown[256] = { 0 };
  380. ::GetKeyboardState((PBYTE)&keysDown);
  381. for (int i = 1; i < 256; i++)
  382. if (keysDown[i] & 0x80)
  383. hasKeyDown = true;
  384. if ((hasKeyDown) && (::GetTickCount() - mAwaitKeyReleasesEventTick >= 600))
  385. {
  386. // String dbgStr = "CheckKeyReleases timeout. Keys down:";
  387. // for (int i = 1; i < 256; i++)
  388. // if (keysDown[i] & 0x80)
  389. // dbgStr += StrFormat(" %2X", i);
  390. // dbgStr += "\n";
  391. // OutputDebugStr(dbgStr);
  392. hasKeyDown = false;
  393. }
  394. if (!hasKeyDown)
  395. {
  396. mAwaitKeyReleases = false;
  397. mAwaitKeyReleasesCheckIdx = 0;
  398. mAwaitKeyReleasesEventTick = 0;
  399. }
  400. return !mAwaitKeyReleases;
  401. }
  402. LRESULT WinBFWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  403. {
  404. WinBFApp* app = (WinBFApp*) gBFApp;
  405. if (app == NULL)
  406. {
  407. PrintStuff();
  408. }
  409. switch (uMsg)
  410. {
  411. case WM_KEYDOWN:
  412. case WM_KEYUP:
  413. case WM_SYSKEYDOWN:
  414. case WM_CHAR:
  415. for (auto child : mChildren)
  416. {
  417. auto childWindow = (WinBFWindow*)child;
  418. if ((childWindow->mSoftHasFocus) && (childWindow->mFlags & BFWINDOW_FAKEFOCUS))
  419. {
  420. return childWindow->WindowProc(hWnd, uMsg, wParam, lParam);
  421. }
  422. }
  423. break;
  424. }
  425. switch (uMsg)
  426. {
  427. case WM_CLOSE:
  428. {
  429. //OutputDebugStrF("WM_CLOSE %08X NewFocus:%08X\n", hWnd, GetFocus());
  430. if (mCloseQueryFunc(this) != 0)
  431. gBFApp->RemoveWindow(this);
  432. HWND newFocus = GetFocus();
  433. for (auto window : gBFApp->mWindowList)
  434. {
  435. WinBFWindow* winWindow = (WinBFWindow*)window;
  436. if (winWindow->mHWnd == newFocus)
  437. {
  438. while (true)
  439. {
  440. WinBFWindow* altFocusWindow = NULL;
  441. for (auto checkChild : winWindow->mChildren)
  442. {
  443. auto checkWinChild = (WinBFWindow*)checkChild;
  444. if (checkWinChild->mFlags & BFWINDOW_FAKEFOCUS)
  445. altFocusWindow = checkWinChild;
  446. }
  447. if (altFocusWindow == NULL)
  448. break;
  449. winWindow = altFocusWindow;
  450. }
  451. winWindow->mHasFocus = true;
  452. winWindow->mSoftHasFocus = true;
  453. winWindow->mGotFocusFunc(winWindow);
  454. }
  455. }
  456. return 0;
  457. }
  458. break;
  459. case WM_DESTROY:
  460. /*if (mFlags & BFWINDOW_QUIT_ON_CLOSE)
  461. {
  462. gBFApp->mRunning = false;
  463. }*/
  464. SetWindowLongPtr(mHWnd, GWLP_USERDATA, (LONG_PTR)0);
  465. mHWnd = NULL;
  466. if (!mChildren.IsEmpty())
  467. {
  468. NOP;
  469. }
  470. break;
  471. }
  472. LRESULT result = 0;
  473. bool doResult = false;
  474. if (!app->mInMsgProc)
  475. {
  476. if (mNeedsStateReset)
  477. {
  478. for (int i = 0; i < KEYCODE_MAX; i++)
  479. {
  480. if (mIsKeyDown[i])
  481. {
  482. mIsKeyDown[i] = false;
  483. mKeyUpFunc(this, i);
  484. }
  485. }
  486. POINT mousePoint;
  487. ::GetCursorPos(&mousePoint);
  488. ::ScreenToClient(hWnd, &mousePoint);
  489. for (int i = 0; i < MOUSEBUTTON_MAX; i++)
  490. {
  491. if (mIsMouseDown[i])
  492. {
  493. mMouseUpFunc(this, mousePoint.x, mousePoint.y, i);
  494. mIsMouseDown[i] = false;
  495. //OutputDebugStrF("Wnd %p mNeedsStateReset MouseUp %d\n", this, i);
  496. }
  497. }
  498. //OutputDebugStrF("Rehup ReleaseCapture()\n");
  499. ReleaseCapture();
  500. mNeedsStateReset = false;
  501. mIsMouseInside = false;
  502. }
  503. WinBFWindow* menuTarget = this;
  504. if (mFlags & BFWINDOW_USE_PARENT_MENU)
  505. menuTarget = ((WinBFWindow*)mParent);
  506. auto* menuIDMap = &menuTarget->mMenuIDMap;
  507. auto* hMenuMap = &menuTarget->mHMenuMap;
  508. app->mInMsgProc = true;
  509. switch (uMsg)
  510. {
  511. case WM_DISPLAYCHANGE:
  512. ((DXRenderWindow*)mRenderWindow)->mRefreshRate = 0;
  513. break;
  514. case WM_SIZE:
  515. mRenderWindow->Resized();
  516. if (mMovedFunc != NULL)
  517. mMovedFunc(this);
  518. break;
  519. case WM_PAINT:
  520. break;
  521. case WM_NCHITTEST:
  522. {
  523. //OutputDebugStrF("WM_NCHITTEST %X\n", mHWnd);
  524. int x = (short)LOWORD(lParam);
  525. int y = (short)HIWORD(lParam);
  526. result = mHitTestFunc(this, x, y);
  527. doResult = (result != -3);
  528. }
  529. break;
  530. case WM_LBUTTONDOWN:
  531. case WM_RBUTTONDOWN:
  532. case WM_MBUTTONDOWN:
  533. case WM_XBUTTONDOWN:
  534. case WM_LBUTTONDBLCLK:
  535. case WM_RBUTTONDBLCLK:
  536. case WM_LBUTTONUP:
  537. case WM_RBUTTONUP:
  538. case WM_MBUTTONUP:
  539. case WM_XBUTTONUP:
  540. case WM_MOUSEWHEEL:
  541. case WM_MOUSEHWHEEL:
  542. case WM_MOUSEMOVE:
  543. {
  544. int x = (short)LOWORD(lParam);
  545. int y = (short)HIWORD(lParam);
  546. bool releaseCapture = false;
  547. POINT point = {x, y};
  548. if ((uMsg != WM_MOUSEWHEEL) && (uMsg != WM_MOUSEHWHEEL))
  549. ::ClientToScreen(hWnd, &point);
  550. if ((uMsg == WM_MOUSEMOVE) && (point.x == gLastScreenMouseCoords.x) && (point.y == gLastScreenMouseCoords.y))
  551. {
  552. // Don't process a WM_MOUSEMOVE if it's at the same point at the last mouse event
  553. // This keeps us from getting a WM_MOUSEMOVE when we popup a window under the current cursor location.
  554. // This is important for keyboard cursor control - so the first down arrow keypress selects the first item in the list
  555. // irregardless of the mouse cursor position.
  556. break;
  557. }
  558. gLastScreenMouseCoords.x = point.x;
  559. gLastScreenMouseCoords.y = point.y;
  560. HWND windowAtPoint = ::WindowFromPoint(point);
  561. bool isMouseOver = windowAtPoint == hWnd;
  562. RehupMouseOver(isMouseOver);
  563. //OutputDebugStrF("HWnd: %X Focus Window: %X Capture: %X\n", hWnd, windowAtPoint, ::GetCapture());
  564. bool checkNonTransparentMousePosition = mNonExclusiveMouseCapture;
  565. auto _BtnDown = [&](int btn)
  566. {
  567. BF_ASSERT(btn < MOUSEBUTTON_MAX);
  568. if (btn >= MOUSEBUTTON_MAX)
  569. return;
  570. //OutputDebugStrF("Wnd %p BtnDown %d\n", this, btn);
  571. DWORD tickNow = BFTickCount();
  572. if (::SetCapture(hWnd) != hWnd)
  573. {
  574. // Not captured, no buttons were down
  575. for (int i = 0; i < MOUSEBUTTON_MAX; i++)
  576. {
  577. if (mIsMouseDown[i])
  578. {
  579. mMouseUpFunc(this, x, y, i);
  580. mIsMouseDown[i] = false;
  581. //OutputDebugStrF("Wnd %p BtnDown MouseUp %d\n", this, i);
  582. }
  583. }
  584. }
  585. mIsMouseDown[btn] = true;
  586. BFCoord mouseCoords = { x, y };
  587. if ((mouseCoords.mX != mMouseDownCoords[btn].mX) || (mouseCoords.mY != mMouseDownCoords[btn].mY) ||
  588. (tickNow - mMouseDownTicks[btn] > ::GetDoubleClickTime()))
  589. mMouseClickCount[btn] = 0;
  590. mMouseDownCoords[btn] = mouseCoords;
  591. mMouseClickCount[btn]++;
  592. mMouseDownTicks[btn] = tickNow;
  593. mMouseDownFunc(this, x, y, btn, mMouseClickCount[btn]);
  594. };
  595. auto _BtnUp = [&](int btn)
  596. {
  597. BF_ASSERT(btn < MOUSEBUTTON_MAX);
  598. if (btn >= MOUSEBUTTON_MAX)
  599. return;
  600. //OutputDebugStrF("Wnd %p BtnUp %d\n", this, btn);
  601. releaseCapture = true;
  602. mIsMouseDown[btn] = false;
  603. mMouseUpFunc(this, x, y, btn);
  604. };
  605. switch (uMsg)
  606. {
  607. case WM_LBUTTONDOWN:
  608. //OutputDebugStrF("WM_LBUTTONDOWN Capture HWnd: %X\n", hWnd);
  609. _BtnDown(0);
  610. break;
  611. case WM_RBUTTONDOWN:
  612. //OutputDebugStrF("WM_RBUTTONDOWN Capture HWnd: %X\n", hWnd);
  613. _BtnDown(1);
  614. break;
  615. case WM_MBUTTONDOWN:
  616. _BtnDown(2);
  617. break;
  618. case WM_XBUTTONDOWN:
  619. _BtnDown((int)(wParam >> 16) + 2);
  620. break;
  621. case WM_LBUTTONUP:
  622. _BtnUp(0);
  623. break;
  624. case WM_RBUTTONUP:
  625. _BtnUp(1);
  626. break;
  627. case WM_MBUTTONUP:
  628. _BtnUp(2);
  629. break;
  630. case WM_XBUTTONUP:
  631. _BtnUp((int)(wParam >> 16) + 2);
  632. break;
  633. case WM_MOUSEWHEEL:
  634. case WM_MOUSEHWHEEL:
  635. {
  636. WinBFWindow* cursorWindow = this;
  637. if ((gBFApp->mWindowList.size() > 1) && (GetCapture() == NULL))
  638. {
  639. // See if our mouse is down and has entered into another window's space
  640. POINT point = { x, y };
  641. HWND windowAtPoint = ::WindowFromPoint(point);
  642. BFWindowList::iterator itr = gBFApp->mWindowList.begin();
  643. while (itr != gBFApp->mWindowList.end())
  644. {
  645. WinBFWindow* aWindow = (WinBFWindow*) *itr;
  646. LONG targetStyle = ::GetWindowLong(aWindow->mHWnd, GWL_EXSTYLE);
  647. if ((::IsWindowEnabled(aWindow->mHWnd)) && ((targetStyle & WS_EX_TRANSPARENT) == 0))
  648. {
  649. if (aWindow->mHWnd == windowAtPoint)
  650. {
  651. aWindow->mIsMouseInside = true;
  652. cursorWindow = aWindow;
  653. }
  654. }
  655. ++itr;
  656. }
  657. }
  658. UINT ucNumLines = 0;
  659. SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &ucNumLines, 0);
  660. if (ucNumLines == 0)
  661. ucNumLines = 3; // Default
  662. if ((cursorWindow != this) && (mIsMouseInside))
  663. {
  664. mMouseLeaveFunc(this);
  665. mIsMouseInside = false;
  666. }
  667. POINT pt = {x, y};
  668. ScreenToClient(cursorWindow->mHWnd, &pt);
  669. if (uMsg == WM_MOUSEWHEEL)
  670. {
  671. float delta = ((int16)HIWORD(wParam)) / 120.0f * (float)ucNumLines;
  672. mMouseWheelFunc(cursorWindow, pt.x, pt.y, 0, delta);
  673. }
  674. else
  675. {
  676. float delta = ((int16)HIWORD(wParam)) / 120.0f;
  677. mMouseWheelFunc(cursorWindow, pt.x, pt.y, delta, 0);
  678. }
  679. }
  680. break;
  681. case WM_MOUSEMOVE:
  682. {
  683. //OutputDebugStrF("WM_MOUSEMOVE %d\n", hWnd);
  684. mMouseMoveFunc(this, x, y);
  685. // If we are dragging a transparent window then check for mouse positions under cursor
  686. HWND captureWindow = GetCapture();
  687. if (captureWindow != NULL)
  688. {
  689. LONG captureStyle = ::GetWindowLong(captureWindow, GWL_EXSTYLE);
  690. if ((captureStyle & WS_EX_TRANSPARENT) != 0)
  691. checkNonTransparentMousePosition = true;
  692. }
  693. }
  694. break;
  695. }
  696. if ((checkNonTransparentMousePosition) && (gBFApp->mWindowList.size() > 1))
  697. {
  698. // See if our mouse is down and has entered into another window's space
  699. POINT point = { x, y };
  700. ::ClientToScreen(hWnd, &point);
  701. HWND windowAtPoint = ::WindowFromPoint(point);
  702. BFWindowList::iterator itr = gBFApp->mWindowList.begin();
  703. while (itr != gBFApp->mWindowList.end())
  704. {
  705. WinBFWindow* aWindow = (WinBFWindow*) *itr;
  706. if (aWindow != this)
  707. {
  708. LONG myStyle = ::GetWindowLong(mHWnd, GWL_EXSTYLE);
  709. LONG targetStyle = ::GetWindowLong(aWindow->mHWnd, GWL_EXSTYLE);
  710. if ((targetStyle & WS_EX_TRANSPARENT) == 0)
  711. {
  712. if (aWindow->mHWnd == windowAtPoint)
  713. {
  714. POINT clientPt = point;
  715. ::ScreenToClient(aWindow->mHWnd, &clientPt);
  716. aWindow->mMouseProxyMoveFunc(aWindow, clientPt.x, clientPt.y);
  717. aWindow->mIsMouseInside = true;
  718. }
  719. else if (aWindow->mIsMouseInside)
  720. {
  721. aWindow->mMouseLeaveFunc(aWindow);
  722. aWindow->mIsMouseInside = false;
  723. }
  724. }
  725. }
  726. ++itr;
  727. }
  728. }
  729. if (releaseCapture)
  730. {
  731. for (int i = 0; i < MOUSEBUTTON_MAX; i++)
  732. if (mIsMouseDown[i])
  733. releaseCapture = false;
  734. }
  735. if (releaseCapture)
  736. {
  737. //OutputDebugStrF("Wnd %p Release Capture\n", this);
  738. //OutputDebugStrF("ReleaseCapture\n");
  739. ReleaseCapture();
  740. BFWindowList::iterator itr = gBFApp->mWindowList.begin();
  741. while (itr != gBFApp->mWindowList.end())
  742. {
  743. WinBFWindow* aWindow = (WinBFWindow*) *itr;
  744. if ((aWindow != this) && (aWindow->mIsMouseInside))
  745. {
  746. aWindow->mMouseLeaveFunc(aWindow);
  747. aWindow->mIsMouseInside = false;
  748. }
  749. ++itr;
  750. }
  751. }
  752. }
  753. break;
  754. case WM_COMMAND:
  755. {
  756. WinBFMenu* aMenu = (*menuIDMap)[(uint32)wParam];
  757. if (aMenu != NULL)
  758. menuTarget->mMenuItemSelectedFunc(menuTarget, aMenu);
  759. }
  760. break;
  761. case WM_APPCOMMAND:
  762. {
  763. if ((mFlags & BFWINDOW_CAPTURE_MEDIA_KEYS) != 0)
  764. {
  765. int cmd = GET_APPCOMMAND_LPARAM(lParam);
  766. int uDevice = GET_DEVICE_LPARAM(lParam);
  767. int dwKeys = GET_KEYSTATE_LPARAM(lParam);
  768. int key = cmd | 0x1000;
  769. mKeyDownFunc(this, key, false);
  770. result = TRUE;
  771. doResult = true;
  772. }
  773. }
  774. break;
  775. case WM_INITMENUPOPUP:
  776. {
  777. if (mIsKeyDown[VK_MENU])
  778. {
  779. mKeyUpFunc(this, (int)VK_MENU);
  780. mIsKeyDown[VK_MENU] = false;
  781. }
  782. HMENU hMenu = (HMENU) wParam;
  783. WinBFMenu* aMenu = (*hMenuMap)[hMenu];
  784. if (aMenu != NULL)
  785. menuTarget->mMenuItemSelectedFunc(menuTarget, aMenu);
  786. }
  787. break;
  788. case WM_MOUSEACTIVATE:
  789. if (mFlags & BFWINDOW_NO_MOUSE_ACTIVATE)
  790. {
  791. doResult = true;
  792. result = MA_NOACTIVATE;
  793. }
  794. else if (mFlags & BFWINDOW_FAKEFOCUS)
  795. {
  796. doResult = true;
  797. result = MA_NOACTIVATE;
  798. SetForeground();
  799. }
  800. break;
  801. case WM_ACTIVATE:
  802. //OutputDebugStrF("WM_ACTIVATE %p\n", hWnd);
  803. break;
  804. case WM_KILLFOCUS:
  805. //OutputDebugStrF("WM_KILLFOCUS %p\n", hWnd);
  806. mHasFocus = false;
  807. mSoftHasFocus = false;
  808. LostFocus(NULL);
  809. mLostFocusFunc(this);
  810. break;
  811. case WM_SETFOCUS:
  812. //OutputDebugStrF("WM_SETFOCUS %p\n", hWnd);
  813. if (!mHasFocus)
  814. GotFocus();
  815. mHasFocus = true;
  816. mSoftHasFocus = true;
  817. mGotFocusFunc(this);
  818. break;
  819. case WM_ENTERMENULOOP:
  820. //OutputDebugStrF("WM_ENTERMENULOOP %08X\n", hWnd);
  821. if (mMenu != NULL)
  822. mNeedsStateReset = true;
  823. break;
  824. case WM_EXITMENULOOP:
  825. //OutputDebugStrF("WM_EXITMENULOOP %08X\n", hWnd);
  826. if (mMenu != NULL)
  827. mNeedsStateReset = true;
  828. break;
  829. case WM_NCMOUSELEAVE:
  830. mIsMouseInside = false;
  831. mMouseLeaveFunc(this);
  832. break;
  833. case WM_CHAR:
  834. {
  835. /*if (wParam == 'z')
  836. {
  837. ID3D11Debug* pD3DDebug = NULL;
  838. HRESULT hr = ((DXRenderDevice*)mRenderWindow->mRenderDevice)->mD3DDevice->QueryInterface(__uuidof(ID3D11Debug), (void**)&pD3DDebug);
  839. if (pD3DDebug != NULL)
  840. {
  841. pD3DDebug->ReportLiveDeviceObjects(D3D11_RLDO_DETAIL);
  842. pD3DDebug->Release();
  843. }
  844. }*/
  845. //NOTE: This line broke Alt+Gr for braces and such. Determine why this was needed.
  846. //if ((!mIsKeyDown[VK_MENU]) && (!mIsKeyDown[VK_CONTROL]))
  847. if (CheckKeyReleases(true))
  848. {
  849. for (int i = 0; i < (lParam & 0x7FFF); i++)
  850. mKeyCharFunc(this, (WCHAR)wParam);
  851. }
  852. }
  853. break;
  854. case WM_MENUCHAR:
  855. if (mIsMenuKeyHandled)
  856. {
  857. result = MNC_CLOSE << 16;
  858. doResult = true;
  859. }
  860. break;
  861. case WM_SYSKEYDOWN:
  862. case WM_KEYDOWN:
  863. {
  864. mIsMenuKeyHandled = false;
  865. int keyCode = (int) wParam;
  866. if (keyCode == VK_APPS)
  867. break; // This is handled in WM_CONTEXTMENU
  868. if ((mKeyLayoutHasAltGr) && (keyCode == VK_MENU) && ((lParam & 0x01000000) != 0))
  869. keyCode = VK_RMENU;
  870. mIsKeyDown[keyCode] = true;
  871. // if ((keyCode == 192) && (mIsKeyDown[VK_MENU]))
  872. // {
  873. // ((DXRenderDevice*)mRenderWindow->mRenderDevice)->mNeedsReinitNative = true;
  874. // }
  875. for (auto kv : *menuIDMap)
  876. {
  877. WinBFMenu* aMenu = kv.mValue;
  878. if ((aMenu->mKeyCode == keyCode) &&
  879. (aMenu->mKeyShift == mIsKeyDown[VK_SHIFT]) &&
  880. (aMenu->mKeyCtrl == mIsKeyDown[VK_CONTROL]) &&
  881. (aMenu->mKeyAlt == mIsKeyDown[VK_MENU]))
  882. {
  883. mIsMenuKeyHandled = true;
  884. menuTarget->mMenuItemSelectedFunc(menuTarget, aMenu);
  885. doResult = true;
  886. break;
  887. }
  888. }
  889. if (!mIsMenuKeyHandled)
  890. {
  891. if ((CheckKeyReleases(true)) && (mKeyDownFunc(this, keyCode, (lParam & 0x7FFF) != 0)))
  892. {
  893. mIsMenuKeyHandled = true;
  894. doResult = true;
  895. }
  896. }
  897. }
  898. break;
  899. case WM_SYSCHAR:
  900. {
  901. int keyCode = toupper((int) wParam);
  902. for (auto& menuKV : *menuIDMap)
  903. {
  904. WinBFMenu* aMenu = menuKV.mValue;
  905. if ((aMenu->mKeyCode == keyCode) &&
  906. (aMenu->mKeyShift == mIsKeyDown[VK_SHIFT]) &&
  907. (aMenu->mKeyCtrl == mIsKeyDown[VK_CONTROL]) &&
  908. (aMenu->mKeyAlt == mIsKeyDown[VK_MENU]))
  909. {
  910. if (CheckKeyReleases(true))
  911. doResult = true;
  912. break;
  913. }
  914. }
  915. if (!mIsKeyDown[VK_MENU])
  916. {
  917. // If we don't have the alt key down then we assume we must have
  918. // had keyups forced by losing focus from the mMenuItemSelectedFunc
  919. doResult = true;
  920. }
  921. }
  922. break;
  923. case WM_SYSKEYUP:
  924. case WM_KEYUP:
  925. {
  926. int keyCode = (int) wParam;
  927. if ((mKeyLayoutHasAltGr) && (keyCode == VK_MENU) && ((lParam & 0x01000000) != 0))
  928. keyCode = VK_RMENU;
  929. if (mIsKeyDown[keyCode])
  930. {
  931. mKeyUpFunc(this, keyCode);
  932. mIsKeyDown[keyCode] = false;
  933. }
  934. CheckKeyReleases(false);
  935. }
  936. break;
  937. case WM_SYSCOMMAND:
  938. // Ignore F10
  939. if ((wParam == SC_KEYMENU) && (lParam == 0))
  940. {
  941. doResult = true;
  942. result = 0;
  943. }
  944. break;
  945. case WM_CONTEXTMENU:
  946. {
  947. int x = (short)LOWORD(lParam);
  948. int y = (short)HIWORD(lParam);
  949. if ((x == -1) && (y == -1))
  950. {
  951. mKeyDownFunc(this, VK_APPS, false);
  952. mKeyUpFunc(this, VK_APPS);
  953. }
  954. }
  955. break;
  956. case WM_TIMER:
  957. if (gBFApp->mSysDialogCnt == 0)
  958. {
  959. auto checkNonFake = this;
  960. while (checkNonFake->mFlags & BFWINDOW_FAKEFOCUS)
  961. {
  962. checkNonFake = (WinBFWindow*)checkNonFake->mParent;
  963. }
  964. bool isFocused = GetForegroundWindow() == checkNonFake->mHWnd;
  965. if ((!isFocused) && (mHasFocus))
  966. {
  967. mSoftHasFocus = false;
  968. mHasFocus = false;
  969. LostFocus(NULL);
  970. mLostFocusFunc(this);
  971. //OutputDebugStrF("Timer detected lost focus %p\r\n", hWnd);
  972. }
  973. else if ((isFocused) && (!mHasFocus) && (checkNonFake == this))
  974. {
  975. //OutputDebugStrF("Timer detected got focus %p\r\n", hWnd);
  976. GotFocus();
  977. mHasFocus = true;
  978. mSoftHasFocus = true;
  979. mGotFocusFunc(this);
  980. }
  981. mSoftHasFocus = mHasFocus;
  982. gBFApp->Process();
  983. // Don't do anything with 'this' after Process, we may be deleted now
  984. doResult = true;
  985. result = 0;
  986. }
  987. break;
  988. case WM_SETCURSOR:
  989. gBFApp->PhysSetCursor();
  990. break;
  991. case WM_GETMINMAXINFO:
  992. {
  993. MINMAXINFO* minMaxInfo = (MINMAXINFO*)lParam;
  994. minMaxInfo->ptMinTrackSize.x = mMinWidth;
  995. minMaxInfo->ptMinTrackSize.y = mMinHeight;
  996. result = 0;
  997. doResult = true;
  998. }
  999. break;
  1000. case WM_MOVE:
  1001. case WM_MOVING:
  1002. if (mMovedFunc != NULL)
  1003. mMovedFunc(this);
  1004. break;
  1005. case WM_SIZING:
  1006. mRenderWindow->Resized();
  1007. if (mMovedFunc != NULL)
  1008. mMovedFunc(this);
  1009. if (gBFApp->mSysDialogCnt == 0)
  1010. gBFApp->Process();
  1011. break;
  1012. case WM_INPUTLANGCHANGE:
  1013. mKeyLayoutHasAltGr = (KeyboardLayoutHasAltGr((HKL)lParam) == TRUE);
  1014. break;
  1015. case WM_DROPFILES:
  1016. {
  1017. HDROP hDropInfo = (HDROP)wParam;
  1018. char sItem[MAX_PATH];
  1019. for(int i = 0; DragQueryFileA(hDropInfo, i, (LPSTR)sItem, sizeof(sItem)); i++)
  1020. mDragDropFileFunc(this, sItem);
  1021. DragFinish(hDropInfo);
  1022. }
  1023. break;
  1024. }
  1025. app->mInMsgProc = false;
  1026. }
  1027. else
  1028. {
  1029. // We got messages we couldn't process (due to reentrancy)
  1030. switch (uMsg)
  1031. {
  1032. case WM_LBUTTONUP:
  1033. case WM_RBUTTONUP:
  1034. case WM_MBUTTONUP:
  1035. case WM_MOUSEMOVE:
  1036. case WM_KEYUP:
  1037. case WM_MOUSELEAVE:
  1038. mNeedsStateReset = true;
  1039. break;
  1040. }
  1041. }
  1042. if (doResult)
  1043. return result;
  1044. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  1045. }
  1046. LRESULT CALLBACK WinBFWindow::WindowProcStub(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  1047. {
  1048. WinBFWindow* aWindow = (WinBFWindow*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
  1049. if (aWindow != NULL)
  1050. return aWindow->WindowProc(hWnd, Msg, wParam, lParam);
  1051. else
  1052. return DefWindowProc(hWnd, Msg, wParam, lParam);
  1053. }
  1054. //
  1055. static int WinBFReportHook( int reportType, char *message, int *returnValue )
  1056. {
  1057. if (reportType == 0)
  1058. return 0;
  1059. //__crtMessageWindowW(nRptType, szFile, (nLine ? szLineMessage : NULL), szModule, szUserMessage);;
  1060. if (gBFApp)
  1061. ((WinBFApp*) gBFApp)->mInMsgProc = true;
  1062. int nCode = ::MessageBoxA(NULL, message,
  1063. "Microsoft Visual C++ Debug Library",
  1064. MB_TASKMODAL|MB_ICONHAND|MB_ABORTRETRYIGNORE|MB_SETFOREGROUND);
  1065. if (gBFApp)
  1066. ((WinBFApp*) gBFApp)->mInMsgProc = false;
  1067. /* Abort: abort the program */
  1068. if (IDABORT == nCode)
  1069. {
  1070. /* note that it is better NOT to call abort() here, because the
  1071. * default implementation of abort() will call Watson
  1072. */
  1073. /* raise abort signal */
  1074. raise(SIGABRT);
  1075. /* We usually won't get here, but it's possible that
  1076. SIGABRT was ignored. So exit the program anyway. */
  1077. _exit(3);
  1078. }
  1079. /* Retry: return 1 to call the debugger */
  1080. if (IDRETRY == nCode)
  1081. return 1;
  1082. /* Ignore: continue execution */
  1083. return 0;
  1084. return 1;
  1085. }
  1086. extern HINSTANCE gDLLInstance;
  1087. typedef UINT(NTAPI *GetDpiForWindow_t)(HWND);
  1088. static GetDpiForWindow_t gGetDpiForWindow = NULL;
  1089. static HMODULE gUserDll = NULL;
  1090. WinBFApp::WinBFApp()
  1091. {
  1092. #ifndef BF_MINGW
  1093. //_CrtSetReportHook(WinBFReportHook);
  1094. #endif
  1095. if (gUserDll == NULL)
  1096. {
  1097. gUserDll = ::LoadLibraryA("user32.dll");
  1098. gGetDpiForWindow = (GetDpiForWindow_t)::GetProcAddress(gUserDll, "GetDpiForWindow");
  1099. }
  1100. mRunning = false;
  1101. mRenderDevice = NULL;
  1102. mInstallDir = "Hey";
  1103. WCHAR aStr[MAX_PATH];
  1104. GetModuleFileNameW(gDLLInstance, aStr, MAX_PATH);
  1105. mInstallDir = UTF8Encode(aStr);
  1106. int a2DArray[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
  1107. int lastSlash = (int) mInstallDir.LastIndexOf(L'\\');
  1108. if (lastSlash != -1)
  1109. mInstallDir = mInstallDir.Substring(0, lastSlash + 1);
  1110. mDataDir = mInstallDir;
  1111. mInMsgProc = false;
  1112. mDSoundManager = NULL;
  1113. mDInputManager = NULL;
  1114. mVSyncThreadId = 0;
  1115. mClosing = false;
  1116. mVSyncActive = false;
  1117. mVSyncThread = BfpThread_Create(VSyncThreadProcThunk, (void*)this, 128 * 1024, BfpThreadCreateFlag_StackSizeReserve, &mVSyncThreadId);
  1118. BfpThread_SetPriority(mVSyncThread, BfpThreadPriority_High, NULL);
  1119. }
  1120. void BFP_CALLTYPE WinBFApp::VSyncThreadProcThunk(void* ptr)
  1121. {
  1122. ((WinBFApp*)ptr)->VSyncThreadProc();
  1123. }
  1124. void WinBFApp::VSyncThreadProc()
  1125. {
  1126. while (!mClosing)
  1127. {
  1128. bool didWait = false;
  1129. IDXGIOutput* output = NULL;
  1130. //
  1131. {
  1132. AutoCrit autoCrit(mCritSect);
  1133. if ((mRenderDevice != NULL) && (!mRenderDevice->mRenderWindowList.IsEmpty()))
  1134. {
  1135. auto renderWindow = (DXRenderWindow*)mRenderDevice->mRenderWindowList[0];
  1136. renderWindow->mDXSwapChain->GetContainingOutput(&output);
  1137. }
  1138. }
  1139. if (output != NULL)
  1140. {
  1141. DWORD start = GetTickCount();
  1142. bool success = output->WaitForVBlank() == 0;
  1143. int elapsed = (int)(GetTickCount() - start);
  1144. if (elapsed >= 20)
  1145. {
  1146. NOP;
  1147. }
  1148. if (success)
  1149. {
  1150. didWait = true;
  1151. mVSyncActive = true;
  1152. mVSyncEvent.Set();
  1153. }
  1154. output->Release();
  1155. }
  1156. if (!didWait)
  1157. {
  1158. mVSyncActive = false;
  1159. BfpThread_Sleep(20);
  1160. }
  1161. }
  1162. }
  1163. WinBFApp::~WinBFApp()
  1164. {
  1165. mClosing = true;
  1166. BfpThread_WaitFor(mVSyncThread, -1);
  1167. BfpThread_Release(mVSyncThread);
  1168. delete mRenderDevice;
  1169. delete mDSoundManager;
  1170. delete mDInputManager;
  1171. }
  1172. void WinBFApp::Init()
  1173. {
  1174. BP_ZONE("WinBFApp::Init");
  1175. AutoCrit autoCrit(mCritSect);
  1176. mRunning = true;
  1177. mInMsgProc = false;
  1178. mRenderDevice = new DXRenderDevice();
  1179. mRenderDevice->Init(this);
  1180. }
  1181. void WinBFApp::Run()
  1182. {
  1183. MSG msg;
  1184. while (mRunning)
  1185. {
  1186. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  1187. {
  1188. TranslateMessage(&msg);
  1189. DispatchMessage(&msg);
  1190. }
  1191. if (mRunning)
  1192. Process();
  1193. }
  1194. }
  1195. void WinBFApp::Process()
  1196. {
  1197. BFApp::Process();
  1198. auto dxRenderDevice = (DXRenderDevice*)mRenderDevice;
  1199. if (dxRenderDevice->mNeedsReinitNative)
  1200. {
  1201. dxRenderDevice->mNeedsReinitNative = false;
  1202. dxRenderDevice->ReinitNative();
  1203. mForceNextDraw = true;
  1204. }
  1205. }
  1206. void WinBFApp::Draw()
  1207. {
  1208. mRenderDevice->FrameStart();
  1209. BFApp::Draw();
  1210. mRenderDevice->FrameEnd();
  1211. }
  1212. void WinBFApp::GetDesktopResolution(int& width, int& height)
  1213. {
  1214. width = ::GetSystemMetrics(SM_CXSCREEN);
  1215. height = ::GetSystemMetrics(SM_CYSCREEN);
  1216. }
  1217. static BOOL InflateRectToMonitor(HMONITOR mon, HDC hdc, LPRECT monRect, LPARAM userArg)
  1218. {
  1219. AdjustedMonRect* inflatedRect = (AdjustedMonRect*)userArg;
  1220. MONITORINFO monitorInfo = { sizeof(MONITORINFO) };
  1221. if (::GetMonitorInfo(mon, &monitorInfo) == 0)
  1222. return TRUE;
  1223. inflatedRect->mMonCount++;
  1224. if (inflatedRect->mMonCount == 1)
  1225. {
  1226. inflatedRect->mX = monitorInfo.rcWork.left;
  1227. inflatedRect->mY = monitorInfo.rcWork.top;
  1228. inflatedRect->mWidth = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  1229. inflatedRect->mHeight = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  1230. }
  1231. else
  1232. {
  1233. int minLeft = BF_MIN(inflatedRect->mX, monitorInfo.rcWork.left);
  1234. int minTop = BF_MIN(inflatedRect->mY, monitorInfo.rcWork.top);
  1235. int maxRight = BF_MAX(inflatedRect->mX + inflatedRect->mWidth, monitorInfo.rcWork.right);
  1236. int maxBottom = BF_MAX(inflatedRect->mY + inflatedRect->mHeight, monitorInfo.rcWork.bottom);
  1237. inflatedRect->mX = minLeft;
  1238. inflatedRect->mY = minTop;
  1239. inflatedRect->mWidth = maxRight - minLeft;
  1240. inflatedRect->mHeight = maxBottom - minTop;
  1241. }
  1242. return TRUE;
  1243. }
  1244. void WinBFApp::GetWorkspaceRect(int& x, int& y, int& width, int& height)
  1245. {
  1246. AdjustedMonRect inflateRect = { 0 };
  1247. EnumDisplayMonitors(NULL, NULL, InflateRectToMonitor, (LPARAM)&inflateRect);
  1248. x = inflateRect.mX;
  1249. y = inflateRect.mY;
  1250. width = inflateRect.mWidth;
  1251. height = inflateRect.mHeight;
  1252. }
  1253. void WinBFApp::GetWorkspaceRectFrom(int fromX, int fromY, int fromWidth, int fromHeight, int & outX, int & outY, int & outWidth, int & outHeight)
  1254. {
  1255. AdjustedMonRect inflateRect = { 0 };
  1256. RECT wantRect;
  1257. wantRect.left = fromX;
  1258. wantRect.top = fromY;
  1259. wantRect.right = fromX + BF_MAX(fromWidth, 1);
  1260. wantRect.bottom = fromY + BF_MAX(fromHeight, 1);
  1261. EnumDisplayMonitors(NULL, &wantRect, InflateRectToMonitor, (LPARAM)&inflateRect);
  1262. if (inflateRect.mMonCount == 0)
  1263. {
  1264. GetWorkspaceRect(outX, outY, outWidth, outHeight);
  1265. return;
  1266. }
  1267. outX = inflateRect.mX;
  1268. outY = inflateRect.mY;
  1269. outWidth = inflateRect.mWidth;
  1270. outHeight = inflateRect.mHeight;
  1271. }
  1272. BFWindow* WinBFApp::CreateNewWindow(BFWindow* parent, const StringImpl& title, int x, int y, int width, int height, int windowFlags)
  1273. {
  1274. AutoCrit autoCrit(mCritSect);
  1275. BFWindow* aWindow = new WinBFWindow(parent, title, x, y, width, height, windowFlags);
  1276. mWindowList.push_back(aWindow);
  1277. return aWindow;
  1278. }
  1279. void WinBFApp::RehupMouse()
  1280. {
  1281. HWND windowAtPoint = ::WindowFromPoint(gLastScreenMouseCoords);
  1282. for (auto window : mWindowList)
  1283. {
  1284. auto winWindow = (WinBFWindow*)window;
  1285. winWindow->RehupMouseOver(winWindow->mHWnd == windowAtPoint);
  1286. }
  1287. }
  1288. String WinBFApp::EnumerateInputDevices()
  1289. {
  1290. if (mDInputManager == NULL)
  1291. mDInputManager = new DInputManager();
  1292. return mDInputManager->EnumerateDevices();
  1293. }
  1294. BFInputDevice* WinBFApp::CreateInputDevice(const StringImpl& guid)
  1295. {
  1296. if (mDInputManager == NULL)
  1297. mDInputManager = new DInputManager();
  1298. return mDInputManager->CreateInputDevice(guid);
  1299. }
  1300. void WinBFWindow::SetMinimumSize(int minWidth, int minHeight, bool clientSized)
  1301. {
  1302. if (clientSized)
  1303. {
  1304. DWORD windowFlags = ::GetWindowLong(mHWnd, GWL_STYLE);
  1305. DWORD windowFlagsEx = ::GetWindowLong(mHWnd, GWL_EXSTYLE);
  1306. RECT rect = { 0, 0, minWidth, minHeight };
  1307. AdjustWindowRectEx(&rect, windowFlags, mMenu != NULL, windowFlagsEx);
  1308. minWidth = rect.right - rect.left;
  1309. minHeight = rect.bottom - rect.top;
  1310. }
  1311. mMinWidth = minWidth;
  1312. mMinHeight = minHeight;
  1313. if (mHWnd != NULL)
  1314. {
  1315. RECT windowRect;
  1316. ::GetWindowRect(mHWnd, &windowRect);
  1317. bool resized = false;
  1318. if (windowRect.right - windowRect.left < minWidth)
  1319. {
  1320. windowRect.right = windowRect.left + minWidth;
  1321. resized = true;
  1322. }
  1323. if (windowRect.bottom - windowRect.top < minHeight)
  1324. {
  1325. windowRect.bottom = windowRect.top + minHeight;
  1326. resized = true;
  1327. }
  1328. if (resized)
  1329. {
  1330. ::MoveWindow(mHWnd, windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, TRUE);
  1331. mRenderWindow->Resized();
  1332. if (mMovedFunc != NULL)
  1333. mMovedFunc(this);
  1334. }
  1335. }
  1336. }
  1337. void WinBFWindow::GetPosition(int* x, int* y, int* width, int* height, int* clientX, int* clientY, int* clientWidth, int* clientHeight)
  1338. {
  1339. RECT windowRect;
  1340. ::GetWindowRect(mHWnd, &windowRect);
  1341. RECT clientRect;
  1342. ::GetClientRect(mHWnd, &clientRect);
  1343. if (clientRect.right <= clientRect.left)
  1344. return; // TODO: return failure?
  1345. *x = windowRect.left;
  1346. *y = windowRect.top;
  1347. *width = windowRect.right - windowRect.left;
  1348. *height = windowRect.bottom - windowRect.top;
  1349. *clientWidth = clientRect.right - clientRect.left;
  1350. *clientHeight = clientRect.bottom - clientRect.top;
  1351. POINT startPt = {0, 0};
  1352. ::ClientToScreen(mHWnd, &startPt);
  1353. *clientX = startPt.x;
  1354. *clientY = startPt.y;
  1355. }
  1356. void WinBFWindow::GetPlacement(int* normX, int* normY, int* normWidth, int* normHeight, int* showKind)
  1357. {
  1358. WINDOWPLACEMENT wndPlacement = { sizeof(WINDOWPLACEMENT), 0 };
  1359. ::GetWindowPlacement(mHWnd, &wndPlacement);
  1360. *normX = wndPlacement.rcNormalPosition.left;
  1361. *normY = wndPlacement.rcNormalPosition.top;
  1362. *normWidth = wndPlacement.rcNormalPosition.right - wndPlacement.rcNormalPosition.left;
  1363. *normHeight = wndPlacement.rcNormalPosition.bottom - wndPlacement.rcNormalPosition.top;
  1364. switch (wndPlacement.showCmd)
  1365. {
  1366. case SW_SHOWMINIMIZED:
  1367. *showKind = 1;
  1368. break;
  1369. case SW_SHOWMAXIMIZED:
  1370. *showKind = 2;
  1371. break;
  1372. default:
  1373. *showKind = 0;
  1374. break;
  1375. }
  1376. }
  1377. void WinBFWindow::Resize(int x, int y, int width, int height, int showKind)
  1378. {
  1379. WINDOWPLACEMENT wndPlacement = { sizeof(WINDOWPLACEMENT), 0 };
  1380. ::GetWindowPlacement(mHWnd, &wndPlacement);
  1381. switch (showKind)
  1382. {
  1383. case 1:
  1384. wndPlacement.showCmd = SW_SHOWMINIMIZED;
  1385. break;
  1386. case 2:
  1387. wndPlacement.showCmd = SW_SHOWMAXIMIZED;
  1388. break;
  1389. case 3:
  1390. wndPlacement.showCmd = SW_SHOWNORMAL;
  1391. break;
  1392. }
  1393. wndPlacement.rcNormalPosition.left = x;
  1394. wndPlacement.rcNormalPosition.top = y;
  1395. wndPlacement.rcNormalPosition.right = x + width;
  1396. wndPlacement.rcNormalPosition.bottom = y + height;
  1397. ::SetWindowPlacement(mHWnd, &wndPlacement);
  1398. //::MoveWindow(mHWnd, x, y, width, height, FALSE);
  1399. mRenderWindow->Resized();
  1400. if (mMovedFunc != NULL)
  1401. mMovedFunc(this);
  1402. }
  1403. void WinBFApp::PhysSetCursor()
  1404. {
  1405. static HCURSOR cursors [] =
  1406. {
  1407. ::LoadCursor(NULL, IDC_ARROW),
  1408. //TODO: mApp->mHandCursor);
  1409. ::LoadCursor(NULL, IDC_HAND),
  1410. //TODO: mApp->mDraggingCursor);
  1411. ::LoadCursor(NULL, IDC_SIZEALL),
  1412. ::LoadCursor(NULL, IDC_IBEAM),
  1413. ::LoadCursor(NULL, IDC_NO),
  1414. ::LoadCursor(NULL, IDC_SIZEALL),
  1415. ::LoadCursor(NULL, IDC_SIZENESW),
  1416. ::LoadCursor(NULL, IDC_SIZENS),
  1417. ::LoadCursor(NULL, IDC_SIZENWSE),
  1418. ::LoadCursor(NULL, IDC_SIZEWE),
  1419. ::LoadCursor(NULL, IDC_WAIT),
  1420. NULL
  1421. };
  1422. ::SetCursor(cursors[mCursor]);
  1423. }
  1424. void WinBFWindow::SetClientPosition(int x, int y)
  1425. {
  1426. RECT rect;
  1427. ::GetClientRect(mHWnd, &rect);
  1428. ::OffsetRect(&rect, x, y);
  1429. LONG aStyle = ::GetWindowLong(mHWnd, GWL_STYLE);
  1430. LONG exStyle = ::GetWindowLong(mHWnd, GWL_EXSTYLE);
  1431. ::AdjustWindowRectEx(&rect, aStyle, mMenu != NULL, exStyle);
  1432. ::MoveWindow(mHWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, FALSE);
  1433. if (mMovedFunc != NULL)
  1434. mMovedFunc(this);
  1435. }
  1436. void WinBFWindow::SetMouseVisible(bool isMouseVisible)
  1437. {
  1438. mMouseVisible = isMouseVisible;
  1439. LONG aStyle = ::GetWindowLong(mHWnd, GWL_EXSTYLE);
  1440. if (!isMouseVisible)
  1441. aStyle |= WS_EX_TRANSPARENT;
  1442. else
  1443. aStyle &= ~WS_EX_TRANSPARENT;
  1444. ::SetWindowLong(mHWnd, GWL_EXSTYLE, aStyle);
  1445. }
  1446. void WinBFWindow::SetAlpha(float alpha, uint32 destAlphaSrcMask, bool isMouseVisible)
  1447. {
  1448. if (destAlphaSrcMask != 0)
  1449. {
  1450. if (mAlphaMaskBitmap == NULL)
  1451. {
  1452. RECT clientRect;
  1453. GetClientRect(mHWnd, &clientRect);
  1454. RECT windowRect;
  1455. GetWindowRect(mHWnd, &windowRect);
  1456. int aWidth = clientRect.right - clientRect.left;
  1457. int aHeight = clientRect.bottom - clientRect.top;
  1458. mAlphaMaskWidth = aWidth;
  1459. mAlphaMaskHeight = aHeight;
  1460. mAlphaMaskDC = CreateCompatibleDC(NULL);
  1461. BITMAPINFO bi = {};
  1462. bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1463. bi.bmiHeader.biBitCount = 32;
  1464. bi.bmiHeader.biWidth = aWidth;
  1465. bi.bmiHeader.biHeight = -aHeight;
  1466. bi.bmiHeader.biCompression = BI_RGB;
  1467. bi.bmiHeader.biPlanes = 1;
  1468. mAlphaMaskBitmap = CreateDIBSection(mAlphaMaskDC, &bi,
  1469. DIB_RGB_COLORS, (void**)&mAlphaMaskPixels, NULL, 0);
  1470. GdiFlush();
  1471. }
  1472. HDC hdc = GetDC(mHWnd);
  1473. if (hdc)
  1474. {
  1475. DXRenderWindow* renderWindow = (DXRenderWindow*) mRenderWindow;
  1476. renderWindow->CopyBitsTo(mAlphaMaskPixels, mAlphaMaskWidth, mAlphaMaskHeight);
  1477. //for (int i = 0; i < mAlphaMaskWidth*mAlphaMaskHeight/2; i++)
  1478. //mAlphaMaskPixels[i] = 0x80FF8000;
  1479. HGDIOBJ hPrevObj = 0;
  1480. POINT ptDest = {0, 0};
  1481. POINT ptSrc = {0, 0};
  1482. SIZE client = {mAlphaMaskWidth, mAlphaMaskHeight};
  1483. BLENDFUNCTION blendFunc = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
  1484. hPrevObj = SelectObject(mAlphaMaskDC, mAlphaMaskBitmap);
  1485. ClientToScreen(mHWnd, &ptDest);
  1486. BOOL worked = ::UpdateLayeredWindow(mHWnd, hdc, NULL, &client,
  1487. mAlphaMaskDC, &ptSrc, 0, &blendFunc, ULW_ALPHA);
  1488. DWORD error = GetLastError();
  1489. SelectObject(mAlphaMaskDC, hPrevObj);
  1490. ReleaseDC(mHWnd, hdc);
  1491. }
  1492. }
  1493. else
  1494. {
  1495. ::SetLayeredWindowAttributes(mHWnd, 0, (int) (alpha * 255), LWA_ALPHA);
  1496. }
  1497. SetMouseVisible(isMouseVisible);
  1498. }
  1499. void WinBFWindow::CaptureMouse()
  1500. {
  1501. //OutputDebugStrF("Wnd %p CaptureMouse", this);
  1502. ::SetCapture(mHWnd);
  1503. for (auto window : gBFApp->mWindowList)
  1504. {
  1505. if (window == this)
  1506. continue;
  1507. for (int i = 0; i < MOUSEBUTTON_MAX; i++)
  1508. {
  1509. if (window->mIsMouseDown[i])
  1510. {
  1511. window->mMouseUpFunc(window, -1, -1, i);
  1512. window->mIsMouseDown[i] = false;
  1513. }
  1514. }
  1515. }
  1516. }
  1517. bool WinBFWindow::IsMouseCaptured()
  1518. {
  1519. return (mHWnd != NULL) && (GetCapture() == mHWnd);
  1520. }
  1521. int WinBFWindow::GetDPI()
  1522. {
  1523. if (gGetDpiForWindow != NULL)
  1524. return (int)gGetDpiForWindow(mHWnd);
  1525. return 96; // Default DPI
  1526. }
  1527. uint32 WinBFApp::GetClipboardFormat(const StringImpl& format)
  1528. {
  1529. if (format == "text")
  1530. return CF_UNICODETEXT;
  1531. else if (format == "atext")
  1532. return CF_TEXT;
  1533. uint32 aFormat;
  1534. if (mClipboardFormatMap.TryGetValue(format, &aFormat))
  1535. return aFormat;
  1536. aFormat = ::RegisterClipboardFormatA(format.c_str());
  1537. mClipboardFormatMap[format] = aFormat;
  1538. return aFormat;
  1539. }
  1540. static String gClipboardData;
  1541. void* WinBFApp::GetClipboardData(const StringImpl& format, int* size)
  1542. {
  1543. HWND aWindow = NULL;
  1544. if (!mWindowList.empty())
  1545. aWindow = ((WinBFWindow*)mWindowList.front())->mHWnd;
  1546. uint32 aFormat = GetClipboardFormat(format);
  1547. if (aFormat != 0)
  1548. {
  1549. if (OpenClipboard(aWindow))
  1550. {
  1551. HGLOBAL globalHandle = ::GetClipboardData(aFormat);
  1552. if (globalHandle == NULL)
  1553. {
  1554. if (aFormat == CF_UNICODETEXT)
  1555. {
  1556. CloseClipboard();
  1557. // Return ascii text
  1558. return (char*)GetClipboardData("atext", size);
  1559. }
  1560. CloseClipboard();
  1561. *size = 0;
  1562. return NULL;
  1563. }
  1564. *size = (int)::GlobalSize(globalHandle);
  1565. void* aPtr = ::GlobalLock(globalHandle);
  1566. if (aFormat == CF_UNICODETEXT)
  1567. {
  1568. gClipboardData = UTF8Encode((WCHAR*)aPtr);
  1569. *size = (int)gClipboardData.length() + 1;
  1570. }
  1571. else
  1572. {
  1573. gClipboardData.Clear();
  1574. gClipboardData.Insert(0, (char*)aPtr, *size);
  1575. }
  1576. ::GlobalUnlock(globalHandle);
  1577. CloseClipboard();
  1578. return (void*)gClipboardData.c_str();
  1579. }
  1580. }
  1581. *size = 0;
  1582. return NULL;
  1583. }
  1584. void WinBFApp::ReleaseClipboardData(void* ptr)
  1585. {
  1586. }
  1587. void WinBFApp::SetClipboardData(const StringImpl& format, const void* ptr, int size, bool resetClipboard)
  1588. {
  1589. BP_ZONE("WinBFApp::SetClipboardData");
  1590. HWND aWindow = NULL;
  1591. if (!mWindowList.empty())
  1592. aWindow = ((WinBFWindow*) mWindowList.front())->mHWnd;
  1593. uint32 aFormat = GetClipboardFormat(format);
  1594. if (aFormat != 0)
  1595. {
  1596. if (OpenClipboard(aWindow))
  1597. {
  1598. if (resetClipboard)
  1599. {
  1600. BP_ZONE("WinBFApp::SetClipboardData:empty");
  1601. EmptyClipboard();
  1602. }
  1603. if (format == "text")
  1604. {
  1605. BP_ZONE("WinBFApp::SetClipboardData:text");
  1606. HGLOBAL globalHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size * 2);
  1607. char* data = (char*)GlobalLock(globalHandle);
  1608. UTF16String wString;
  1609. //
  1610. {
  1611. BP_ZONE("WinBFApp::SetClipboardData:utf8decode");
  1612. wString = UTF8Decode((char*)ptr);
  1613. }
  1614. memcpy(data, wString.c_str(), size * 2);
  1615. GlobalUnlock(globalHandle);
  1616. ::SetClipboardData(CF_UNICODETEXT, globalHandle);
  1617. }
  1618. else
  1619. {
  1620. HGLOBAL globalHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, size);
  1621. char* data = (char*)GlobalLock(globalHandle);
  1622. memcpy(data, ptr, size);
  1623. GlobalUnlock(globalHandle);
  1624. ::SetClipboardData(aFormat, globalHandle);
  1625. }
  1626. CloseClipboard();
  1627. }
  1628. }
  1629. }
  1630. BFMenu* WinBFWindow::AddMenuItem(BFMenu* parent, int insertIdx, const char* text, const char* hotKey, BFSysBitmap* sysBitmap, bool enabled, int checkState, bool radioCheck)
  1631. {
  1632. UTF16String lText;
  1633. if (text != NULL)
  1634. lText = UTF8Decode(text);
  1635. UTF16String lHotKey;
  1636. if (hotKey != NULL)
  1637. lHotKey = UTF8Decode(hotKey);
  1638. bool wasEmpty = mMenu->mBFMenuList.size() == 0;
  1639. if (parent == NULL)
  1640. parent = mMenu;
  1641. if ((parent->mBFMenuList.size() == 1) && (((WinBFMenu*) parent->mBFMenuList.front())->mIsPlaceholder))
  1642. {
  1643. // Get rid of placeholder menu item
  1644. auto placeholderMenuItem = parent->mBFMenuList.front();
  1645. RemoveMenuItem(placeholderMenuItem);
  1646. delete placeholderMenuItem;
  1647. }
  1648. WinBFMenu* winBFMenuParent = (WinBFMenu*) parent;
  1649. WinBFMenu* newMenu = new WinBFMenu();
  1650. newMenu->mMenuId = ++WinBFMenu::mMenuCount;
  1651. newMenu->mParent = parent;
  1652. //static int allocIdx = 0;
  1653. //allocIdx++;
  1654. //OutputDebugStrF("MenuIdx %d %@\n", allocIdx, newMenu);
  1655. if ((winBFMenuParent != NULL) && (winBFMenuParent->mMenu == NULL))
  1656. {
  1657. winBFMenuParent->mMenu = ::CreateMenu();
  1658. mHMenuMap[winBFMenuParent->mMenu] = winBFMenuParent;
  1659. MENUITEMINFO menuItem;
  1660. memset(&menuItem, 0, sizeof(MENUITEMINFO));
  1661. menuItem.cbSize = sizeof(MENUITEMINFO);
  1662. menuItem.fMask = MIIM_SUBMENU;
  1663. menuItem.hSubMenu = winBFMenuParent->mMenu;
  1664. ::SetMenuItemInfo(((WinBFMenu*) winBFMenuParent->mParent)->mMenu, winBFMenuParent->mMenuId, FALSE, &menuItem);
  1665. }
  1666. mMenuIDMap[newMenu->mMenuId] = newMenu;
  1667. BF_ASSERT(insertIdx <= (int) parent->mBFMenuList.size());
  1668. ////
  1669. UTF16String lCombinedName;
  1670. MENUITEMINFOW menuItem;
  1671. memset(&menuItem, 0, sizeof(MENUITEMINFO));
  1672. menuItem.cbSize = sizeof(MENUITEMINFO);
  1673. menuItem.fMask = MIIM_FTYPE | MIIM_ID;
  1674. if (text != NULL)
  1675. {
  1676. menuItem.fMask |= MIIM_STRING;
  1677. menuItem.fType = MFT_STRING;
  1678. }
  1679. else
  1680. menuItem.fType = MFT_SEPARATOR;
  1681. menuItem.fState = enabled ? MFS_DEFAULT : MFS_GRAYED;
  1682. if (checkState == 0)
  1683. menuItem.fState |= MFS_UNCHECKED;
  1684. if (checkState == 1)
  1685. menuItem.fState |= MFS_CHECKED;
  1686. if (radioCheck)
  1687. menuItem.fType = MFT_RADIOCHECK;
  1688. menuItem.wID = newMenu->mMenuId;
  1689. if (text != NULL)
  1690. {
  1691. menuItem.dwTypeData = (WCHAR*)lText.c_str();
  1692. }
  1693. if (hotKey != NULL)
  1694. {
  1695. String combinedName = String(text);
  1696. combinedName += "\t";
  1697. if (hotKey[0] == '#')
  1698. {
  1699. combinedName += hotKey + 1;
  1700. }
  1701. else
  1702. {
  1703. combinedName += hotKey;
  1704. newMenu->ParseHotKey(hotKey);
  1705. }
  1706. lCombinedName = UTF8Decode(combinedName);
  1707. menuItem.dwTypeData = (WCHAR*)lCombinedName.c_str();
  1708. }
  1709. ::InsertMenuItemW(winBFMenuParent->mMenu, insertIdx, TRUE, &menuItem);
  1710. ////////
  1711. /*MENUITEMINFO menuItem;
  1712. memset(&menuItem, 0, sizeof(MENUITEMINFO));
  1713. menuItem.cbSize = sizeof(MENUITEMINFO);
  1714. menuItem.fMask = MIIM_ID;
  1715. menuItem.wID = newMenu->mMenuId;
  1716. ::InsertMenuItem(winBFMenuParent->mMenu, insertIdx, TRUE, &menuItem);*/
  1717. ModifyMenuItem(newMenu, text, hotKey, sysBitmap, enabled, checkState, radioCheck);
  1718. parent->mBFMenuList.push_back(newMenu);
  1719. return newMenu;
  1720. }
  1721. void WinBFWindow::ModifyMenuItem(BFMenu* item, const char* text, const char* hotKey, BFSysBitmap* sysBitmap, bool enabled, int checkState, bool radioCheck)
  1722. {
  1723. UTF16String lText;
  1724. if (text != NULL)
  1725. lText = UTF8Decode(text);
  1726. UTF16String lHotKey;
  1727. if (hotKey != NULL)
  1728. lHotKey = UTF8Decode(hotKey);
  1729. WinBFMenu* aMenu = (WinBFMenu*)item;
  1730. WinBFMenu* parentMenu = (WinBFMenu*) item->mParent;
  1731. UTF16String lCombinedName;
  1732. MENUITEMINFOW menuItem;
  1733. memset(&menuItem, 0, sizeof(MENUITEMINFO));
  1734. menuItem.cbSize = sizeof(MENUITEMINFO);
  1735. menuItem.fMask = MIIM_FTYPE | MIIM_ID;
  1736. ::GetMenuItemInfoW(parentMenu->mMenu, aMenu->mMenuId, FALSE, &menuItem);
  1737. menuItem.fMask |= MIIM_STATE;
  1738. if (text != NULL)
  1739. {
  1740. menuItem.fMask |= MIIM_STRING;
  1741. //menuItem.fType = MFT_STRING;
  1742. }
  1743. /*else
  1744. menuItem.fType = MFT_SEPARATOR;*/
  1745. menuItem.fState = enabled ? /*MFS_DEFAULT*/0 : MFS_GRAYED;
  1746. if (checkState != -1)
  1747. {
  1748. menuItem.fMask |= MIIM_CHECKMARKS;
  1749. }
  1750. if (checkState == 0)
  1751. menuItem.fState |= MFS_UNCHECKED;
  1752. if (checkState == 1)
  1753. menuItem.fState |= MFS_CHECKED;
  1754. if (radioCheck)
  1755. menuItem.fType = MFT_RADIOCHECK;
  1756. menuItem.wID = aMenu->mMenuId;
  1757. menuItem.dwTypeData = (WCHAR*)lText.c_str();
  1758. if (hotKey != NULL)
  1759. {
  1760. menuItem.fMask |= MIIM_DATA;
  1761. String combinedName = String(text);
  1762. combinedName += "\t";
  1763. if (hotKey[0] == '#')
  1764. {
  1765. combinedName += hotKey + 1;
  1766. }
  1767. else
  1768. {
  1769. combinedName += hotKey;
  1770. aMenu->ParseHotKey(hotKey);
  1771. }
  1772. lCombinedName = UTF8Decode(combinedName);
  1773. menuItem.dwTypeData = (WCHAR*)lCombinedName.c_str();
  1774. }
  1775. ::SetMenuItemInfoW(parentMenu->mMenu, aMenu->mMenuId, FALSE, &menuItem);
  1776. }
  1777. void WinBFWindow::RemoveMenuItem(BFMenu* item)
  1778. {
  1779. WinBFMenu* aMenu = (WinBFMenu*) item;
  1780. WinBFMenu* parentMenu = (WinBFMenu*) item->mParent;
  1781. //auto itr = mMenuIDMap.find(aMenu->mMenuId);
  1782. //mMenuIDMap.erase(itr);
  1783. mMenuIDMap.Remove(aMenu->mMenuId);
  1784. ::RemoveMenu(parentMenu->mMenu, aMenu->mMenuId, MF_BYCOMMAND);
  1785. }
  1786. BFSysBitmap* WinBFApp::LoadSysBitmap(const WCHAR* fileName)
  1787. {
  1788. return NULL;
  1789. }
  1790. BFSoundManager* WinBFApp::GetSoundManager()
  1791. {
  1792. if (mDSoundManager == NULL)
  1793. mDSoundManager = new DSoundManager(NULL);
  1794. return mDSoundManager;
  1795. }
  1796. intptr WinBFApp::GetCriticalThreadId(int idx)
  1797. {
  1798. if (idx == 0)
  1799. return mVSyncThreadId;
  1800. return 0;
  1801. }
  1802. void WinBFWindow::ModalsRemoved()
  1803. {
  1804. ::EnableWindow(mHWnd, TRUE);
  1805. if (mChildren.empty())
  1806. ::SetFocus(mHWnd);
  1807. }
  1808. DrawLayer* WinBFApp::CreateDrawLayer(BFWindow* window)
  1809. {
  1810. DXDrawLayer* drawLayer = new DXDrawLayer();
  1811. if (window != NULL)
  1812. {
  1813. drawLayer->mRenderWindow = window->mRenderWindow;
  1814. window->mRenderWindow->mDrawLayerList.push_back(drawLayer);
  1815. }
  1816. drawLayer->mRenderDevice = mRenderDevice;
  1817. return drawLayer;
  1818. }