std_wnd.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. #include "precomp_wasabi_bfc.h"
  2. #include "wasabi_std_wnd.h"
  3. #include "api.h"
  4. #include <api/wnd/api_window.h>
  5. #include <api/wnd/wndevent.h>
  6. #include <api/wnd/wndevent.h>
  7. #include <shobjidl.h>
  8. #include "../winamp/wa_ipc.h"
  9. #ifndef AC_SRC_ALPHA
  10. const int AC_SRC_ALPHA = 1;
  11. #endif
  12. #ifndef THBN_CLICKED
  13. #define THBN_CLICKED 0x1800
  14. #endif
  15. static int nreal = 0;
  16. #ifdef __APPLE__
  17. OSStatus MyWindowEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);
  18. OSStatus MyControlEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData);
  19. #endif
  20. #ifdef _WIN32
  21. static HINSTANCE gdi32instance = NULL;
  22. static int(WINAPI *getRandomRgn)(HDC dc, HRGN rgn, int i) = NULL;
  23. static int grrfailed = 0;
  24. static void register_wndClass(HINSTANCE hInstance);
  25. static int versionChecked = 0;
  26. static int isNT = 0;
  27. static int IsNT()
  28. {
  29. if (versionChecked)
  30. return isNT;
  31. if (GetVersion() < 0x80000000)
  32. isNT = 1;
  33. versionChecked = 1;
  34. return isNT;
  35. }
  36. int Wasabi::Std::Wnd::alphaStretchBlit(HDC destHDC, int dstx, int dsty, int dstw, int dsth, HDC sourceHDC, int srcx, int srcy, int srcw, int srch)
  37. {
  38. if (IsNT())
  39. {
  40. SetStretchBltMode(destHDC, HALFTONE);
  41. StretchBlt(destHDC, dstx, dsty, dstw, dsth, sourceHDC, srcx , srcy, srcw, srch, SRCCOPY);
  42. return 1;
  43. }
  44. else
  45. return 0;
  46. }
  47. #endif
  48. #ifdef __APPLE__
  49. enum
  50. {
  51. kWasabi = 'WASA'
  52. };
  53. const ControlID kWasabiID = { kWasabi, 0 };
  54. void GetWasabiHIView(WindowRef window, HIViewRef *control)
  55. {
  56. GetControlByID(window, &kWasabiID, control);
  57. }
  58. OSStatus CreateHIView ( WindowRef inWindow, const Rect* inBounds,
  59. ControlRef* outControl )
  60. {
  61. OSStatus err;
  62. ControlRef root;
  63. EventRef event;
  64. // Make an initialization event
  65. err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize,
  66. GetCurrentEventTime(), 0, &event );
  67. require_noerr( err, CantCreateEvent );
  68. // If bounds were specified, push the them into the initialization event
  69. // so that they can be used in the initialization handler.
  70. if ( inBounds != NULL )
  71. {
  72. err = SetEventParameter( event, 'boun', typeQDRectangle,
  73. sizeof( Rect ), inBounds );
  74. require_noerr( err, CantSetParameter );
  75. }
  76. err = HIObjectCreate( kHIViewClassID, event, (HIObjectRef*)
  77. outControl );
  78. require_noerr( err, CantCreate );
  79. // If a parent window was specified, place the new view into the
  80. // parent window.
  81. if ( inWindow != NULL )
  82. {
  83. err = GetRootControl( inWindow, &root );
  84. require_noerr( err, CantGetRootControl );
  85. err = HIViewAddSubview( root, *outControl );
  86. SetControlID( *outControl, &kWasabiID );
  87. HIViewSetVisible(*outControl, true);
  88. }
  89. CantCreate:
  90. CantGetRootControl:
  91. CantSetParameter:
  92. CantCreateEvent:
  93. ReleaseEvent( event );
  94. CantRegister:
  95. return err;
  96. }
  97. #endif
  98. OSWINDOWHANDLE Wasabi::Std::Wnd::createWnd(RECT *r, int nochild, int acceptdrops, OSWINDOWHANDLE parent, OSMODULEHANDLE module, ifc_window *rw)
  99. {
  100. #ifdef _WIN32
  101. register_wndClass(module);
  102. int style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  103. int exstyle=0;
  104. if (parent == NULL) {
  105. exstyle |= WS_EX_TOOLWINDOW;
  106. style |= WS_POPUP;
  107. } else
  108. style |= WS_CHILD;
  109. if (nochild) style=WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  110. if (acceptdrops) exstyle |= WS_EX_ACCEPTFILES;
  111. HWND ret = CreateWindowExW(exstyle, BASEWNDCLASSNAME, NULL, style,
  112. r->left, r->top, r->right - r->left, r->bottom - r->top, parent, NULL, module, (LPVOID)rw);
  113. if (ret != INVALIDOSWINDOWHANDLE)
  114. {
  115. nreal++;
  116. if(NULL != WASABI_API_APP && 0 != (WS_POPUP & style))
  117. WASABI_API_APP->app_registerGlobalWindow(ret);
  118. }
  119. return ret;
  120. #elif defined(__APPLE__)
  121. Rect wndRect;
  122. SetRect(&wndRect, r->left, r->top, r->right, r->bottom);
  123. WindowRef newWnd;
  124. CreateNewWindow(kOverlayWindowClass, kWindowCompositingAttribute, &wndRect, &newWnd);
  125. SetWindowGroup(newWnd, GetWindowGroupOfClass(kDocumentWindowClass));
  126. // install a window event handler
  127. const EventTypeSpec windowEventList[] =
  128. {
  129. {
  130. kEventClassCommand, kEventProcessCommand
  131. },
  132. // { kEventClassWindow, kEventWindowBoundsChanging },
  133. { kEventClassWindow, kEventWindowBoundsChanged },
  134. // { kEventClassWindow, kEventWindowInit },
  135. {kEventClassMouse, kEventMouseDown},
  136. {kEventClassMouse, kEventMouseUp},
  137. {kEventClassMouse, kEventMouseMoved},
  138. {kEventClassMouse, kEventMouseDragged},
  139. //{kEventClassMouse, kEventMouseEntered},
  140. //{kEventClassMouse, kEventMouseExited},
  141. //{kEventClassMouse, kEventMouseWheelMoved},
  142. {kEventClassKeyboard, kEventRawKeyDown},
  143. {kEventClassKeyboard, kEventRawKeyUp},
  144. };
  145. InstallWindowEventHandler(newWnd, MyWindowEventHandler, GetEventTypeCount(windowEventList), windowEventList, rw, NULL);
  146. // create the content view
  147. HIViewRef myHIView;
  148. CreateHIView(newWnd,&wndRect,(ControlRef*)&myHIView);
  149. const EventTypeSpec controlEventList[] =
  150. {
  151. { kEventClassControl, kEventControlDraw},
  152. { kEventClassControl, kEventControlApplyBackground},
  153. };
  154. InstallEventHandler(GetControlEventTarget(myHIView), MyControlEventHandler, GetEventTypeCount(controlEventList), controlEventList, rw, NULL);
  155. return newWnd;
  156. #endif
  157. }
  158. void Wasabi::Std::Wnd::destroyWnd(OSWINDOWHANDLE wnd)
  159. {
  160. #ifdef _WIN32
  161. if(NULL != WASABI_API_APP && 0 != (WS_POPUP & GetWindowLongPtr(wnd, GWL_STYLE)))
  162. WASABI_API_APP->app_unregisterGlobalWindow(wnd);
  163. DestroyWindow(wnd);
  164. nreal--;
  165. if (nreal == 0)
  166. {
  167. if (gdi32instance) FreeLibrary(gdi32instance);
  168. gdi32instance = NULL;
  169. getRandomRgn = NULL;
  170. }
  171. #elif defined(__APPLE__)
  172. DisposeWindow(wnd);
  173. #endif
  174. }
  175. int Wasabi::Std::Wnd::isValidWnd(OSWINDOWHANDLE wnd)
  176. {
  177. #ifdef _WIN32
  178. return IsWindow(wnd);
  179. #elif defined(__APPLE__)
  180. // TODO: docs suggest that this function is slow
  181. if (!wnd)
  182. return 0;
  183. return IsValidWindowPtr(wnd);
  184. #endif
  185. }
  186. #ifdef _WIN32
  187. void Wasabi::Std::Wnd::setLayeredWnd(OSWINDOWHANDLE wnd, int layered)
  188. {
  189. if (layered)
  190. {
  191. // have to clear and reset, can't just set
  192. SetWindowLong(wnd, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
  193. SetWindowLong(wnd, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) | WS_EX_LAYERED);
  194. }
  195. else
  196. {
  197. SetWindowLong(wnd, GWL_EXSTYLE, GetWindowLong(wnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
  198. }
  199. }
  200. int Wasabi::Std::Wnd::isLayeredWnd(OSWINDOWHANDLE wnd)
  201. {
  202. DWORD dwLong = GetWindowLong(wnd, GWL_EXSTYLE);
  203. return !!(dwLong & WS_EX_LAYERED);
  204. }
  205. void Wasabi::Std::Wnd::setLayeredAlpha(OSWINDOWHANDLE wnd, int amount)
  206. {
  207. if (!isDesktopAlphaAvailable()) return;
  208. SetLayeredWindowAttributes(wnd, RGB(0, 0, 0), amount, LWA_ALPHA);
  209. }
  210. void Wasabi::Std::Wnd::updateLayeredWnd(OSWINDOWHANDLE wnd, int x, int y, int w, int h, HDC surfdc, int alpha)
  211. {
  212. if (!isDesktopAlphaAvailable()) return;
  213. BLENDFUNCTION blend = {AC_SRC_OVER, 0, (BYTE)alpha, AC_SRC_ALPHA };
  214. POINT sp = {x, y}, pt = {0, 0};
  215. SIZE ss = { w, h };
  216. //HDC sysdc = GetDC(NULL);
  217. UpdateLayeredWindow(wnd, NULL/*sysdc*/, &sp, &ss, surfdc, &pt, 0, &blend, ULW_ALPHA);
  218. //ReleaseDC(NULL, sysdc);
  219. }
  220. #endif
  221. void Wasabi::Std::Wnd::setWndPos(OSWINDOWHANDLE wnd, OSWINDOWHANDLE zorder, int x, int y, int w, int h,
  222. int nozorder, int noactive, int nocopybits, int nomove, int noresize)
  223. {
  224. #ifdef _WIN32
  225. SetWindowPos(wnd, zorder, x, y, w, h,
  226. SWP_DEFERERASE | // we ignore WM_SYNCPAINT anyway
  227. (nozorder ? SWP_NOZORDER : 0) |
  228. (noactive ? SWP_NOACTIVATE : 0) |
  229. (nocopybits ? SWP_NOCOPYBITS : 0) |
  230. (nomove ? SWP_NOMOVE : 0) |
  231. (noresize ? SWP_NOSIZE : 0));
  232. #elif defined(__APPLE__)
  233. if (!nomove)
  234. {
  235. MoveWindow(wnd, x, y, false);
  236. }
  237. if (!noresize)
  238. {
  239. Rect newRect;
  240. newRect.left = x;
  241. newRect.right=x+w;
  242. newRect.top = y;
  243. newRect.bottom = y+h;
  244. // SetWindowBounds(wnd, kWindowStructureRgn, &newRect);
  245. SizeWindow(wnd, w, h, false);
  246. }
  247. if (!noactive)
  248. SelectWindow(wnd);
  249. if (!nocopybits)
  250. Wasabi::Std::Wnd::invalidateRect(wnd, 0);
  251. #endif
  252. }
  253. void Wasabi::Std::Wnd::bringToFront(OSWINDOWHANDLE wnd)
  254. {
  255. #ifdef _WIN32
  256. SetWindowPos(wnd, HWND_TOP, 0, 0, 0, 0,
  257. SWP_NOMOVE | SWP_NOSIZE | SWP_DEFERERASE | SWP_NOOWNERZORDER);
  258. #elif defined(__APPLE__)
  259. BringToFront(wnd);
  260. #endif
  261. }
  262. void Wasabi::Std::Wnd::sendToBack(OSWINDOWHANDLE wnd)
  263. {
  264. #ifdef _WIN32
  265. SetWindowPos(wnd, HWND_BOTTOM, 0, 0, 0, 0,
  266. SWP_NOMOVE | SWP_NOSIZE | SWP_DEFERERASE | SWP_NOOWNERZORDER);
  267. #elif defined(__APPLE__)
  268. SendBehind(wnd, 0);
  269. #endif
  270. }
  271. OSWINDOWHANDLE Wasabi::Std::Wnd::getWindowFromPoint(POINT pt)
  272. {
  273. #ifdef _WIN32
  274. return ::WindowFromPoint(pt);
  275. #else
  276. #warning port me
  277. return 0;
  278. #endif
  279. }
  280. int Wasabi::Std::Wnd::isWndVisible(OSWINDOWHANDLE wnd)
  281. {
  282. return IsWindowVisible(wnd);
  283. }
  284. void Wasabi::Std::Wnd::showWnd(OSWINDOWHANDLE wnd, int noactivate)
  285. {
  286. #ifdef _WIN32
  287. ShowWindow(wnd, noactivate ? SW_SHOWNA : SW_SHOWNORMAL);
  288. #elif defined(__APPLE__)
  289. ShowWindow(wnd);
  290. if (!noactivate)
  291. ActivateWindow(wnd, 1);
  292. #endif
  293. }
  294. void Wasabi::Std::Wnd::hideWnd(OSWINDOWHANDLE wnd)
  295. {
  296. #ifdef _WIN32
  297. ShowWindow(wnd, SW_HIDE);
  298. #elif defined(__APPLE__)
  299. HideWindow(wnd);
  300. #endif
  301. }
  302. int Wasabi::Std::Wnd::isPopup(OSWINDOWHANDLE wnd)
  303. {
  304. #ifdef _WIN32
  305. return !!(GetWindowLong(wnd, GWL_STYLE) & WS_POPUP);
  306. #elif defined(__APPLE__)
  307. return 0; // TODO: maybe use window class or window group to determine
  308. #endif
  309. }
  310. #ifdef _WIN32
  311. void Wasabi::Std::Wnd::setEnabled(OSWINDOWHANDLE wnd, int enabled)
  312. {
  313. EnableWindow(wnd, enabled);
  314. }
  315. #endif
  316. void Wasabi::Std::Wnd::setFocus(OSWINDOWHANDLE wnd)
  317. {
  318. #ifdef _WIN32
  319. SetFocus(wnd);
  320. #elif defined(__APPLE__)
  321. SetUserFocusWindow(wnd);
  322. #endif
  323. }
  324. OSWINDOWHANDLE Wasabi::Std::Wnd::getFocus()
  325. {
  326. #ifdef _WIN32
  327. return GetFocus();
  328. #elif defined(__APPLE__)
  329. return GetUserFocusWindow();
  330. #endif
  331. }
  332. #ifdef _WIN32
  333. void Wasabi::Std::Wnd::setTopmost(OSWINDOWHANDLE wnd, int topmost)
  334. {
  335. SetWindowPos(wnd, topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
  336. }
  337. #endif
  338. void Wasabi::Std::Wnd::invalidateRect(OSWINDOWHANDLE wnd, RECT *r)
  339. {
  340. #ifdef _WIN32
  341. OSREGIONHANDLE reg = NULL;
  342. if (r == NULL)
  343. {
  344. RECT cr;
  345. if (!IsWindow(wnd))
  346. return;
  347. GetClientRect(wnd, &cr);
  348. reg = CreateRectRgnIndirect(&cr);
  349. }
  350. else
  351. reg = CreateRectRgnIndirect(r);
  352. invalidateRegion(wnd, reg);
  353. DeleteObject(reg);
  354. #elif defined(__APPLE__)
  355. HIViewRef view;
  356. GetWasabiHIView(wnd, &view);
  357. if (r == 0)
  358. {
  359. HIViewSetNeedsDisplay(view, true);
  360. }
  361. else
  362. {
  363. HIRect rect = CGRectMake(r->left, r->top, r->right-r->left, r->bottom - r->top);
  364. HIViewSetNeedsDisplayInRect(view, &rect, true);
  365. }
  366. #endif
  367. }
  368. void Wasabi::Std::Wnd::invalidateRegion(OSWINDOWHANDLE wnd, OSREGIONHANDLE region)
  369. {
  370. #ifdef _WIN32
  371. clipOSChildren(wnd, region);
  372. InvalidateRgn(wnd, region, FALSE);
  373. #elif defined(__APPLE__)
  374. HIViewRef view;
  375. GetWasabiHIView(wnd, &view);
  376. HIViewSetNeedsDisplayInShape(view, region, true);
  377. #endif
  378. }
  379. void Wasabi::Std::Wnd::validateRect(OSWINDOWHANDLE wnd, RECT *r)
  380. {
  381. #ifdef _WIN32
  382. ValidateRect(wnd, r);
  383. #elif defined(__APPLE__)
  384. HIViewRef view;
  385. GetWasabiHIView(wnd, &view);
  386. if (r == 0)
  387. {
  388. HIViewSetNeedsDisplay(view, false);
  389. }
  390. else
  391. {
  392. HIRect rect = CGRectMake(r->left, r->top, r->right-r->left, r->bottom - r->top);
  393. HIViewSetNeedsDisplayInRect(view, &rect, false);
  394. }
  395. #endif
  396. }
  397. void Wasabi::Std::Wnd::validateRegion(OSWINDOWHANDLE wnd, OSREGIONHANDLE region)
  398. {
  399. #ifdef _WIN32
  400. ValidateRgn(wnd, region);
  401. #elif defined(__APPLE__)
  402. HIViewRef view;
  403. GetWasabiHIView(wnd, &view);
  404. HIViewSetNeedsDisplayInShape(view, region, false);
  405. #endif
  406. }
  407. void Wasabi::Std::Wnd::update(OSWINDOWHANDLE wnd)
  408. {
  409. #ifdef _WIN32
  410. if (wnd != NULL)
  411. UpdateWindow(wnd);
  412. #elif defined(__APPLE__)
  413. if (wnd)
  414. {
  415. HIViewRef view;
  416. GetWasabiHIView(wnd, &view);
  417. HIViewRender(view);
  418. }
  419. #endif
  420. }
  421. int Wasabi::Std::Wnd::getUpdateRect(OSWINDOWHANDLE wnd, RECT *r)
  422. {
  423. #ifdef _WIN32
  424. return GetUpdateRect(wnd, r, FALSE);
  425. #else
  426. Rect updateRect;
  427. GetWindowBounds(wnd, kWindowUpdateRgn, &updateRect);
  428. r->left=updateRect.left;
  429. r->right = updateRect.right;
  430. r->top = updateRect.top;
  431. r->bottom = updateRect.bottom;
  432. #endif
  433. }
  434. #ifdef _WIN32
  435. void Wasabi::Std::Wnd::getUpdateRegion(OSWINDOWHANDLE wnd, OSREGIONHANDLE region)
  436. {
  437. GetUpdateRgn(wnd, region, FALSE);
  438. }
  439. #elif defined(__APPLE__)
  440. // TODO: GetWindowRegion(wnd, kWindowUpdateRgn, region);
  441. #endif
  442. #ifdef _WIN32
  443. int Wasabi::Std::Wnd::haveGetRandomRegion()
  444. {
  445. // I assume linux will just return FALSE
  446. if (gdi32instance == NULL && !grrfailed)
  447. {
  448. gdi32instance = LoadLibrary(L"GDI32.dll");
  449. if (gdi32instance != NULL)
  450. {
  451. getRandomRgn = (int(WINAPI *)(HDC, HRGN, int)) GetProcAddress(gdi32instance, "GetRandomRgn");
  452. if (getRandomRgn == NULL)
  453. {
  454. grrfailed = 1;
  455. FreeLibrary(gdi32instance);
  456. gdi32instance = NULL;
  457. }
  458. }
  459. else
  460. {
  461. grrfailed = 1;
  462. }
  463. }
  464. return (getRandomRgn != NULL);
  465. }
  466. void Wasabi::Std::Wnd::getRandomRegion(HDC hdc, OSREGIONHANDLE region)
  467. {
  468. if (!haveGetRandomRegion()) return;
  469. (*getRandomRgn)(hdc, region, SYSRGN);
  470. }
  471. #endif
  472. void Wasabi::Std::Wnd::setWndRegion(OSWINDOWHANDLE wnd, OSREGIONHANDLE region, int redraw)
  473. {
  474. #ifdef _WIN32
  475. SetWindowRgn(wnd, region, !!redraw);
  476. #elif defined(__APPLE__)
  477. #warning port me?
  478. #endif
  479. }
  480. #ifdef _WIN32
  481. int Wasabi::Std::Wnd::isDesktopAlphaAvailable()
  482. {
  483. return 1; // we're only targetting windows 2000 and up, so it's always available
  484. }
  485. #endif
  486. int Wasabi::Std::Wnd::isTransparencyAvailable()
  487. {
  488. #ifdef _WIN32
  489. // there is no win32 implementation that supports setLayeredWindowAttributes but not updateLayeredWindow
  490. return Wasabi::Std::Wnd::isDesktopAlphaAvailable();
  491. #elif defined(__APPLE__)
  492. return 1;
  493. #else
  494. #error port me
  495. #endif
  496. }
  497. void Wasabi::Std::Wnd::getClientRect(OSWINDOWHANDLE wnd, RECT *r)
  498. {
  499. #ifdef _WIN32
  500. GetClientRect(wnd, r);
  501. #elif defined(__APPLE__)
  502. Rect temp;
  503. GetWindowBounds(wnd, kWindowContentRgn, &temp);
  504. r->left = 0;
  505. r->top = 0;
  506. r->right = temp.right-temp.left;
  507. r->bottom = temp.bottom-temp.top;
  508. #endif
  509. }
  510. void Wasabi::Std::Wnd::getWindowRect(OSWINDOWHANDLE wnd, RECT *r)
  511. {
  512. #ifdef _WIN32
  513. GetWindowRect(wnd, r);
  514. #elif defined(__APPLE__)
  515. Rect temp;
  516. GetWindowBounds(wnd, kWindowGlobalPortRgn, &temp);
  517. r->left = temp.left;
  518. r->top = temp.top;
  519. r->right = temp.right;
  520. r->bottom = temp.bottom;
  521. #endif
  522. }
  523. void Wasabi::Std::Wnd::clientToScreen(OSWINDOWHANDLE wnd, int *x, int *y)
  524. {
  525. #ifdef _WIN32
  526. POINT p = { x ? *x : 0, y ? *y : 0 };
  527. ClientToScreen(wnd, &p);
  528. if (x) *x = p.x;
  529. if (y) *y = p.y;
  530. #elif defined(__APPLE__)
  531. Point pt;
  532. pt.h = x?*x:0;
  533. pt.v = y?*y:0;
  534. QDLocalToGlobalPoint(GetWindowPort(wnd) , &pt);
  535. if (x) *x = pt.h;
  536. if (y) *y = pt.v;
  537. #endif
  538. }
  539. void Wasabi::Std::Wnd::screenToClient(OSWINDOWHANDLE wnd, int *x, int *y)
  540. {
  541. #ifdef _WIN32
  542. POINT p = { x ? *x : 0, y ? *y : 0 };
  543. ScreenToClient(wnd, &p);
  544. if (x) *x = p.x;
  545. if (y) *y = p.y;
  546. #elif defined(__APPLE__)
  547. Point pt;
  548. pt.h = x?*x:0;
  549. pt.v = y?*y:0;
  550. QDGlobalToLocalPoint(GetWindowPort(wnd) , &pt);
  551. if (x) *x = pt.h;
  552. if (y) *y = pt.v;
  553. #endif
  554. }
  555. #ifdef _WIN32
  556. void Wasabi::Std::Wnd::setParent(OSWINDOWHANDLE child, OSWINDOWHANDLE newparent)
  557. {
  558. SetParent(child, newparent);
  559. }
  560. #endif
  561. OSWINDOWHANDLE Wasabi::Std::Wnd::getParent(OSWINDOWHANDLE wnd)
  562. {
  563. #ifdef _WIN32
  564. return GetParent(wnd);
  565. #else
  566. return 0;
  567. #endif
  568. }
  569. #ifdef _WIN32
  570. OSWINDOWHANDLE Wasabi::Std::Wnd::getTopmostChild(OSWINDOWHANDLE wnd)
  571. {
  572. return GetWindow(wnd, GW_CHILD);
  573. }
  574. void Wasabi::Std::Wnd::setCapture(OSWINDOWHANDLE wnd)
  575. {
  576. SetCapture(wnd);
  577. }
  578. void Wasabi::Std::Wnd::releaseCapture()
  579. {
  580. ReleaseCapture();
  581. }
  582. OSWINDOWHANDLE Wasabi::Std::Wnd::getCapture()
  583. {
  584. return GetCapture();
  585. }
  586. void Wasabi::Std::Wnd::revokeDragNDrop(OSWINDOWHANDLE wnd)
  587. {
  588. if (wnd == NULL) return;
  589. RevokeDragDrop(wnd);
  590. }
  591. #endif
  592. void Wasabi::Std::Wnd::setWndName(OSWINDOWHANDLE wnd, const wchar_t *name)
  593. {
  594. #ifdef _WIN32
  595. SetWindowTextW(wnd, name);
  596. #elif defined(__APPLE__)
  597. // TODO:
  598. //SetWindowTitleWithCFString(wnd,
  599. #endif
  600. }
  601. void Wasabi::Std::Wnd::getWndName(OSWINDOWHANDLE wnd, wchar_t *name, int maxlen)
  602. {
  603. #ifdef _WIN32
  604. GetWindowTextW(wnd, name, maxlen);
  605. #else
  606. name[0]=0;
  607. #warning port me
  608. #endif
  609. }
  610. void Wasabi::Std::Wnd::setIcon(OSWINDOWHANDLE wnd, OSICONHANDLE icon, int large)
  611. {
  612. #ifdef _WIN32
  613. SendMessageW(wnd, WM_SETICON, !large ? ICON_SMALL : ICON_BIG, (LPARAM)icon);
  614. #else
  615. #warning port me
  616. #endif
  617. }
  618. OSWINDOWHANDLE Wasabi::Std::Wnd::getActiveWindow()
  619. {
  620. #ifdef _WIN32
  621. return GetActiveWindow();
  622. #elif defined(__APPLE__)
  623. return ActiveNonFloatingWindow();
  624. #endif
  625. }
  626. void Wasabi::Std::Wnd::setActiveWindow(OSWINDOWHANDLE wnd)
  627. {
  628. #ifdef _WIN32
  629. SetActiveWindow(wnd);
  630. #elif defined(__APPLE__)
  631. SelectWindow(wnd);
  632. #endif
  633. }
  634. #ifdef _WIN32
  635. // clip oswindow children off the invalidation region
  636. void Wasabi::Std::Wnd::clipOSChildren(OSWINDOWHANDLE wnd, OSREGIONHANDLE reg)
  637. {
  638. HWND w = GetWindow(wnd, GW_CHILD);
  639. while (w != NULL)
  640. {
  641. if (IsWindowVisible(w))
  642. {
  643. RECT r;
  644. GetClientRect(w, &r);
  645. POINT p = {r.left, r.top};
  646. ClientToScreen(w, &p);
  647. ScreenToClient(wnd, &p);
  648. OffsetRect(&r, p.x, p.y);
  649. OSREGIONHANDLE cr = CreateRectRgnIndirect(&r);
  650. OSREGIONHANDLE or = CreateRectRgn(0, 0, 0, 0);
  651. CombineRgn(or, reg, 0, RGN_COPY);
  652. CombineRgn(reg, or, cr, RGN_DIFF);
  653. DeleteObject(cr);
  654. DeleteObject(or);
  655. }
  656. w = GetWindow(w, GW_HWNDNEXT);
  657. }
  658. }
  659. //////////////////////////
  660. static LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  661. {
  662. // fetch out the api_window *
  663. if (uMsg == WM_CREATE)
  664. {
  665. CREATESTRUCT *cs = (CREATESTRUCT *)lParam;
  666. ASSERT(cs->lpCreateParams != NULL);
  667. // stash pointer to self
  668. SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)cs->lpCreateParams);
  669. }
  670. // pass the messages into the BaseWnd
  671. ifc_window *rootwnd = (ifc_window*)GetWindowLongPtrW(hWnd, GWLP_USERDATA);
  672. if (rootwnd == NULL || rootwnd->getOsWindowHandle() != hWnd)
  673. {
  674. return DefWindowProcW(hWnd, uMsg, wParam, lParam);
  675. }
  676. else
  677. {
  678. LRESULT r = 0;
  679. switch (uMsg)
  680. {
  681. case WM_CLOSE:
  682. PostMessage(GetParent(hWnd), WM_CLOSE, wParam, lParam);
  683. return 0;
  684. case WM_NCPAINT: return 0;
  685. case WM_SYNCPAINT: return 0;
  686. // case WM_SETFOCUS: r = rootwnd->windowEvent(WndEvent::SETFOCUS);
  687. // case WM_KILLFOCUS: r = rootwnd->windowEvent(WndEvent::KILLFOCUS);
  688. case WM_ACTIVATE:
  689. if ( (LOWORD(wParam) == WA_ACTIVE || LOWORD(wParam) == WA_CLICKACTIVE) && IsIconic(GetParent(hWnd)) )
  690. {
  691. ShowWindow(GetParent(hWnd), SW_RESTORE);
  692. }
  693. r = rootwnd->wndProc(hWnd, uMsg, wParam, lParam);
  694. break;
  695. case WM_COMMAND:
  696. if (HIWORD(wParam) == THBN_CLICKED)
  697. {
  698. HWND parentWnd = GetParent(hWnd);
  699. if (parentWnd == NULL)
  700. parentWnd = hWnd;
  701. switch (LOWORD(wParam))
  702. {
  703. case 0: //previous
  704. {
  705. SendMessage(parentWnd, WM_COMMAND, 40044, 0);
  706. }
  707. break;
  708. case 1: //play
  709. {
  710. SendMessage(parentWnd, WM_COMMAND, 40045, 0);
  711. }
  712. break;
  713. case 2: //pause
  714. {
  715. SendMessage(parentWnd, WM_COMMAND, 40046, 0);
  716. }
  717. break;
  718. case 3: //stop
  719. {
  720. SendMessage(parentWnd, WM_COMMAND, 40047, 0);
  721. }
  722. break;
  723. case 4: //next
  724. {
  725. SendMessage(parentWnd, WM_COMMAND, 40048, 0);
  726. }
  727. break;
  728. }
  729. }
  730. default:
  731. r = rootwnd->wndProc(hWnd, uMsg, wParam, lParam);
  732. }
  733. if (IsWindow(hWnd)) // wndProc may have switched skin and killed this window
  734. rootwnd->performBatchProcesses();
  735. return r;
  736. }
  737. }
  738. static void register_wndClass(HINSTANCE hInstance)
  739. {
  740. WNDCLASSW wc;
  741. if (GetClassInfoW(hInstance, BASEWNDCLASSNAME, &wc)) return;
  742. // regiszter pane class
  743. wc.style = 0;
  744. wc.lpfnWndProc = (WNDPROC)wndProc;
  745. wc.cbClsExtra = 0;
  746. wc.cbWndExtra = 0;
  747. wc.hInstance = hInstance;
  748. wc.hIcon = NULL;
  749. wc.hCursor = NULL;
  750. wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
  751. wc.lpszMenuName = NULL;
  752. wc.lpszClassName = BASEWNDCLASSNAME;
  753. int r = RegisterClassW(&wc);
  754. if (r == 0)
  755. {
  756. int res = GetLastError();
  757. if (res != ERROR_CLASS_ALREADY_EXISTS)
  758. {
  759. char florp[WA_MAX_PATH] = {0};
  760. SPRINTF(florp, "Failed to register class, err %d", res);
  761. ASSERTPR(0, florp);
  762. }
  763. }
  764. }
  765. #endif
  766. #ifdef __APPLE__
  767. // TODO: call into BaseWnd and take this out of here
  768. #include <tataki/canvas/canvas.h>
  769. #include <tataki/region/region.h>
  770. #include <api/wnd/basewnd.h>
  771. OSStatus MyWindowEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
  772. {
  773. HICommand hiCommand;
  774. OSStatus err = eventNotHandledErr;
  775. UInt32 eventClass = GetEventClass(inEvent);
  776. UInt32 eventKind = GetEventKind(inEvent);
  777. ifc_window *wasabiWnd = (ifc_window *)inUserData;
  778. WindowRef window = wasabiWnd->getOsWindowHandle();
  779. switch (eventClass)
  780. {
  781. case kEventClassKeyboard:
  782. switch (eventKind)
  783. {
  784. case kEventRawKeyDown:
  785. {
  786. UInt32 keyCode;
  787. GetEventParameter(inEvent, kEventParamKeyCode, typeUInt32,
  788. NULL, sizeof(keyCode), NULL, &keyCode);
  789. if (wasabiWnd->onKeyDown(keyCode))
  790. err = noErr;
  791. }
  792. break;
  793. case kEventRawKeyUp:
  794. {
  795. UInt32 keyCode;
  796. GetEventParameter(inEvent, kEventParamKeyCode, typeUInt32,
  797. NULL, sizeof(keyCode), NULL, &keyCode);
  798. if (wasabiWnd->onKeyUp(keyCode))
  799. err = noErr;
  800. }
  801. break;
  802. }
  803. break;
  804. case kEventClassMouse:
  805. switch (eventKind)
  806. {
  807. case kEventMouseDown:
  808. {
  809. HIPoint point;
  810. GetEventParameter(inEvent, kEventParamWindowMouseLocation, typeHIPoint,
  811. NULL, sizeof(point), NULL, &point);
  812. EventMouseButton button;
  813. GetEventParameter(inEvent, kEventParamMouseButton, typeMouseButton,
  814. NULL, sizeof(button), NULL, &button);
  815. UInt32 modifiers;
  816. GetEventParameter(inEvent, kEventParamKeyModifiers, typeUInt32,
  817. NULL, sizeof(modifiers), NULL, &modifiers);
  818. switch (button)
  819. {
  820. case kEventMouseButtonPrimary:
  821. if (modifiers & controlKey) // fake right click
  822. wasabiWnd->onRightButtonDown(point.x, point.y);
  823. else
  824. wasabiWnd->onLeftButtonDown(point.x, point.y);
  825. break;
  826. case kEventMouseButtonSecondary:
  827. wasabiWnd->onRightButtonDown(point.x, point.y);
  828. break;
  829. }
  830. }
  831. break;
  832. case kEventMouseUp:
  833. {
  834. HIPoint point;
  835. GetEventParameter(inEvent, kEventParamWindowMouseLocation, typeHIPoint,
  836. NULL, sizeof(point), NULL, &point);
  837. EventMouseButton button;
  838. GetEventParameter(inEvent, kEventParamMouseButton, typeMouseButton,
  839. NULL, sizeof(button), NULL, &button);
  840. switch (button)
  841. {
  842. case kEventMouseButtonPrimary:
  843. wasabiWnd->onLeftButtonUp(point.x, point.y);
  844. break;
  845. case kEventMouseButtonSecondary:
  846. wasabiWnd->onRightButtonUp(point.x, point.y);
  847. break;
  848. }
  849. }
  850. break;
  851. case kEventMouseMoved:
  852. case kEventMouseDragged:
  853. {
  854. HIPoint point;
  855. GetEventParameter(inEvent, kEventParamWindowMouseLocation, typeHIPoint,
  856. NULL, sizeof(point), NULL, &point);
  857. wasabiWnd->onMouseMove(point.x, point.y);
  858. }
  859. break;
  860. }
  861. break;
  862. case kEventClassCommand:
  863. if (eventKind == kEventProcessCommand)
  864. {
  865. err = GetEventParameter(inEvent, kEventParamDirectObject, typeHICommand,
  866. NULL, sizeof(HICommand), NULL, &hiCommand);
  867. switch (hiCommand.commandID)
  868. {
  869. default:
  870. err = eventNotHandledErr;
  871. break;
  872. }
  873. }
  874. break;
  875. case kEventClassWindow:
  876. switch (eventKind)
  877. {
  878. case kEventWindowBoundsChanging:
  879. break;
  880. case kEventWindowBoundsChanged:
  881. {
  882. HIRect bounds;
  883. UInt32 attributes;
  884. Rect origBounds;
  885. Rect currBounds;
  886. ControlRef control;
  887. // Get the change attributes
  888. err = GetEventParameter(inEvent, kEventParamAttributes, typeUInt32,
  889. NULL, sizeof(UInt32), NULL, &attributes);
  890. // If the window size changed
  891. //kWindowBoundsChangeOriginChanged
  892. if (attributes & kWindowBoundsChangeSizeChanged)
  893. {
  894. GetEventParameter(inEvent, kEventParamOriginalBounds, typeQDRectangle,
  895. NULL, sizeof(Rect), NULL, &origBounds);
  896. GetEventParameter(inEvent, kEventParamCurrentBounds, typeQDRectangle,
  897. NULL, sizeof(Rect), NULL, &currBounds);
  898. // Get the clock control
  899. err = GetControlByID( window, &kWasabiID, &control );
  900. // Hide it (to avoid remnants when downsizing)
  901. //HIViewSetVisible( control, false );
  902. // Resize it
  903. HIViewGetFrame( control, &bounds );
  904. #define QDRECTWIDTH(R) ((R).right - (R).left)
  905. #define QDRECTHEIGHT(R) ((R).bottom - (R).top)
  906. bounds.size.width += QDRECTWIDTH( currBounds ) - QDRECTWIDTH( origBounds );
  907. bounds.size.height += QDRECTHEIGHT( currBounds ) - QDRECTHEIGHT( origBounds );
  908. HIViewSetFrame( control, &bounds );
  909. // Show it
  910. //HIViewSetVisible( control, true );
  911. }
  912. err = noErr;
  913. }
  914. break;
  915. case kEventWindowInit:
  916. break;
  917. }
  918. break;
  919. }
  920. return err;
  921. }
  922. OSStatus MyControlEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
  923. {
  924. HICommand hiCommand;
  925. OSStatus err = eventNotHandledErr;
  926. UInt32 eventClass = GetEventClass(inEvent);
  927. UInt32 eventKind = GetEventKind(inEvent);
  928. ifc_window *wasabiWnd = (ifc_window *)inUserData;
  929. switch(eventClass)
  930. {
  931. case kEventClassControl:
  932. switch(eventKind)
  933. {
  934. case kEventControlDraw:
  935. {
  936. CGContextRef context;
  937. RgnHandle rgn;
  938. HIShapeRef shape;
  939. HIViewRef view;
  940. if (GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(context), NULL, &context) == noErr
  941. && GetEventParameter(inEvent, kEventParamRgnHandle, typeQDRgnHandle, NULL, sizeof(rgn), NULL, &rgn) == noErr
  942. && GetEventParameter(inEvent, kEventParamDirectObject, typeControlRef, NULL, sizeof(view), NULL, &view) == noErr)
  943. {
  944. GetEventParameter(inEvent, kEventParamShape, typeHIShapeRef, NULL, sizeof(shape), NULL, &shape);
  945. HIRect bounds;
  946. HIViewGetBounds(view, &bounds);
  947. Canvas canvas(context);
  948. // RegionI region(rgn);
  949. RegionI region(shape);
  950. //((RootWndI *)wasabiWnd)->rootwnd_paintTree(&canvas, &region);
  951. wasabiWnd->paint(&canvas, &region); // TODO: should be virtualOnPaint()
  952. err = noErr;
  953. }
  954. }
  955. break;
  956. }
  957. break;
  958. }
  959. return err;
  960. }
  961. #endif