CustomizeDialog.cpp
上传用户:lczygg
上传日期:2007-07-03
资源大小:2947k
文件大小:17k
源码类别:

语音合成与识别

开发平台:

Visual C++

  1.  /////////////////////////////////////////////////////////////////////////////
  2. // CustomizeDialog.cpp: implementation of the CCustomizeDialog class.
  3. //
  4. /////////////////////////////////////////////////////////////////////////////
  5. // Copyright (c) 2001 by Nikolay Denisov. All rights reserved.
  6. //
  7. // This code is free for personal and commercial use, providing this 
  8. // notice remains intact in the source files and all eventual changes are
  9. // clearly marked with comments.
  10. //
  11. // You must obtain the author's consent before you can include this code
  12. // in a software library.
  13. //
  14. // No warrantee of any kind, express or implied, is included with this
  15. // software; use at your own risk, responsibility for damages (if any) to
  16. // anyone resulting from the use of this software rests entirely with the
  17. // user.
  18. //
  19. // Please email bug reports, bug fixes, enhancements, requests and
  20. // comments to: nick@actor.ru
  21. /////////////////////////////////////////////////////////////////////////////
  22. #include "stdafx.h"
  23. #include "CustomizeDialog.h"
  24. #include "ToolBarEx.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CCustomizeDialog dialog
  32. #ifdef CUSTOM_DRAW
  33. #define IDC_LB_AVAILABLE    0x00C9  // revealed using Spy++
  34. #define IDC_LB_CURRENT      0x00CB
  35. #define  STR_SEPARATOR  _T("Separator")
  36. #endif // CUSTOM_DRAW
  37. static const int cxPadding = 3;
  38. static const int cyPadding = 3;
  39. IMPLEMENT_DYNAMIC( CCustomizeDialog, CWnd )
  40. CCustomizeDialog::CCustomizeDialog( CToolBarEx* pToolBar,
  41.                                     TextOptions TextOptions,
  42.                                     IconOptions IconOptions )
  43. {
  44. ASSERT_VALID(pToolBar);
  45.     m_pToolBar = pToolBar;
  46. m_eTextOptions=TextOptions;
  47. m_eIconOptions=IconOptions;
  48. #ifdef CUSTOM_DRAW
  49. GetColors();
  50. #endif // CUSTOM_DRAW
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. // Operations
  54. void CCustomizeDialog::SetTextOptions( TextOptions eTextOptions)
  55. {
  56.     m_eTextOptions = eTextOptions;
  57. if (IsWindow(m_wndTextOptions.m_hWnd))
  58.   m_wndTextOptions.SetCurSel(eTextOptions);
  59.     
  60. }
  61. void CCustomizeDialog::SetIconOptions( IconOptions eIconOptions)
  62. {
  63.     m_eIconOptions = eIconOptions;
  64. if (IsWindow(m_wndIconOptions.m_hWnd))
  65. {
  66.   m_wndIconOptions.SetCurSel(eIconOptions);   
  67. #ifdef CUSTOM_DRAW
  68.   // set the height also
  69.    int nHeight = GetButtonSize().cy;
  70.           CWnd* pWnd = GetDlgItem( IDC_LB_AVAILABLE );
  71. if ( pWnd != 0 )
  72. {
  73. pWnd->SendMessage( LB_SETITEMHEIGHT, 0, nHeight );
  74. pWnd->Invalidate();
  75. }
  76. pWnd = GetDlgItem( IDC_LB_CURRENT );
  77. if ( pWnd != 0 )
  78. {
  79. pWnd->SendMessage( LB_SETITEMHEIGHT, 0, nHeight );
  80. pWnd->Invalidate();
  81. }
  82. #endif // CUSTOM_DRAW
  83. }
  84. }
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CCustomizeDialog message handlers
  87. BEGIN_MESSAGE_MAP(CCustomizeDialog, CWnd)
  88.     //{{AFX_MSG_MAP(CCustomizeDialog)
  89.     ON_WM_MEASUREITEM()
  90.     ON_WM_DRAWITEM()
  91.     ON_WM_CTLCOLOR()
  92. ON_WM_SYSCOLORCHANGE()
  93. //}}AFX_MSG_MAP
  94.     ON_MESSAGE( WM_INITDIALOG, OnInitDialog )
  95.     ON_CBN_SELENDOK(IDC_CB_TEXTOPTIONS, OnTextOptions)
  96.     ON_CBN_SELENDOK(IDC_CB_ICONOPTIONS, OnIconOptions)
  97. END_MESSAGE_MAP()
  98. LRESULT CCustomizeDialog::OnInitDialog( WPARAM /*wParam*/, LPARAM /*lParam*/ )
  99. {
  100.     CRect rcClient;
  101.     GetClientRect( rcClient );
  102.     CRect rcWindow;
  103.     GetWindowRect( rcWindow );
  104. // create  the windows // sizes are hardcoded //ugly way
  105. CRect  rc;
  106. CFont *pFont =GetFont();
  107. ASSERT_VALID(pFont);
  108.   
  109. //static wnd
  110. rc=CRect(4,2,49,8);
  111. MapDialogRect(m_hWnd,&rc);
  112. rc.left+=rcClient.left;
  113. rc.top+=rcClient.bottom;
  114. VERIFY(m_wndTextOptionsText.Create(_T("Te&xt options:"),WS_CHILD|WS_VISIBLE,rc,this));
  115.   m_wndTextOptionsText.SetFont(pFont);
  116. m_wndTextOptionsText.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);
  117. //Text options
  118. rc=CRect(52,0,123,50);
  119. MapDialogRect(m_hWnd,&rc);
  120. rc.left+=rcClient.left;
  121. rc.top+=rcClient.bottom;
  122. VERIFY(m_wndTextOptions.Create(WS_CHILD |WS_VISIBLE |CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,rc,this, IDC_CB_TEXTOPTIONS));
  123.   m_wndTextOptions.SetFont(pFont);
  124. m_wndTextOptions.AddString(_T("Show Text Labels"));
  125. m_wndTextOptions.AddString(_T("Selective Text on Right"));
  126. m_wndTextOptions.AddString(_T("No Text Labels"));
  127. SetTextOptions(m_eTextOptions);
  128. m_wndTextOptions.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);
  129.  
  130. //static wnd
  131. rc=CRect(4,20,49,8);
  132. MapDialogRect(m_hWnd,&rc);
  133. rc.left+=rcClient.left;
  134. rc.top+=rcClient.bottom;
  135. VERIFY(m_wndIconOptionsText.Create(_T("Ico&n options:"),WS_CHILD|WS_VISIBLE,rc,this));
  136. m_wndIconOptionsText.SetFont(pFont);
  137. m_wndIconOptionsText.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);
  138. //icon combo
  139. rc=CRect(52,18,123,50);
  140. MapDialogRect(m_hWnd,&rc);
  141. rc.left+=rcClient.left;
  142. rc.top+=rcClient.bottom;
  143. VERIFY(m_wndIconOptions.Create(WS_CHILD |WS_VISIBLE |CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP,rc,this, IDC_CB_ICONOPTIONS));
  144.   m_wndIconOptions.SetFont(pFont);
  145. m_wndIconOptions.AddString(_T("Small Icons"));
  146. m_wndIconOptions.AddString(_T("Large Icons"));
  147. SetIconOptions(m_eIconOptions);
  148. m_wndIconOptions.SetWindowPos(NULL,rc.left,rc.top,rc.right,rc.bottom,SWP_NOZORDER|SWP_NOACTIVATE);
  149. // final rect
  150. rc=CRect(0,0,179,34);
  151. MapDialogRect(m_hWnd,&rc);
  152. rc.left+=rcClient.left;
  153. rc.top+=rcClient.bottom;
  154.  
  155.     // Increase height of Customize Toolbar dialog accordingly
  156.     rcWindow.bottom += (rc.bottom+cyPadding);
  157.     VERIFY( SetWindowPos( 0, 0, 0, rcWindow.Width(), rcWindow.Height(),
  158.         SWP_NOZORDER | SWP_NOMOVE ) );
  159. //Set Title
  160. ASSERT(m_pToolBar);
  161. CString strName,strOrgName;
  162. m_pToolBar->GetWindowText(strName);
  163. if (!strName.IsEmpty())
  164. {
  165. GetWindowText(strOrgName);
  166. SetWindowText(strOrgName + _T(" - ")+ strName);
  167. }
  168.     return Default();
  169. }
  170. HBRUSH CCustomizeDialog::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
  171. {
  172.     HBRUSH hbr = CWnd::OnCtlColor( pDC, pWnd, nCtlColor );
  173. #ifdef CUSTOM_DRAW
  174.     if ( nCtlColor == CTLCOLOR_LISTBOX )
  175.     {
  176.         UINT nIDCtl = ( UINT )pWnd->GetDlgCtrlID();
  177.         if ( ( nIDCtl == IDC_LB_AVAILABLE ) ||
  178.              ( nIDCtl == IDC_LB_CURRENT ) )
  179.         {
  180.             hbr = ::GetSysColorBrush( COLOR_BTNFACE );
  181.         }
  182.     }
  183. #endif // CUSTOM_DRAW
  184.     return hbr;
  185. }
  186. void CCustomizeDialog::OnTextOptions()
  187. {
  188. m_eTextOptions=(TextOptions)m_wndTextOptions.GetCurSel();
  189. m_pToolBar->SetTextOptions(( TextOptions )m_eTextOptions,TRUE);
  190. }
  191. void CCustomizeDialog::OnIconOptions()
  192. {
  193. m_eIconOptions=(IconOptions)m_wndIconOptions.GetCurSel();
  194. #ifdef CUSTOM_DRAW
  195. m_pToolBar->SetIconOptions(( IconOptions )m_eIconOptions,TRUE);   // do not update if custom draw is not enabled
  196. int nHeight = GetButtonSize().cy;
  197.     CWnd* pWnd = GetDlgItem( IDC_LB_AVAILABLE );
  198.     if ( pWnd != 0 )
  199.     {
  200.         pWnd->SendMessage( LB_SETITEMHEIGHT, 0, nHeight );
  201.         pWnd->Invalidate();
  202.     }
  203.     pWnd = GetDlgItem( IDC_LB_CURRENT );
  204.     if ( pWnd != 0 )
  205.     {
  206.         pWnd->SendMessage( LB_SETITEMHEIGHT, 0, nHeight );
  207.         pWnd->Invalidate();
  208.     }
  209. #endif // CUSTOM_DRAW
  210. }
  211. void CCustomizeDialog::OnMeasureItem( int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct )
  212. {
  213.    
  214. #ifdef CUSTOM_DRAW
  215. if ( lpMeasureItemStruct->CtlType == ODT_LISTBOX )
  216.     {
  217.         if ( ( nIDCtl == IDC_LB_AVAILABLE ) ||
  218.              ( nIDCtl == IDC_LB_CURRENT ) )
  219.         {
  220.             lpMeasureItemStruct->itemHeight = GetButtonSize().cy;
  221.             return;
  222.         }
  223.     }
  224. #endif // CUSTOM_DRAW
  225.     CWnd::OnMeasureItem( nIDCtl, lpMeasureItemStruct );
  226. }
  227. void CCustomizeDialog::OnDrawItem( int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct )
  228. {
  229. #ifdef CUSTOM_DRAW
  230.     if ( lpDrawItemStruct->CtlType == ODT_LISTBOX )
  231.     {
  232.         if ( ( nIDCtl == IDC_LB_AVAILABLE ) || ( nIDCtl == IDC_LB_CURRENT ) )
  233.         {
  234.             CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  235.             int nSavedDC = pDC->SaveDC();
  236.             ASSERT( nSavedDC );
  237. CFont *pToolBarFont= m_pToolBar->GetFont();
  238. if (pToolBarFont)  pDC->SelectObject(pToolBarFont);
  239.             // Get all needed info about the item being drawn
  240.             CRect rcItem( &lpDrawItemStruct->rcItem );
  241.             int nItem  = ( SHORT )HIWORD( lpDrawItemStruct->itemData );
  242.             int nImage = ( SHORT )LOWORD( lpDrawItemStruct->itemData );
  243.             int nIndex = ( int )lpDrawItemStruct->itemID;
  244.             bool bSelected  = ( lpDrawItemStruct->itemState & ODS_SELECTED ) != 0;
  245.             bool bFocused   = ( lpDrawItemStruct->itemState & ODS_FOCUS    ) != 0;
  246.             bool bSeparator = false;
  247. bool bHidden    = false;
  248. //I found these magic numbers by trial, they may be wrong
  249. if ( ( nItem < 0 ) || ( nItem > m_pToolBar->m_ToolBarInfo.GetUpperBound() ) )
  250. {
  251. // actually hidden or last seperator
  252.  bHidden    = ((nItem & 0x4000)==0x4000) || ((nItem & 0xa000)==0xa000); //magic  number
  253.  bSeparator =(nItem &0x2000)==0x2000;   //magic  number
  254.  nItem  &=0xFF;  //magic  number
  255. }
  256.             // Calculate rectangles for image, text and focus frame
  257.             CSize szButton = GetButtonSize();
  258.             CRect rcImage( rcItem );
  259.             rcImage.right = rcImage.left + szButton.cx;
  260.             CRect rcText( rcItem );
  261.             rcText.left  += szButton.cx + 5;
  262.             rcText.right -= 2;
  263.             CRect rcLabel( rcItem );
  264.             rcLabel.left += szButton.cx + 1;
  265.             // Cache system colors
  266.             COLORREF clrText     =  bSelected ? m_clrHighLightText : m_clrBtnText ;
  267.             COLORREF clrBack     =  bSelected ? m_clrHighLight : m_clrBtnFace ;
  268.             pDC->FillSolidRect( rcImage, m_clrBtnFace );
  269.             pDC->FillSolidRect( rcLabel, clrBack );
  270.             // Draw focus rect, if needed
  271.             if ( bFocused )
  272.             {
  273.                 CBrush brush( RGB(
  274.                     255 - GetRValue( clrBack ),
  275.                     255 - GetGValue( clrBack ),
  276.                     255 - GetBValue( clrBack ) ) );
  277.                 rcLabel.DeflateRect( 2, 2 );
  278.                 pDC->FrameRect( rcLabel, &brush );
  279.                 VERIFY( brush.DeleteObject() );
  280.             }
  281.             // Get item text to be drawn
  282.             CString strText;
  283.             if ( !bSeparator )
  284.                 strText= m_pToolBar->m_ToolBarInfo[ nItem ].btnText;
  285.             else
  286. strText=STR_SEPARATOR;
  287.             // Prepare to draw item text
  288.             int nFormat = DT_SINGLELINE | DT_LEFT | DT_VCENTER | DT_END_ELLIPSIS;
  289.             pDC->SetBkMode( TRANSPARENT );
  290.             pDC->SetTextColor( clrText );
  291.             if ( !bSeparator )
  292.             {
  293.                 // Draw item image, if any
  294.                 if ( nImage >= 0 )
  295.                 {
  296.                     if ( bSelected )
  297.                     {
  298.                         pDC->Draw3dRect( rcImage, m_clr3dHilite, m_clr3dShadow );
  299.                     }
  300.                     CToolBarCtrl& tbCtrl = m_pToolBar->GetToolBarCtrl();
  301. ASSERT(m_pToolBar->m_hbmImageWell);
  302. CSize szImage = ( m_pToolBar->GetIconOptions() == ioSmallIcons ) ?
  303. CToolBarEx::m_szImageSmall : CToolBarEx::m_szImageLarge;
  304.                     CDC dc;
  305.                     VERIFY( dc.CreateCompatibleDC( pDC ) );
  306.                     CBitmap  * pbmpImage =CBitmap::FromHandle(m_pToolBar->m_hbmImageWell);
  307.                     CBitmap* pOldBitmap = dc.SelectObject( pbmpImage );
  308. ASSERT(rcImage.Width()>=szImage.cx);
  309. ASSERT(rcImage.Height()>=szImage.cy);
  310.                     VERIFY( pDC->BitBlt((rcImage.Width()-szImage.cx)/2+ rcImage.left,(rcImage.Height()-szImage.cy)/2+rcImage.top , szImage.cx, szImage.cy,&dc, nImage*szImage.cx, 0, SRCCOPY ) );
  311.                     dc.SelectObject( pOldBitmap );
  312.                     VERIFY( pbmpImage->Detach() );
  313.                 }
  314.    
  315.                 if ( m_pToolBar->m_ToolBarInfo[ nItem ].tbButton.fsStyle & TBSTYLE_DROPDOWN )
  316.                 {
  317.                     // This button is drop-down button.  To indicate this simple fact
  318.                     // we draw an arrow on right like one that submenu items have.
  319.                     CRect rcArrow( rcText );
  320.                     rcArrow.left = rcArrow.right - ::GetSystemMetrics( SM_CXMENUCHECK );
  321.                     rcText.right -= rcArrow.Width();
  322.                     CBrush brush( clrText );
  323.                     CBrush* pOldBrush = pDC->SelectObject( &brush );
  324.                     CDC dc;
  325.                     VERIFY( dc.CreateCompatibleDC( pDC ) );
  326.                     CBitmap bmpArrow;
  327.                     VERIFY( bmpArrow.CreateCompatibleBitmap( pDC, rcArrow.Width(), rcArrow.Height() ) );
  328.                     CBitmap* pOldBitmap = dc.SelectObject( &bmpArrow );
  329.                     CRect rc( 0, 0, rcArrow.Width(), rcArrow.Height() );
  330.                     VERIFY( dc.DrawFrameControl( rc, DFC_MENU, DFCS_MENUARROW ) );
  331.                     VERIFY( pDC->BitBlt( rcArrow.left, rcArrow.top, rcArrow.Width(), rcArrow.Height(),
  332.                         &dc, 0, 0, 0x00B8074A /*=PSDPxax*/ ) );
  333.                     dc.SelectObject( pOldBitmap );
  334.                     VERIFY( bmpArrow.DeleteObject() );
  335.                     pDC->SelectObject( pOldBrush );
  336.                     VERIFY( brush.DeleteObject() );
  337.                 }
  338. if (bHidden)
  339.                 {
  340.                     if ( bSelected )
  341.                     {
  342.                         pDC->SetTextColor( m_clrBtnFace );
  343.                         pDC->DrawText( strText, rcText, nFormat );
  344.                     }
  345.                     else
  346.                     {
  347.                         CRect rcTemp( rcText );
  348.                         rcTemp.OffsetRect( 1, 1 );
  349.                         pDC->SetTextColor( m_clr3dHilite );
  350.                         pDC->DrawText( strText, -1, rcTemp, nFormat );
  351.                         pDC->SetTextColor( m_clrGrayText );
  352.                         pDC->DrawText( strText, rcText, nFormat );
  353.                     }
  354. }
  355. else
  356. {
  357. pDC->DrawText( strText, rcText, nFormat );
  358. }
  359.             }
  360. else
  361. {
  362.  if (bHidden)
  363.                 {
  364.                     if ( bSelected )
  365.                     {
  366.                         pDC->SetTextColor( m_clrBtnFace );
  367.                         pDC->DrawText( strText, rcText, nFormat );
  368.                     }
  369.                     else
  370.                     {
  371.                         CRect rcTemp( rcText );
  372.                         rcTemp.OffsetRect( 1, 1 );
  373.                         pDC->SetTextColor( m_clr3dHilite );
  374.                         pDC->DrawText( strText, -1, rcTemp, nFormat );
  375.                         pDC->SetTextColor( m_clrGrayText );
  376.                         pDC->DrawText( strText, rcText, nFormat );
  377.                     }
  378.                 }
  379.                 else    
  380.                 {
  381.                     // Draw the separator bar in the middle
  382.                     rcImage.DeflateRect( cxPadding, cyPadding );
  383.                     int y = ( rcImage.top + rcImage.bottom ) / 2;
  384.                     rcImage.top    = y - 1;
  385.                     rcImage.bottom = y + 1;
  386.                     pDC->Draw3dRect( rcImage, m_clr3dShadow, m_clr3dHilite );
  387.                     pDC->DrawText( strText, rcText, nFormat );
  388.                 }
  389. }
  390.             VERIFY( pDC->RestoreDC( nSavedDC ) );
  391.             return;
  392.         }
  393.     }
  394. #endif //CUSTOM_DRAW
  395.     CWnd::OnDrawItem( nIDCtl, lpDrawItemStruct );
  396. }
  397. #ifdef CUSTOM_DRAW
  398. CSize CCustomizeDialog::GetButtonSize() const
  399. {
  400.     CSize szImage = ( m_pToolBar->GetIconOptions() == ioSmallIcons ) ?
  401. CToolBarEx::m_szImageSmall : CToolBarEx::m_szImageLarge;
  402.     return szImage + CSize( cxPadding * 2, cyPadding * 2 );
  403. }
  404. #endif //CUSTOM_DRAW
  405. LRESULT CCustomizeDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  406. {
  407. // TODO: Add your specialized code here and/or call the base class
  408. if (message==WM_HELP)
  409. {
  410.   LPHELPINFO lphi = (LPHELPINFO) lParam; 
  411.   if ( (lphi->iCtrlId != IDC_CB_TEXTOPTIONS) && (lphi->iCtrlId != IDC_CB_ICONOPTIONS) )
  412. return DefWindowProc(message,wParam,lParam);
  413. }
  414. return CWnd::WindowProc(message, wParam, lParam);
  415. }
  416. void CCustomizeDialog::OnSysColorChange() 
  417. {
  418. #ifdef CUSTOM_DRAW
  419. //get the color again
  420. GetColors();
  421. #endif // CUSTOM_DRAW
  422. CWnd::OnSysColorChange();
  423. }
  424. #ifdef CUSTOM_DRAW
  425. void CCustomizeDialog::GetColors()
  426. {
  427. m_clrHighLightText=::GetSysColor( COLOR_HIGHLIGHTTEXT);
  428. m_clrBtnText=::GetSysColor( COLOR_BTNTEXT );
  429.     m_clrHighLight= ::GetSysColor(COLOR_HIGHLIGHT );
  430.     m_clrGrayText = ::GetSysColor( COLOR_GRAYTEXT );
  431.     m_clrBtnFace  = ::GetSysColor( COLOR_BTNFACE );
  432.     m_clr3dShadow = ::GetSysColor( COLOR_3DSHADOW );
  433.     m_clr3dHilite = ::GetSysColor( COLOR_3DHILIGHT );
  434. }
  435. #endif // CUSTOM_DRAW