home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / ctlhtmlc / uipropsh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-21  |  8.4 KB  |  262 lines

  1. /*---------------------------------------------------------------------------*\
  2.  | UIPropSh.cpp - DevUI(tm) property sheet class                             |
  3.  |                From Developing User Interfaces for Microsoft Windows      |
  4.  |                Copyright (c) 1999, Everett N. McKay                       |
  5. \*---------------------------------------------------------------------------*/
  6.  
  7. #include "stdafx.h"
  8. #include "DevUI.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16.  
  17. // class CMcPropertySheet
  18.  
  19. IMPLEMENT_DYNAMIC(CMcPropertySheet, CPropertySheet)
  20.  
  21. BEGIN_MESSAGE_MAP (CMcPropertySheet, CPropertySheet)
  22. END_MESSAGE_MAP ()
  23.  
  24. CMcPropertySheet::CMcPropertySheet() : 
  25.    CPropertySheet()
  26. {
  27.    m_RemoveApply = m_RemoveCancel = FALSE;
  28.    m_pButtonList = 0;
  29. }
  30.  
  31. CMcPropertySheet::CMcPropertySheet (UINT titleID, CWnd *pParent,
  32.                                     UINT selectPage) :
  33.    CPropertySheet(titleID, pParent, selectPage)
  34. {
  35.    m_RemoveApply = m_RemoveCancel = FALSE;
  36.    m_pButtonList = 0;
  37. }
  38.  
  39. CMcPropertySheet::CMcPropertySheet (LPCTSTR titleText, CWnd *pParent,
  40.                                     UINT selectPage) :
  41.    CPropertySheet (titleText, pParent, selectPage)
  42. {
  43.    m_RemoveApply = m_RemoveCancel = FALSE;
  44.    m_pButtonList = 0;
  45. }
  46.  
  47. // effect:  Initializes the property sheet by creating and moving buttons as necessary.
  48. //          Note that this code handles the Help button (obtained with 
  49. //          m_psh.dwFlags |= PSH_HASHELP;), since all calculations are relative to the 
  50. //          Apply button and the right margin (which includes the Help button if it is
  51. //          there.)  However, this code does not work with the PSH_NOAPPLYNOW flag, again
  52. //          since calculations are relative to the Apply button and will be wrong if the
  53. //          Apply button has been removed.
  54. BOOL CMcPropertySheet::OnInitDialog ()
  55. {
  56.    CWnd  *pOK, *pCancel, *pApply;
  57.    CRect cancelRect, applyRect;
  58.    BOOL  retVal;
  59.  
  60.    // call base class
  61.    retVal = CPropertySheet::OnInitDialog();
  62.  
  63.    // gather info
  64.    if ((pOK     = GetDlgItem(IDOK)) == 0 ||
  65.        (pCancel = GetDlgItem(IDCANCEL)) == 0 ||
  66.        (pApply  = GetDlgItem(ID_APPLY_NOW)) == 0)
  67.       return retVal;
  68.    ASSERT_VALID(pOK);
  69.    ASSERT_VALID(pCancel);
  70.    ASSERT_VALID(pApply);
  71.    pApply->GetWindowRect(&applyRect);
  72.    ScreenToClient(&applyRect);
  73.    pCancel->GetWindowRect(&cancelRect);
  74.    ScreenToClient(&cancelRect);
  75.  
  76.    // deal with button changes
  77.    if (m_pButtonList)
  78.    {
  79.       CWnd *pLastButton = 0;
  80.       CRect psRect;
  81.       int   i, newButtonNumber, buttonCount = 0, buttonOffset, buttonSpaceWidth, 
  82.             dialogWidth, rightMarginWidth, buttonWidth, buttonHeight;
  83.       BOOL  cancelUsed = FALSE, applyUsed = FALSE;
  84.  
  85.       // set sizes
  86.       GetClientRect(&psRect);
  87.       dialogWidth  = psRect.Width();
  88.       buttonWidth  = cancelRect.Width();
  89.       buttonHeight = cancelRect.Height();
  90.       if ((rightMarginWidth  = dialogWidth - applyRect.left - buttonWidth) <= 0)
  91.          rightMarginWidth  = 6;  // default value (4 du in small fonts mode)
  92.       if ((buttonSpaceWidth = applyRect.left - cancelRect.left - buttonWidth) <= 0)
  93.          buttonSpaceWidth = 6;   // default value (4 du in small fonts mode)
  94.  
  95.       // create new buttons
  96.       for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
  97.       {
  98.          if (m_pButtonList[i].ID == -1)
  99.             break;
  100.          buttonCount++;
  101.  
  102.          // handle each button, create new buttons
  103.          if (m_pButtonList[i].ID == IDOK)
  104.          {
  105.             pOK->SetWindowText(m_pButtonList[i].Name);      // rename - may be different
  106.  
  107.             // clear the default style if button doesn't have focus
  108.             if (!m_pButtonList[i].IsDefault)
  109.             {
  110.                LONG style = pOK->GetStyle();
  111.                style &= ~BS_DEFPUSHBUTTON;
  112.                SetWindowLong(pOK->m_hWnd, GWL_STYLE, style);
  113.             }
  114.          }
  115.          else if (m_pButtonList[i].ID == IDCANCEL)
  116.             cancelUsed = TRUE;
  117.          else if (m_pButtonList[i].ID == ID_APPLY_NOW)
  118.             applyUsed = TRUE;
  119.          else
  120.          {
  121.             // create a new button
  122.             m_Buttons[newButtonNumber].Create(m_pButtonList[i].Name, BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
  123.                                               CRect(0, 0, buttonWidth, buttonHeight), pOK->GetParent(), m_pButtonList[i].ID);
  124.             m_Buttons[newButtonNumber].SetFont(pOK->GetFont());
  125.             newButtonNumber++;
  126.          }
  127.       }
  128.  
  129.       // move buttons
  130.       buttonOffset = dialogWidth - (rightMarginWidth + buttonCount * buttonWidth + (buttonCount - 1) * buttonSpaceWidth);
  131.       for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
  132.       {
  133.          if (m_pButtonList[i].ID == -1)
  134.             break;
  135.  
  136.          // move each button
  137.          if (m_pButtonList[i].ID == IDOK)
  138.          {
  139.             pOK->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
  140.             pLastButton = pOK;
  141.          }
  142.          else if (m_pButtonList[i].ID == IDCANCEL)
  143.          {
  144.             pCancel->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
  145.             pLastButton = pCancel;
  146.          }
  147.          else if (m_pButtonList[i].ID == ID_APPLY_NOW)
  148.          {
  149.             pApply->MoveWindow(buttonOffset, cancelRect.top, buttonWidth, buttonHeight);
  150.             pLastButton = pApply;
  151.          }
  152.          else
  153.          {
  154.             // set tab order of new buttons
  155.             m_Buttons[newButtonNumber].SetWindowPos(pLastButton, buttonOffset, cancelRect.top, buttonWidth, buttonHeight, SWP_NOACTIVATE);
  156.             pLastButton = &m_Buttons[newButtonNumber++];
  157.          }
  158.          buttonOffset += buttonWidth + buttonSpaceWidth;
  159.       }
  160.  
  161.       // hide and disable unused buttons
  162.       if (!cancelUsed)
  163.       {
  164.          pCancel->ShowWindow(SW_HIDE);
  165.          pCancel->EnableWindow(FALSE);
  166.       }
  167.       if (!applyUsed)
  168.       {
  169.          pApply->ShowWindow(SW_HIDE);
  170.          pApply->EnableWindow(FALSE);
  171.       }
  172.  
  173.       // set the input focus
  174.       for (i = newButtonNumber = 0; newButtonNumber < MaxPropSheetButtons; i++)
  175.       {
  176.          if (m_pButtonList[i].ID == -1)
  177.             break;
  178.  
  179.          if (m_pButtonList[i].ID == IDOK)
  180.          {
  181.             if (m_pButtonList[i].IsDefault)
  182.             {
  183.                pOK->SetFocus();
  184.  
  185.                LONG style = pOK->GetStyle();
  186.                style |= BS_DEFPUSHBUTTON;
  187.                SetWindowLong(pOK->m_hWnd, GWL_STYLE, style);
  188.                break;
  189.             }
  190.          }
  191.          else if (m_pButtonList[i].ID == IDCANCEL)
  192.          {
  193.             if (m_pButtonList[i].IsDefault)
  194.             {
  195.                pCancel->SetFocus();
  196.  
  197.                LONG style = pCancel->GetStyle();
  198.                style |= BS_DEFPUSHBUTTON;
  199.                SetWindowLong(pCancel->m_hWnd, GWL_STYLE, style);
  200.                break;
  201.             }
  202.          }
  203.          else if (m_pButtonList[i].ID == ID_APPLY_NOW)
  204.          {
  205.             if (m_pButtonList[i].IsDefault)
  206.             {
  207.                pApply->SetFocus();
  208.  
  209.                LONG style = pApply->GetStyle();
  210.                style |= BS_DEFPUSHBUTTON;
  211.                SetWindowLong(pApply->m_hWnd, GWL_STYLE, style);
  212.                break;
  213.             }
  214.          }
  215.          else
  216.          {
  217.             pLastButton = &m_Buttons[newButtonNumber++];
  218.             if (m_pButtonList[i].IsDefault)
  219.             {
  220.                pLastButton->SetFocus();
  221.  
  222.                LONG style = pLastButton->GetStyle();
  223.                style |= BS_DEFPUSHBUTTON;
  224.                SetWindowLong(pLastButton->m_hWnd, GWL_STYLE, style);
  225.                break;
  226.             }
  227.          }
  228.       }
  229.  
  230.       return 0;   // return 0 to preserve current input focus
  231.    }
  232.    else if (m_RemoveApply || m_RemoveCancel)
  233.    {
  234.       // handle Apply
  235.       if (m_RemoveApply)
  236.       {
  237.          pApply->ShowWindow(SW_HIDE);
  238.          pApply->EnableWindow(FALSE);
  239.       }
  240.  
  241.       // handle Cancel
  242.       if (m_RemoveCancel)
  243.       {
  244.          pCancel->ShowWindow(SW_HIDE);
  245.          pCancel->EnableWindow(FALSE);
  246.       }
  247.       else if (m_RemoveApply)
  248.       {
  249.          pCancel->MoveWindow(&applyRect, FALSE);
  250.       }
  251.  
  252.       // handle OK
  253.       if (m_RemoveApply && m_RemoveCancel)
  254.          pOK->MoveWindow(&applyRect, FALSE);
  255.       else
  256.          pOK->MoveWindow(&cancelRect, FALSE);
  257.    }
  258.  
  259.    return retVal;
  260. }
  261.  
  262.