home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Win95 / Fun&Utils / MFCMSG.EXE / COMCOMBO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-01  |  3.4 KB  |  135 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. // COMCOMBO Shows how to implement a self-contained combo box with buttons.
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include "comcombo.h"
  7.  
  8. BEGIN_MESSAGE_MAP(CComboCombo, CComboBox)
  9.    ON_COMMAND_RANGE(0, 0xFFFF, OnCommandRange)
  10.    ON_UPDATE_COMMAND_UI_RANGE(0, 0xFFFF, OnCommandUiRange)
  11. END_MESSAGE_MAP()
  12.  
  13. CComboCombo::CComboCombo()
  14. {
  15. }
  16.  
  17. CComboCombo::~CComboCombo()
  18. {
  19. }
  20.  
  21. //////////////////
  22. // Subclass a the combo box and Add/Delete buttons. 
  23. // The Dialog must call this to tell the combo box what the control IDs 
  24. // are and to hook everything up.
  25. //
  26. BOOL CComboCombo::SubclassControls(CWnd* pParent, 
  27.    UINT idCombo, UINT idAdd, UINT idDel)
  28. {
  29.    if (!SubclassDlgItem(idCombo, pParent))
  30.       return FALSE;
  31.  
  32.    if (!m_buttonAdd.SubclassDlgItem(idAdd, pParent))
  33.       return FALSE;
  34.  
  35.    if (!m_buttonDel.SubclassDlgItem(idDel, pParent))
  36.       return FALSE;
  37.  
  38.    m_idAdd = idAdd;
  39.    m_idDel = idDel;
  40.  
  41.    // I own the buttons now
  42.    m_buttonAdd.SetOwner(this);
  43.    m_buttonDel.SetOwner(this);
  44.  
  45.    UpdateButtons();  // Update (enable/disable) the buttons
  46.  
  47.    return TRUE;
  48. }
  49.  
  50. //////////////////
  51. // When the edit text or selection changes, 
  52. // button states may change, so I need to update them.
  53. //
  54. BOOL 
  55. CComboCombo::OnChildNotify(UINT msg, WPARAM wp, LPARAM lp, LRESULT* pLRes) 
  56. {
  57.    if (msg==WM_COMMAND) {
  58.       int nCode = HIWORD(wp);
  59.       if (nCode==CBN_SELCHANGE || nCode==CBN_EDITCHANGE)
  60.          UpdateButtons();
  61.    }
  62.    return CComboBox::OnChildNotify(msg,wp,lp,pLRes);
  63. }
  64.  
  65. ////////////////
  66. // Helper function updates Add/Delete buttons
  67. //
  68. void CComboCombo::UpdateButtons()
  69. {
  70.    // Update buttons, using myself as target!
  71.    GetParent()->UpdateDialogControls(this, FALSE);
  72. }
  73.  
  74. //////////////////
  75. // Handle command in range 0 to 0xFFFF (all commands). Check for Add/Delete.
  76. //
  77. void CComboCombo::OnCommandRange(UINT id)
  78. {
  79.    if (id==m_idAdd) {
  80.       // Add command: add contents of edit control to list box.
  81.       // Beep if item already added.
  82.       CString s;
  83.       GetWindowText(s);
  84.       if (FindStringExact(0, s) >= 0)
  85.          MessageBeep(0);
  86.       else {
  87.          AddString(s);
  88.          SetEditSel(0, -1);
  89.          Clear();
  90.          SetFocus();
  91.       }
  92.       UpdateButtons();
  93.  
  94.    } else if (id==m_idDel) {
  95.       // Delete command: Delete selected item.
  96.       DeleteString(GetCurSel());
  97.       UpdateButtons();
  98.    }
  99. }
  100.  
  101. //////////////////
  102. // Handle command update (0-0xFFFF, all commands). Check for Add/Delete.
  103. //
  104. void CComboCombo::OnCommandUiRange(CCmdUI* pCmdUI)
  105. {
  106.    UINT id = pCmdUI->m_nID;
  107.    if (id==m_idAdd)
  108.       // Add button is enabled iff there's text.
  109.       pCmdUI->Enable(GetWindowTextLength()>0);
  110.  
  111.    else if (id==m_idDel)
  112.       // Delete is enable iff an item is selected.
  113.       pCmdUI->Enable(GetCurSel()>=0);
  114. }
  115.  
  116. ////////////////////////////////////////////////////////////////
  117. // Implementation of owned buttons.
  118.  
  119. //////////////////
  120. // Re-route WM_COMMAND notification to owner instead of parent.
  121. // bLockout flag prevents infinite recursion, because owner will call
  122. // OnChildNotify too.
  123. //
  124. BOOL 
  125. COwnedButton::OnChildNotify(UINT msg, WPARAM wp, LPARAM lp, LRESULT* pLRes) 
  126. {
  127.    static BOOL bLockout = FALSE;
  128.    if (msg==WM_COMMAND && !bLockout) {
  129.       bLockout = TRUE;
  130.       GetOwner()->SendMessage(msg, wp, lp);
  131.       bLockout = FALSE;
  132.    }
  133.    return CButton::OnChildNotify(msg,wp,lp,pLRes);
  134. }
  135.