Benny Prijono | 5d9c16f | 2008-02-22 23:38:47 +0000 | [diff] [blame] | 1 | #include "stdafx.h" |
| 2 | #include "PopUpWnd.h" |
| 3 | #include "resource.h" |
| 4 | #include "PocketPJDlg.h" |
| 5 | |
| 6 | #ifdef _DEBUG |
| 7 | #define new DEBUG_NEW |
| 8 | #undef THIS_FILE |
| 9 | static char THIS_FILE[] = __FILE__; |
| 10 | #endif |
| 11 | |
| 12 | #define IDC_BTN1 10 |
| 13 | #define IDC_BTN2 11 |
| 14 | |
| 15 | |
| 16 | ///////////////////////////////////////////////////////////////////////////// |
| 17 | // CPopUpWnd |
| 18 | |
| 19 | CPopUpWnd::CPopUpWnd(CPocketPJDlg* pParent) |
| 20 | { |
| 21 | Create(pParent); |
| 22 | } |
| 23 | |
| 24 | CPopUpWnd::~CPopUpWnd() |
| 25 | { |
| 26 | DestroyWindow(); |
| 27 | } |
| 28 | |
| 29 | BOOL CPopUpWnd::Create(CPocketPJDlg* pParent) |
| 30 | { |
| 31 | BOOL bSuccess; |
| 32 | |
| 33 | m_ParentWnd = pParent; |
| 34 | |
| 35 | // Register window class |
| 36 | CString csClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, |
| 37 | 0, |
| 38 | CBrush(::GetSysColor(COLOR_BTNFACE))); |
| 39 | |
| 40 | // Create popup window |
| 41 | bSuccess = CreateEx(WS_EX_DLGMODALFRAME|WS_EX_TOPMOST, // Extended style |
| 42 | csClassName, // Classname |
| 43 | _T("PocketPJ"), // Title |
| 44 | WS_POPUP|WS_BORDER|WS_CAPTION, // style |
| 45 | 0,0, // position - updated soon. |
| 46 | 1,1, // Size - updated soon |
| 47 | pParent->GetSafeHwnd(), // handle to parent |
| 48 | 0, // No menu |
| 49 | NULL); |
| 50 | if (!bSuccess) |
| 51 | return FALSE; |
| 52 | |
| 53 | ShowWindow(SW_HIDE); |
| 54 | |
| 55 | // Now create the controls |
| 56 | CRect TempRect(0,0,10,10); |
| 57 | |
| 58 | /* |SS_LEFTNOWORDWRAP */ |
| 59 | bSuccess = m_Title1.Create(_T("Title1"), WS_CHILD|WS_VISIBLE|SS_NOPREFIX, |
| 60 | TempRect, this, IDC_TITLE1); |
| 61 | if (!bSuccess) |
| 62 | return FALSE; |
| 63 | |
| 64 | bSuccess = m_Title2.Create(_T("Title2"), WS_CHILD|WS_VISIBLE|SS_NOPREFIX, |
| 65 | TempRect, this, IDC_TITLE2); |
| 66 | if (!bSuccess) |
| 67 | return FALSE; |
| 68 | |
| 69 | bSuccess = m_Title3.Create(_T("Title3"), WS_CHILD|WS_VISIBLE|SS_NOPREFIX, |
| 70 | TempRect, this, IDC_TITLE3); |
| 71 | if (!bSuccess) |
| 72 | return FALSE; |
| 73 | |
| 74 | bSuccess = m_Btn1.Create(_T("Button1"), |
| 75 | WS_CHILD|WS_VISIBLE|WS_TABSTOP| BS_PUSHBUTTON, |
| 76 | TempRect, this, IDC_BTN1); |
| 77 | if (!bSuccess) |
| 78 | return FALSE; |
| 79 | |
| 80 | bSuccess = m_Btn2.Create(_T("Button2"), |
| 81 | WS_CHILD|WS_VISIBLE|WS_TABSTOP| BS_PUSHBUTTON, |
| 82 | TempRect, this, IDC_BTN2); |
| 83 | if (!bSuccess) |
| 84 | return FALSE; |
| 85 | |
| 86 | CFont *ft1 = new CFont, |
| 87 | *ft2 = new CFont, |
| 88 | *ft3 = new CFont; |
| 89 | |
| 90 | |
| 91 | LOGFONT lf; |
| 92 | memset(&lf, 0, sizeof(LOGFONT)); |
| 93 | lf.lfHeight = 12; |
| 94 | lstrcpy(lf.lfFaceName, _T("Arial")); |
| 95 | VERIFY(ft1->CreateFontIndirect(&lf)); |
| 96 | VERIFY(ft3->CreateFontIndirect(&lf)); |
| 97 | |
| 98 | lf.lfHeight = 20; |
| 99 | VERIFY(ft2->CreateFontIndirect(&lf)); |
| 100 | |
| 101 | m_Title1.SetFont(ft1, TRUE); |
| 102 | m_Title2.SetFont(ft2, TRUE); |
| 103 | m_Title3.SetFont(ft3, TRUE); |
| 104 | |
| 105 | |
| 106 | SetWindowSize(); |
| 107 | |
| 108 | // Center and show window |
| 109 | CenterWindow(); |
| 110 | |
| 111 | Show(); |
| 112 | |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | void CPopUpWnd::SetContent(const CPopUpContent &content) |
| 117 | { |
| 118 | m_Title1.SetWindowText(content.m_Title1); |
| 119 | m_Title2.SetWindowText(content.m_Title2); |
| 120 | m_Title3.SetWindowText(content.m_Title3); |
| 121 | |
| 122 | if (content.m_Btn1 != "") { |
| 123 | m_Btn1.SetWindowText(content.m_Btn1); |
| 124 | m_Btn1.ShowWindow(SW_SHOW); |
| 125 | } else { |
| 126 | m_Btn1.ShowWindow(SW_HIDE); |
| 127 | } |
| 128 | |
| 129 | if (content.m_Btn2 != "") { |
| 130 | m_Btn2.SetWindowText(content.m_Btn2); |
| 131 | m_Btn2.ShowWindow(SW_SHOW); |
| 132 | } else { |
| 133 | m_Btn2.ShowWindow(SW_HIDE); |
| 134 | } |
| 135 | |
| 136 | UpdateWindow(); |
| 137 | ShowWindow(SW_SHOW); |
| 138 | } |
| 139 | |
| 140 | void CPopUpWnd::SetWindowSize(int width, int height) |
| 141 | { |
| 142 | enum { H1 = 16, H2 = 40, H3 = 16, S = 5, G = 10, BW=60, BH=20, BG=40}; |
| 143 | |
| 144 | CRect rootRect(0, 0, 320, 240); |
| 145 | int Y; |
| 146 | |
| 147 | MoveWindow((rootRect.Width() - width)/2, (rootRect.Height() - height)/2, |
| 148 | width, height); |
| 149 | |
| 150 | m_Title1.MoveWindow(10, Y=S, width-20, H1); |
| 151 | m_Title2.MoveWindow(10, Y+=H1+G, width-20, H2); |
| 152 | m_Title3.MoveWindow(10, Y+=H2+G, width-20, H3); |
| 153 | |
| 154 | m_Btn1.MoveWindow((width-2*BW-BG)/2, Y+=H3+G, BW, BH); |
| 155 | m_Btn2.MoveWindow((width-2*BW-BG)/2+BW+BG, Y, BW, BH); |
| 156 | } |
| 157 | |
| 158 | void CPopUpWnd::Hide() |
| 159 | { |
| 160 | if (!::IsWindow(GetSafeHwnd())) |
| 161 | return; |
| 162 | |
| 163 | if (IsWindowVisible()) |
| 164 | { |
| 165 | ShowWindow(SW_HIDE); |
| 166 | ModifyStyle(WS_VISIBLE, 0); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void CPopUpWnd::Show() |
| 171 | { |
| 172 | if (!::IsWindow(GetSafeHwnd())) |
| 173 | return; |
| 174 | |
| 175 | ModifyStyle(0, WS_VISIBLE); |
| 176 | ShowWindow(SW_SHOWNA); |
| 177 | RedrawWindow(NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW); |
| 178 | } |
| 179 | |
| 180 | BEGIN_MESSAGE_MAP(CPopUpWnd, CWnd) |
| 181 | //{{AFX_MSG_MAP(CPopUpWnd) |
| 182 | ON_WM_ERASEBKGND() |
| 183 | //}}AFX_MSG_MAP |
| 184 | ON_BN_CLICKED(IDC_BTN1, OnCancel1) |
| 185 | ON_BN_CLICKED(IDC_BTN2, OnCancel2) |
| 186 | END_MESSAGE_MAP() |
| 187 | |
| 188 | |
| 189 | ///////////////////////////////////////////////////////////////////////////// |
| 190 | // CPopUpWnd message handlers |
| 191 | |
| 192 | BOOL CPopUpWnd::OnEraseBkgnd(CDC* pDC) |
| 193 | { |
| 194 | CBrush backBrush; |
| 195 | backBrush.CreateSolidBrush(RGB(255,255,255)); |
| 196 | CBrush* pOldBrush = pDC->SelectObject(&backBrush); |
| 197 | |
| 198 | CRect rect; |
| 199 | pDC->GetClipBox(&rect); // Erase the area needed |
| 200 | pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); |
| 201 | pDC->SelectObject(pOldBrush); |
| 202 | |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | void CPopUpWnd::OnCancel1() |
| 207 | { |
| 208 | m_ParentWnd->OnPopUpButton(1); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | void CPopUpWnd::OnCancel2() |
| 213 | { |
| 214 | m_ParentWnd->OnPopUpButton(2); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | BOOL CPopUpWnd::DestroyWindow() |
| 219 | { |
| 220 | return CWnd::DestroyWindow(); |
| 221 | } |
| 222 | |
| 223 | void CPopUpWnd::PeekAndPump() |
| 224 | { |
| 225 | MSG msg; |
| 226 | while (::PeekMessage(&msg, NULL,0,0,PM_NOREMOVE)) |
| 227 | { |
| 228 | if (!AfxGetApp()->PumpMessage()) |
| 229 | { |
| 230 | ::PostQuitMessage(0); |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |