home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / banner / banndlg.cpp next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.2 KB  |  238 lines

  1. // BannDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Banner.h"
  6. #include "BannDlg.h"
  7. #include "Metrics.h"
  8.  
  9. #include <math.h> // for log10 function
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CBannerDlg dialog
  19.  
  20.  
  21. CBannerDlg::CBannerDlg(CWnd* pParent /*=NULL*/)
  22.     : CDialog(CBannerDlg::IDD, pParent)
  23. {
  24.     //{{AFX_DATA_INIT(CBannerDlg)
  25.     m_bBold         = TRUE;
  26.     m_bItalic       = FALSE;
  27.     m_bBorder       = TRUE;
  28.     m_bCenterH      = TRUE;
  29.     m_bCenterV      = TRUE;
  30.     m_nFontHeight   = 3500; // 3.5"
  31.     m_strBannerText.LoadString(IDS_BANNER);
  32.     m_strFontName.LoadString(IDS_FONT);
  33.     m_lfCharSet = ANSI_CHARSET;
  34.     //}}AFX_DATA_INIT
  35. }
  36.  
  37. CBannerDlg::CBannerDlg(const CBannerDlg& dlg)
  38. {
  39.     operator=(dlg);
  40. }
  41.  
  42. const CBannerDlg& CBannerDlg::operator=(const CBannerDlg& dlg)
  43. {
  44.     m_bBold         = dlg.m_bBold;
  45.     m_bBorder       = dlg.m_bBorder;
  46.     m_bCenterH      = dlg.m_bCenterH;
  47.     m_bCenterV      = dlg.m_bCenterV;
  48.     m_strBannerText = dlg.m_strBannerText;
  49.     m_nFontHeight   = dlg.m_nFontHeight;
  50.     m_strFontName   = dlg.m_strFontName;
  51.     m_lfCharSet     = dlg.m_lfCharSet;
  52.     m_bItalic       = dlg.m_bItalic;
  53.     m_pDC           = dlg.m_pDC;
  54.  
  55.     return *this;
  56. }
  57.  
  58.  
  59. void CBannerDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61.     CDialog::DoDataExchange(pDX);
  62.     //{{AFX_DATA_MAP(CBannerDlg)
  63.     DDX_Check(pDX,    IDC_BOLD,        m_bBold);
  64.     DDX_Check(pDX,    IDC_CENTER_H,    m_bCenterH);
  65.     DDX_Check(pDX,    IDC_CENTER_V,    m_bCenterV);
  66.     DDX_Text(pDX,     IDC_BANNER_TEXT, m_strBannerText);
  67.     DDX_CBString(pDX, IDC_FONTNAME,    m_strFontName);
  68.     DDX_Check(pDX,    IDC_ITALIC,      m_bItalic);
  69.     DDX_Check(pDX,    IDC_BORDER,      m_bBorder);
  70.     //}}AFX_DATA_MAP
  71.  
  72.     // Handle lfCharSet
  73.     if(pDX->m_bSaveAndValidate) 
  74.     {
  75.         CComboBox listBox;
  76.         listBox.Attach(GetDlgItem(IDC_FONTNAME)->m_hWnd);
  77.         int nIndex = listBox.GetCurSel();
  78.         m_lfCharSet = (BYTE)listBox.GetItemData(nIndex);
  79.         listBox.Detach();
  80.     }
  81.  
  82.     // Font height is handled differently
  83.     double lfFontHeight;
  84.  
  85.     if(pDX->m_bSaveAndValidate) 
  86.     {
  87.         CString szCheck;
  88.  
  89.         DDX_Text(pDX, IDC_FONTHEIGHT, szCheck);
  90.         if(szCheck.FindOneOf(_T("123456789")) == -1)
  91.             lfFontHeight = 1.0;
  92.         else
  93.             DDX_Text(pDX, IDC_FONTHEIGHT, lfFontHeight);
  94.         if(lfFontHeight < 0)
  95.             lfFontHeight = 1.0;
  96.         if(lfFontHeight > 8.0)
  97.             lfFontHeight = 8.0;
  98.         m_nFontHeight = (int)(lfFontHeight*1000.0);
  99.     }
  100.     else
  101.     {
  102.         TCHAR szHeight[20];
  103.         lfFontHeight = (double)m_nFontHeight/1000.0;
  104.         swprintf(szHeight,CString((LPCTSTR)IDS_HEIGHT),(int)(log10(lfFontHeight)+1)+1,lfFontHeight);
  105.         DDX_Text(pDX, IDC_FONTHEIGHT, CString(szHeight));
  106.     }
  107. }
  108.  
  109.  
  110. BEGIN_MESSAGE_MAP(CBannerDlg, CDialog)
  111.     //{{AFX_MSG_MAP(CBannerDlg)
  112.     ON_EN_CHANGE(IDC_FONTHEIGHT, UpdateSizeControl)
  113.     ON_BN_CLICKED(IDC_CENTER_V, UpdateSizeControl)
  114.     ON_BN_CLICKED(IDC_CENTER_H, UpdateSizeControl)
  115.     ON_BN_CLICKED(IDC_BORDER, UpdateSizeControl)
  116.     ON_BN_CLICKED(IDC_BOLD, UpdateSizeControl)
  117.     ON_EN_CHANGE(IDC_BANNER_TEXT, UpdateSizeControl)
  118.     ON_BN_CLICKED(IDC_ITALIC, UpdateSizeControl)
  119.     ON_CBN_SELCHANGE(IDC_FONTNAME, UpdateSizeControl)
  120.     //}}AFX_MSG_MAP
  121. END_MESSAGE_MAP()
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. // CBannerDlg message handlers
  125.  
  126. void CBannerDlg::UpdateSizeControl()
  127. {
  128.     // Save off original values (when DoModal() was called) so we can do a
  129.     // data exchange to calculate size
  130.     CBannerDlg tempDlg = *this;
  131.     UpdateData(TRUE);
  132.  
  133.     CSize size = GetTextSize(m_pDC);
  134.  
  135.     // Revert to original values (when DoModal() was called)
  136.     *this = tempDlg;
  137.  
  138.     CBannerMetrics mx(m_pDC);
  139.     double lfHorz = (double)size.cx/mx.Inch().cx;
  140.     double lfVert = (double)size.cy/mx.Inch().cy;
  141.  
  142.     TCHAR szSize[50];
  143.     swprintf(szSize,CString((LPCTSTR)IDS_THORZ),(int)(log10(lfHorz)+1)+1,lfHorz);
  144.     swprintf(szSize+wcslen(szSize),CString((LPCTSTR)IDS_TVERT),(int)(log10(lfVert)+1)+1,lfVert);
  145.     GetDlgItem(IDC_BANNER_SIZE)->SetWindowText(szSize);
  146. }
  147.  
  148.  
  149. CSize CBannerDlg::GetTextSize(CDC *pDC, int nCurrZoom /* = -1 */)
  150. {
  151.     CFont font, *pPrevFont;
  152.     if(!CreateFont(pDC, &font, nCurrZoom))
  153.         return CSize(0,0);
  154.     pPrevFont = pDC->SelectObject(&font);
  155.  
  156.     // GetTextExtent only works on single-line strings, so we need to loop through lines
  157.     TCHAR *str = new TCHAR[m_strBannerText.GetLength()+1];
  158.     TCHAR *curr = str, *end;
  159.     CSize size(0,0), tempSize;
  160.     _tcscpy(str, m_strBannerText);
  161.     do
  162.     {
  163.         if((end = wcschr(curr,'\n')) != NULL)
  164.             *end = _T('\0');
  165.         tempSize = pDC->GetTextExtent(curr);
  166.         if(tempSize.cx >= size.cx)
  167.             size.cx = tempSize.cx;
  168.         size.cy += tempSize.cy;
  169.         curr = end+1;
  170.     } while(end != NULL);
  171.  
  172.     delete [] str;
  173.  
  174.     if(m_bBorder)
  175.     {
  176.         CBannerMetrics mx(pDC, nCurrZoom);
  177.         size.cx += mx.BorderGap().cx*2 + mx.BorderThickness().cx; 
  178.         size.cy += mx.BorderGap().cy*2 + mx.BorderThickness().cy; 
  179.     }
  180.  
  181.     pDC->SelectObject(pPrevFont);
  182.     font.DeleteObject();
  183.  
  184.     return size;
  185. }
  186.  
  187. BOOL CBannerDlg::CreateFont(CDC *pDC, CFont *pFont, int nCurrZoom /* = -1 */)
  188. {
  189.     ASSERT(pDC != NULL);
  190.  
  191.     CBannerMetrics mx(pDC, nCurrZoom);
  192.     int nHeight = -m_nFontHeight * mx.Inch().cy / 1000; // m_nFontHeight in 1/1000"
  193.     return pFont->CreateFont(nHeight, 0, 0, 0,
  194.                              m_bBold ? FW_BOLD : FW_NORMAL, m_bItalic, 0, 0, 
  195.                              m_lfCharSet,
  196.                              0, 0, 0, 0,
  197.                              m_strFontName);
  198. }
  199.  
  200. int CBannerDlg::DoModal(CDC *pDC) 
  201. {
  202.     m_pDC = pDC;
  203.     ASSERT(m_pDC != NULL);
  204.     
  205.     return CDialog::DoModal();
  206. }
  207.  
  208. BOOL CBannerDlg::OnInitDialog() 
  209. {
  210.     CDialog::OnInitDialog();
  211.     UpdateSizeControl();
  212.     FillFontDropdown();
  213.     return TRUE;
  214. }
  215.  
  216. int CALLBACK EnumFontFamProc(const LOGFONT FAR *lplf, const TEXTMETRIC FAR *lpntm, 
  217.                              DWORD FontType, LPARAM lParam) 
  218. {
  219.     CComboBox *pListBox = (CComboBox*)lParam;
  220.     if(FontType == TRUETYPE_FONTTYPE)
  221.     {
  222.         int nIndex = pListBox->AddString(lplf->lfFaceName);
  223.         pListBox->SetItemData(nIndex, (DWORD)lplf->lfCharSet);
  224.     }
  225.     return 1;
  226. }
  227.  
  228. void CBannerDlg::FillFontDropdown()
  229. {
  230.     ASSERT(m_pDC != NULL);
  231.  
  232.     CComboBox listBox;
  233.     listBox.Attach(GetDlgItem(IDC_FONTNAME)->m_hWnd);
  234.     EnumFontFamilies(m_pDC->m_hDC, NULL, EnumFontFamProc, (LPARAM)&listBox);
  235.     listBox.SelectString(0, m_strFontName);
  236.     listBox.Detach();
  237. }
  238.