home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / 3DTOSHI2.ZIP / mpgui / source / guicmdlg.cpp < prev    next >
C/C++ Source or Header  |  1996-04-18  |  13KB  |  450 lines

  1.  
  2. // guicmdlg.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "guicmdlg.h"
  8.  
  9. UINT APIPROC FileDlgProc ( HWINDOW hWindow, UINT iMessage, PARAM1 Param1, 
  10.                            PARAM2 Param2 );
  11.  
  12. //***********************************************
  13. //
  14. // GUI File Dialog Class
  15. //
  16. //***********************************************
  17.  
  18. GUIFILEDIALOG::GUIFILEDIALOG ( FD_TYPE forOpen ) : GUIDIALOG ()
  19.   {
  20.     ForOpen = forOpen;
  21.     FileAttr.DefPath = NULL;
  22.     FileAttr.DefExt = NULL;
  23.     FileAttr.DefName = NULL;
  24.     FileAttr.NumExt = 0;
  25.     FileAttr.Extensions = NULL;
  26.     FileName = NULL;
  27.   } // End of Constructor for GUIFILEDIALOG
  28.  
  29. GUIFILEDIALOG::~GUIFILEDIALOG ()
  30.   {
  31.     if (FileAttr.DefPath!=NULL)
  32.       delete FileAttr.DefPath;
  33.     if (FileAttr.DefExt!=NULL)
  34.       delete FileAttr.DefExt;
  35.     if (FileAttr.DefName!=NULL)
  36.       delete FileAttr.DefName;
  37.  
  38.     INT i;
  39.     if (FileAttr.Extensions!=NULL)
  40.       {
  41.         for (i=0;i<FileAttr.NumExt;i++)
  42.           {
  43.             if (FileAttr.Extensions[i].Ext!=NULL)
  44.               delete FileAttr.Extensions[i].Ext;
  45.             FileAttr.Extensions[i].Ext = NULL;
  46.             if (FileAttr.Extensions[i].Description!=NULL)
  47.               delete FileAttr.Extensions[i].Description;
  48.             FileAttr.Extensions[i].Description = NULL;
  49.           } // End for
  50.         delete FileAttr.Extensions;
  51.       } // End if
  52.     FileAttr.DefPath = NULL;
  53.     FileAttr.DefExt = NULL;
  54.     FileAttr.DefName = NULL;
  55.     FileAttr.Extensions = NULL;
  56.     FileAttr.NumExt = 0;
  57.     if (FileName!=NULL)
  58.       delete FileName;
  59.     FileName = NULL;
  60.   } // End of Destructor for GUIFILEDIALOG
  61.  
  62. VOID GUIFILEDIALOG::CloseWindow ()
  63.   {
  64.   } // End of CloseWindow for GUIFILEDIALOG
  65.  
  66. LONG GUIFILEDIALOG::OnCreate ()
  67.   {
  68.     return 0;
  69.   } // End of OnCreate for GUIFILEDIALOG
  70.  
  71. LONG GUIFILEDIALOG::OnClose ()
  72.   {
  73.     return 0;
  74.   } // End of OnClose for GUIFILEDIALOG
  75.  
  76. BOOLEAN GUIFILEDIALOG::Create ( STRING Title, LONG x, LONG y, LONG Wd, LONG Ht,
  77.                                 GUIWINDOW *Parent )
  78.   {
  79.     if (Title)
  80.       {}
  81.     if (x&y&Wd&Ht)
  82.       {}
  83.     if (Parent)
  84.       {}
  85.     return FAILURE;
  86.   } // End of Create for GUIFILEDIALOG
  87.  
  88. VOID GUIFILEDIALOG::SetDefaultPath ( STRING Path )
  89.   {
  90.     if (FileAttr.DefPath!=NULL)
  91.       delete FileAttr.DefPath;
  92.     FileAttr.DefPath = new CHAR [strlen(Path)+1];
  93.     strcpy ( FileAttr.DefPath, Path );
  94.   } // End of SetDefaultPath for GUIFILEDIALOG
  95.  
  96. VOID GUIFILEDIALOG::SetDefaultName ( STRING FileName )
  97.   {
  98.     if (FileAttr.DefName!=NULL)
  99.       delete FileAttr.DefName;
  100.     FileAttr.DefName = new CHAR [strlen(FileName)+1];
  101.     strcpy ( FileAttr.DefName, FileName );
  102.   } // End of SetDefaultName for GUIFILEDIALOG
  103.  
  104. VOID GUIFILEDIALOG::SetDefaultExt ( STRING ExtName )
  105.   {
  106.     if (FileAttr.DefExt!=NULL)
  107.       delete FileAttr.DefExt;
  108.     FileAttr.DefExt = new CHAR [strlen(ExtName)+1];
  109.     strcpy ( FileAttr.DefExt, ExtName );
  110.   } // End of SetDefaultExt for GUIFILEDIALOG
  111.  
  112. VOID GUIFILEDIALOG::CreateExtensions ( LONG Num )
  113.   {
  114.     if (Num<=0)
  115.       return;
  116.     LONG i;
  117.     if (FileAttr.Extensions!=NULL)
  118.       {
  119.         for (i=0;i<Num;i++)
  120.           {
  121.             if (FileAttr.Extensions[i].Ext!=NULL)
  122.               delete FileAttr.Extensions[i].Ext;
  123.             FileAttr.Extensions[i].Ext = NULL;
  124.             if (FileAttr.Extensions[i].Description!=NULL)
  125.               delete FileAttr.Extensions[i].Description;
  126.             FileAttr.Extensions[i].Description = NULL;
  127.           } // End for
  128.         delete FileAttr.Extensions;
  129.       } // End if
  130.  
  131.     FileAttr.Extensions = new EXTENSION [Num];
  132.     if (FileAttr.Extensions==NULL)
  133.       {
  134.         return;
  135.       } // End if
  136.  
  137.     FileAttr.NumExt = Num;
  138.     for (i=0;i<Num;i++)
  139.       {
  140.         FileAttr.Extensions[i].Ext = NULL;
  141.         FileAttr.Extensions[i].Description = NULL;
  142.       } // End for
  143.   } // End of CreateExtensions for GUIFILEDIALOG
  144.  
  145. VOID GUIFILEDIALOG::SetExtension ( LONG Index, STRING Ext, STRING Description )
  146.   {
  147.     if ((Index<0)||(Index>=FileAttr.NumExt))
  148.       return;
  149.  
  150.     if (FileAttr.Extensions[Index].Ext!=NULL)
  151.       delete FileAttr.Extensions[Index].Ext;
  152.     FileAttr.Extensions[Index].Ext = new CHAR [strlen(Ext)+1];
  153.     strcpy ( FileAttr.Extensions[Index].Ext, Ext );
  154.  
  155.     if (FileAttr.Extensions[Index].Description!=NULL)
  156.       delete FileAttr.Extensions[Index].Description;
  157.     FileAttr.Extensions[Index].Description = new CHAR [strlen(Description)+1];
  158.     strcpy ( FileAttr.Extensions[Index].Description, Description );
  159.   } // End of SetExtension for GUIFILEDIALOG
  160.  
  161. LONG GUIFILEDIALOG::ExecuteOpen ( HWINDOW hWindow )
  162.   {
  163.     LONG Result;
  164.  
  165.     if (FileName!=NULL)
  166.       delete FileName;
  167.     FileName = NULL;
  168.  
  169.     if (hWindow)
  170.       {}
  171.     #if defined (__FORWINDOWS__)
  172.       OPENFILENAME ofn;
  173.       ofn.lStructSize = sizeof(OPENFILENAME);
  174.       ofn.hwndOwner = hWindow;
  175.       ofn.hInstance = hInstance;
  176.       ofn.lpstrCustomFilter = new CHAR [10];
  177.         memset ( ofn.lpstrCustomFilter, 0, 10 );
  178.       ofn.nMaxFileTitle = 256;
  179.       ofn.nMaxCustFilter = 20;
  180.       ofn.nFilterIndex = 0;
  181.       ofn.lpstrFileTitle = new CHAR [256];
  182.         strcpy ( ofn.lpstrFileTitle, "" );
  183.       ofn.nMaxFile = 256;
  184.       ofn.lpstrFile = new CHAR [256];
  185.       if (FileAttr.DefName==NULL)
  186.         {
  187.           strcpy ( ofn.lpstrFile, "" );
  188.         } // End if  
  189.       else
  190.         {
  191.           strcpy ( ofn.lpstrFile, FileAttr.DefName );
  192.         } // End else
  193.       if (FileAttr.DefPath==NULL)
  194.         {
  195.           ofn.lpstrInitialDir = new CHAR [4];
  196.           strcpy ( (CHAR*)ofn.lpstrInitialDir, "" );
  197.         } // End if
  198.       else
  199.         {
  200.           ofn.lpstrInitialDir = new CHAR [strlen(FileAttr.DefPath)+1];
  201.                          strcpy ( (CHAR*)ofn.lpstrInitialDir, FileAttr.DefPath );
  202.         } // End else    
  203.       ofn.lpstrTitle = new CHAR [10];
  204.         strcpy ( (CHAR*)ofn.lpstrTitle, "" );
  205.       ofn.Flags = OFN_FILEMUSTEXIST | OFN_ENABLEHOOK;
  206.       ofn.nFileOffset = 0;
  207.       ofn.nFileExtension = 0;
  208.       if (FileAttr.DefExt==NULL)
  209.         {
  210.           ofn.lpstrDefExt = new CHAR [10];
  211.           strcpy ( (CHAR*)ofn.lpstrDefExt, "*.*" );
  212.         } // End if
  213.                 else
  214.         {
  215.           ofn.lpstrDefExt = new CHAR [strlen(FileAttr.DefExt)+1];
  216.           strcpy ( (CHAR*)ofn.lpstrDefExt, FileAttr.DefExt );
  217.         } // End else
  218.         
  219.       ofn.lCustData = 0; 
  220.       ofn.lpfnHook = FileDlgProc;
  221.       ofn.lpTemplateName = NULL;
  222.  
  223.       LONG i;
  224.  
  225.       ofn.lpstrFilter = new CHAR [256]; // Change it
  226.       memset ( (CHAR*)ofn.lpstrFilter, 0, 256 );
  227.       LONG j,Count=0,NumStr;
  228.       for (i=0;i<FileAttr.NumExt;i++)
  229.         {
  230.           if (FileAttr.Extensions[i].Description)
  231.             {
  232.               NumStr = strlen (FileAttr.Extensions[i].Description);  
  233.               for (j=0;j<NumStr;j++)
  234.                 {
  235.                   ((CHAR*)ofn.lpstrFilter)[Count++] =
  236.                     FileAttr.Extensions[i].Description[j];
  237.                 } // End for
  238.             } // End if
  239.           ((CHAR*)ofn.lpstrFilter)[Count++] = '\0';
  240.           if (FileAttr.Extensions[i].Ext)
  241.             {
  242.               NumStr = strlen (FileAttr.Extensions[i].Ext);  
  243.               for (j=0;j<NumStr;j++)
  244.                 {
  245.                   ((CHAR*)ofn.lpstrFilter)[Count++] =
  246.                     FileAttr.Extensions[i].Ext[j];
  247.                 } // End for  
  248.             } // End if          
  249.                          ((CHAR*)ofn.lpstrFilter)[Count++] = '\0';
  250.         } // End for
  251.  
  252.       Result = GetOpenFileName ( &ofn );
  253.       if (Result)
  254.         {
  255.           FileName = new CHAR [strlen(ofn.lpstrFileTitle)+1];
  256.           strcpy ( FileName, ofn.lpstrFileTitle );
  257.         } // End if
  258.  
  259.       // Clean Up
  260.       delete (CHAR*)(ofn.lpstrCustomFilter);
  261.                 //delete (CHAR*)(ofn.lpstrFile);
  262.       delete (CHAR*)(ofn.lpstrFileTitle);
  263.       delete (CHAR*)(ofn.lpstrInitialDir);
  264.       delete (CHAR*)(ofn.lpstrTitle);
  265.       delete (CHAR*)(ofn.lpstrDefExt);
  266.       delete (CHAR*)(ofn.lpstrFilter);
  267.     #endif
  268.     return Result;
  269.   } // End of ExecuteOpen for GUIFILEDIALOG
  270.  
  271. LONG GUIFILEDIALOG::ExecuteSave ( HWINDOW hWindow )
  272.   {
  273.          LONG Result;
  274.  
  275.     if (FileName!=NULL)
  276.       delete FileName;
  277.     FileName = NULL;
  278.  
  279.     if (hWindow)
  280.       {}
  281.     #if defined (__FORWINDOWS__)
  282.       OPENFILENAME ofn;
  283.       ofn.lStructSize = sizeof(OPENFILENAME);
  284.       ofn.hwndOwner = hWindow;
  285.                 ofn.hInstance = hInstance;
  286.       ofn.lpstrCustomFilter = new CHAR [10];
  287.         memset ( ofn.lpstrCustomFilter, 0, 10 );
  288.       ofn.nMaxFileTitle = 256;
  289.       ofn.nMaxCustFilter = 20;
  290.       ofn.nFilterIndex = 0;
  291.       ofn.lpstrFileTitle = new CHAR [256];
  292.         strcpy ( ofn.lpstrFileTitle, "" );
  293.       ofn.nMaxFile = 256;
  294.       ofn.lpstrFile = new CHAR [256];
  295.       if (FileAttr.DefName==NULL)
  296.         {
  297.           strcpy ( ofn.lpstrFile, "" );
  298.         } // End if  
  299.       else
  300.         {
  301.           strcpy ( ofn.lpstrFile, FileAttr.DefName );
  302.         } // End else
  303.       if (FileAttr.DefPath==NULL)
  304.         {
  305.           ofn.lpstrInitialDir = new CHAR [4];
  306.           strcpy ( (CHAR*)ofn.lpstrInitialDir, "" );
  307.         } // End if
  308.       else
  309.         {
  310.           ofn.lpstrInitialDir = new CHAR [strlen(FileAttr.DefPath)+1];
  311.           strcpy ( (CHAR*)ofn.lpstrInitialDir, FileAttr.DefPath );
  312.         } // End else    
  313.       ofn.lpstrTitle = new CHAR [10];
  314.         strcpy ( (CHAR*)ofn.lpstrTitle, "" );
  315.       ofn.Flags = OFN_OVERWRITEPROMPT | OFN_ENABLEHOOK;
  316.       ofn.nFileOffset = 0;
  317.       ofn.nFileExtension = 0;
  318.       if (FileAttr.DefExt==NULL)
  319.         {
  320.           ofn.lpstrDefExt = new CHAR [10];
  321.           strcpy ( (CHAR*)ofn.lpstrDefExt, "*.*" );
  322.         } // End if
  323.       else
  324.         {
  325.           ofn.lpstrDefExt = new CHAR [strlen(FileAttr.DefExt)+1];
  326.           strcpy ( (CHAR*)ofn.lpstrDefExt, FileAttr.DefExt );
  327.         } // End else
  328.         
  329.       ofn.lCustData = 0;
  330.       ofn.lpfnHook = FileDlgProc;
  331.       ofn.lpTemplateName = NULL;
  332.  
  333.       LONG i;
  334.  
  335.       ofn.lpstrFilter = new CHAR [256]; // Change it
  336.       memset ( (CHAR*)ofn.lpstrFilter, 0, 256 );
  337.       LONG j,Count=0,NumStr;
  338.       for (i=0;i<FileAttr.NumExt;i++)
  339.         {
  340.           if (FileAttr.Extensions[i].Description)
  341.             {
  342.               NumStr = strlen (FileAttr.Extensions[i].Description);  
  343.               for (j=0;j<NumStr;j++)
  344.                 {
  345.                   ((CHAR*)ofn.lpstrFilter)[Count++] =
  346.                     FileAttr.Extensions[i].Description[j];
  347.                 } // End for  
  348.             } // End if
  349.           ((CHAR*)ofn.lpstrFilter)[Count++] = '\0';
  350.           if (FileAttr.Extensions[i].Ext)
  351.             {
  352.               NumStr = strlen (FileAttr.Extensions[i].Ext);  
  353.               for (j=0;j<NumStr;j++)
  354.                 {
  355.                   ((CHAR*)ofn.lpstrFilter)[Count++] =
  356.                     FileAttr.Extensions[i].Ext[j];
  357.                 } // End for  
  358.             } // End if          
  359.           ((CHAR*)ofn.lpstrFilter)[Count++] = '\0';
  360.         } // End for
  361.  
  362.       Result = GetSaveFileName ( &ofn );
  363.       if (Result)
  364.         {
  365.           FileName = new CHAR [strlen(ofn.lpstrFileTitle)+1];
  366.           strcpy ( FileName, ofn.lpstrFileTitle );
  367.         } // End if
  368.  
  369.       // Clean Up
  370.       delete (CHAR*)ofn.lpstrCustomFilter;
  371.       delete (CHAR*)ofn.lpstrFile;
  372.       delete (CHAR*)ofn.lpstrFileTitle;
  373.       delete (CHAR*)ofn.lpstrInitialDir;
  374.       delete (CHAR*)ofn.lpstrTitle;
  375.       delete (CHAR*)ofn.lpstrDefExt;
  376.       delete (CHAR*)ofn.lpstrFilter;
  377.     #endif
  378.     return Result;
  379.   } // End of ExecuteSave for GUIFILEDIALOG
  380.  
  381. LONG GUIFILEDIALOG::Execute ( GUIWINDOW *Parent, STRING Name )
  382.   {
  383.     HWINDOW hWindow;
  384.  
  385.     if (Name)
  386.       {}
  387.     if (Parent==NULL)
  388.       hWindow = NULL;
  389.     else
  390.       hWindow = Parent->GetHandle ();
  391.  
  392.     LONG Result;
  393.     if (ForOpen==FD_OPEN)
  394.       Result = ExecuteOpen ( hWindow );
  395.     else if (ForOpen==FD_SAVE)
  396.       Result = ExecuteSave ( hWindow );
  397.     return Result;
  398.   } // End of Execute for GUIFILEDIALOG
  399.  
  400. VOID GUIFILEDIALOG::GetFileName ( STRING Name )
  401.   {
  402.     if (FileName!=NULL)
  403.       strcpy ( Name, FileName );
  404.   } // End of GetFileName for GUIFILEDIALOG
  405.  
  406. LONG GUIFILEDIALOG::WndProc ( HWINDOW hWnd, MESSAGE iMessage, PARAM1 Param1, PARAM2 Param2, BOOLEAN FromClient )
  407.   {
  408.     if (hWnd)
  409.       {}
  410.     if (iMessage)
  411.       {}
  412.     if (Param1)
  413.       {}
  414.     if (Param2)
  415.       {}
  416.     if (FromClient)
  417.       {}
  418.     return 0;
  419.   } // End of WndProc for GUIFILEDIALOG
  420.  
  421.  
  422. //*********************************************************
  423. //
  424. // File Dialog Procedure
  425. //
  426. //*********************************************************
  427.  
  428. UINT APIPROC FileDlgProc ( HWINDOW hWindow, UINT iMessage, PARAM1 Param1, PARAM2 Param2 )
  429.   {
  430.     if (hWindow)
  431.       {}
  432.     if (iMessage)
  433.       {}
  434.     if (Param1)
  435.       {}      
  436.     if (Param2)
  437.       {}      
  438.     switch (iMessage)
  439.       {
  440.         case GUI_WM_INITDIALOG :
  441.           #if defined (__FORWINDOWS__)
  442.             SetWindowPos ( hWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );            
  443.           #endif
  444.           break;   
  445.       } // End of switch
  446.         
  447.     return 0;  
  448.   } // End of FileDlgProc 
  449.  
  450.