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.

227 lines
6.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include <commctrl.h>
  9. #include "TitleWnd.h"
  10. #include "MainFrm.h"
  11. #include "Resource.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. CFont CTitleWnd::m_FontNormal;
  15. CFont CTitleWnd::m_FontActive;
  16. BEGIN_MESSAGE_MAP(CTitleWnd, CWnd)
  17. ON_WM_PAINT()
  18. ON_WM_RBUTTONDOWN()
  19. ON_WM_LBUTTONDOWN()
  20. ON_WM_MOUSEMOVE()
  21. ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
  22. END_MESSAGE_MAP()
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Factory. Registers the title window class if necessary and creates
  25. // a title window.
  26. // Input : pwndParent - Pointer to parent window.
  27. // uID - Window ID to use for the title window.
  28. // Output : Returns a pointer to the newly created title window.
  29. //-----------------------------------------------------------------------------
  30. CTitleWnd *CTitleWnd::CreateTitleWnd(CWnd *pwndParent, UINT uID)
  31. {
  32. //
  33. // Register the window class if we have not done so already.
  34. //
  35. static CString strTitleWndClass;
  36. if (strTitleWndClass.IsEmpty())
  37. {
  38. strTitleWndClass = AfxRegisterWndClass(CS_BYTEALIGNCLIENT, AfxGetApp()->LoadStandardCursor(IDC_ARROW), HBRUSH(GetStockObject(BLACK_BRUSH)));
  39. }
  40. //
  41. // Create the title window.
  42. //
  43. CTitleWnd *pWnd = new CTitleWnd();
  44. if (pWnd != NULL)
  45. {
  46. pWnd->Create(strTitleWndClass, "Title Window", WS_CHILD | WS_VISIBLE, CRect(0, 0, 5, 5), pwndParent, uID);
  47. }
  48. return(pWnd);
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose: Constructor. Creates fonts the first time it is called.
  52. //-----------------------------------------------------------------------------
  53. CTitleWnd::CTitleWnd(void)
  54. {
  55. if (!m_FontNormal.m_hObject)
  56. {
  57. //
  58. // Create two fonts, a normal one and a bold one for when we are active.
  59. //
  60. m_FontNormal.CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  61. m_FontActive.CreateFont(16, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  62. }
  63. m_bMenuOpen = false;
  64. m_bMouseOver = false;
  65. m_szTitle[0] = '\0';
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: Sets the text to display in the window. The window size is recalculated
  69. // to ensure that the text fits.
  70. // Input : pszTitle - Text to display in the window.
  71. //-----------------------------------------------------------------------------
  72. void CTitleWnd::SetTitle(LPCTSTR pszTitle)
  73. {
  74. Assert(pszTitle != NULL);
  75. if (pszTitle != NULL)
  76. {
  77. strcpy(m_szTitle, pszTitle);
  78. if (::IsWindow(m_hWnd))
  79. {
  80. CDC *pDC = GetDC();
  81. if (pDC != NULL)
  82. {
  83. pDC->SelectObject(&m_FontActive);
  84. CSize TextSize = pDC->GetTextExtent(m_szTitle, strlen(m_szTitle));
  85. SetWindowPos(NULL, 0, 0, TextSize.cx, TextSize.cy, SWP_NOMOVE | SWP_NOZORDER);
  86. Invalidate();
  87. UpdateWindow();
  88. }
  89. }
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Turns off the active font when the mouse leaves our client area.
  94. // Input : Per WM_MOUSELEAVE.
  95. //-----------------------------------------------------------------------------
  96. LRESULT CTitleWnd::OnMouseLeave(WPARAM wParam, LPARAM lParam)
  97. {
  98. m_bMouseOver = false;
  99. Invalidate();
  100. UpdateWindow();
  101. return(0);
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose: Enables mouse tracking so we can render with a special font when
  105. // the mouse floats over the window.
  106. // Input : Per MFC OnMouseMove.
  107. //-----------------------------------------------------------------------------
  108. void CTitleWnd::OnMouseMove(UINT nFlags, CPoint point)
  109. {
  110. if (!m_bMouseOver)
  111. {
  112. TRACKMOUSEEVENT Track;
  113. Track.cbSize = sizeof(Track);
  114. Track.dwFlags = TME_HOVER | TME_LEAVE;
  115. Track.hwndTrack = m_hWnd;
  116. Track.dwHoverTime = 0.1;
  117. _TrackMouseEvent(&Track);
  118. m_bMouseOver = true;
  119. Invalidate();
  120. UpdateWindow();
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose: Renders the title window. A special font is used if the mouse is
  125. // over the title window or if the window's menu is open.
  126. //-----------------------------------------------------------------------------
  127. void CTitleWnd::OnPaint(void)
  128. {
  129. if (m_szTitle[0] != '\0')
  130. {
  131. if (GetUpdateRect(NULL, TRUE))
  132. {
  133. CPaintDC dc(this);
  134. CFont *pFontOld;
  135. if ((m_bMouseOver) || (m_bMenuOpen))
  136. {
  137. pFontOld = dc.SelectObject(&m_FontActive);
  138. dc.SetTextColor(RGB(255, 255, 255));
  139. }
  140. else
  141. {
  142. pFontOld = dc.SelectObject(&m_FontNormal);
  143. dc.SetTextColor(RGB(200, 200, 200));
  144. }
  145. dc.SetBkMode(TRANSPARENT);
  146. dc.TextOut(0, 0, m_szTitle, strlen(m_szTitle));
  147. dc.SelectObject(pFontOld);
  148. }
  149. }
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose: Opens the context menu when right-clicked upon.
  153. // Input : Per MFC OnRightButtonDown.
  154. //-----------------------------------------------------------------------------
  155. void CTitleWnd::OnLButtonDown(UINT nFlags, CPoint point)
  156. {
  157. OnMouseButton();
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Purpose: Opens the context menu when right-clicked upon.
  161. // Input : Per MFC OnRightButtonDown.
  162. //-----------------------------------------------------------------------------
  163. void CTitleWnd::OnRButtonDown(UINT nFlags, CPoint point)
  164. {
  165. OnMouseButton();
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose: Opens the context menu when right-clicked upon.
  169. // Input : Per MFC OnRightButtonDown.
  170. //-----------------------------------------------------------------------------
  171. void CTitleWnd::OnMouseButton(void)
  172. {
  173. static BOOL bFirstTime = TRUE;
  174. static CMenu Menu;
  175. if (bFirstTime)
  176. {
  177. Menu.LoadMenu(IDR_POPUPS);
  178. bFirstTime = FALSE;
  179. }
  180. CMenu *pPopupMenu = Menu.GetSubMenu(5);
  181. Assert(pPopupMenu);
  182. CRect rect;
  183. GetClientRect(&rect);
  184. CPoint MenuLocation(0, rect.bottom);
  185. ClientToScreen(&MenuLocation);
  186. m_bMenuOpen = true;
  187. pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, MenuLocation.x, MenuLocation.y, (CWnd *)GetMainWnd(), NULL);
  188. m_bMenuOpen = false;
  189. }