grppage.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 11k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /********************************************************
  2.  grppg.cpp
  3.   User Manager group membership prop page implementation
  4.  History:
  5.   09/23/98: dsheldon created
  6. ********************************************************/
  7. #include "stdafx.h"
  8. #include "resource.h"
  9. #include "grppage.h"
  10. #include "misc.h"
  11. /**************************************************************
  12.  CGroupPageBase Implementation
  13.   Functions common to both the group prop page and the group
  14.   wizard page.
  15. **************************************************************/
  16. INT_PTR CGroupPageBase::HandleGroupMessage(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  17. {
  18.     switch (uMsg)
  19.     {
  20.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  21.         HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  22.     };
  23.     return FALSE;
  24. }
  25. void CGroupPageBase::InitializeLocalGroupCombo(HWND hwndCombo)
  26. {
  27.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::InitializeLocalGroupCombo");
  28.     ComboBox_ResetContent(hwndCombo);
  29.     // Add all of the groups in the list to the box
  30.     for(int i = 0; i < m_pGroupList->GetPtrCount(); i ++)
  31.     {
  32.         CGroupInfo* pGroupInfo = m_pGroupList->GetPtr(i);
  33.         int index = ComboBox_AddString(hwndCombo, pGroupInfo->m_szGroup);
  34.         ComboBox_SetItemData(hwndCombo, index, pGroupInfo->m_szComment);
  35.     }
  36.     TCHAR szSelectGroup[MAX_GROUP + 1];
  37.     // Load a local group name from the resources to select by default
  38.     LoadString(g_hInstance, IDS_USR_DEFAULTGROUP, szSelectGroup, ARRAYSIZE(szSelectGroup));
  39.     if (ComboBox_SelectString(hwndCombo, 0, szSelectGroup) == CB_ERR)
  40.     {
  41.         ComboBox_SetCurSel(hwndCombo, 0);
  42.     }
  43.     TraceLeaveVoid();
  44. }
  45. void CGroupPageBase::SetGroupDescription(HWND hwndCombo, HWND hwndEdit)
  46. {
  47.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::SetGroupDescription");
  48.     int iItem = ComboBox_GetCurSel(hwndCombo);
  49.     TraceAssert(iItem != CB_ERR);
  50.     TCHAR* pszDescription = (TCHAR*) ComboBox_GetItemData(hwndCombo, iItem);
  51.     SetWindowText(hwndEdit, pszDescription);
  52.     TraceLeaveVoid();
  53. }
  54. BOOL CGroupPageBase::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  55. {
  56.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::OnInitDialog");
  57.     // Fill in the local group combo box
  58.     HWND hwndCombo = GetDlgItem(hwnd, IDC_GROUPS);
  59.     InitializeLocalGroupCombo(hwndCombo);
  60.     HWND hwndEdit = GetDlgItem(hwnd, IDC_GROUPDESC);
  61.     
  62.     if ((NULL != m_pUserInfo) && (m_pUserInfo->m_szGroups[0] != TEXT('')))
  63.     {
  64.         // Select the local group corresponding to the first one in the user's groups
  65.         // string
  66.         TCHAR szSelect[MAX_GROUP + 1];
  67.         // Copy the string since we might shorten our copy
  68.         lstrcpyn(szSelect, m_pUserInfo->m_szGroups, ARRAYSIZE(szSelect));
  69.         
  70.         TCHAR* pchEndOfFirst = StrChr(szSelect, TEXT(';'));
  71.         if (pchEndOfFirst)
  72.         {
  73.             // More than one group; we'll fix that!
  74.             *pchEndOfFirst = TEXT('');
  75.         }
  76.         SelectGroup(hwnd, szSelect);
  77.     }
  78.     else
  79.     {
  80.         // Select the power user group by default 
  81.         SendDlgItemMessage(hwnd, IDC_POWERUSERS, BM_SETCHECK, 
  82.             (WPARAM) BST_CHECKED, 0);
  83.         OnRadioChanged(hwnd, IDC_POWERUSERS);
  84.     }
  85.    
  86.     SetGroupDescription(hwndCombo, hwndEdit);
  87.     // Bold the group names
  88.     BoldGroupNames(hwnd);
  89.     TraceLeaveValue(TRUE);
  90. }
  91. BOOL CGroupPageBase::GetSelectedGroup(HWND hwnd, LPTSTR pszGroupOut, DWORD cchGroup, CUserInfo::GROUPPSEUDONYM* pgsOut)
  92. {
  93.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::GetSelectedGroup");
  94.     
  95.     BOOL fSuccess = FALSE;
  96.     *pgsOut = CUserInfo::USEGROUPNAME;
  97.     UINT idString = 0;
  98.     if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_POWERUSERS)))
  99.     {
  100.         idString = IDS_USR_POWERUSERS;
  101.         *pgsOut = CUserInfo::STANDARD;
  102.     }
  103.     else if (BST_CHECKED == Button_GetCheck(GetDlgItem(hwnd, IDC_USERS)))
  104.     {
  105.         idString = IDS_USR_USERS;
  106.         *pgsOut = CUserInfo::RESTRICTED;
  107.     }
  108.     
  109.     if (0 != idString)
  110.     {
  111.         LoadString(g_hInstance, idString, pszGroupOut, cchGroup);
  112.         // Success
  113.         fSuccess = TRUE;
  114.     }
  115.     else
  116.     {
  117.         // 'other' must be selected; get the string from the dropdown
  118.         GetWindowText(GetDlgItem(hwnd, IDC_GROUPS), pszGroupOut, cchGroup);
  119.     }
  120.     TraceLeaveValue(fSuccess);
  121. }
  122. // Returns IDC_OTHER if no radio button id corresponds to the group
  123. UINT CGroupPageBase::RadioIdForGroup(LPCTSTR pszGroup)
  124. {
  125.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::RadioIdForGroup");
  126.     TCHAR szPowerUsers[MAX_GROUP + 1];
  127.     TCHAR szUsers[MAX_GROUP + 1];
  128.     LoadString(g_hInstance, IDS_USR_POWERUSERS, szPowerUsers,
  129.         ARRAYSIZE(szPowerUsers));
  130.     LoadString(g_hInstance, IDS_USR_USERS, szUsers,
  131.         ARRAYSIZE(szUsers));
  132.     // Assume IDC_OTHER to start
  133.     UINT uiRadio = IDC_OTHER;
  134.     if (0 == StrCmpI(pszGroup, szPowerUsers))
  135.     {
  136.         uiRadio = IDC_POWERUSERS;
  137.     }
  138.     else if (0 == StrCmpI(pszGroup, szUsers))
  139.     {
  140.         uiRadio = IDC_USERS;
  141.     }
  142.     TraceLeaveValue(uiRadio);
  143. }
  144. // Disable/update as appropriate when radio selection changes
  145. void CGroupPageBase::OnRadioChanged(HWND hwnd, UINT idRadio)
  146. {
  147.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::OnRadioChanged");
  148.     BOOL fEnableGroupDropdown = (IDC_OTHER == idRadio);
  149.     EnableWindow(GetDlgItem(hwnd, IDC_GROUPS), fEnableGroupDropdown);
  150.     EnableWindow(GetDlgItem(hwnd, IDC_OTHER_STATIC), fEnableGroupDropdown);
  151.     ShowWindow(GetDlgItem(hwnd, IDC_GROUPDESC), 
  152.         fEnableGroupDropdown ? SW_SHOW : SW_HIDE);
  153.     TraceLeaveVoid();
  154. }
  155. void CGroupPageBase::SelectGroup(HWND hwnd, LPCTSTR pszSelect)
  156. {
  157.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::SelectGroup");
  158.     // Always select the group in the 'other' dropdown
  159.     ComboBox_SelectString(GetDlgItem(hwnd, IDC_GROUPS),
  160.         -1, pszSelect);
  161.     
  162.     // Check the appropriate radio button
  163.     UINT idRadio = RadioIdForGroup(pszSelect);
  164.     Button_SetCheck(GetDlgItem(hwnd, idRadio), BST_CHECKED);
  165.     OnRadioChanged(hwnd, idRadio);
  166.     TraceLeaveVoid();
  167. }
  168. void CGroupPageBase::BoldGroupNames(HWND hwnd)
  169. {
  170.     TraceEnter(TRACE_USR_CORE, "CCroupPageBase::BoldGroupNames");
  171.     HWND hwndPowerUsers = GetDlgItem(hwnd, IDC_POWERUSERS);
  172.     HFONT hfont = (HFONT) SendMessage(hwndPowerUsers, WM_GETFONT, 0, 0);
  173.     if (hfont)
  174.     {
  175.         LOGFONT lf;
  176.         if (FALSE != GetObject((HGDIOBJ) hfont, sizeof(lf), &lf))
  177.         {
  178.             lf.lfWeight = FW_BOLD;
  179.             m_hBoldFont = CreateFontIndirect(&lf);
  180.             if (NULL != m_hBoldFont)
  181.             {
  182.                 // Set the font
  183.                 SendMessage(hwndPowerUsers, WM_SETFONT, 
  184.                     (WPARAM) m_hBoldFont, 0);
  185.                 SendDlgItemMessage(hwnd, IDC_USERS,
  186.                     WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  187.                 SendDlgItemMessage(hwnd, IDC_OTHER,
  188.                     WM_SETFONT, (WPARAM) m_hBoldFont, 0);
  189.             }
  190.         }
  191.     }
  192.     TraceLeaveVoid();
  193. }
  194. BOOL CGroupPageBase::OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  195. {
  196.     TraceEnter(TRACE_USR_CORE, "CGroupPageBase::OnCommand");
  197.     switch(codeNotify)
  198.     {
  199.     case CBN_SELCHANGE:
  200.         SetGroupDescription(hwndCtl, GetDlgItem(hwnd, IDC_GROUPDESC));
  201.         PropSheet_Changed(GetParent(hwnd), hwnd);
  202.         break;
  203.     case BN_CLICKED:
  204.         // Handle radio clicks
  205.         switch (id)
  206.         {
  207.         case IDC_POWERUSERS:
  208.         case IDC_USERS:
  209.         case IDC_OTHER:
  210.             PropSheet_Changed(GetParent(hwnd), hwnd);
  211.             OnRadioChanged(hwnd, id);
  212.         }
  213.         break;
  214.     }
  215.     TraceLeaveValue(FALSE);
  216. }
  217. /**************************************************************
  218.  CGroupWizardPage Implementation
  219. **************************************************************/
  220. INT_PTR CGroupWizardPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  221. {
  222.     switch (uMsg)
  223.     {
  224.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  225.         HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  226.         HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  227.     }
  228.     return FALSE;
  229. }
  230. BOOL CGroupWizardPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  231. {
  232.     TraceEnter(TRACE_USR_CORE, "CGroupWizardPage::OnNotify");
  233.            
  234.     switch (pnmh->code)
  235.     {
  236.     case PSN_SETACTIVE:
  237.         PropSheet_SetWizButtons(pnmh->hwndFrom, PSWIZB_BACK | PSWIZB_FINISH);
  238.         SetWindowLongPtr(hwnd, DWLP_MSGRESULT, 0);
  239.         TraceLeaveValue(TRUE);
  240.     case PSN_WIZFINISH:
  241.         {
  242.             // Read in the local group name
  243.             CUserInfo::GROUPPSEUDONYM gs;
  244.             GetSelectedGroup(hwnd, m_pUserInfo->m_szGroups,
  245.                 ARRAYSIZE(m_pUserInfo->m_szGroups), &gs);
  246.             // Don't close wizard by default
  247.             LONG_PTR finishResult = (LONG_PTR) hwnd;
  248.             SetCursor(LoadCursor(NULL, IDC_WAIT));
  249.             if (SUCCEEDED(m_pUserInfo->Create(hwnd, gs)))
  250.             {
  251.                 m_pUserInfo->m_fHaveExtraUserInfo = FALSE;
  252.                 // Close wizard
  253.                 finishResult = 0;
  254.             }
  255.             SetWindowLongPtr(hwnd, DWLP_MSGRESULT, finishResult);
  256.             TraceLeaveValue(TRUE);
  257.         }
  258.     }
  259.     TraceLeaveValue(FALSE);
  260. }
  261. /**************************************************************
  262.  CGroupPropertyPage Implementation
  263. **************************************************************/
  264. INT_PTR CGroupPropertyPage::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  265. {
  266.     switch (uMsg)
  267.     {
  268.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  269.         HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  270.         HANDLE_MSG(hwndDlg, WM_COMMAND, OnCommand);
  271.     }
  272.     return FALSE;
  273. }
  274. BOOL CGroupPropertyPage::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  275. {
  276.     BOOL fReturn = FALSE;
  277.     switch(pnmh->code)
  278.     {
  279.     case PSN_APPLY:
  280.         {
  281.             // Check to see if the group needs updating on Apply
  282.             TCHAR szTemp[MAX_GROUP + 1];
  283.             // Read in the local group name
  284.             CUserInfo::GROUPPSEUDONYM gs;
  285.             GetSelectedGroup(hwnd, szTemp,
  286.                 ARRAYSIZE(szTemp), &gs);
  287.             if (StrCmp(szTemp, m_pUserInfo->m_szGroups) != 0)
  288.             {
  289.                 HRESULT hr = m_pUserInfo->UpdateGroup(hwnd, szTemp, gs);
  290.                 if (SUCCEEDED(hr))
  291.                 {
  292.                     SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_NOERROR);
  293.                 }
  294.                 else
  295.                 {
  296.                     TCHAR szDomainUser[MAX_DOMAIN + MAX_USER + 2];
  297.                     MakeDomainUserString(m_pUserInfo->m_szDomain, m_pUserInfo->m_szUsername,
  298.                         szDomainUser, ARRAYSIZE(szDomainUser));
  299.                     ::DisplayFormatMessage(hwnd, IDS_USR_APPLET_CAPTION, 
  300.                         IDS_USR_UPDATE_GROUP_ERROR, MB_ICONERROR | MB_OK,
  301.                         szDomainUser);
  302.                     SetWindowLongPtr(hwnd, DWLP_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
  303.                 }
  304.             }
  305.         }
  306.         fReturn = TRUE;
  307.         break;
  308.     default:
  309.         fReturn = FALSE;
  310.         break;
  311.     }
  312.     TraceLeaveValue(fReturn);
  313. }