home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / tool / various / iniwiz / iniwiz.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  10KB  |  412 lines

  1. //---------------------------------------------------------------------------
  2. // INIWIZ.c
  3. //---------------------------------------------------------------------------
  4. // Contains control procedure for INIWIZ control
  5. //---------------------------------------------------------------------------
  6.  
  7. #include <windows.h>
  8. #include <vbapi.h>
  9. #include "inires.h"
  10. #include "iniprop.h"
  11. #include "inievent.h"
  12. #include "INIWIZ.h"
  13.  
  14.  
  15. VBXINFO INIWIZ[MAX_VBXS];
  16.  
  17. //---------------------------------------------------------------------------
  18. // Global Variables
  19. //---------------------------------------------------------------------------
  20. HMODULE hModDLL,hMod3d;
  21. typBUFF szFile, szSection, szEntry, szData;
  22.  
  23.  
  24. //---------------------------------------------------------------------------
  25. //    Local Function Prototypes
  26. //---------------------------------------------------------------------------
  27. static void    GetIniFileProperty(HCTL , LPSTR , LPSTR , LPSTR , LPSTR );
  28. static int     SearchVBXINFOByHCTL ( HCTL );
  29. static int     SearchVBXINFOByHPARENT ( HWND );
  30. static HWND HwndInitAboutPopup(void);
  31.  
  32.  
  33. //---------------------------------------------------------------------------
  34. // Initialize library. This routine is called when the first client loads
  35. // the DLL.
  36. //---------------------------------------------------------------------------
  37. int FAR PASCAL LibMain
  38. (
  39.     HANDLE hModule,
  40.     WORD   wDataSeg,
  41.     WORD   cbHeapSize,
  42.     LPSTR  lpszCmdLine
  43. )
  44. {
  45.  
  46.     // Avoid warnings on unused (but required) formal parameters
  47.     wDataSeg    = wDataSeg;
  48.     cbHeapSize    = cbHeapSize;
  49.     lpszCmdLine = lpszCmdLine;
  50.  
  51.     hModDLL = hModule;
  52.  
  53.     return 1;
  54. }
  55.  
  56.  
  57. //---------------------------------------------------------------------------
  58. // Register custom control. This routine is called by VB when the custom
  59. // control DLL is loaded for use.
  60. //---------------------------------------------------------------------------
  61. BOOL FAR PASCAL _export VBINITCC
  62. (
  63.     USHORT usVersion,
  64.     BOOL   fRuntime
  65. )
  66. {
  67.      WNDCLASS class;
  68.  
  69.     // Avoid warnings on unused (but required) formal parameters
  70.     fRuntime  = fRuntime;
  71.     usVersion = usVersion;
  72.  
  73.     // Register control(s)
  74.     if (!VBRegisterModel(hModDLL, &modelINIWIZ)) return FALSE;
  75.  
  76.     class.style              = 0;
  77.     class.lpfnWndProc   = (FARPROC)AboutPopupWndProc;
  78.     class.cbClsExtra    = 0;
  79.     class.cbWndExtra    = 0;
  80.     class.hInstance     = hModDLL;
  81.     class.hIcon              = NULL;
  82.     class.hCursor         = NULL;
  83.    class.hbrBackground = NULL;
  84.     class.lpszMenuName  = NULL;
  85.     class.lpszClassName = CLASS_ABOUTPOPUP;
  86.  
  87.     if (!RegisterClass(&class))
  88.         return FALSE;
  89.  
  90.     
  91.     return TRUE;
  92. }
  93.  
  94.  
  95. //---------------------------------------------------------------------------
  96. // WEP
  97. //---------------------------------------------------------------------------
  98. // C7 and QCWIN provide default a WEP:
  99. //---------------------------------------------------------------------------
  100. #if (_MSC_VER < 610)
  101.  
  102. int FAR PASCAL WEP(int fSystemExit);
  103.  
  104. #pragma alloc_text(WEP_TEXT,WEP)
  105.  
  106. int FAR PASCAL WEP
  107. (
  108.     int fSystemExit
  109. )
  110. {
  111.     fSystemExit = fSystemExit;
  112.      return 1;
  113. }
  114. #endif // C6
  115.  
  116.  //----------------------------------------------------------
  117.  // INIWIZ Control Procedure
  118.  //----------------------------------------------------------
  119. LONG FAR PASCAL _export INIWIZCtlProc (HCTL hctl, HWND hwnd,
  120.       USHORT msg, USHORT wp, LONG lp)
  121. {
  122.   LONG lResult ;
  123.   int i;
  124.  
  125.   switch (msg)
  126.   {
  127.     case VBM_SETPROPERTY:
  128.           if ( wp == IPROP_INIWIZ_ACTION )
  129.          {
  130.           switch ((int)lp)
  131.           {
  132.             case INI_READ    :
  133.               {
  134.                   GetIniFileProperty(hctl, szFile, szSection, szEntry , NULL );
  135.                   GetPrivateProfileString (szSection, szEntry,""     ,
  136.                                                       szData, sizeof (szData),
  137.                                                     szFile );
  138.                   VBSetControlProperty (hctl, IPROP_INIWIZ_VALUE, (LONG)(LPSTR) szData );
  139.                   return 0;
  140.  
  141.               }
  142.             case INI_WRITE :
  143.               {
  144.                   GetIniFileProperty(hctl, szFile, szSection, szEntry , szData );
  145.                   WritePrivateProfileString (szSection, szEntry, szData, szFile );
  146.                   return    0;
  147.                  
  148.               }
  149.             case INI_DELENTRY :
  150.               {
  151.                   GetIniFileProperty(hctl, szFile, szSection, szEntry , NULL );
  152.                   WritePrivateProfileString (szSection, szEntry, NULL , szFile );
  153.                   return    0;
  154.               }
  155.             case INI_DELSECTION :
  156.               {
  157.                   GetIniFileProperty(hctl, szFile, szSection, NULL , NULL );
  158.                   WritePrivateProfileString (szSection, NULL , NULL , szFile );
  159.                   return    0;
  160.               }
  161.             }
  162.         }
  163.          break;
  164.  
  165.      case VBM_INITPROPPOPUP : 
  166.              if ( wp == IPROP_INIWIZ_ABOUT )
  167.                     return HwndInitAboutPopup();
  168.             break;
  169.                 
  170.      case WM_NCCREATE : 
  171.       {
  172.         if ( VBGetMode()== MODE_RUN )
  173.          {
  174.               HWND hParent;
  175.             hParent = GetParent(hwnd);
  176.  
  177.              /* Find an empty spot and initialize the VBX Info Array  */
  178.            for(i=0;i<MAX_VBXS;i++)
  179.                if ( INIWIZ[i].hctl == NULL ) 
  180.              {
  181.                  INIWIZ[i].hControl    = hwnd;
  182.                  INIWIZ[i].hctl         = hctl;
  183.                  INIWIZ[i].hParent     = hParent;
  184.                  INIWIZ[i].lpfnOldProc = (FARPROC)NULL;
  185.                 break;
  186.              }
  187.             else
  188.                 if ( INIWIZ[i].hParent == hParent )
  189.                 {
  190.                      MessageBox ( hwnd,"Too Many INI VBXS on One Form!", NULL, MB_OK );
  191.                      return 0L;
  192.                  }
  193.  
  194.              if (i==MAX_VBXS) 
  195.              {
  196.                  MessageBox ( hwnd,"Too Many INI VBXS", NULL, MB_OK );
  197.                  return 0L;
  198.              }
  199.          }
  200.         }
  201.          VBSetControlProperty(hctl, IPROP_INIWIZ_ABOUT, 
  202.                (LONG)(LPSTR)"Click on \"...\" for the About Box");
  203.         break;
  204.  
  205.      case WM_CREATE:
  206.         switch (VBGetMode())
  207.              {
  208.                  // This will only be processed during run mode.
  209.                  case MODE_RUN:
  210.                          if ((i=SearchVBXINFOByHCTL(hctl)) != -1 )
  211.                         {
  212.                            FARPROC lpfnOldProc = (FARPROC) NULL ;
  213.                              // Get the address instance to normal proc.
  214.                              lpfnOldProc = (FARPROC) GetWindowLong(INIWIZ[i].hParent, GWL_WNDPROC) ;
  215.                              // Reset the address instance to the new proc.
  216.                              SetWindowLong (INIWIZ[i].hParent,GWL_WNDPROC, (LONG) SbClsProc) ;
  217.                               INIWIZ[i].lpfnOldProc = lpfnOldProc;
  218.                         }
  219.                         else
  220.                             return -1L;
  221.                          break ;
  222.            }
  223.          break ;
  224.   }
  225.   // Call the default VB for Windows proc.
  226.   return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  227. }
  228.           
  229. LONG FAR PASCAL _export SbClsProc (HWND hwnd, USHORT msg,USHORT wp, LONG lp)
  230. {
  231.  
  232.     int i;
  233.     if ((i=SearchVBXINFOByHPARENT(hwnd)) == -1) return 0L;
  234.  
  235.      switch (msg)
  236.      {
  237.          case WM_WININICHANGE:
  238.             {
  239.                if ((LPINIWIZINFO)LpIniDEREF(INIWIZ[i].hctl)->bNotify ) 
  240.                {
  241.                     CHANGEPARAMS cp;
  242.                     cp.hlstrSection = VBCreateHlstr((LPSTR)lp, lstrlen((LPSTR)lp));
  243.                     VBFireEvent(INIWIZ[i].hctl, IEVENT_INIWIZ_CHANGE, &cp );
  244.                     VBDestroyHlstr(cp.hlstrSection);
  245.                     return 0;
  246.                 }
  247.                 else
  248.                     break;
  249.             }
  250.             break;
  251.  
  252.         case WM_DESTROY:
  253.                 // Reset the Old Window Proc and Also free up the INIWIZ array slot
  254.                 if ((i=SearchVBXINFOByHPARENT(hwnd)) != -1)
  255.                 {
  256.                       SetWindowLong (INIWIZ[i].hParent, GWL_WNDPROC,
  257.                                       (LONG) INIWIZ[i].lpfnOldProc) ;
  258.                     INIWIZ[i].hctl = NULL;
  259.                 }
  260.                 break ;
  261.      }
  262.     // Call INIWIZCtlProc to process any other messages.
  263.      return (CallWindowProc(INIWIZ[i].lpfnOldProc, hwnd, msg, wp, lp));
  264. }
  265.  
  266.  
  267. static int SearchVBXINFOByHCTL ( HCTL hCtl )
  268. {
  269.  int i;
  270.  
  271.   for(i=0;i<MAX_VBXS;i++)
  272.       if ( INIWIZ[i].hctl != NULL ) 
  273.         if (INIWIZ[i].hctl == hCtl ) 
  274.               return i;
  275.   return -1 ; // Not Found 
  276. }
  277.  
  278. static int SearchVBXINFOByHPARENT ( HWND hParent)
  279. {
  280.  int i;
  281.  
  282.   for(i=0;i<MAX_VBXS;i++)
  283.       if ( INIWIZ[i].hctl != NULL ) 
  284.         if (INIWIZ[i].hParent == hParent ) 
  285.               return i;
  286.   return -1 ; // Not Found 
  287. }
  288.  
  289. static void GetIniFileProperty(HCTL hCtl, LPSTR lpszFile, LPSTR lpszSection, LPSTR lpszEntry , LPSTR lpszValue )
  290. {
  291.   HSZ hszFile, hszSection, hszEntry, hszValue;
  292.  
  293.   if (lpszFile)
  294.   {
  295.           VBGetControlProperty ( hCtl, IPROP_INIWIZ_FILE , &hszFile );
  296.         lstrcpy ( lpszFile , VBDerefHsz(hszFile ));
  297.         VBDestroyHsz ( hszFile );
  298.   }
  299.  
  300.   if (lpszSection)
  301.   {
  302.           VBGetControlProperty ( hCtl, IPROP_INIWIZ_SECTION , &hszSection );
  303.         lstrcpy ( lpszSection , VBDerefHsz(hszSection ));
  304.        VBDestroyHsz ( hszSection );
  305.   }
  306.  
  307.   if (lpszEntry)
  308.   {
  309.           VBGetControlProperty ( hCtl, IPROP_INIWIZ_ENTRY , &hszEntry );
  310.         lstrcpy ( lpszEntry , VBDerefHsz(hszEntry ));
  311.         VBDestroyHsz ( hszEntry   );
  312.   }
  313.  
  314.   if (lpszValue)
  315.   {
  316.           VBGetControlProperty ( hCtl, IPROP_INIWIZ_VALUE , &hszValue );
  317.         lstrcpy ( lpszValue , VBDerefHsz(hszValue ));
  318.        VBDestroyHsz ( hszValue );
  319.   }
  320.  
  321. }
  322.  
  323. static HWND HwndInitAboutPopup
  324. (
  325.     VOID
  326. )
  327. {
  328.     return CreateWindow(CLASS_ABOUTPOPUP, NULL, WS_POPUP,
  329.             0, 0, 0, 0, NULL, NULL,
  330.             hModDLL, NULL);
  331. }
  332.  
  333.  
  334. //---------------------------------------------------------------------------
  335. // We asked to show ourself, remain invisible and post a CM_OPENABOUTDLG to
  336. // ourself.  When we receive this message, open the dialog box.
  337. //---------------------------------------------------------------------------
  338. LONG _export FAR PASCAL AboutPopupWndProc
  339. (
  340.     HWND   hwnd,
  341.     USHORT msg,
  342.     USHORT wp,
  343.     LONG   lp
  344. )
  345. {
  346.      FARPROC Ctl3dRegister, Ctl3dAutoSubClass,Ctl3dUnRegister;
  347.  
  348.     switch (msg)
  349.     {
  350.      case WM_SHOWWINDOW:
  351.        if (wp)
  352.         {
  353.             PostMessage(hwnd, CM_OPENABOUTDLG, 0, 0L);
  354.             return 0L;
  355.         }
  356.             break;
  357.  
  358.     case CM_OPENABOUTDLG:
  359.  
  360.          if ( (hMod3d = LoadLibrary ( "CTL3DV2.DLL")) < HINSTANCE_ERROR ) 
  361.                     hMod3d = NULL ;
  362.          else
  363.          {
  364.             Ctl3dRegister = GetProcAddress ( hMod3d, "Ctl3dRegister");
  365.             Ctl3dAutoSubClass = GetProcAddress ( hMod3d, "Ctl3dAutoSubClass" );
  366.  
  367.             Ctl3dRegister (hModDLL);
  368.             Ctl3dAutoSubClass ( hModDLL );
  369.          }
  370.        
  371.         VBDialogBoxParam(hModDLL, "AboutDlg", (FARPROC)AboutDlgProc, 0L);
  372.         if ( hMod3d )
  373.          {
  374.              Ctl3dUnRegister = GetProcAddress ( hMod3d, "Ctl3dUnregister");
  375.              Ctl3dUnRegister ( hModDLL );
  376.           }
  377.           FreeLibrary ( hMod3d );
  378.  
  379.        return 0L;
  380.     }
  381.  
  382.     return DefWindowProc(hwnd, msg, wp, lp);
  383. }
  384.  
  385.  
  386.  
  387.  
  388. BOOL FAR PASCAL _export AboutDlgProc
  389. (
  390.     HWND   hDlg,
  391.     USHORT msg,
  392.     USHORT wp,
  393.     LONG   lp
  394. )
  395. {
  396.     switch (msg)
  397.     {
  398.  
  399.     case WM_COMMAND:
  400.         switch (wp)
  401.         {
  402.                 case IDOK:
  403.                           EndDialog(hDlg, TRUE);
  404.                     return TRUE;
  405.                      case IDCANCEL:
  406.                           EndDialog(hDlg, FALSE );
  407.                     return TRUE;
  408.             }
  409.     }
  410.     return FALSE;
  411. }
  412.