home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / graphic / showbmp / filereq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-19  |  16.4 KB  |  448 lines

  1. #define  PROGRAM   "FILEREQ"
  2. #define  LEVEL     "Level 00"
  3. #define  COPYRIGHT "Copyright (c) 1990 George S. Brickner"
  4.  
  5. #pragma title (PROGRAM " " LEVEL " - File Requester")
  6. #pragma linesize(120)
  7. #pragma pagesize(55)
  8.  
  9. /********************************************************************
  10. **       FILEREQ - File Requester and Open Dialog Procedure        **
  11. **                 Level 00 - June 19th, 1990                      **
  12. ********************************************************************/
  13. #define INCL_WIN                       // Include Windows (PM) Defs
  14. #define INCL_GPI                       // Include OS/2 GPI Calls
  15. #define INCL_ERRORS                    // Include Error Defs
  16. #include <os2.h>                       // OS/2 Standard Defs
  17.  
  18. #include <mt\stdio.h>                  // IBM C/2 Standard I/O Defs
  19. #include <mt\stdlib.h>                 // IBM C/2 Standard Library Defs
  20. #include <mt\string.h>                 // IBM C/2 String Defs
  21.  
  22. #include <FILEREQ.h>                   // FILEREQ header defines
  23. #include <SHOWBMP.h>                   // SHOWBMP header defines
  24.  
  25. /********************************************************************
  26. **                   Define Static And Global Data                 **
  27. ********************************************************************/
  28. static CHAR const eyepopper[] = PROGRAM "-" LEVEL "-" __TIMESTAMP__ "-" COPYRIGHT;
  29.  
  30. static CHAR szFileMask[CCHMAXPATH] = "*.*";    // Search mask buffer
  31.  
  32. #pragma subtitle ("Get Current Path")
  33. #pragma page()
  34. /********************************************************************
  35. **                        Get Current Path                         **
  36. ********************************************************************/
  37. static VOID GetCurrentPath (HWND hwnd, PCHAR pcCurrentPath)
  38.   {
  39.   USHORT      usDriveNum, usCurPathLen;
  40.   ULONG       ulDriveMap;
  41.  
  42.     DosQCurDisk (&usDriveNum, &ulDriveMap);
  43.     pcCurrentPath [0] = (CHAR) usDriveNum + (UCHAR) '@';
  44.     pcCurrentPath [1] = ':';
  45.     pcCurrentPath [2] = '\\';
  46.     usCurPathLen = 64;
  47.     DosQCurDir (0, pcCurrentPath + 3, &usCurPathLen);
  48.  
  49.     WinSetDlgItemText (hwnd, IDD_PATH, pcCurrentPath);
  50.   }
  51.  
  52. #pragma subtitle ("Fill Drive List Box")
  53. #pragma page()
  54. /********************************************************************
  55. **                      Fill Drive List Box                        **
  56. ********************************************************************/
  57. static VOID FillDrvListBox (HWND hwnd)
  58.   {
  59.   static CHAR szDrive [] = "[ :]";
  60.   SHORT       sDrive;
  61.   USHORT      usDriveNum;
  62.   ULONG       ulDriveMap;
  63.  
  64.     DosQCurDisk (&usDriveNum, &ulDriveMap);
  65.     WinSendDlgItemMsg (hwnd, IDD_DEVLIST, LM_DELETEALL, NULL, NULL);
  66.  
  67.     for (sDrive = 0; sDrive < 26; sDrive++)
  68.       {
  69.         if (ulDriveMap & 1L << sDrive)
  70.           {
  71.             szDrive [1] = (CHAR) sDrive + (UCHAR) 'A';
  72.             WinSendDlgItemMsg (hwnd, IDD_DEVLIST, LM_INSERTITEM,
  73.                                MPFROM2SHORT (LIT_END, 0),
  74.                                MPFROMP (szDrive));
  75.           }
  76.       }
  77.   }
  78.  
  79. #pragma subtitle ("Fill Directory List Box")
  80. #pragma page()
  81. /********************************************************************
  82. **                    Fill Directory List Box                      **
  83. ********************************************************************/
  84. static VOID FillDirListBox (HWND hwnd)
  85.   {
  86.   static CHAR szTemp[CCHMAXPATH];      // Temp directory name buffer
  87.   FILEFINDBUF findbuf;
  88.   HDIR        hDir = 1;
  89.   USHORT      usSearchCount = 1;
  90.  
  91.     WinSendDlgItemMsg (hwnd, IDD_DIRLIST, LM_DELETEALL, NULL, NULL);
  92.     DosFindFirst ("*.*", &hDir, 0x0017, &findbuf, sizeof findbuf,
  93.                   &usSearchCount, 0L);
  94.     while (usSearchCount)
  95.       {
  96.         if (findbuf.attrFile & 0x0010) // Directory entry
  97.           {
  98.             if (findbuf.achName [0] != '.' ||
  99.                 findbuf.achName [1])   // Insert directory into list box
  100.               {
  101.                 strcpy (szTemp, "[");  // Enclose directory name in brackets
  102.                 strcat (szTemp, findbuf.achName);
  103.                 strcat (szTemp, "]");
  104.                 WinSendDlgItemMsg (hwnd, IDD_DIRLIST, LM_INSERTITEM,
  105.                                    MPFROM2SHORT (LIT_SORTASCENDING, 0),
  106.                                    MPFROMP (szTemp));
  107.               }
  108.           }
  109.         DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount);
  110.       }
  111.   }
  112.  
  113. #pragma subtitle ("Fill File List Box")
  114. #pragma page()
  115. /********************************************************************
  116. **                      Fill File List Box                         **
  117. ********************************************************************/
  118. static VOID FillFileListBox (HWND hwnd)
  119.   {
  120.   FILEFINDBUF findbuf;
  121.   HDIR        hDir = 1;
  122.   USHORT      usSearchCount = 1;
  123.  
  124.     WinSendDlgItemMsg (hwnd, IDD_FILELIST, LM_DELETEALL, NULL, NULL);
  125.  
  126.     DosFindFirst (szFileMask, &hDir, 0x0007, &findbuf, sizeof findbuf,
  127.                   &usSearchCount, 0L);
  128.  
  129.     while (usSearchCount)
  130.       {
  131.         WinSendDlgItemMsg (hwnd, IDD_FILELIST, LM_INSERTITEM,
  132.                            MPFROM2SHORT (LIT_SORTASCENDING, 0),
  133.                            MPFROMP (findbuf.achName));
  134.         DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount);
  135.       }
  136.   }
  137.  
  138. #pragma subtitle ("Set Open Search Mask")
  139. #pragma page()
  140. /********************************************************************
  141. **                    Set Initial Open Search Mask                 **
  142. ********************************************************************/
  143. VOID SetOpenMask (PCHAR pSearchMask)
  144.   {
  145.     if (pSearchMask)                   // Search mask supplied
  146.       {
  147.         strcpy (szFileMask, pSearchMask);
  148.       }
  149.     else                               // Use default mask
  150.       {
  151.         strcpy (szFileMask, "*.*");
  152.       }
  153.   }
  154.  
  155. #pragma subtitle ("Process Open Dialog")
  156. #pragma page()
  157. /********************************************************************
  158. **                        Process Open Dialog                      **
  159. ********************************************************************/
  160. MRESULT EXPENTRY OpenDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  161.   {
  162.   static CHAR  szCurrentPath [CCHMAXPATH], szBuffer [CCHMAXPATH];
  163.   static PCHAR pFileName;              // File name ptr
  164.   SHORT        sSelect;                // Current listbox selection
  165.   PCHAR        p;                      // Misc pointer
  166.  
  167.     switch (msg)                       // Process Dialog Message
  168.       {
  169.         case WM_INITDLG:               // Initialize Dialog Box
  170.           {
  171.             if (mp2)                   // File Name Ptr Supplied
  172.               {
  173.                 pFileName = (PCHAR) mp2;   // Save ptr to user's filename area
  174.                 WinSendDlgItemMsg (hwnd, IDD_FILEEDIT, EM_SETTEXTLIMIT,
  175.                                    MPFROM2SHORT (CCHMAXPATH, 0), NULL);
  176.                 WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask); // Show search mask
  177.                 FillDrvListBox (hwnd);
  178.                 GetCurrentPath (hwnd, szCurrentPath);
  179.                 FillDirListBox (hwnd);
  180.                 FillFileListBox (hwnd);
  181.               }
  182.             else                       // File Name Ptr Not Supplied
  183.               {
  184.                 pFileName = NULL;
  185.                 WinDismissDlg (hwnd, FALSE);
  186.               }
  187.             return FALSE;
  188.           }
  189.         case WM_CONTROL:               // Dialog Control
  190.           {
  191.             if (SHORT1FROMMP (mp1) == IDD_DEVLIST ||  // Drive
  192.                 SHORT1FROMMP (mp1) == IDD_DIRLIST ||  // Directory
  193.                 SHORT1FROMMP (mp1) == IDD_FILELIST)   // Filename
  194.               {
  195.                 sSelect = SHORT1FROMMR (WinSendDlgItemMsg (hwnd,
  196.                               SHORT1FROMMP (mp1), LM_QUERYSELECTION, 0L, 0L));
  197.                 WinSendDlgItemMsg (hwnd, SHORT1FROMMP (mp1),
  198.                                    LM_QUERYITEMTEXT,
  199.                                    MPFROM2SHORT (sSelect, sizeof szBuffer),
  200.                                    MPFROMP (szBuffer));
  201.               }
  202.  
  203.             if (SHORT1FROMMP (mp1) == IDD_DEVLIST) // Drive List Box
  204.               {
  205.                 switch (SHORT2FROMMP (mp1))    // Notification code
  206.                   {
  207.                     case LN_ENTER:             // ENTER
  208.                       {
  209.                         if (szBuffer [0] == '[')
  210.                           {
  211.                             DosSelectDisk (szBuffer [1] - '@');
  212.                           }
  213.                         else
  214.                           {
  215.                             DosChDir (szBuffer, 0L);
  216.                           }
  217.                         FillDrvListBox (hwnd);
  218.                         GetCurrentPath (hwnd, szCurrentPath);
  219.                         FillDirListBox (hwnd);
  220.                         FillFileListBox (hwnd);
  221.                         WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask);
  222.                         return FALSE;
  223.                       }
  224.                   }
  225.               }
  226.             else if (SHORT1FROMMP (mp1) == IDD_DIRLIST) // Dir List Box
  227.               {
  228.                 switch (SHORT2FROMMP (mp1))    // Notification code
  229.                   {
  230.                     case LN_ENTER:             // ENTER
  231.                       {
  232.                         if (p = strchr (szBuffer, ']')) // Delete "]"
  233.                           {
  234.                             *p = '\0';
  235.                           }
  236.                         DosChDir (&szBuffer[1], 0L);
  237.                         GetCurrentPath (hwnd, szCurrentPath);
  238.                         FillDirListBox (hwnd);
  239.                         FillFileListBox (hwnd);
  240.                         WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask);
  241.                         return FALSE;
  242.                       }
  243.                   }
  244.               }
  245.             else if (SHORT1FROMMP (mp1) == IDD_FILELIST) // File List Box
  246.               {
  247.                 switch (SHORT2FROMMP (mp1))    // Notification code
  248.                   {
  249.                     case LN_SELECT:            // Selected
  250.                       {
  251.                         WinSetDlgItemText (hwnd, IDD_FILEEDIT, szBuffer);
  252.                         return FALSE;
  253.                       }
  254.                     case LN_ENTER:             // ENTER
  255.                       {
  256.                         ParseFileName (pFileName, szBuffer);
  257.                         WinDismissDlg (hwnd, TRUE);
  258.                         return FALSE;
  259.                       }
  260.                   }
  261.               }
  262.             else if (SHORT1FROMMP (mp1) == IDD_FILEEDIT) // File edit field
  263.               {
  264.               }
  265.             break;
  266.           }
  267.         case WM_COMMAND:               // Window Command
  268.           {
  269.             switch (COMMANDMSG(&msg)->cmd) // Get Diaglog Item Clicked
  270.               {
  271.                 case DID_OK:           // Clicked "OK"
  272.                   {
  273.                     WinQueryDlgItemText (hwnd, IDD_FILEEDIT, sizeof szBuffer, szBuffer);
  274.                     switch (ParseFileName (szCurrentPath, szBuffer))
  275.                       {
  276.                         case 0:        // Invalid Drive Or Directory
  277.                           {
  278.                             WinAlarm (HWND_DESKTOP, WA_ERROR);
  279.                             FillDrvListBox (hwnd);
  280.                             GetCurrentPath (hwnd, szCurrentPath);
  281.                             FillDirListBox (hwnd);
  282.                             FillFileListBox (hwnd);
  283.                             WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask);
  284.                             return FALSE;
  285.                           }
  286.                         case 1:        // Empty Or No Filename
  287.                           {
  288.                             FillDrvListBox (hwnd);
  289.                             GetCurrentPath (hwnd, szCurrentPath);
  290.                             FillDirListBox (hwnd);
  291.                             FillFileListBox (hwnd);
  292.                             WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask);
  293.                             return FALSE;
  294.                           }
  295.                         case 2:        // Valid Path & Filename
  296.                           {
  297.                             strcpy (pFileName, szCurrentPath);
  298.                             WinDismissDlg (hwnd, TRUE);
  299.                             return FALSE;
  300.                           }
  301.                         case 3:        // Wildcards in filename
  302.                           {
  303.                             strcpy (szFileMask, szBuffer); // Save as mask
  304.                             FillDrvListBox (hwnd);
  305.                             GetCurrentPath (hwnd, szCurrentPath);
  306.                             FillDirListBox (hwnd);
  307.                             FillFileListBox (hwnd);
  308.                             WinSetDlgItemText (hwnd, IDD_FILEEDIT, szFileMask);
  309.                             return FALSE;
  310.                           }
  311.                       }
  312.                     break;
  313.                   }
  314.                 case DID_CANCEL:       // Clicked "CANCEL"
  315.                   {
  316.                     WinDismissDlg (hwnd, FALSE);
  317.                     return FALSE;
  318.                   }
  319.               }
  320.             break;
  321.           }
  322.       }
  323.     return WinDefDlgProc (hwnd, msg, mp1, mp2);
  324.   }
  325.  
  326. #pragma subtitle ("Parse File Name")
  327. #pragma page()
  328. /********************************************************************
  329. **                       Parse File Name                           **
  330. **                                                                 **
  331. ** Input:    pcOut -- Pointer to parsed file specification.        **
  332. **           pcIn  -- Pointer to raw file specification.           **
  333. **                                                                 **
  334. ** Returns:  0 -- pcIn had invalid drive or directory.             **
  335. **           1 -- pcIn was empty or had no filename.               **
  336. **           2 -- pcOut points to drive, full dir, and file name.  **
  337. **           3 -- pcIn contains wildcard characters.               **
  338. **                                                                 **
  339. ** Changes current drive and directory per pcIn string.            **
  340. ********************************************************************/
  341. USHORT APIENTRY ParseFileName (PCHAR pcOut, PCHAR pcIn)
  342.   {
  343.   PCHAR  pcLastSlash, pcFileOnly;
  344.   ULONG  ulDriveMap;
  345.   USHORT usDriveNum, usDirLen = 64;
  346.  
  347.     strupr (pcIn);
  348.  
  349.     if (pcIn [0] == '\0')  // If input string is empty, return 1
  350.       {
  351.         return 1;
  352.       }
  353.  
  354.     if (pcIn [1] == ':')  // Get drive from input string or current drive
  355.       {
  356.         if (DosSelectDisk (pcIn [0] - '@'))
  357.           {
  358.             return 0;
  359.           }
  360.         pcIn += 2;
  361.       }
  362.  
  363.     DosQCurDisk (&usDriveNum, &ulDriveMap);
  364.     *pcOut++ = (CHAR) usDriveNum + (UCHAR) '@';
  365.     *pcOut++ = ':';
  366.     *pcOut++ = '\\';
  367.     *pcOut   = '\0';
  368.  
  369.     if (strpbrk (pcIn, "*?"))          // Wildcard chars in file name
  370.       {
  371.         return 3;
  372.       }
  373.  
  374.     if (pcIn [0] == '\0') // If rest of string is empty, return 1
  375.       {
  376.         return 1;
  377.       }
  378.  
  379.     // Search for last backslash.  If none, could be directory.
  380.  
  381.     if (NULL == (pcLastSlash = strrchr (pcIn, '\\')))
  382.       {
  383.         if (!DosChDir (pcIn, 0L))
  384.           {
  385.             return 1;
  386.           }
  387.  
  388.         // Otherwise, get current dir & attach input filename
  389.  
  390.         DosQCurDir (0, pcOut, &usDirLen);
  391.  
  392.         if (strlen (pcIn) > 12)
  393.           {
  394.             return 0;
  395.           }
  396.  
  397.         if (*(pcOut + strlen (pcOut) - 1) != '\\')
  398.           {
  399.             strcat (pcOut++, "\\");
  400.           }
  401.  
  402.         strcat (pcOut, pcIn);
  403.         return 2;
  404.       }
  405.  
  406.     if (pcIn == pcLastSlash) // If the only backslash is at beginning, change to root
  407.       {
  408.         DosChDir ("\\", 0L);
  409.  
  410.         if (pcIn [1] == '\0')
  411.           {
  412.             return 1;
  413.           }
  414.  
  415.         strcpy (pcOut, pcIn + 1);
  416.         return 2;
  417.       }
  418.  
  419.     *pcLastSlash = '\0';
  420.  
  421.     if (DosChDir (pcIn, 0L)) // Attempt to change directory -- Get current dir if OK
  422.       {
  423.         return 0;
  424.       }
  425.  
  426.     DosQCurDir (0, pcOut, &usDirLen);
  427.  
  428.     pcFileOnly = pcLastSlash + 1; // Append input filename, if any
  429.  
  430.     if (*pcFileOnly == '\0')
  431.       {
  432.         return 1;
  433.       }
  434.  
  435.     if (strlen (pcFileOnly) > 12)
  436.       {
  437.         return 0;
  438.       }
  439.  
  440.     if (*(pcOut + strlen (pcOut) - 1) != '\\')
  441.       {
  442.         strcat (pcOut++, "\\");
  443.       }
  444.  
  445.     strcat (pcOut, pcFileOnly);
  446.     return 2;
  447.   }
  448.