/* * Copyright (C) 2006 Dorian Boissonnade * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "stdafx.h" #include "GenericDlg.h" const static BYTE tplGenericDlg[] = { 0x01, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0xc8, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x4d, 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00, 0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x44, 0x00, 0x6c, 0x00, 0x67, 0x00, 0x00, 0x00 }; CGenericDlg::CGenericDlg(CWnd* pParent /*=NULL*/) { m_pParentWnd = pParent; m_hIcon = NULL; m_hDlgIcon = NULL; m_uDefault = -1; m_uCancel = -1; m_IsModeless = FALSE; } INT_PTR CGenericDlg::DoModal() { InitModalIndirect((LPCDLGTEMPLATE)tplGenericDlg, m_pParentWnd); return CDialog::DoModal(); } BOOL CGenericDlg::DoModeless() { m_IsModeless = TRUE; if (CreateIndirect((LPCDLGTEMPLATE)tplGenericDlg, m_pParentWnd)) { ShowWindow(SW_SHOW); return TRUE; } return FALSE; } void CGenericDlg::AddButton(UINT nID, LPCTSTR pszText) { ASSERT(nIDsize.cy) { BOOL stop = FALSE; int maxSize = ::GetSystemMetrics(SM_CXFULLSCREEN)*2/3; // If the message is too narrow try to widen it to give // it a better aspect. int width = rect.Width(); while (width maxSize) { rect.right = maxSize; stop = TRUE; } dc.DrawText(m_csMsgText, rect ,DT_CALCRECT|DT_WORDBREAK); // If the width is still the same than the previous one // or narrower, stop too. We can't make it bigger. if (stop || width >= rect.Width()) break; width = rect.Width(); } } textSize.cx = rect.Width(); textSize.cy = rect.Height(); // Compute the dialog size CSize dlgSize; dlgSize.cx = std::max(textSize.cx + msgPadding, (LONG)totalButtonsWidth); dlgSize.cx = std::max(dlgSize.cx, cbxSize.cx + msgPadding); dlgSize.cx = std::max(dlgSize.cx, editSize.cx + msgPadding); dlgSize.cx += ConvX(BORDER_LEFT) + ConvX(BORDER_RIGHT); dlgSize.cy = ConvY(BORDER_TOP) + ConvY(BORDER_BOTTOM) + std::max(textSize.cy, iconSize.cy) + (ned ? yMsgSpace + totalEditHeight : 0) + (ncb ? yMsgSpace + totalCheckBoxHeight : 0) + (nb ? yMsgSpace + buttonSize.cy : 0); int maxWidth = ::GetSystemMetrics(SM_CXFULLSCREEN) - 2*::GetSystemMetrics(SM_CXEDGE); ASSERT(dlgSize.cx < maxWidth); if (dlgSize.cx > maxWidth) dlgSize.cx = maxWidth; rc.SetRect(0, 0, dlgSize.cx, dlgSize.cy); CalcWindowRect(rc); MoveWindow(rc); CenterWindow(); // Create and place the controls. msgPadding = std::max( (dlgSize.cx - std::max(baseWidth, (LONG)textSize.cx) - msgPadding)/2 + msgPadding, ConvX(BORDER_LEFT) + msgPadding); // Message m_edCtrl.Create(m_csMsgText, WS_CHILD|WS_VISIBLE|SS_LEFT, CRect(0,0,0,0), this, (UINT)IDC_STATIC); //m_edCtrl.Create(WS_CHILD|WS_VISIBLE|ES_LEFT|ES_MULTILINE|ES_READONLY, CRect(0,0,0,0), this, (UINT)IDC_STATIC); //m_edCtrl.SetBackgroundColor(FALSE, ::GetSysColor(COLOR_3DFACE)); m_edCtrl.SetFont(GetFont()); m_edCtrl.MoveWindow( msgPadding, ConvY(BORDER_TOP), textSize.cx, textSize.cy); // Icon if (m_hIcon) { m_stIconCtrl.Create(NULL, WS_CHILD|WS_VISIBLE|WS_DISABLED|SS_ICON, CRect(0,0,0,0), this); m_stIconCtrl.SetIcon(m_hIcon); m_stIconCtrl.MoveWindow( ConvX(BORDER_LEFT), ConvY(BORDER_TOP), iconSize.cx, iconSize.cy); } // Edit controls int bx = msgPadding; int by = ConvY(BORDER_TOP) + std::max(textSize.cy, iconSize.cy) + yMsgSpace; for(int i=0;iSetFocus(); return FALSE; } } return TRUE; } BOOL CGenericDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { if(pHandlerInfo == NULL && nCode == CN_COMMAND) { // Look if it's one of the button, since it could also // be also one of the checkbox. int nb = (int)m_aButtons.GetSize(); for (int i=0;imessage==WM_KEYDOWN) { if (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_CANCEL) { if (m_uCancel != -1) CloseDialog(m_uCancel); return TRUE; } } return CDialog::PreTranslateMessage(pMsg); } void CGenericDlg::CloseDialog(int nResult) { int ncb = (int)m_aCheckBoxes.GetSize(); for (int i=0;iresult) = IsDlgButtonChecked(cbi->id); } int ned = (int)m_aEdits.GetSize(); for (int i=0;iid); ASSERT(pEdit); if (pEdit) pEdit->GetWindowText(*(ei->result)); } if (m_IsModeless) DestroyWindow(); else EndDialog(nResult); } void CGenericDlg::PostNcDestroy() { CDialog::PostNcDestroy(); if (m_IsModeless) delete this; } void CGenericDlg::OnClose() { if (m_uCancel != -1) CloseDialog(m_uCancel); }