Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1152 lines
23 KiB

  1. //
  2. // mxToolKit (c) 1999 by Mete Ciragan
  3. //
  4. // file: mx.cpp
  5. // implementation: Win32 API
  6. // last modified: Apr 18 1999, Mete Ciragan
  7. // copyright: The programs and associated files contained in this
  8. // distribution were developed by Mete Ciragan. The programs
  9. // are not in the public domain, but they are freely
  10. // distributable without licensing fees. These programs are
  11. // provided without guarantee or warrantee expressed or
  12. // implied.
  13. //
  14. #include "mxtk/mx.h"
  15. #include "mxtk/mxWindow.h"
  16. #include "mxtk/mxEvent.h"
  17. #include "mxtk/mxLinkedList.h"
  18. #include <windows.h>
  19. #include <commctrl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "tier1/utlvector.h"
  24. #define WM_MOUSEWHEEL 0x020A
  25. //#include <ostream.h"
  26. void mxTab_resizeChild (HWND hwnd);
  27. mxWindow *g_mainWindow = 0;
  28. static mxLinkedList *g_widgetList = 0;
  29. static mxWindow *g_idleWindow = 0;
  30. static MSG msg;
  31. static HWND g_hwndToolTipControl = 0;
  32. static bool isClosing = false;
  33. static HACCEL g_hAcceleratorTable = NULL;
  34. void mx::createAccleratorTable( int numentries, Accel_t *entries )
  35. {
  36. CUtlVector< ACCEL > accelentries;
  37. for ( int i = 0; i < numentries; ++i )
  38. {
  39. const Accel_t& entry = entries[ i ];
  40. ACCEL add;
  41. add.key = entry.key;
  42. add.cmd = entry.command;
  43. add.fVirt = 0;
  44. if ( entry.flags & ACCEL_ALT )
  45. {
  46. add.fVirt |= FALT;
  47. }
  48. if ( entry.flags & ACCEL_CONTROL )
  49. {
  50. add.fVirt |= FCONTROL;
  51. }
  52. if ( entry.flags & ACCEL_SHIFT )
  53. {
  54. add.fVirt |= FSHIFT;
  55. }
  56. if ( entry.flags & ACCEL_VIRTKEY )
  57. {
  58. add.fVirt |= FVIRTKEY;
  59. }
  60. accelentries.AddToTail( add );
  61. }
  62. g_hAcceleratorTable = ::CreateAcceleratorTable( accelentries.Base(), accelentries.Count() );
  63. }
  64. void
  65. mx_addWidget (mxWidget *widget)
  66. {
  67. if (g_widgetList)
  68. g_widgetList->add ((void *) widget);
  69. }
  70. void
  71. mx_removeWidget (mxWidget *widget)
  72. {
  73. if (g_widgetList)
  74. g_widgetList->remove ((void *) widget);
  75. }
  76. HWND
  77. mx_CreateToolTipControl ()
  78. {
  79. if (!g_hwndToolTipControl)
  80. {
  81. if (g_mainWindow)
  82. {
  83. g_hwndToolTipControl = CreateWindowEx (0, TOOLTIPS_CLASS, "", WS_POPUP | WS_EX_TOPMOST,
  84. 0, 0, 0, 0, (HWND) g_mainWindow->getHandle (),
  85. (HMENU) NULL, (HINSTANCE) GetModuleHandle (NULL), NULL);
  86. }
  87. }
  88. return g_hwndToolTipControl;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. // Input : *window -
  93. // *event -
  94. // Output : static void
  95. //-----------------------------------------------------------------------------
  96. static void RecursiveHandleEvent( mxWindow *window, mxEvent *event )
  97. {
  98. while ( window )
  99. {
  100. if ( window->handleEvent ( event ) )
  101. break;
  102. window = window->getParent();
  103. }
  104. }
  105. char const *translatecode( int code )
  106. {
  107. switch ( code )
  108. {
  109. case NM_CLICK:
  110. return "NM_CLICK";
  111. case NM_CUSTOMDRAW:
  112. return "NM_CUSTOMDRAW";
  113. case NM_DBLCLK:
  114. return "NM_DBLCLK";
  115. case NM_KILLFOCUS:
  116. return "NM_KILLFOCUS";
  117. case NM_RCLICK:
  118. return "NM_RCLICK";
  119. case NM_RETURN:
  120. return "NM_RETURN";
  121. case NM_SETCURSOR:
  122. return "NM_SETCURSOR";
  123. case NM_SETFOCUS:
  124. return "NM_SETFOCUS";
  125. case TVN_BEGINDRAG:
  126. return "TVN_BEGINDRAG";
  127. case TVN_BEGINLABELEDIT:
  128. return "TVN_BEGINLABELEDIT";
  129. case TVN_BEGINRDRAG:
  130. return "TVN_BEGINRDRAG";
  131. case TVN_DELETEITEM:
  132. return "TVN_DELETEITEM";
  133. case TVN_ENDLABELEDIT:
  134. return "TVN_ENDLABELEDIT";
  135. case TVN_GETDISPINFO:
  136. return "TVN_GETDISPINFO";
  137. case TVN_GETINFOTIP:
  138. return "TVN_GETINFOTIP";
  139. case TVN_ITEMEXPANDED:
  140. return "TVN_ITEMEXPANDED";
  141. case TVN_ITEMEXPANDING:
  142. return "TVN_ITEMEXPANDING";
  143. case TVN_KEYDOWN :
  144. return "TVN_KEYDOWN";
  145. case TVN_SELCHANGED :
  146. return "TVN_SELCHANGED";
  147. case TVN_SELCHANGING :
  148. return "TVN_SELCHANGING";
  149. case TVN_SETDISPINFO :
  150. return "TVN_SETDISPINFO";
  151. case TVN_SINGLEEXPAND:
  152. return "TVN_SINGLEEXPAND";
  153. }
  154. return "Unknown!!!";
  155. }
  156. static LRESULT CALLBACK WndProc (HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  157. {
  158. static bool bDragging = FALSE;
  159. switch (uMessage)
  160. {
  161. case WM_SETFOCUS:
  162. case WM_KILLFOCUS:
  163. {
  164. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  165. if ( window )
  166. {
  167. mxEvent event;
  168. event.event = mxEvent::Focus;
  169. event.widget = NULL;
  170. event.action = (uMessage == WM_SETFOCUS);
  171. RecursiveHandleEvent( window, &event );
  172. return 0;
  173. }
  174. }
  175. break;
  176. case WM_ACTIVATE:
  177. {
  178. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  179. if ( window )
  180. {
  181. mxEvent event;
  182. event.event = mxEvent::Activate;
  183. event.widget = NULL;
  184. event.action = (LOWORD( wParam ) != WA_INACTIVE);
  185. RecursiveHandleEvent( window, &event );
  186. return 0;
  187. }
  188. }
  189. break;
  190. case WM_COMMAND:
  191. {
  192. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  193. if (LOWORD (wParam) > 0 && window)
  194. {
  195. WORD wNotifyCode = (WORD) HIWORD (wParam);
  196. HWND hwndCtrl = (HWND) lParam;
  197. mxEvent event;
  198. CHAR className[128];
  199. GetClassName (hwndCtrl, className, 128);
  200. if (!strcmpi (className, "edit"))
  201. {
  202. if (wNotifyCode != EN_CHANGE)
  203. break;
  204. }
  205. else if (!strcmpi (className, "combobox"))
  206. {
  207. if (wNotifyCode != CBN_SELCHANGE)
  208. break;
  209. }
  210. else if (!strcmpi (className, "listbox"))
  211. {
  212. if (wNotifyCode != LBN_SELCHANGE)
  213. break;
  214. }
  215. event.event = mxEvent::Action;
  216. event.widget = (mxWidget *) GetWindowLong ((HWND) lParam, GWL_USERDATA);
  217. event.action = (int) LOWORD (wParam);
  218. RecursiveHandleEvent( window, &event );
  219. }
  220. }
  221. break;
  222. case WM_NOTIFY:
  223. {
  224. if (isClosing)
  225. break;
  226. NMHDR *nmhdr = (NMHDR *) lParam;
  227. mxEvent event;
  228. #if 0
  229. //if ( nmhdr->idFrom > 0 )
  230. {
  231. mxWidget *temp = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  232. if ( temp && temp->getType() == MX_TREEVIEW )
  233. {
  234. NMTREEVIEW *nmt = ( NMTREEVIEW * )nmhdr;
  235. HTREEITEM hItem = TreeView_GetSelection (nmhdr->hwndFrom);
  236. char sz[ 256 ];
  237. sprintf( sz, "tree view receiving notify %i : %s action %i old %p new %p selection %p\n", nmhdr->code, translatecode( nmhdr->code ),
  238. nmt->action, nmt->itemOld, nmt->itemNew, hItem );
  239. OutputDebugString( sz );
  240. }
  241. }
  242. #endif
  243. if (nmhdr->code == TVN_SELCHANGED)
  244. {
  245. if (nmhdr->idFrom > 0)
  246. {
  247. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  248. event.event = mxEvent::Action;
  249. event.widget = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  250. event.action = (int) nmhdr->idFrom;
  251. RECT rc;
  252. HTREEITEM hItem = TreeView_GetSelection (nmhdr->hwndFrom);
  253. TreeView_GetItemRect (nmhdr->hwndFrom, hItem, &rc, TRUE);
  254. event.x = (int) rc.left;
  255. event.y = (int) rc.bottom;
  256. RecursiveHandleEvent( window, &event );
  257. }
  258. }
  259. else if (nmhdr->code == LVN_ITEMCHANGED)
  260. {
  261. if (nmhdr->idFrom > 0)
  262. {
  263. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  264. event.event = mxEvent::Action;
  265. event.widget = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  266. event.action = (int) nmhdr->idFrom;
  267. RecursiveHandleEvent( window, &event );
  268. }
  269. }
  270. else if (nmhdr->code == NM_RCLICK)
  271. {
  272. if (nmhdr->idFrom > 0)
  273. {
  274. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  275. event.event = mxEvent::Action;
  276. event.widget = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  277. event.action = (int) nmhdr->idFrom;
  278. event.flags = mxEvent::RightClicked;
  279. if ( event.widget )
  280. {
  281. if ( event.widget->getType () == MX_TREEVIEW )
  282. {
  283. RECT rc;
  284. HTREEITEM hItem = TreeView_GetSelection (nmhdr->hwndFrom);
  285. TreeView_GetItemRect (nmhdr->hwndFrom, hItem, &rc, TRUE);
  286. event.x = (int) rc.left;
  287. event.y = (int) rc.bottom;
  288. }
  289. }
  290. RecursiveHandleEvent( window, &event );
  291. }
  292. }
  293. else if (nmhdr->code == NM_DBLCLK)
  294. {
  295. if (nmhdr->idFrom > 0)
  296. {
  297. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  298. event.event = mxEvent::Action;
  299. event.widget = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  300. event.action = (int) nmhdr->idFrom;
  301. event.flags = mxEvent::DoubleClicked;
  302. if (event.widget )
  303. {
  304. if ( event.widget->getType () == MX_TREEVIEW )
  305. {
  306. RECT rc;
  307. HTREEITEM hItem = TreeView_GetSelection (nmhdr->hwndFrom);
  308. TreeView_GetItemRect (nmhdr->hwndFrom, hItem, &rc, TRUE);
  309. event.x = (int) rc.left;
  310. event.y = (int) rc.bottom;
  311. }
  312. }
  313. RecursiveHandleEvent( window, &event );
  314. return TRUE;
  315. }
  316. }
  317. else if (nmhdr->code == TCN_SELCHANGING)
  318. {
  319. TC_ITEM ti;
  320. int index = TabCtrl_GetCurSel (nmhdr->hwndFrom);
  321. if (index >= 0)
  322. {
  323. ti.mask = TCIF_PARAM;
  324. TabCtrl_GetItem (nmhdr->hwndFrom, index, &ti);
  325. mxWindow *window = (mxWindow *) ti.lParam;
  326. if (window)
  327. window->setVisible (false);
  328. }
  329. }
  330. else if (nmhdr->code == TCN_SELCHANGE)
  331. {
  332. mxTab_resizeChild (nmhdr->hwndFrom);
  333. if (nmhdr->idFrom > 0)
  334. {
  335. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  336. event.event = mxEvent::Action;
  337. event.widget = (mxWidget *) GetWindowLong (nmhdr->hwndFrom, GWL_USERDATA);
  338. event.action = (int) nmhdr->idFrom;
  339. RecursiveHandleEvent( window, &event );
  340. }
  341. }
  342. }
  343. break;
  344. case WM_SIZE:
  345. {
  346. mxEvent event;
  347. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  348. if (window)
  349. {
  350. event.event = mxEvent::Size;
  351. event.width = (int) LOWORD (lParam);
  352. event.height = (int) HIWORD (lParam);
  353. window->handleEvent (&event);
  354. }
  355. }
  356. break;
  357. case WM_WINDOWPOSCHANGED:
  358. {
  359. mxEvent event;
  360. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  361. if (window)
  362. {
  363. event.event = mxEvent::PosChanged;
  364. WINDOWPOS *wp = ( WINDOWPOS * )lParam;
  365. event.x = wp->x;
  366. event.y = wp->y;
  367. event.width = wp->cx;
  368. event.height = wp->cy;
  369. window->handleEvent (&event);
  370. }
  371. }
  372. break;
  373. case WM_ERASEBKGND:
  374. {
  375. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  376. if (window)
  377. {
  378. if (window->getType () == MX_GLWINDOW)
  379. return 0;
  380. if (window->getType () == MX_MATSYSWINDOW)
  381. return 0;
  382. if ( !isClosing && !window->PaintBackground() )
  383. {
  384. return 0;
  385. }
  386. }
  387. }
  388. break;
  389. case WM_HSCROLL:
  390. case WM_VSCROLL:
  391. {
  392. mxWidget *widget = (mxWidget *) GetWindowLong ((HWND) lParam, GWL_USERDATA);
  393. if (!widget)
  394. {
  395. break;
  396. }
  397. if (widget->getType() != MX_SCROLLBAR && widget->getType() != MX_SLIDER)
  398. {
  399. break;
  400. }
  401. switch (LOWORD (wParam))
  402. {
  403. case TB_LINEUP: // SB_LINEUP SB_LINELEFT
  404. break;
  405. case TB_LINEDOWN: // SB_LINEDOWN SB_LINERIGHT
  406. break;
  407. case TB_PAGEUP: // SB_PAGEUP SB_PAGELEFT
  408. break;
  409. case TB_PAGEDOWN: // SB_PAGEDOWN SB_PAGERIGHT
  410. break;
  411. case TB_THUMBPOSITION: // SB_THUMBPOSITION
  412. break;
  413. case TB_THUMBTRACK: // SB_THUMBTRACK
  414. break;
  415. case TB_TOP: // SB_TOP SB_LEFT
  416. break;
  417. case TB_BOTTOM: // SB_BOTTOM SB_RIGHT
  418. break;
  419. case TB_ENDTRACK: // SB_ENDSCROLL
  420. break;
  421. default:
  422. break;
  423. }
  424. switch (LOWORD (wParam))
  425. {
  426. case TB_LINEUP: // SB_LINEUP SB_LINELEFT
  427. case TB_LINEDOWN: // SB_LINEDOWN SB_LINERIGHT
  428. case TB_PAGEUP: // SB_PAGEUP SB_PAGELEFT
  429. case TB_PAGEDOWN: // SB_PAGEDOWN SB_PAGERIGHT
  430. case TB_THUMBPOSITION: // SB_THUMBPOSITION
  431. case TB_THUMBTRACK: // SB_THUMBTRACK
  432. case TB_TOP: // SB_TOP SB_LEFT
  433. case TB_BOTTOM: // SB_BOTTOM SB_RIGHT
  434. case TB_ENDTRACK: // SB_ENDSCROLL
  435. {
  436. mxEvent event;
  437. event.event = mxEvent::Action;
  438. event.widget = widget;
  439. event.action = widget->getId ();
  440. event.modifiers = LOWORD (wParam);
  441. event.height = HIWORD( wParam );
  442. mxWindow *window = widget->getParent ();
  443. if ( event.action > 0 )
  444. {
  445. RecursiveHandleEvent( window, &event );
  446. }
  447. }
  448. break;
  449. }
  450. }
  451. break;
  452. case WM_PAINT:
  453. {
  454. if ( !isClosing )
  455. {
  456. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  457. if (window)
  458. {
  459. window->redraw ();
  460. }
  461. }
  462. }
  463. break;
  464. case WM_PARENTNOTIFY:
  465. {
  466. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  467. if (window)
  468. {
  469. if ( wParam == WM_LBUTTONDOWN ||
  470. wParam == WM_MBUTTONDOWN ||
  471. wParam == WM_RBUTTONDOWN /*||
  472. wParam & WM_XBUTTONDOWN*/ )
  473. {
  474. mxEvent event;
  475. event.event = mxEvent::ParentNotify;
  476. event.x = (short)LOWORD (lParam);
  477. event.y = (short)HIWORD (lParam);
  478. event.buttons = 0;
  479. event.modifiers = 0;
  480. if ( wParam == WM_LBUTTONDOWN )
  481. event.buttons |= mxEvent::MouseLeftButton;
  482. if ( wParam == WM_RBUTTONDOWN )
  483. event.buttons |= mxEvent::MouseRightButton;
  484. if ( wParam == WM_MBUTTONDOWN )
  485. event.buttons |= mxEvent::MouseMiddleButton;
  486. window->handleEvent (&event);
  487. RecursiveHandleEvent( window, &event );
  488. return 0;
  489. }
  490. }
  491. }
  492. break;
  493. case WM_LBUTTONDOWN:
  494. case WM_MBUTTONDOWN:
  495. case WM_RBUTTONDOWN:
  496. {
  497. bDragging = TRUE;
  498. SetCapture (hwnd);
  499. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  500. if (window)
  501. {
  502. mxEvent event;
  503. event.event = mxEvent::MouseDown;
  504. event.x = (short)LOWORD (lParam);
  505. event.y = (short)HIWORD (lParam);
  506. event.buttons = 0;
  507. event.modifiers = 0;
  508. if (uMessage == WM_MBUTTONDOWN)
  509. event.buttons |= mxEvent::MouseMiddleButton;
  510. else if (uMessage == WM_RBUTTONDOWN)
  511. event.buttons |= mxEvent::MouseRightButton;
  512. else
  513. event.buttons |= mxEvent::MouseLeftButton;
  514. if (wParam & MK_LBUTTON)
  515. event.buttons |= mxEvent::MouseLeftButton;
  516. if (wParam & MK_RBUTTON)
  517. event.buttons |= mxEvent::MouseRightButton;
  518. if (wParam & MK_MBUTTON)
  519. event.buttons |= mxEvent::MouseMiddleButton;
  520. if (wParam & MK_CONTROL)
  521. event.modifiers |= mxEvent::KeyCtrl;
  522. if (wParam & MK_SHIFT)
  523. event.modifiers |= mxEvent::KeyShift;
  524. window->handleEvent (&event);
  525. }
  526. }
  527. break;
  528. case WM_LBUTTONUP:
  529. case WM_MBUTTONUP:
  530. case WM_RBUTTONUP:
  531. {
  532. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  533. if (window)
  534. {
  535. mxEvent event;
  536. event.event = mxEvent::MouseUp;
  537. event.x = (short) LOWORD (lParam);
  538. event.y = (short) HIWORD (lParam);
  539. event.buttons = 0;
  540. event.modifiers = 0;
  541. if (uMessage == WM_MBUTTONUP)
  542. event.buttons |= mxEvent::MouseMiddleButton;
  543. else if (uMessage == WM_RBUTTONUP)
  544. event.buttons |= mxEvent::MouseRightButton;
  545. else
  546. event.buttons |= mxEvent::MouseLeftButton;
  547. if (wParam & MK_LBUTTON)
  548. event.buttons |= mxEvent::MouseLeftButton;
  549. if (wParam & MK_RBUTTON)
  550. event.buttons |= mxEvent::MouseRightButton;
  551. if (wParam & MK_MBUTTON)
  552. event.buttons |= mxEvent::MouseMiddleButton;
  553. if (wParam & MK_CONTROL)
  554. event.modifiers |= mxEvent::KeyCtrl;
  555. if (wParam & MK_SHIFT)
  556. event.modifiers |= mxEvent::KeyShift;
  557. window->handleEvent (&event);
  558. }
  559. bDragging = FALSE;
  560. ReleaseCapture ();
  561. }
  562. break;
  563. case WM_MOUSEMOVE:
  564. {
  565. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  566. if (window)
  567. {
  568. mxEvent event;
  569. if (bDragging)
  570. event.event = mxEvent::MouseDrag;
  571. else
  572. event.event = mxEvent::MouseMove;
  573. event.x = (short) LOWORD (lParam);
  574. event.y = (short) HIWORD (lParam);
  575. event.buttons = 0;
  576. event.modifiers = 0;
  577. if (wParam & MK_LBUTTON)
  578. event.buttons |= mxEvent::MouseLeftButton;
  579. if (wParam & MK_RBUTTON)
  580. event.buttons |= mxEvent::MouseRightButton;
  581. if (wParam & MK_MBUTTON)
  582. event.buttons |= mxEvent::MouseMiddleButton;
  583. if (wParam & MK_CONTROL)
  584. event.modifiers |= mxEvent::KeyCtrl;
  585. if (wParam & MK_SHIFT)
  586. event.modifiers |= mxEvent::KeyShift;
  587. window->handleEvent (&event);
  588. }
  589. }
  590. break;
  591. case WM_NCLBUTTONDOWN:
  592. case WM_NCMBUTTONDOWN:
  593. case WM_NCRBUTTONDOWN:
  594. {
  595. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  596. if (window)
  597. {
  598. mxEvent event;
  599. event.event = mxEvent::NCMouseDown;
  600. event.x = (short) LOWORD (lParam);
  601. event.y = (short) HIWORD (lParam);
  602. event.buttons = 0;
  603. event.modifiers = 0;
  604. if (uMessage == WM_NCMBUTTONDOWN)
  605. event.buttons |= mxEvent::MouseMiddleButton;
  606. else if (uMessage == WM_NCRBUTTONDOWN)
  607. event.buttons |= mxEvent::MouseRightButton;
  608. else
  609. event.buttons |= mxEvent::MouseLeftButton;
  610. window->handleEvent (&event);
  611. }
  612. }
  613. break;
  614. case WM_NCLBUTTONUP:
  615. case WM_NCMBUTTONUP:
  616. case WM_NCRBUTTONUP:
  617. {
  618. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  619. if (window)
  620. {
  621. mxEvent event;
  622. event.event = mxEvent::NCMouseUp;
  623. event.x = (short) LOWORD (lParam);
  624. event.y = (short) HIWORD (lParam);
  625. event.buttons = 0;
  626. event.modifiers = 0;
  627. if (uMessage == WM_NCMBUTTONUP)
  628. event.buttons |= mxEvent::MouseMiddleButton;
  629. else if (uMessage == WM_NCRBUTTONUP)
  630. event.buttons |= mxEvent::MouseRightButton;
  631. else
  632. event.buttons |= mxEvent::MouseLeftButton;
  633. window->handleEvent (&event);
  634. }
  635. }
  636. break;
  637. case WM_NCMOUSEMOVE:
  638. {
  639. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  640. if (window)
  641. {
  642. mxEvent event;
  643. event.event = mxEvent::NCMouseMove;
  644. event.x = (short) LOWORD (lParam);
  645. event.y = (short) HIWORD (lParam);
  646. event.buttons = 0;
  647. event.modifiers = 0;
  648. window->handleEvent (&event);
  649. }
  650. }
  651. break;
  652. case WM_KEYDOWN:
  653. case WM_SYSKEYDOWN:
  654. {
  655. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  656. if (window)
  657. {
  658. mxEvent event;
  659. event.event = mxEvent::KeyDown;
  660. event.key = (int) wParam;
  661. if ( window->handleEvent (&event) )
  662. return 0;
  663. }
  664. }
  665. break;
  666. case WM_CHAR:
  667. {
  668. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  669. if (window)
  670. {
  671. mxEvent event;
  672. event.event = mxEvent::Char;
  673. event.key = (int) wParam;
  674. if ( window->handleEvent (&event) )
  675. return 0;
  676. }
  677. }
  678. break;
  679. case WM_SYSCHAR:
  680. return 0;
  681. break;
  682. case WM_KEYUP:
  683. case WM_SYSKEYUP:
  684. {
  685. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  686. if (window)
  687. {
  688. mxEvent event;
  689. event.event = mxEvent::KeyUp;
  690. event.key = (int) wParam;
  691. if ( window->handleEvent (&event) )
  692. return 0;
  693. }
  694. }
  695. break;
  696. case WM_MOUSEWHEEL:
  697. {
  698. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  699. if (window)
  700. {
  701. mxEvent event;
  702. memset( &event, 0, sizeof( event ) );
  703. event.event = mxEvent::MouseWheeled;
  704. event.x = (short) LOWORD (lParam);
  705. event.y = (short) HIWORD (lParam);
  706. if (wParam & MK_LBUTTON)
  707. event.buttons |= mxEvent::MouseLeftButton;
  708. if (wParam & MK_RBUTTON)
  709. event.buttons |= mxEvent::MouseRightButton;
  710. if (wParam & MK_MBUTTON)
  711. event.buttons |= mxEvent::MouseMiddleButton;
  712. if (wParam & MK_CONTROL)
  713. event.modifiers |= mxEvent::KeyCtrl;
  714. if (wParam & MK_SHIFT)
  715. event.modifiers |= mxEvent::KeyShift;
  716. event.height = (short)HIWORD( wParam );;
  717. RecursiveHandleEvent( window, &event );
  718. }
  719. }
  720. break;
  721. case WM_TIMER:
  722. {
  723. if (isClosing)
  724. break;
  725. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  726. if (window)
  727. {
  728. mxEvent event;
  729. event.event = mxEvent::Timer;
  730. window->handleEvent (&event);
  731. }
  732. }
  733. break;
  734. case WM_CLOSE:
  735. if (g_mainWindow)
  736. {
  737. if ((void *) hwnd == g_mainWindow->getHandle ())
  738. {
  739. mx::quit ();
  740. }
  741. else
  742. {
  743. ShowWindow (hwnd, SW_HIDE);
  744. mxWindow *window = (mxWindow *) GetWindowLong (hwnd, GWL_USERDATA);
  745. if (window)
  746. {
  747. mxEvent event;
  748. event.event = mxEvent::Close;
  749. window->handleEvent( &event );
  750. }
  751. }
  752. }
  753. //else // shouldn't happen
  754. //DestroyWindow (hwnd);
  755. return 0;
  756. /*
  757. case WM_DESTROY:
  758. if (g_mainWindow)
  759. {
  760. if ((void *) hwnd == g_mainWindow->getHandle ())
  761. mx::quit ();
  762. }
  763. break;
  764. */
  765. }
  766. return DefWindowProc (hwnd, uMessage, wParam, lParam);
  767. }
  768. int
  769. mx::init(int argc, char **argv)
  770. {
  771. WNDCLASS wc;
  772. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  773. wc.lpfnWndProc = WndProc;
  774. wc.cbClsExtra = 0;
  775. wc.cbWndExtra = 0;
  776. wc.hInstance = (HINSTANCE) GetModuleHandle (NULL);
  777. wc.hIcon = LoadIcon (wc.hInstance, "MX_ICON");
  778. wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  779. wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  780. wc.lpszMenuName = NULL;
  781. wc.lpszClassName = "mx_class";
  782. if (!wc.hIcon)
  783. wc.hIcon = LoadIcon (NULL, IDI_WINLOGO);
  784. if (!RegisterClass (&wc))
  785. return 0;
  786. InitCommonControls ();
  787. g_widgetList = new mxLinkedList ();
  788. isClosing = false;
  789. return 1;
  790. }
  791. int
  792. mx::run()
  793. {
  794. int messagecount = 0;
  795. while (1)
  796. {
  797. bool doframe = false;
  798. if ( PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE) || !g_idleWindow )
  799. {
  800. if (!GetMessage (&msg, NULL, 0, 0))
  801. {
  802. doframe = false;
  803. break;
  804. }
  805. if ( !g_hAcceleratorTable ||
  806. !TranslateAccelerator( (HWND)g_mainWindow->getHandle (), g_hAcceleratorTable, &msg ))
  807. {
  808. TranslateMessage( &msg );
  809. DispatchMessage( &msg );
  810. }
  811. messagecount++;
  812. if ( messagecount > 10 )
  813. {
  814. messagecount = 0;
  815. doframe = true;
  816. }
  817. }
  818. else if (g_idleWindow)
  819. {
  820. doframe = true;
  821. messagecount = 0;
  822. }
  823. if ( doframe && g_idleWindow )
  824. {
  825. mxEvent event;
  826. event.event = mxEvent::Idle;
  827. g_idleWindow->handleEvent (&event);
  828. }
  829. }
  830. return msg.wParam;
  831. }
  832. int
  833. mx::check ()
  834. {
  835. if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
  836. {
  837. if (GetMessage (&msg, NULL, 0, 0))
  838. {
  839. TranslateMessage (&msg);
  840. DispatchMessage (&msg);
  841. }
  842. return 1;
  843. }
  844. return 0;
  845. }
  846. void
  847. mx::quit ()
  848. {
  849. isClosing = true;
  850. mxWindow *mainwnd = getMainWindow();
  851. if ( mainwnd )
  852. {
  853. if ( !mainwnd->Closing() )
  854. {
  855. isClosing = false;
  856. return;
  857. }
  858. }
  859. if (g_widgetList)
  860. {
  861. // remove from back to front
  862. mxListNode *node = g_widgetList->getLast ();
  863. // Pass 1, see if anyone objects to closing
  864. while (node)
  865. {
  866. mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
  867. node = g_widgetList->getPrev (node);
  868. bool canclose = true;
  869. if ( widget )
  870. {
  871. if ( !widget->CanClose() )
  872. {
  873. canclose = false;
  874. }
  875. }
  876. if ( !canclose )
  877. {
  878. isClosing = false;
  879. return;
  880. }
  881. }
  882. node = g_widgetList->getLast ();
  883. // Pass 2, call OnDelete to allow final cleanup
  884. while (node)
  885. {
  886. mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
  887. node = g_widgetList->getPrev (node);
  888. if ( widget )
  889. {
  890. widget->OnDelete();
  891. }
  892. }
  893. node = g_widgetList->getLast ();
  894. // Pass 3, delete stuff
  895. while (node)
  896. {
  897. mxWidget *widget = (mxWidget *) g_widgetList->getData (node);
  898. node = g_widgetList->getPrev (node);
  899. // remove it!
  900. if ( widget )
  901. {
  902. delete widget;
  903. }
  904. }
  905. delete g_widgetList;
  906. }
  907. if (g_hwndToolTipControl)
  908. DestroyWindow (g_hwndToolTipControl);
  909. if ( g_hAcceleratorTable )
  910. {
  911. DestroyAcceleratorTable( g_hAcceleratorTable );
  912. g_hAcceleratorTable = 0;
  913. }
  914. PostQuitMessage (0);
  915. UnregisterClass ("mx_class", (HINSTANCE) GetModuleHandle (NULL));
  916. }
  917. int
  918. mx::setDisplayMode (int w, int h, int bpp)
  919. {
  920. DEVMODE dm;
  921. dm.dmSize = sizeof (DEVMODE);
  922. dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  923. dm.dmBitsPerPel = bpp;
  924. dm.dmPelsWidth = w;
  925. dm.dmPelsHeight = h;
  926. if (w == 0 || h == 0 || bpp == 0)
  927. ChangeDisplaySettings (0, 0);
  928. else
  929. ChangeDisplaySettings (&dm, CDS_FULLSCREEN);
  930. return 0;
  931. }
  932. void
  933. mx::setIdleWindow (mxWindow *window)
  934. {
  935. g_idleWindow = window;
  936. }
  937. int
  938. mx::getDisplayWidth ()
  939. {
  940. return (int) GetSystemMetrics (SM_CXSCREEN);
  941. }
  942. int
  943. mx::getDisplayHeight ()
  944. {
  945. return (int) GetSystemMetrics (SM_CYSCREEN);
  946. }
  947. mxWindow*
  948. mx::getMainWindow ()
  949. {
  950. return g_mainWindow;
  951. }
  952. const char *
  953. mx::getApplicationPath ()
  954. {
  955. static char path[256];
  956. GetModuleFileName (0, path, 256);
  957. char *ptr = strrchr (path, '\\');
  958. if (ptr)
  959. *ptr = '\0';
  960. return path;
  961. }
  962. int
  963. mx::getTickCount ()
  964. {
  965. return (int) GetTickCount ();
  966. }