Toolbar.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //////////////////////////////////////////////////////////////////////
  19. //
  20. // Toolbar.CPP
  21. //
  22. // Implementation of a 'fancy' toolbar using hi-color buttons
  23. //
  24. #include "stdafx.h"
  25. #include "Toolbar.H"
  26. BEGIN_MESSAGE_MAP(CFancyToolbar, CControlBar)
  27. //{{AFX_MSG_MAP(CFancyToolbar)
  28. ON_WM_PAINT()
  29. ON_WM_LBUTTONDOWN()
  30. ON_WM_LBUTTONUP()
  31. //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33. //////////////////////////////////////////////////////////////
  34. //
  35. // Local Constants
  36. //
  37. const TCHAR * const TOOLBAR_CLASS_NAME = TEXT ("FANCYTOOLBAR");
  38. //////////////////////////////////////////////////////////////
  39. //
  40. // CFancyToolbar
  41. //
  42. CFancyToolbar::CFancyToolbar (void)
  43. : m_iButtons (0),
  44. m_iCurrentButton (-1)
  45. {
  46. // Ensure that the toolbar window class is registered
  47. RegisterFancyToolbarClass ();
  48. ::memset (m_pButtonArray, 0, sizeof (m_pButtonArray));
  49. return ;
  50. }
  51. //////////////////////////////////////////////////////////////
  52. //
  53. // ~CFancyToolbar
  54. //
  55. CFancyToolbar::~CFancyToolbar (void)
  56. {
  57. for (int iButton = 0; iButton < m_iButtons; iButton ++)
  58. {
  59. if (m_pButtonArray[iButton].hBMPUp)
  60. {
  61. // Free the BMP for this button
  62. ::DeleteObject (m_pButtonArray[iButton].hBMPUp);
  63. m_pButtonArray[iButton].hBMPUp = NULL;
  64. }
  65. if (m_pButtonArray[iButton].hBMPDn)
  66. {
  67. // Free the BMP for this button
  68. ::DeleteObject (m_pButtonArray[iButton].hBMPDn);
  69. m_pButtonArray[iButton].hBMPDn = NULL;
  70. }
  71. }
  72. return ;
  73. }
  74. //////////////////////////////////////////////////////////////
  75. //
  76. // RegisterFancyToolbarClass
  77. //
  78. void
  79. CFancyToolbar::RegisterFancyToolbarClass (void)
  80. {
  81. // Is this class already registered?
  82. WNDCLASS classInfo = { 0 };
  83. if (::GetClassInfo (::AfxGetInstanceHandle (),
  84. TOOLBAR_CLASS_NAME,
  85. &classInfo) != TRUE)
  86. {
  87. classInfo.style = CS_PARENTDC;
  88. classInfo.lpfnWndProc = ::DefWindowProc;
  89. classInfo.hInstance = ::AfxGetInstanceHandle ();
  90. classInfo.hCursor = ::LoadCursor (NULL, IDC_ARROW);
  91. classInfo.hbrBackground = (HBRUSH)COLOR_BTNFACE;
  92. classInfo.lpszClassName = TOOLBAR_CLASS_NAME;
  93. // Register the class with windows
  94. ::RegisterClass (&classInfo);
  95. }
  96. return ;
  97. }
  98. //////////////////////////////////////////////////////////////
  99. //
  100. // Create
  101. //
  102. void
  103. CFancyToolbar::OnPaint ()
  104. {
  105. Paint ();
  106. return ;
  107. }
  108. //////////////////////////////////////////////////////////////
  109. //
  110. // Create
  111. //
  112. BOOL
  113. CFancyToolbar::Create
  114. (
  115. LPCTSTR pszWindowName,
  116. CWnd *pCParentWnd,
  117. UINT uiID
  118. )
  119. {
  120. // Create the toolbar window using our own special window class
  121. RECT rect = { 0 };
  122. BOOL bReturn = CWnd::Create (TOOLBAR_CLASS_NAME, pszWindowName, WS_CHILD | WS_VISIBLE, rect, pCParentWnd, uiID);
  123. // Were we successful?
  124. ASSERT (bReturn);
  125. if (bReturn)
  126. {
  127. // Set the bar style, but don't allow docking
  128. SetBarStyle (GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  129. EnableDocking (CBRS_ALIGN_ANY);
  130. }
  131. // Return the TRUE/FALSE result code
  132. return bReturn;
  133. }
  134. //////////////////////////////////////////////////////////////
  135. //
  136. // DrawButton
  137. //
  138. void
  139. CFancyToolbar::DrawButton
  140. (
  141. HDC hDC,
  142. int iXPos,
  143. int iYPos,
  144. HBITMAP hBMP
  145. )
  146. {
  147. // Create a screen-compatible DC
  148. HDC hMemDC = ::CreateCompatibleDC (hDC);
  149. if (hMemDC)
  150. {
  151. // Select the BMP to paint into the DC
  152. HBITMAP hOldBMP = (HBITMAP)::SelectObject (hMemDC, hBMP);
  153. // Paint the BMP in its slot
  154. ::BitBlt (hDC, iXPos, iYPos, BUTTON_WIDTH, BUTTON_HEIGHT, hMemDC, 0, 0, SRCCOPY);
  155. // Select the old BMP back into the DC and free the DC
  156. ::SelectObject (hMemDC, hOldBMP);
  157. DeleteDC (hMemDC);
  158. }
  159. return ;
  160. }
  161. //////////////////////////////////////////////////////////////
  162. //
  163. // AddButton
  164. //
  165. void
  166. CFancyToolbar::AddButton
  167. (
  168. UINT iBMPUp,
  169. UINT iBMPDn,
  170. int iCommandID,
  171. BUTTON_TYPE buttonType
  172. )
  173. {
  174. // Increment the button count
  175. int iButton = m_iButtons ++;
  176. // Fill in the internal button structure for this entry
  177. m_pButtonArray[iButton].hBMPUp = ::LoadBitmap (::AfxGetResourceHandle (), MAKEINTRESOURCE (iBMPUp));
  178. m_pButtonArray[iButton].hBMPDn = ::LoadBitmap (::AfxGetResourceHandle (), MAKEINTRESOURCE (iBMPDn));
  179. m_pButtonArray[iButton].iCommandID = iCommandID;
  180. m_pButtonArray[iButton].buttonType = buttonType;
  181. m_pButtonArray[iButton].currentState = StateUp;
  182. m_pButtonArray[iButton].bVisible = TRUE;
  183. return ;
  184. }
  185. //////////////////////////////////////////////////////////////
  186. //
  187. // Paint
  188. //
  189. void
  190. CFancyToolbar::Paint (void)
  191. {
  192. // Get the window's DC
  193. HDC hDC = ::GetDC (m_hWnd);
  194. if (hDC)
  195. {
  196. // Get the bounding rectangle of the window
  197. RECT rect;
  198. ::GetClientRect (m_hWnd, &rect);
  199. // Paint the background light gray
  200. HBRUSH hBrush = ::CreateSolidBrush (RGB (192, 192, 192));
  201. ::FillRect (hDC, &rect, hBrush);
  202. ::DeleteObject (hBrush);
  203. // Loop through each button and paint it
  204. int iXPos = BORDER_LEFT;
  205. for (int iButton = 0; iButton < m_iButtons; iButton ++)
  206. {
  207. // Is this button visible?
  208. if (m_pButtonArray[iButton].bVisible)
  209. {
  210. // Determine which BMP to use
  211. HBITMAP hBMP = m_pButtonArray[iButton].hBMPUp;
  212. if (m_pButtonArray[iButton].currentState == StateDn)
  213. {
  214. hBMP = m_pButtonArray[iButton].hBMPDn;
  215. }
  216. // Draw this button
  217. DrawButton (hDC,
  218. iXPos,
  219. ((rect.bottom-rect.top) >> 1) - BUTTON_MIDDLE,
  220. hBMP);
  221. // Increment the current x position
  222. iXPos += BUTTON_WIDTH;
  223. }
  224. }
  225. // Free the windows DC
  226. ::ReleaseDC (m_hWnd, hDC);
  227. }
  228. // Let the window know its done painting
  229. ::ValidateRect (m_hWnd, NULL);
  230. return ;
  231. }
  232. //////////////////////////////////////////////////////////////
  233. //
  234. // ButtonFromPoint
  235. //
  236. int
  237. CFancyToolbar::ButtonFromPoint (const CPoint &point)
  238. {
  239. int iIndex = -1;
  240. // Loop through all the buttons until we've found the
  241. // one we're looking for
  242. int iXPos = BORDER_LEFT;
  243. for (int iButton = 0; (iButton < m_iButtons) && (iIndex == -1); iButton ++)
  244. {
  245. // Is this the button we're looking for?
  246. if (m_pButtonArray[iButton].bVisible &&
  247. (point.x >= iXPos) && (point.x <= iXPos + BUTTON_WIDTH) &&
  248. (point.y >= BORDER_TOP) && (point.y <= BORDER_TOP+BUTTON_HEIGHT))
  249. {
  250. // Yup, this is the button we're looking for
  251. iIndex = iButton;
  252. }
  253. // Increment the current position
  254. iXPos += BUTTON_WIDTH;
  255. }
  256. // Return the zero based index of the button we're looking for
  257. return iIndex;
  258. }
  259. //////////////////////////////////////////////////////////////
  260. //
  261. // HandleLButtonUp
  262. //
  263. void
  264. CFancyToolbar::OnLButtonDown
  265. (
  266. UINT nFlags,
  267. CPoint point
  268. )
  269. {
  270. // Determine which button was clicked
  271. int iButton = ButtonFromPoint (point);
  272. if (iButton >= 0)
  273. {
  274. // Flip the button state
  275. m_pButtonArray[iButton].currentState = (STATE_INFO)!m_pButtonArray[iButton].currentState;
  276. // Determine which BMP to paint
  277. HBITMAP hBMP = m_pButtonArray[iButton].hBMPUp;
  278. if (m_pButtonArray[iButton].currentState == StateDn)
  279. {
  280. hBMP = m_pButtonArray[iButton].hBMPDn;
  281. }
  282. // Get the DC for the window
  283. HDC hDC = ::GetDC (m_hWnd);
  284. if (hDC)
  285. {
  286. // Get the bounding rectangle for the window
  287. RECT rect;
  288. ::GetClientRect (m_hWnd, &rect);
  289. // Draw the selected button
  290. DrawButton (hDC,
  291. BORDER_LEFT + iButton*BUTTON_WIDTH,
  292. ((rect.bottom-rect.top) >> 1) - BUTTON_MIDDLE,
  293. hBMP);
  294. // Release the window's DC
  295. ::ReleaseDC (m_hWnd, hDC);
  296. }
  297. // Is this a 'normal' or 2 state button?
  298. if (m_pButtonArray[iButton].buttonType == TypeNormal)
  299. {
  300. // Normal button, so trap all mouse message so if the user
  301. // lets up the mouse button outside the button rectangle we don't do anything
  302. m_iCurrentButton = iButton;
  303. ::SetCapture (m_hWnd);
  304. }
  305. else
  306. {
  307. // 2 state button
  308. m_iCurrentButton = -1;
  309. // Send the message to the window's parent to let them know a command has occured
  310. ::AfxGetMainWnd ()->PostMessage (WM_COMMAND,
  311. MAKELONG (m_pButtonArray[iButton].iCommandID, BN_CLICKED),
  312. (LPARAM)m_hWnd);
  313. }
  314. }
  315. else
  316. {
  317. CControlBar::OnLButtonDown (nFlags, point);
  318. }
  319. return ;
  320. }
  321. //////////////////////////////////////////////////////////////
  322. //
  323. // HandleLButtonUp
  324. //
  325. void
  326. CFancyToolbar::OnLButtonUp
  327. (
  328. UINT nFlags,
  329. CPoint point
  330. )
  331. {
  332. // Were we currently processing a button?
  333. if (m_iCurrentButton >= 0)
  334. {
  335. // Which button were we processing?
  336. int iButton = ButtonFromPoint (point);
  337. if (iButton == m_iCurrentButton)
  338. {
  339. // Fire a command to the parent
  340. ::AfxGetMainWnd ()->PostMessage (WM_COMMAND,
  341. MAKELONG (m_pButtonArray[iButton].iCommandID, BN_CLICKED),
  342. (LPARAM)m_hWnd);
  343. }
  344. // Reset the button state
  345. m_pButtonArray[m_iCurrentButton].currentState = StateUp;
  346. // Get the window's DC
  347. HDC hDC = ::GetDC (m_hWnd);
  348. if (hDC)
  349. {
  350. // Get the bounding rectangle of the window
  351. RECT rect;
  352. ::GetClientRect (m_hWnd, &rect);
  353. // Paint the button
  354. DrawButton (hDC,
  355. BORDER_LEFT + m_iCurrentButton*BUTTON_WIDTH,
  356. ((rect.bottom-rect.top) >> 1) - BUTTON_MIDDLE,
  357. m_pButtonArray[m_iCurrentButton].hBMPUp);
  358. // Free the window's DC
  359. ::ReleaseDC (m_hWnd, hDC);
  360. }
  361. // Let go of the mouse capture
  362. ::ReleaseCapture ();
  363. }
  364. else
  365. {
  366. CControlBar::OnLButtonUp (nFlags, point);
  367. }
  368. return ;
  369. }
  370. //////////////////////////////////////////////////////////////
  371. //
  372. // OnDraw
  373. //
  374. void
  375. CFancyToolbar::OnDraw (CDC* pDC)
  376. {
  377. return ;
  378. }
  379. //////////////////////////////////////////////////////////////
  380. //
  381. // PreCreateWindow
  382. //
  383. BOOL
  384. CFancyToolbar::PreCreateWindow (CREATESTRUCT& cs)
  385. {
  386. // Allow the base class to process this message
  387. return CControlBar::PreCreateWindow (cs);
  388. }
  389. //////////////////////////////////////////////////////////////
  390. //
  391. // SetButtonState
  392. //
  393. void
  394. CFancyToolbar::SetButtonState
  395. (
  396. int iCommandID,
  397. STATE_INFO newState,
  398. BOOL bRepaint
  399. )
  400. {
  401. BOOL bFound = FALSE;
  402. // Loop through all the buttons until we've found the one we're looking for
  403. for (int iButton = 0;
  404. (iButton < m_iButtons) && (bFound == FALSE);
  405. iButton ++)
  406. {
  407. if (m_pButtonArray[iButton].iCommandID == iCommandID)
  408. {
  409. // Set the new state
  410. m_pButtonArray[iButton].currentState = newState;
  411. if (bRepaint)
  412. {
  413. // Repaint the toolbar
  414. //Paint ();
  415. InvalidateRect (NULL);
  416. UpdateWindow ();
  417. }
  418. // Found it!
  419. bFound = TRUE;
  420. }
  421. }
  422. return ;
  423. }
  424. //////////////////////////////////////////////////////////////
  425. //
  426. // GetButtonState
  427. //
  428. CFancyToolbar::STATE_INFO
  429. CFancyToolbar::GetButtonState (int iCommandID) const
  430. {
  431. STATE_INFO stateInfo = StateUp;
  432. // Loop through all the buttons until we've found the one we're looking for
  433. BOOL bFound = FALSE;
  434. for (int iButton = 0;
  435. (iButton < m_iButtons) && (bFound == FALSE);
  436. iButton ++)
  437. {
  438. if (m_pButtonArray[iButton].iCommandID == iCommandID)
  439. {
  440. // Return the current state to the caller
  441. stateInfo = m_pButtonArray[iButton].currentState;
  442. // Found it!
  443. bFound = TRUE;
  444. }
  445. }
  446. // Return the state of the requested button
  447. return stateInfo;
  448. }