home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0107.zip / Anderson / source / edlg.c < prev    next >
C/C++ Source or Header  |  1992-01-27  |  16KB  |  489 lines

  1. #define INCL_WINHELP
  2. #define INCL_WIN
  3. #define INCL_GPI
  4. #define INCL_DOS
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "edit.h"
  10. #include "edlg.h"
  11.  
  12. /* window procedure for about box */
  13. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  14. {
  15.    switch (msg) {
  16.       case WM_COMMAND:
  17.          switch (SHORT1FROMMP (mp1)) {
  18.             case DID_OK:
  19.             case DID_CANCEL:
  20.                WinDismissDlg (hwnd, TRUE);
  21.                return 0;
  22.             default:
  23.                break;
  24.          }
  25.    }
  26.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  27. }
  28.  
  29.  
  30. /* fill directory box with subdirectory names */
  31. /* by Charles Petzold, from Programming the OS/2 Presentation Manager */
  32. VOID FillDirListBox (HWND hwnd, CHAR *pcCurrentPath)
  33. {
  34.    static CHAR szDrive[] = "  :";
  35.    FILEFINDBUF findbuf;
  36.    HDIR hDir = 1;
  37.    SHORT sDrive;
  38.    USHORT usDriveNum, usCurPathLen, usSearchCount = 1;
  39.    ULONG ulDriveMap;
  40.  
  41.    DosQCurDisk (&usDriveNum, &ulDriveMap);
  42.    pcCurrentPath[0] = (CHAR)((CHAR) usDriveNum + '@');
  43.    pcCurrentPath[1] = ':';
  44.    pcCurrentPath[2] = '\\';
  45.    usCurPathLen = 64;
  46.    DosQCurDir (0, pcCurrentPath + 3, &usCurPathLen);
  47.  
  48.    WinSetDlgItemText (hwnd, DID_PATH, pcCurrentPath);
  49.    WinSendDlgItemMsg (hwnd, DID_DIRLIST, LM_DELETEALL, NULL, NULL);
  50.  
  51.    for (sDrive = 0 ; sDrive < 26 ; sDrive++) {
  52.       if (ulDriveMap & 1L << sDrive) {
  53.          szDrive[1] = (CHAR)((CHAR) sDrive + 'A');
  54.  
  55.          WinSendDlgItemMsg (hwnd, DID_DIRLIST, LM_INSERTITEM,
  56.             MPFROM2SHORT (LIT_END, 0), MPFROMP (szDrive));
  57.       }
  58.    }
  59.  
  60.    DosFindFirst ("*.*", &hDir, 0x0010, &findbuf, sizeof findbuf,
  61.       &usSearchCount, 0L);
  62.    while (usSearchCount) {
  63.       if (findbuf.attrFile & 0x0010 &&
  64.        (findbuf.achName[0] != '.' || findbuf.achName[1])) {
  65.          WinSendDlgItemMsg (hwnd, DID_DIRLIST, LM_INSERTITEM,
  66.             MPFROM2SHORT (LIT_SORTASCENDING, 0), MPFROMP (findbuf.achName));
  67.       }                            
  68.       DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount);
  69.    }
  70. }
  71.  
  72.  
  73. /* fill file box with filenames */
  74. /* by Charles Petzold, from Programming the OS/2 Presentation Manager */
  75. VOID FillFileListBox (HWND hwnd)
  76. {
  77.    FILEFINDBUF findbuf;
  78.    HDIR hDir = 1;
  79.    USHORT usSearchCount = 1;
  80.  
  81.    WinSendDlgItemMsg (hwnd, DID_FILELIST, LM_DELETEALL, NULL, NULL);
  82.  
  83.    DosFindFirst ("*.*", &hDir, 0x0000, &findbuf, sizeof findbuf,
  84.       &usSearchCount, 0L);
  85.    while (usSearchCount) {
  86.       WinSendDlgItemMsg (hwnd, DID_FILELIST, LM_INSERTITEM,
  87.          MPFROM2SHORT (LIT_SORTASCENDING, 0),
  88.          MPFROMP (findbuf.achName));
  89.  
  90.       DosFindNext (hDir, &findbuf, sizeof findbuf, &usSearchCount);
  91.    }
  92. }
  93.  
  94.  
  95. /* window procedure for open file dialog box */
  96. /* by Charles Petzold, from Programming the OS/2 Presentation Manager */
  97. MRESULT EXPENTRY OpenDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  98. {
  99.    static CHAR szCurrentPath[80], szBuffer[80];
  100.    SHORT sSelect;
  101.  
  102.    switch (msg) {
  103.       case WM_INITDLG:
  104.          FillDirListBox (hwnd, szCurrentPath);
  105.          FillFileListBox (hwnd);
  106.  
  107.          WinSendDlgItemMsg (hwnd, DID_FILEEDIT, EM_SETTEXTLIMIT,
  108.             MPFROM2SHORT (70, 0), NULL);
  109.          return 0;
  110.  
  111.       case WM_CONTROL:
  112.          if (SHORT1FROMMP (mp1) == DID_DIRLIST ||
  113.           SHORT1FROMMP (mp1) == DID_FILELIST) {
  114.             sSelect = SHORT1FROMMR (WinSendDlgItemMsg (hwnd,
  115.                                        SHORT1FROMMP (mp1),
  116.                                        LM_QUERYSELECTION, 
  117.                                        (MPARAM) 0L, (MPARAM) 0L));
  118.  
  119.             WinSendDlgItemMsg (hwnd, SHORT1FROMMP (mp1), LM_QUERYITEMTEXT,
  120.                MPFROM2SHORT (sSelect, sizeof szBuffer), MPFROMP (szBuffer));
  121.          }
  122.  
  123.          switch (SHORT1FROMMP (mp1)) {             /* Control ID */
  124.             case DID_DIRLIST:
  125.                switch (SHORT2FROMMP (mp1)) {   /* notification code */
  126.                   case LN_ENTER:
  127.                      if (szBuffer[0] == ' ')
  128.                         DosSelectDisk (szBuffer[1] - '@');
  129.                      else
  130.                              DosChDir (szBuffer, 0L);
  131.  
  132.                         FillDirListBox (hwnd, szCurrentPath);
  133.                         FillFileListBox (hwnd);
  134.  
  135.                         WinSetDlgItemText (hwnd, DID_FILEEDIT, "");
  136.                         return 0;
  137.                }
  138.                break;
  139.  
  140.             case DID_FILELIST:
  141.                switch (SHORT2FROMMP (mp1)) {   /* notification code */
  142.                   case LN_SELECT:
  143.                      WinSetDlgItemText (hwnd, DID_FILEEDIT, szBuffer);
  144.                      return 0;
  145.  
  146.                   case LN_ENTER:
  147.                      ParseFileName (szFileName, szBuffer);
  148.                      WinDismissDlg (hwnd, TRUE);
  149.                      return 0;
  150.                }
  151.                break;
  152.          }
  153.          break;
  154.  
  155.       case WM_COMMAND:
  156.          switch (COMMANDMSG(&msg)->cmd) {
  157.             case DID_OK:
  158.                WinQueryDlgItemText (hwnd, DID_FILEEDIT,
  159.                   sizeof szBuffer, szBuffer);
  160.  
  161.                switch (ParseFileName (szCurrentPath, szBuffer)) {
  162.                   case 0:
  163.                      WinAlarm (HWND_DESKTOP, WA_ERROR);
  164.                      FillDirListBox (hwnd, szCurrentPath);
  165.                      FillFileListBox (hwnd);
  166.                      return 0;
  167.  
  168.                   case 1:
  169.                      WinAlarm (HWND_DESKTOP, WA_NOTE);
  170.                      FillDirListBox (hwnd, szCurrentPath);
  171.                      FillFileListBox (hwnd);
  172.                      WinSetDlgItemText (hwnd, DID_FILEEDIT, "");
  173.                      return 0;
  174.  
  175.                   case 2:
  176.                      strcpy (szFileName, szCurrentPath);
  177.                      WinDismissDlg (hwnd, TRUE);
  178.                      return 0;
  179.                }
  180.                break;
  181.  
  182.                case DID_CANCEL:
  183.                   WinDismissDlg (hwnd, FALSE);
  184.                   return 0;
  185.          }
  186.          break;
  187.    }
  188.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  189. }
  190.  
  191.  
  192. /* window procedure for save as dialog box */
  193. /* by Charles Petzold, from Programming the OS/2 Presentation Manager */
  194. /* Modified (from OpenDlgProc) by Brian R. Anderson */
  195. MRESULT EXPENTRY SaveasDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  196. {
  197.    static CHAR szCurrentPath[80], szBuffer[80];
  198.    SHORT sSelect;
  199.  
  200.    switch (msg) {
  201.       case WM_INITDLG:
  202.          FillDirListBox (hwnd, szCurrentPath);
  203.  
  204.          WinSendDlgItemMsg (hwnd, DID_FILEEDIT, EM_SETTEXTLIMIT,
  205.             MPFROM2SHORT (70, 0), NULL);
  206.          return 0;
  207.  
  208.       case WM_CONTROL:
  209.          if (SHORT1FROMMP (mp1) == DID_DIRLIST) {
  210.             sSelect = SHORT1FROMMR (WinSendDlgItemMsg (hwnd,
  211.                                        SHORT1FROMMP (mp1),
  212.                                        LM_QUERYSELECTION, 
  213.                                        (MPARAM) 0L, (MPARAM) 0L));
  214.  
  215.             WinSendDlgItemMsg (hwnd, SHORT1FROMMP (mp1), LM_QUERYITEMTEXT,
  216.                MPFROM2SHORT (sSelect, sizeof szBuffer), MPFROMP (szBuffer));
  217.          }
  218.  
  219.          switch (SHORT1FROMMP (mp1)) {             /* Control ID */
  220.             case DID_DIRLIST:
  221.                switch (SHORT2FROMMP (mp1)) {   /* notification code */
  222.                   case LN_ENTER:
  223.                      if (szBuffer[0] == ' ')
  224.                         DosSelectDisk (szBuffer[1] - '@');
  225.                      else
  226.                              DosChDir (szBuffer, 0L);
  227.  
  228.                         FillDirListBox (hwnd, szCurrentPath);
  229.                         FillFileListBox (hwnd);
  230.  
  231.                         WinSetDlgItemText (hwnd, DID_FILEEDIT, "");
  232.                         return 0;
  233.                }
  234.                break;
  235.          }
  236.          break;
  237.  
  238.       case WM_COMMAND:
  239.          switch (COMMANDMSG(&msg)->cmd) {
  240.             case DID_OK:
  241.                WinQueryDlgItemText (hwnd, DID_FILEEDIT,
  242.                   sizeof szBuffer, szBuffer);
  243.  
  244.                switch (ParseFileName (szCurrentPath, szBuffer)) {
  245.                   case 0:
  246.                      WinAlarm (HWND_DESKTOP, WA_ERROR);
  247.                      FillDirListBox (hwnd, szCurrentPath);
  248.                      FillFileListBox (hwnd);
  249.                      return 0;
  250.  
  251.                   case 1:
  252.                      WinAlarm (HWND_DESKTOP, WA_NOTE);
  253.                      FillDirListBox (hwnd, szCurrentPath);
  254.                      WinSetDlgItemText (hwnd, DID_FILEEDIT, "");
  255.                      return 0;
  256.  
  257.                   case 2:
  258.                      strcpy (szFileName, szCurrentPath);
  259.                      WinDismissDlg (hwnd, TRUE);
  260.                      return 0;
  261.                }
  262.                break;
  263.  
  264.             case DID_CANCEL:
  265.                WinDismissDlg (hwnd, FALSE);
  266.                return 0;
  267.          }
  268.          break;
  269.    }
  270.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  271. }
  272.  
  273.  
  274. /* determine if pathname is a valid file or directory */
  275. /* by Charles Petzold, from Programming the OS/2 Presentation Manager */
  276. SHORT ParseFileName (CHAR *pcOut, CHAR *pcIn)
  277. {
  278. /*
  279.  *   Input:    pcOut -- Pointer to parsed file specification.
  280.  *             pcIn  -- Pointer to raw file specification.
  281.  *                      
  282.  *   Returns:      0 -- pcIn had invalid drive or directory.
  283.  *                 1 -- pcIn was empty or had no filename.
  284.  *                 2 -- pcOut points to drive, full dir, and file name.
  285.  *
  286.  *   Changes current drive and directory per pcIn string.
  287.  */
  288.  
  289.    CHAR *pcLastSlash, *pcFileOnly;
  290.    ULONG ulDriveMap;
  291.    USHORT usDriveNum, usDirLen = 64;
  292.  
  293.    strupr (pcIn);
  294.  
  295.    /* If input string is empty, return 1 */
  296.    if (pcIn[0] == '\0')
  297.       return 1;
  298.  
  299.    /* Get drive from input string or current drive */
  300.    if (pcIn[1] == ':') {
  301.       if (DosSelectDisk (pcIn[0] - '@'))
  302.          return 0;
  303.  
  304.       pcIn += 2;
  305.    }
  306.    DosQCurDisk (&usDriveNum, &ulDriveMap);
  307.  
  308.    *pcOut++ = (CHAR)((CHAR) usDriveNum + '@');
  309.    *pcOut++ = ':';
  310.    *pcOut++ = '\\';
  311.  
  312.    /* If rest of string is empty, return 1 */
  313.    if (pcIn[0] == '\0')
  314.       return 1;
  315.  
  316.    /* Search for last backslash.  If none, could be directory. */
  317.    if (NULL == (pcLastSlash = strrchr (pcIn, '\\'))) {
  318.        if (!DosChDir (pcIn, 0L))
  319.          return 1;
  320.  
  321.       /* Otherwise, get current dir & attach input filename */
  322.       DosQCurDir (0, pcOut, &usDirLen);
  323.  
  324.       if (strlen (pcIn) > 12)
  325.          return 0;
  326.  
  327.       if (*(pcOut + strlen (pcOut) - 1) != '\\')
  328.          strcat (pcOut++, "\\");
  329.  
  330.       strcat (pcOut, pcIn);
  331.       return 2;
  332.    }
  333.    
  334.    /* If the only backslash is at beginning, change to root */
  335.    if (pcIn == pcLastSlash) {
  336.        DosChDir ("\\", 0L);
  337.  
  338.       if (pcIn[1] == '\0')
  339.          return 1;
  340.  
  341.       strcpy (pcOut, pcIn + 1);
  342.       return 2;
  343.    }
  344.    
  345.    /* Attempt to change directory -- Get current dir if OK */
  346.    *pcLastSlash = '\0';
  347.  
  348.    if (DosChDir (pcIn, 0L))
  349.       return 0;
  350.  
  351.    DosQCurDir (0, pcOut, &usDirLen);
  352.  
  353.    /* Append input filename, if any */
  354.    pcFileOnly = pcLastSlash + 1;
  355.  
  356.    if (*pcFileOnly == '\0')
  357.       return 1;
  358.  
  359.    if (strlen (pcFileOnly) > 12)
  360.       return 0;
  361.  
  362.    if (*(pcOut + strlen (pcOut) - 1) != '\\')
  363.       strcat (pcOut++, "\\");
  364.  
  365.    strcat (pcOut, pcFileOnly);
  366.    return 2;
  367. }
  368.  
  369.  
  370. /* window procedure for find (text search) dialog box */
  371. MRESULT EXPENTRY FindDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  372. {
  373.    switch (msg) {
  374.       case WM_INITDLG:
  375.         WinSendDlgItemMsg (hwnd, DID_FINDTEXT, EM_SETTEXTLIMIT,
  376.            MPFROM2SHORT (50, 0), NULL);
  377.          WinSetDlgItemText (hwnd, DID_FINDTEXT, szFind);
  378.          WinSetFocus (HWND_DESKTOP, WinWindowFromID (hwnd, DID_FINDTEXT));
  379.          return (MRESULT) TRUE;
  380.          
  381.       case WM_COMMAND:
  382.          switch (SHORT1FROMMP (mp1)) {
  383.             case DID_OK:
  384.                if (WinQueryDlgItemText (hwnd, DID_FINDTEXT, 60, szFind))
  385.                   WinDismissDlg (hwnd, DID_OK);
  386.                else
  387.                   WinDismissDlg (hwnd, DID_CANCEL);
  388.                return 0;
  389.                
  390.             case DID_CANCEL:
  391.                WinDismissDlg (hwnd, DID_CANCEL);
  392.                return 0;
  393.                
  394.             default:
  395.                break;
  396.          }
  397.    }
  398.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  399. }
  400.  
  401.  
  402. /* window procedure for replace (text search and replace) dialog box */
  403. MRESULT EXPENTRY ReplaceDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  404. {
  405.    BOOL FAR *pb;
  406.    
  407.    switch (msg) {
  408.       case WM_INITDLG:
  409.          WinSendDlgItemMsg (hwnd, DID_NEWTEXT, EM_SETTEXTLIMIT,
  410.             MPFROM2SHORT (50, 0), NULL);
  411.          WinSendDlgItemMsg (hwnd, DID_OLDTEXT, EM_SETTEXTLIMIT,
  412.             MPFROM2SHORT (50, 0), NULL);
  413.          WinSetDlgItemText (hwnd, DID_OLDTEXT, szFind);
  414.          WinSetDlgItemText (hwnd, DID_NEWTEXT, szReplace);
  415.          pb = (PVOID) mp2;   /* TRUE if first entry */
  416.          if (*pb) {
  417.             WinEnableWindow (WinWindowFromID (hwnd, DID_DOREPLACE), FALSE);
  418.             WinEnableWindow (WinWindowFromID (hwnd, DID_REPLACEALL), FALSE);
  419.             WinSetFocus (HWND_DESKTOP, WinWindowFromID (hwnd, DID_OLDTEXT));
  420.          }
  421.          else
  422.             WinSetFocus (HWND_DESKTOP, WinWindowFromID (hwnd, DID_NEWTEXT));
  423.          return (MRESULT) TRUE;
  424.          
  425.       case WM_COMMAND:
  426.          switch (SHORT1FROMMP (mp1)) {
  427.             case DID_OK:
  428.                if (WinQueryDlgItemText (hwnd, DID_OLDTEXT, 60, szFind)) {
  429.                   WinQueryDlgItemText (hwnd, DID_NEWTEXT, 60, szReplace);
  430.                   WinDismissDlg (hwnd, DID_OK);
  431.                }
  432.                else
  433.                   WinDismissDlg (hwnd, DID_CANCEL);
  434.                return 0;
  435.                
  436.             case DID_DOREPLACE:
  437.                if (WinQueryDlgItemText (hwnd, DID_NEWTEXT, 60, szReplace))
  438.                   WinDismissDlg (hwnd, DID_DOREPLACE);
  439.                else
  440.                   WinDismissDlg (hwnd, DID_CANCEL);
  441.                break;
  442.                
  443.             case DID_REPLACEALL:
  444.                if (WinQueryDlgItemText (hwnd, DID_NEWTEXT, 60, szReplace))
  445.                   WinDismissDlg (hwnd, DID_REPLACEALL);
  446.                else
  447.                   WinDismissDlg (hwnd, DID_CANCEL);
  448.                break;
  449.                
  450.             case DID_CANCEL:
  451.                WinDismissDlg (hwnd, DID_CANCEL);
  452.                return 0;
  453.                
  454.             default:
  455.                break;
  456.          }
  457.    }
  458.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  459. }
  460.  
  461.  
  462. /* window procedure for find (text search) dialog box */
  463. MRESULT EXPENTRY GoLnDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  464. {
  465.    switch (msg) {
  466.       case WM_INITDLG:
  467.          WinSetFocus (HWND_DESKTOP, WinWindowFromID (hwnd, DID_LINENBR));
  468.          return (MRESULT) TRUE;
  469.          
  470.       case WM_COMMAND:
  471.          switch (SHORT1FROMMP (mp1)) {
  472.             case DID_OK:
  473.                if (WinQueryDlgItemText (hwnd, DID_LINENBR, 20, szLine))
  474.                   WinDismissDlg (hwnd, DID_OK);
  475.                else
  476.                   WinDismissDlg (hwnd, DID_CANCEL);
  477.                return 0;
  478.                
  479.             case DID_CANCEL:
  480.                WinDismissDlg (hwnd, DID_CANCEL);
  481.                return 0;
  482.                
  483.             default:
  484.                break;
  485.          }
  486.    }
  487.    return WinDefDlgProc (hwnd, msg, mp1, mp2);
  488. }
  489.