home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / ras / rasberry / phbkdlg.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  9KB  |  311 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   phbkdlg.c
  9. //
  10. //  PURPOSE:   Displays the "Phonebox Entries" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    PhbkDlg           - Processes messages for "PhbkDlg" dialog box.
  14. //    MsgPhbkDlgInit    - To initialize the phbkdlg box with version info
  15. //                      from resources.
  16. //    MsgPhbkDlgCommand - Process WM_COMMAND message sent to the phbkdlg box.
  17. //    CmdPhbkDlgCancel  - Free the phbkdlg box and related data.
  18. //    CmdPhbkDlgEntry   - Process LBN_DBLCLK for the entry list box
  19. //    CmdPhbkDlgDial    - Calls AUTHDLG and/or DIALDLG boxes.
  20. //
  21. //  COMMENTS:
  22. //    Displays and allow user to dial an entry from the default phonebook
  23. //
  24. //
  25.  
  26. #include <windows.h>            // required for all Windows applications
  27. #include <windowsx.h>
  28. #ifdef WIN16
  29. #include "win16ext.h"           // required only for win16 applications
  30. #endif
  31. #include "globals.h"            // prototypes specific to this application
  32. #include <malloc.h>
  33. #include <ras.h>
  34. #include <raserror.h>
  35. #include "phbkdlg.h"
  36. #include "dialdlg.h"
  37. #include "authdlg.h"
  38.  
  39. LRESULT MsgPhbkDlgInit(HWND, UINT, WPARAM, LPARAM);
  40. LRESULT MsgPhbkDlgCommand(HWND, UINT, WPARAM, LPARAM);
  41. LRESULT CmdPhbkDlgCancel(HWND, WORD, WORD, HWND);
  42. LRESULT CmdPhbkDlgEntry(HWND, WORD, WORD, HWND);
  43. LRESULT CmdPhbkDlgDial(HWND, WORD, WORD, HWND);
  44.  
  45.  
  46. // PhbkDlg dialog message table definition.
  47. MSD rgmsdPhbkDlg[] =
  48. {
  49.     {WM_COMMAND,    MsgPhbkDlgCommand},
  50.     {WM_INITDIALOG, MsgPhbkDlgInit}
  51. };
  52.  
  53. MSDI msdiPhbkDlg =
  54. {
  55.     sizeof(rgmsdPhbkDlg) / sizeof(MSD),
  56.     rgmsdPhbkDlg,
  57.     edwpNone
  58. };
  59.  
  60. // PhbkDlg dialog command table definition.
  61. CMD rgcmdPhbkDlg[] =
  62. {
  63.     {IDB_DIAL,  CmdPhbkDlgDial},
  64.     {IDCANCEL,  CmdPhbkDlgCancel},
  65.     {IDL_ENTRY, CmdPhbkDlgEntry}
  66. };
  67.  
  68. CMDI cmdiPhbkDlg =
  69. {
  70.     sizeof(rgcmdPhbkDlg) / sizeof(CMD),
  71.     rgcmdPhbkDlg,
  72.     edwpNone
  73. };
  74.  
  75. // Module specific "globals"  Used when a variable needs to be
  76. // accessed in more than on handler function.
  77.  
  78. #ifdef WIN16                     // for Win16 we require that the user
  79.     BOOL g_bUseCurrent = FALSE;  // always enters username and password
  80. #else
  81.     BOOL g_bUseCurrent = TRUE;
  82. #endif
  83.  
  84. //
  85. //  FUNCTION: PhbkDlg(HWND, UINT, WPARAM, LPARAM)
  86. //
  87. //  PURPOSE:  Processes messages for "PhbkDlg" dialog box.
  88. //
  89. //  PARAMETERS:
  90. //    hdlg - window handle of the dialog box
  91. //    wMessage - type of message
  92. //    wparam - message-specific information
  93. //    lparam - message-specific information
  94. //
  95. //  RETURN VALUE:
  96. //    TRUE - message handled
  97. //    FALSE - message not handled
  98. //
  99. //  COMMENTS:
  100. //    Dispatches messages for dialog box
  101. //
  102.  
  103. LRESULT CALLBACK PhbkDlg(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  104. {
  105.     return DispMessage(&msdiPhbkDlg, hdlg, uMessage, wparam, lparam);
  106. }
  107.  
  108.  
  109. //
  110. //  FUNCTION: MsgPhbkDlgInit(HWND, UINT, WPARAM, LPARAM)
  111. //
  112. //  PURPOSE: To initialize the phbkdlg box.
  113. //
  114. //  PARAMETERS:
  115. //    hwnd - The window handing the message.
  116. //    uMessage - The message number. WM_INITDIALOG.
  117. //    wparam - Message specific data (unused).
  118. //    lparam - Message specific data (unused).
  119. //
  120. //  RETURN VALUE:
  121. //    Always returns TRUE - message handled.
  122. //
  123. //  COMMENTS:
  124. //    Displays default phone book by calling RasEnumEntries
  125. //
  126.  
  127. LRESULT MsgPhbkDlgInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  128. {
  129.     LPRASENTRYNAME lpRasEntry;
  130.     LPRASENTRYNAME lpTemp;
  131.     DWORD cbBuf;
  132.     DWORD cEntry;
  133.     DWORD dwRet;
  134.     UINT  ndx;
  135.     char  szMessage[256];
  136.  
  137.     cbBuf = sizeof(RASENTRYNAME);
  138.     if ((lpRasEntry = ( LPRASENTRYNAME ) _fmalloc((UINT)cbBuf)) != NULL ) 
  139.     {
  140.         lpRasEntry->dwSize = sizeof(RASENTRYNAME);
  141.         dwRet = RasEnumEntries( NULL, NULL, lpRasEntry, &cbBuf, &cEntry );
  142.         if ( dwRet == ERROR_BUFFER_TOO_SMALL )
  143.         {
  144.             if ((lpTemp = ( LPRASENTRYNAME ) _frealloc ( lpRasEntry, (UINT)cbBuf )) != NULL )
  145.             {
  146.                 lpRasEntry = lpTemp;
  147.                 dwRet = RasEnumEntries( NULL, NULL, lpRasEntry, &cbBuf, &cEntry );
  148.             }
  149.             else
  150.             {
  151.                 dwRet = ERROR_NOT_ENOUGH_MEMORY;
  152.             }
  153.         }
  154.         else if ( dwRet != 0 ) // other error
  155.         {
  156.             if ( RasGetErrorString( (UINT)dwRet, szMessage, 256 ) != 0 )
  157.                 wsprintf( (LPSTR)szMessage, "Undefined RasEnumEntries Error." );
  158.  
  159.             MessageBox(hdlg, (LPSTR)szMessage, szAppName, MB_OK | MB_ICONSTOP );
  160.         }
  161.  
  162.         if ( dwRet == 0 )  // No errors
  163.         {
  164.             for ( ndx = 0; ndx < cEntry; ndx++ ) 
  165.                 SendDlgItemMessage( hdlg,
  166.                                     IDL_ENTRY,
  167.                                     LB_ADDSTRING,
  168.                                     0,
  169.                                     (LPARAM)(LPCSTR) lpRasEntry[ndx].szEntryName);
  170.  
  171.             if ( ndx > 0 ) // at least one item was added
  172.             {
  173.                 SendDlgItemMessage( hdlg, IDL_ENTRY, LB_SETCURSEL, 0, 1L );
  174.                 EnableWindow( GetDlgItem( hdlg, IDB_DIAL ), TRUE );
  175.             }
  176.         }
  177.  
  178.         _ffree( lpRasEntry );
  179.     } 
  180.     
  181. #ifdef WIN16
  182.     // for Win16, we require that the user enters their username
  183.     ShowWindow( GetDlgItem( hdlg, IDC_USECURRENT ), SW_HIDE );
  184. #endif
  185.  
  186.     CheckDlgButton( hdlg, IDC_USECURRENT, g_bUseCurrent );
  187.  
  188.     // Center the dialog over the application window
  189.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  190.  
  191.     return TRUE;
  192. }
  193.  
  194. //
  195. //  FUNCTION: MsgPhbkDlgCommand(HWND, UINT, WPARAM, LPARAM)
  196. //
  197. //  PURPOSE: Process WM_COMMAND message sent to the phbkdlg box.
  198. //
  199. //  PARAMETERS:
  200. //    hwnd - The window handing the message.
  201. //    uMessage - The message number. WM_COMMAND.
  202. //    wparam - Message specific data (unused).
  203. //    lparam - Message specific data (unused).
  204. //
  205. //  RETURN VALUE:
  206. //    TRUE for message handled
  207. //    FALSE for message not handled
  208. //
  209. //  COMMENTS:
  210. //    Uses this DispCommand function defined in wndproc.c combined
  211. //    with the cmdiPhbkDlg structure defined in this file to handle
  212. //    the command messages for the phbkdlg dialog box.
  213. //
  214.  
  215. LRESULT MsgPhbkDlgCommand(HWND   hwnd, 
  216.                           UINT   uMessage,
  217.                           WPARAM wparam,
  218.                           LPARAM lparam)
  219. {
  220.     return DispCommand(&cmdiPhbkDlg, hwnd, wparam, lparam);
  221. }
  222.  
  223. //
  224. //  FUNCTION: CmdPhbkDlgCancel(HWND, WORD, WORD, HWND)
  225. //
  226. //  PURPOSE: Free the phbkdlg box and related data.
  227. //
  228. //  PARAMETERS:
  229. //    hwnd - The window handling the command.
  230. //    wCommand - The command to be handled IDCANCEL.
  231. //    wNotify  - The notification code to be handled (unused).
  232. //    hwndCtrl - NULL (unused).
  233. //
  234. //  RETURN VALUE:
  235. //    Always returns TRUE.
  236. //
  237. //  COMMENTS:
  238. //    Calls EndDialog to finish the dialog session.
  239. //
  240.  
  241. LRESULT CmdPhbkDlgCancel(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  242. {
  243.     // save the state of the check box
  244.     g_bUseCurrent = IsDlgButtonChecked( hdlg, IDC_USECURRENT );
  245.     EndDialog(hdlg, FALSE);          // Exit the dialog
  246.     return TRUE;
  247. }
  248.  
  249.  
  250. //
  251. //  FUNCTION: CmdPhbkDlgEntry(HWND, WORD, WORD, HWND)
  252. //
  253. //  PURPOSE: Dial selected phonebook phbkdlg
  254. //
  255. //  PARAMETERS:
  256. //    hwnd - The window handling the command.
  257. //    wCommand - The command to be handled IDL_ENTRY.
  258. //    wNotify  - The notification code to be handled LBN_DBLCLK.
  259. //    hwndCtrl - NULL (unused).
  260. //
  261. //  RETURN VALUE:
  262. //    TRUE if message is processed.
  263. //
  264. //  COMMENTS:
  265. //    Double click on an entry signifies a dial command.
  266. //
  267.  
  268. LRESULT CmdPhbkDlgEntry(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  269. {
  270.     if ( wNotify == LBN_DBLCLK )
  271.     {
  272.         SendMessage( hdlg, WM_COMMAND, IDB_DIAL, 0 );
  273.         return TRUE;
  274.     }
  275.     return FALSE;
  276. }
  277.  
  278. //
  279. //  FUNCTION: CmdPhbkDialDlg(HWND, WORD, WORD, HWND)
  280. //
  281. //  PURPOSE: Displays the "DialDlg" dialog box
  282. //
  283. //  PARAMETERS:
  284. //    hdlg      - Window handle
  285. //    wCommand  - IDB_DIAL(unused)
  286. //    wNotify   - Notification number (unused)
  287. //    hwndCtrl  - NULL (unused)
  288. //
  289. //  RETURN VALUE:
  290. //
  291. //    Always returns TRUE - Message handled
  292. //
  293. //  COMMENTS:
  294. //    Calls authentication and dialing dialog boxes
  295. //
  296.  
  297. LRESULT CmdPhbkDlgDial(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  298. {
  299.     g_bUseCurrent = IsDlgButtonChecked( hdlg, IDC_USECURRENT );
  300.  
  301.     // short circut evaluation will cause the AuthDlg box not to be
  302.     // displayed if g_bUseCurrent is true
  303.     if ( g_bUseCurrent || DialogBox(hInst, "AuthDlgBox", hdlg, (DLGPROC)AuthDlg) )
  304.     {
  305.         if( DialogBox(hInst, "DialDlgBox", hdlg, (DLGPROC)DialDlg))
  306.             EndDialog(hdlg, TRUE);          // Exit Phonebook dialog
  307.                                             // if connection established
  308.     }
  309.     return TRUE;
  310. }
  311.