UIDateTime.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "stdafx.h"
  2. #include "UIDateTime.h"
  3. namespace DuiLib
  4. {
  5. //CDateTimeUI::m_nDTUpdateFlag
  6. #define DT_NONE 0
  7. #define DT_UPDATE 1
  8. #define DT_DELETE 2
  9. #define DT_KEEP 3
  10. class CDateTimeWnd : public CWindowWnd
  11. {
  12. public:
  13. CDateTimeWnd();
  14. void Init(CDateTimeUI* pOwner);
  15. RECT CalPos();
  16. LPCTSTR GetWindowClassName() const;
  17. LPCTSTR GetSuperClassName() const;
  18. void OnFinalMessage(HWND hWnd);
  19. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  20. LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  21. //LRESULT OnEditChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  22. protected:
  23. CDateTimeUI* m_pOwner;
  24. HBRUSH m_hBkBrush;
  25. bool m_bInit;
  26. };
  27. CDateTimeWnd::CDateTimeWnd() : m_pOwner(NULL), m_hBkBrush(NULL), m_bInit(false)
  28. {
  29. }
  30. void CDateTimeWnd::Init(CDateTimeUI* pOwner)
  31. {
  32. m_pOwner = pOwner;
  33. m_pOwner->m_nDTUpdateFlag = DT_NONE;
  34. if (m_hWnd == NULL)
  35. {
  36. RECT rcPos = CalPos();
  37. UINT uStyle = WS_CHILD;
  38. Create(m_pOwner->GetManager()->GetPaintWindow(), NULL, uStyle, 0, rcPos);
  39. SetWindowFont(m_hWnd, m_pOwner->GetManager()->GetFontInfo(m_pOwner->GetFont())->hFont, TRUE);
  40. }
  41. if (m_pOwner->GetText().IsEmpty())
  42. ::GetLocalTime(&m_pOwner->m_sysTime);
  43. ::SendMessage(m_hWnd, DTM_SETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
  44. ::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
  45. ::SetFocus(m_hWnd);
  46. m_bInit = true;
  47. }
  48. RECT CDateTimeWnd::CalPos()
  49. {
  50. CDuiRect rcPos = m_pOwner->GetPos();
  51. CControlUI* pParent = m_pOwner;
  52. RECT rcParent;
  53. while( pParent = pParent->GetParent() ) {
  54. if( !pParent->IsVisible() ) {
  55. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  56. break;
  57. }
  58. rcParent = pParent->GetClientPos();
  59. if( !::IntersectRect(&rcPos, &rcPos, &rcParent) ) {
  60. rcPos.left = rcPos.top = rcPos.right = rcPos.bottom = 0;
  61. break;
  62. }
  63. }
  64. return rcPos;
  65. }
  66. LPCTSTR CDateTimeWnd::GetWindowClassName() const
  67. {
  68. return _T("DateTimeWnd");
  69. }
  70. LPCTSTR CDateTimeWnd::GetSuperClassName() const
  71. {
  72. return DATETIMEPICK_CLASS;
  73. }
  74. void CDateTimeWnd::OnFinalMessage(HWND hWnd)
  75. {
  76. // Clear reference and die
  77. if( m_hBkBrush != NULL ) ::DeleteObject(m_hBkBrush);
  78. m_pOwner->GetManager()->RemoveNativeWindow(hWnd);
  79. m_pOwner->m_pWindow = NULL;
  80. delete this;
  81. }
  82. LRESULT CDateTimeWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  83. {
  84. LRESULT lRes = 0;
  85. BOOL bHandled = TRUE;
  86. if( uMsg == WM_CREATE ) {
  87. m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd);
  88. bHandled = FALSE;
  89. }
  90. else if( uMsg == WM_KILLFOCUS )
  91. {
  92. lRes = OnKillFocus(uMsg, wParam, lParam, bHandled);
  93. }
  94. else if (uMsg == WM_KEYUP && (wParam == VK_DELETE || wParam == VK_BACK))
  95. {
  96. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  97. m_pOwner->m_nDTUpdateFlag = DT_DELETE;
  98. m_pOwner->UpdateText();
  99. PostMessage(WM_CLOSE);
  100. return lRes;
  101. }
  102. else if (uMsg == WM_KEYUP && wParam == VK_ESCAPE)
  103. {
  104. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  105. m_pOwner->m_nDTUpdateFlag = DT_KEEP;
  106. PostMessage(WM_CLOSE);
  107. return lRes;
  108. }
  109. // else if( uMsg == OCM_COMMAND ) {
  110. // if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE ) lRes = OnEditChanged(uMsg, wParam, lParam, bHandled);
  111. // else if( GET_WM_COMMAND_CMD(wParam, lParam) == EN_UPDATE ) {
  112. // RECT rcClient;
  113. // ::GetClientRect(m_hWnd, &rcClient);
  114. // ::InvalidateRect(m_hWnd, &rcClient, FALSE);
  115. // }
  116. // }
  117. // else if( uMsg == WM_KEYDOWN && TCHAR(wParam) == VK_RETURN ) {
  118. // m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_RETURN);
  119. // }
  120. // else if( uMsg == OCM__BASE + WM_CTLCOLOREDIT || uMsg == OCM__BASE + WM_CTLCOLORSTATIC ) {
  121. // if( m_pOwner->GetNativeEditBkColor() == 0xFFFFFFFF ) return NULL;
  122. // ::SetBkMode((HDC)wParam, TRANSPARENT);
  123. // DWORD dwTextColor = m_pOwner->GetTextColor();
  124. // ::SetTextColor((HDC)wParam, RGB(GetBValue(dwTextColor),GetGValue(dwTextColor),GetRValue(dwTextColor)));
  125. // if( m_hBkBrush == NULL ) {
  126. // DWORD clrColor = m_pOwner->GetNativeEditBkColor();
  127. // m_hBkBrush = ::CreateSolidBrush(RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  128. // }
  129. // return (LRESULT)m_hBkBrush;
  130. // }
  131. else if( uMsg == WM_PAINT) {
  132. if (m_pOwner->GetManager()->IsLayered()) {
  133. m_pOwner->GetManager()->AddNativeWindow(m_pOwner, m_hWnd);
  134. }
  135. bHandled = FALSE;
  136. }
  137. else bHandled = FALSE;
  138. if( !bHandled ) return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  139. return lRes;
  140. }
  141. LRESULT CDateTimeWnd::OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  142. {
  143. LRESULT lRes = ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);
  144. if (m_pOwner->m_nDTUpdateFlag == DT_NONE)
  145. {
  146. ::SendMessage(m_hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
  147. m_pOwner->m_nDTUpdateFlag = DT_UPDATE;
  148. m_pOwner->UpdateText();
  149. }
  150. if ((HWND)wParam != m_pOwner->GetManager()->GetPaintWindow()) {
  151. ::SendMessage(m_pOwner->GetManager()->GetPaintWindow(), WM_KILLFOCUS, wParam, lParam);
  152. }
  153. SendMessage(WM_CLOSE);
  154. return lRes;
  155. }
  156. // LRESULT CDateTimeWnd::OnEditChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  157. // {
  158. // if( !m_bInit ) return 0;
  159. // if( m_pOwner == NULL ) return 0;
  160. // // Copy text back
  161. // int cchLen = ::GetWindowTextLength(m_hWnd) + 1;
  162. // LPTSTR pstr = static_cast<LPTSTR>(_alloca(cchLen * sizeof(TCHAR)));
  163. // ASSERT(pstr);
  164. // if( pstr == NULL ) return 0;
  165. // ::GetWindowText(m_hWnd, pstr, cchLen);
  166. // m_pOwner->m_sText = pstr;
  167. // m_pOwner->GetManager()->SendNotify(m_pOwner, DUI_MSGTYPE_TEXTCHANGED);
  168. // return 0;
  169. // }
  170. //////////////////////////////////////////////////////////////////////////
  171. //
  172. CDateTimeUI::CDateTimeUI()
  173. {
  174. ::GetLocalTime(&m_sysTime);
  175. m_bReadOnly = false;
  176. m_pWindow = NULL;
  177. m_nDTUpdateFlag=DT_UPDATE;
  178. UpdateText(); // add by:daviyang35 ³õʼ»¯½çÃæÊ±ÏÔʾʱ¼ä
  179. m_nDTUpdateFlag = DT_NONE;
  180. }
  181. LPCTSTR CDateTimeUI::GetClass() const
  182. {
  183. return DUI_CTR_DATETIME;
  184. }
  185. LPVOID CDateTimeUI::GetInterface(LPCTSTR pstrName)
  186. {
  187. if( _tcscmp(pstrName, DUI_CTR_DATETIME) == 0 ) return static_cast<CDateTimeUI*>(this);
  188. return CLabelUI::GetInterface(pstrName);
  189. }
  190. UINT CDateTimeUI::GetControlFlags() const
  191. {
  192. return UIFLAG_TABSTOP;
  193. }
  194. HWND CDateTimeUI::GetNativeWindow() const
  195. {
  196. if (m_pWindow) return m_pWindow->GetHWND();
  197. return NULL;
  198. }
  199. SYSTEMTIME& CDateTimeUI::GetTime()
  200. {
  201. return m_sysTime;
  202. }
  203. void CDateTimeUI::SetTime(SYSTEMTIME* pst)
  204. {
  205. m_sysTime = *pst;
  206. Invalidate();
  207. }
  208. void CDateTimeUI::SetReadOnly(bool bReadOnly)
  209. {
  210. m_bReadOnly = bReadOnly;
  211. Invalidate();
  212. }
  213. bool CDateTimeUI::IsReadOnly() const
  214. {
  215. return m_bReadOnly;
  216. }
  217. void CDateTimeUI::UpdateText()
  218. {
  219. if (m_nDTUpdateFlag == DT_DELETE)
  220. SetText(_T(""));
  221. else if (m_nDTUpdateFlag == DT_UPDATE)
  222. {
  223. CDuiString sText;
  224. sText.SmallFormat(_T("%4d-%02d-%02d"),
  225. m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay, m_sysTime.wHour, m_sysTime.wMinute);
  226. SetText(sText);
  227. }
  228. }
  229. void CDateTimeUI::SetPos(RECT rc, bool bNeedInvalidate)
  230. {
  231. CControlUI::SetPos(rc, bNeedInvalidate);
  232. if( m_pWindow != NULL ) {
  233. RECT rcPos = m_pWindow->CalPos();
  234. if (::IsRectEmpty(&rcPos)) ::ShowWindow(m_pWindow->GetHWND(), SW_HIDE);
  235. else {
  236. ::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
  237. rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
  238. }
  239. }
  240. }
  241. void CDateTimeUI::Move(SIZE szOffset, bool bNeedInvalidate)
  242. {
  243. CControlUI::Move(szOffset, bNeedInvalidate);
  244. if( m_pWindow != NULL ) {
  245. RECT rcPos = m_pWindow->CalPos();
  246. if (::IsRectEmpty(&rcPos)) ::ShowWindow(m_pWindow->GetHWND(), SW_HIDE);
  247. else {
  248. ::SetWindowPos(m_pWindow->GetHWND(), NULL, rcPos.left, rcPos.top, rcPos.right - rcPos.left,
  249. rcPos.bottom - rcPos.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
  250. }
  251. }
  252. }
  253. void CDateTimeUI::DoEvent(TEventUI& event)
  254. {
  255. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  256. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  257. else CLabelUI::DoEvent(event);
  258. return;
  259. }
  260. if( event.Type == UIEVENT_SETCURSOR && IsEnabled() )
  261. {
  262. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_IBEAM)));
  263. return;
  264. }
  265. if( event.Type == UIEVENT_WINDOWSIZE )
  266. {
  267. if( m_pWindow != NULL ) m_pManager->SetFocusNeeded(this);
  268. }
  269. if( event.Type == UIEVENT_SCROLLWHEEL )
  270. {
  271. if( m_pWindow != NULL ) return;
  272. }
  273. if( event.Type == UIEVENT_SETFOCUS && IsEnabled() )
  274. {
  275. if( m_pWindow ) return;
  276. m_pWindow = new CDateTimeWnd();
  277. ASSERT(m_pWindow);
  278. m_pWindow->Init(this);
  279. m_pWindow->ShowWindow();
  280. }
  281. if( event.Type == UIEVENT_KILLFOCUS && IsEnabled() )
  282. {
  283. Invalidate();
  284. }
  285. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK || event.Type == UIEVENT_RBUTTONDOWN)
  286. {
  287. if( IsEnabled() ) {
  288. GetManager()->ReleaseCapture();
  289. if( IsFocused() && m_pWindow == NULL )
  290. {
  291. m_pWindow = new CDateTimeWnd();
  292. ASSERT(m_pWindow);
  293. }
  294. if( m_pWindow != NULL )
  295. {
  296. m_pWindow->Init(this);
  297. m_pWindow->ShowWindow();
  298. }
  299. }
  300. return;
  301. }
  302. if( event.Type == UIEVENT_MOUSEMOVE )
  303. {
  304. return;
  305. }
  306. if( event.Type == UIEVENT_BUTTONUP )
  307. {
  308. return;
  309. }
  310. if( event.Type == UIEVENT_CONTEXTMENU )
  311. {
  312. return;
  313. }
  314. CLabelUI::DoEvent(event);
  315. }
  316. }