home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / odbcsdk / samples / cppdemo / dialogs.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-07  |  11.0 KB  |  444 lines

  1. /*--------------------------------------------------------------------------
  2.   DIALOGS.CPP - dialog functions for the C++ demonstration of ODBC classes
  3.  
  4.   This code is furnished on an as-is basis as part of the ODBC SDK and is
  5.   intended for example purposes only.
  6.  
  7. --------------------------------------------------------------------------*/
  8.       
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. //    Includes
  12. //
  13. #include    "headers.h"
  14.  
  15. #include    "resource.h"
  16. #include    "cppdemo.h"
  17.  
  18.  
  19. //////////////////////////////////////////////////////////////////////////////
  20. //
  21. //    Defines
  22. //
  23.  
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //
  27. //    Constants
  28. //
  29.  
  30.  
  31. //////////////////////////////////////////////////////////////////////////////
  32. //
  33. //    Types
  34. //
  35.         
  36.         
  37. //////////////////////////////////////////////////////////////////////////////
  38. //
  39. //    Globals
  40. //
  41.  
  42.  
  43. //////////////////////////////////////////////////////////////////////////////
  44. //
  45. //    Prototypes
  46. //
  47. void        INTFUNC    CenterWindows(HWND);
  48. BOOL        INTFUNC    DefaultDlgProc(HWND, UINT, WPARAM, LPARAM);
  49.  
  50.  
  51. //////////////////////////////////////////////////////////////////////////////
  52. //
  53. //  CenterWindow
  54. //
  55. //      Center a window inside its parent.
  56. //
  57. //  Params:
  58. //      hwnd        -- handle to the window to center
  59. //
  60. //  Returns:
  61. //      none
  62. //
  63. void INTFUNC CenterWindow(HWND hwnd)
  64.     {
  65.     HWND    hwndParent;             // parent window handle
  66.     RECT    rcWnd,                  // window rectangle
  67.             rcScr,                  // screen rectangle
  68.             rcParent;               // parent rectangle
  69.     int     cx, cy;                 // window dimensions
  70.  
  71.         // Get the window's parent
  72.  
  73.     hwndParent = GetParent(hwnd);
  74.     if( !hwndParent )
  75.         hwndParent = GetDesktopWindow();
  76.     
  77.         // Get the dimensions of the window we're centering
  78.         
  79.     GetWindowRect(hwnd, &rcWnd);
  80.     cx = rcWnd.right  - rcWnd.left;
  81.     cy = rcWnd.bottom - rcWnd.top;
  82.  
  83.         // Center it inside its parent
  84.         
  85.     GetWindowRect(hwndParent, &rcParent);
  86.     rcWnd.top    = rcParent.top +
  87.                     (((rcParent.bottom - rcParent.top) - cy) >> 1);
  88.     rcWnd.left   = rcParent.left + 
  89.                     (((rcParent.right - rcParent.left) - cx) >> 1);
  90.     rcWnd.bottom = rcWnd.top  + cy;
  91.     rcWnd.right  = rcWnd.left + cx;
  92.                                       
  93.         // Make sure that it is completely on the screen,
  94.         // centered or not, it's more important to see it!
  95.         
  96.     GetWindowRect(GetDesktopWindow(), &rcScr);
  97.     if( rcWnd.bottom > rcScr.bottom )
  98.         {
  99.         rcWnd.bottom = rcScr.bottom;
  100.         rcWnd.top    = rcWnd.bottom - cy;
  101.         }
  102.     if( rcWnd.right  > rcScr.right )
  103.         {
  104.         rcWnd.right = rcScr.right;
  105.         rcWnd.left  = rcWnd.right - cx;
  106.         }
  107.  
  108.     if( rcWnd.left < 0 )
  109.         rcWnd.left = 0;
  110.     if( rcWnd.top  < 0 )
  111.         rcWnd.top  = 0;
  112.  
  113.         // Move the window to its new position
  114.         
  115.     MoveWindow(hwnd, rcWnd.left, rcWnd.top, cx, cy, TRUE);
  116.     return;
  117.     }
  118.  
  119.  
  120. //////////////////////////////////////////////////////////////////////////////
  121. //
  122. //    DefaultDlgProc
  123. //
  124. //      Handle processing common to all dialog boxes.
  125. //
  126. //  Params:
  127. //      hdlg        -- handle to the dialog box
  128. //      wmsg        -- message ID
  129. //      wParam      -- word parameter
  130. //      lParam      -- long parameter
  131. //
  132. //  Returns:
  133. //      BOOL        -- FALSE if DefDlgProc should handle message
  134. //
  135. BOOL INTFUNC DefaultDlgProc(HWND hdlg,
  136.                             UINT wmsg,
  137.                             WPARAM wParam,
  138.                             LPARAM lParam )
  139.     {
  140.     LPAPPINST   lpInst;             // instance information
  141.     
  142.         // Get the instance-specific information
  143.     
  144.     lpInst = (LPAPPINST)GetWindowLong(hdlg, DWL_USER);
  145.     
  146.     switch( wmsg )
  147.         {
  148.         case WM_INITDIALOG:
  149.             CenterWindow(hdlg);
  150.             if( lpInst->fCtl3d && !lpInst->fAutoCtl3d )
  151.                 Ctl3dSubclassDlg(hdlg, CTL3D_ALL);
  152.             
  153.                 // Let DefDlgProc pick the default control
  154.              
  155.             return TRUE;
  156.  
  157. #ifdef WIN32
  158.         case WM_CTLCOLORBTN:
  159.         case WM_CTLCOLORDLG:
  160.         case WM_CTLCOLOREDIT:
  161.         case WM_CTLCOLORLISTBOX:
  162.         case WM_CTLCOLORMSGBOX:
  163.         case WM_CTLCOLORSCROLLBAR:
  164.         case WM_CTLCOLORSTATIC:
  165. #else
  166.         case WM_CTLCOLOR:
  167. #endif        
  168.             if( lpInst->fCtl3d && !lpInst->fAutoCtl3d ) 
  169.                 return (BOOL)Ctl3dCtlColorEx(wmsg, wParam, lParam);
  170.             break;
  171.  
  172.         case WM_SYSCOLORCHANGE:
  173.             if( lpInst->fCtl3d && !lpInst->fAutoCtl3d ) 
  174.                 return Ctl3dColorChange();
  175.             break;
  176.  
  177.         case WM_NCACTIVATE:
  178.         case WM_NCPAINT:
  179.         case WM_SETTEXT:
  180.             if( lpInst->fCtl3d && !lpInst->fAutoCtl3d )
  181.                 {
  182.                 SetWindowLong(hdlg, DWL_MSGRESULT,
  183.                               Ctl3dDlgFramePaint(hdlg, wmsg, wParam, lParam));
  184.                 return TRUE;
  185.                 }
  186.             break;
  187.  
  188.         case WM_COMMAND:
  189.             if( wParam == IDOK || wParam == IDCANCEL )
  190.                 {
  191.                 EndDialog(hdlg, wParam);
  192.                 return TRUE;
  193.                 }
  194.             break;    
  195.            }
  196.            
  197.     return FALSE;
  198.     }
  199.     
  200.     
  201.     
  202. //////////////////////////////////////////////////////////////////////////////
  203. //
  204. //  AboutDlgProc
  205. //
  206. //      Processing for the about dialog box.
  207. //
  208. //  Params:
  209. //      hdlg        -- handle to the dialog box
  210. //      wmsg        -- message ID
  211. //      wParam      -- word parameter
  212. //      lParam      -- long parameter
  213. //
  214. //  Returns:
  215. //      BOOL        -- FALSE if DefDlgProc should handle message
  216. //
  217. BOOL EXPFUNC AboutDlgProc(    HWND hdlg,
  218.                             UINT wmsg,
  219.                             WPARAM wParam,
  220.                             LPARAM lParam )
  221.     {
  222.     char    szSpace[15];            // buffer for free memory display
  223.     
  224.     switch( wmsg )
  225.         {
  226.         case WM_INITDIALOG:
  227.             {
  228.             DWORD           dwFree;
  229. #ifdef WIN32
  230.             MEMORYSTATUS    mst;
  231. #endif
  232.             
  233.                 // Display the current free memory in KB
  234.  
  235. #ifndef WIN32
  236.             dwFree = GetFreeSpace(0) / 1024;
  237. #else
  238.             mst.dwLength = sizeof(mst);
  239.             GlobalMemoryStatus(&mst);
  240.             dwFree = mst.dwAvailVirtual / 1024;
  241. #endif
  242.             if( dwFree < 1000000L )
  243.                 wsprintf(szSpace, "%ld KB", dwFree);
  244.             else
  245.                 wsprintf(szSpace, "%ld MB", dwFree / 1024); 
  246.             SetDlgItemText(hdlg, IDC_STATUS1, szSpace);
  247.             
  248.                 // Store the LPAPPINST pointer
  249.             
  250.             SetWindowLong(hdlg, DWL_USER, lParam);
  251.             }
  252.             break;
  253.            }
  254.            
  255.     return DefaultDlgProc(hdlg, wmsg, wParam, lParam);
  256.     }
  257.     
  258.     
  259. //////////////////////////////////////////////////////////////////////////////
  260. //
  261. //  ConfirmDlgProc
  262. //
  263. //      Dialog procedure for the confirmation box; this will appear whenever
  264. //      the user selects an action that will close the current connection.
  265. //
  266. //  Params:
  267. //      hdlg        -- handle to the dialog box
  268. //      wmsg        -- message ID
  269. //      wParam      -- word parameter
  270. //      lParam      -- long parameter
  271. //
  272. //  Returns:
  273. //      BOOL        -- FALSE if DefDlgProc should handle message
  274. //
  275. BOOL EXPFUNC ConfirmDlgProc(HWND hdlg,
  276.                             UINT wmsg,
  277.                             WPARAM wParam,
  278.                             LPARAM lParam )
  279.     {
  280.     LPAPPINST   lpInst;             // instance information
  281.     char        szConnect[SQL_MAX_DSN_LENGTH+3];    // DSN string
  282.     LPSTR       lpsz;               // pointer for loop
  283.     RECT        rc;                 // status box rectangle
  284.     WORD        wWidth,             // DSN string width in pixels
  285.                 wHalf,              // index of DSN string middle
  286.                 i;                  // loop counter
  287.     HWND        hwnd;               // handle to status box
  288.     HDC         hdc;                // HDC for determining DSN width
  289. #ifdef WIN32
  290.     SIZE        size;               // GetTextExtentPoint return structure
  291. #endif
  292.     
  293.     switch( wmsg )
  294.         {
  295.         case WM_INITDIALOG:
  296.             {
  297.                 // Store the LPAPPINST pointer
  298.                 
  299.             SetWindowLong(hdlg, DWL_USER, lParam);
  300.             lpInst = (LPAPPINST)lParam;
  301.             
  302.                 // Split the data source name in half if it's too long
  303.                 // for the given status box..
  304.             
  305.             hdc = GetDC(NULL);
  306. #ifdef WIN32
  307.             GetTextExtentPoint(hdc, lpInst->szDSN,
  308.                                           lstrlen(lpInst->szDSN), &size);
  309.             wWidth = (WORD)size.cx;
  310. #else
  311.             wWidth = LOWORD(GetTextExtent(hdc, lpInst->szDSN,
  312.                                           lstrlen(lpInst->szDSN)));
  313. #endif
  314.             ReleaseDC(NULL, hdc);
  315.                 
  316.             hwnd = GetDlgItem(hdlg, IDC_STATUS1);
  317.             GetWindowRect(hwnd, &rc);
  318.             
  319.             if( wWidth > (WORD)(rc.right - rc.left) )
  320.                 {
  321.                 wHalf = lstrlen(lpInst->szDSN) >> 1;
  322.                 lpsz = lpInst->szDSN;
  323.                 for( i = 0; i < wHalf; i++ )
  324.                     szConnect[i] = *lpsz++;
  325.                 szConnect[i++] = '\n';
  326.                 while( *lpsz )
  327.                     szConnect[i++] = *lpsz++;
  328.                 szConnect[i] = '\0';
  329.                 }
  330.             else
  331.                 lstrcpy(szConnect, lpInst->szDSN);
  332.                 
  333.                 // Place the name in the status field of the dialog
  334.                 
  335.             SetDlgItemText(hdlg, IDC_STATUS1, szConnect);
  336.             }
  337.             break;
  338.            }
  339.            
  340.     return DefaultDlgProc(hdlg, wmsg, wParam, lParam);
  341.     }
  342.     
  343.     
  344. //////////////////////////////////////////////////////////////////////////////
  345. //
  346. //  OkDlgProc
  347. //
  348. //      Dialog procedure for the informational 'ok' dialog box; this dialog
  349. //      is displayed after successful execution of an SQL statement that
  350. //      does not return a result set, by design.
  351. //
  352. //  Params:
  353. //      hdlg        -- handle to the dialog box
  354. //      wmsg        -- message ID
  355. //      wParam      -- word parameter
  356. //      lParam      -- long parameter
  357. //
  358. //  Returns:
  359. //      BOOL        -- FALSE if DefDlgProc should handle message
  360. //
  361. BOOL EXPFUNC OkDlgProc(    HWND hdlg,
  362.                         UINT wmsg,
  363.                         WPARAM wParam,
  364.                         LPARAM lParam )
  365.     {
  366.     LPAPPINST       lpInst;         // instance information
  367.     
  368.     switch( wmsg )
  369.         {
  370.         case WM_INITDIALOG:
  371.         
  372.                 // Store the LPAPPINST pointer
  373.                 
  374.             SetWindowLong(hdlg, DWL_USER, lParam);
  375.             lpInst = (LPAPPINST)lParam;
  376.             
  377.                 // Set the status text to show the executed SQL statement
  378.             
  379.             SetDlgItemText(hdlg, IDC_STATUS1, lpInst->szSQL);
  380.             break;
  381.            }
  382.            
  383.     return DefaultDlgProc(hdlg, wmsg, wParam, lParam);
  384.     }
  385.     
  386.     
  387. //////////////////////////////////////////////////////////////////////////////
  388. //
  389. //  SQLDlgProc
  390. //
  391. //      Dialog procedure for SQL statement dialog box; this dialog box allows
  392. //      the user to enter and edit a SQL statement.
  393. //
  394. //  Params:
  395. //      hdlg        -- handle to the dialog box
  396. //      wmsg        -- message ID
  397. //      wParam      -- word parameter
  398. //      lParam      -- long parameter
  399. //
  400. //  Returns:
  401. //      BOOL        -- FALSE if DefDlgProc should handle message
  402. //
  403. BOOL EXPFUNC SQLDlgProc(HWND hdlg,
  404.                         UINT wmsg,
  405.                         WPARAM wParam,
  406.                         LPARAM lParam )
  407.     {
  408.     LPAPPINST       lpInst;         // instance information
  409.     
  410.     if( wmsg != WM_INITDIALOG )
  411.         lpInst = (LPAPPINST)GetWindowLong(hdlg, DWL_USER);
  412.     
  413.     switch( wmsg )
  414.         {
  415.         case WM_INITDIALOG:
  416.  
  417.                 // Store the LPAPPINST pointer
  418.                 
  419.             SetWindowLong(hdlg, DWL_USER, lParam);
  420.             lpInst = (LPAPPINST)lParam;
  421.             
  422.             SetDlgItemText(hdlg, IDC_EDIT1, lpInst->szSQL);
  423.             SendDlgItemMessage(hdlg, IDC_EDIT1, EM_LIMITTEXT,
  424.                                cbMAXSQL - 1, 0L);
  425.                                
  426.             break;
  427.             
  428.         case WM_COMMAND:
  429.             switch( wParam )
  430.                 {
  431.                 case IDOK:
  432.                     GetDlgItemText(hdlg, IDC_EDIT1, lpInst->szSQL, cbMAXSQL);
  433.                     // fall through
  434.                     
  435.                 case IDCANCEL:
  436.                     EndDialog(hdlg, wParam);
  437.                     return TRUE;
  438.                 }
  439.             break;
  440.         }
  441.         
  442.     return DefaultDlgProc(hdlg, wmsg, wParam, lParam);
  443.     }
  444.