home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / win100 / winkpd.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  14KB  |  560 lines

  1. /*
  2.  * Windows Kermit
  3.  * 
  4.  * Written by William S. Hall
  5.  *          3665 Benton Street, #66
  6.  *          Santa Clara, CA 95051
  7.  *
  8.  * protocol dialog box support
  9.  */
  10.  
  11. #define NOKANJI
  12. #define NOATOM
  13. #define NOMINMAX
  14. #include <windows.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #include "winasc.h"
  19. #include "winkpr.h"
  20. #include "winkpf.h"
  21.  
  22. #define MAXPATHLEN    128
  23.  
  24. static char *defstring = "*.*";
  25.  
  26. /* local functions */
  27. static void NEAR PathSelect(HWND hDlg, LONG lParam);
  28. static void NEAR ListBoxInit(HWND hDlg);
  29. static int NEAR CheckSelect(HWND hDlg);
  30. static void NEAR ListBoxOK(HWND hDlg, LONG lParam);
  31. static char* NEAR getnamestr(char *str, int len);
  32. static void NEAR GetSendFileList(HWND hDlg, LONG lParam);
  33. static void NEAR SaveMiscParams(HWND hDlg);
  34. static void NEAR krmRemoteCommandInit(HWND hDlg, WORD idd_string1, 
  35.         WORD idd_string2, WORD idd_limit1, WORD idd_limit2, BOOL enable);
  36. static void NEAR krmActivateOK(HWND hDlg, WORD id, LONG lParam);
  37. static BOOL NEAR krmOneParamRemoteCmd(HWND hDlg, WORD id, char cmd);
  38.  
  39. /* send file dialog box */
  40. BOOL FAR PASCAL SendFileDlgProc(hDlg, message, wParam, lParam)
  41. HWND hDlg;
  42. unsigned message;
  43. WORD wParam;
  44. LONG lParam;
  45. {
  46.  
  47.     switch (message) {
  48.     case WM_INITDIALOG:
  49.         ListBoxInit(hDlg);
  50.         break;
  51.  
  52.     case WM_COMMAND:
  53.         switch (wParam) {
  54.  
  55.         case IDOK:
  56.             ListBoxOK(hDlg, lParam);
  57.             break;
  58.         
  59.         case IDCANCEL:
  60.             EndDialog(hDlg, FALSE);
  61.             break;
  62.  
  63.         case IDD_SEND:
  64.             GetSendFileList(hDlg, lParam);
  65.             break;
  66.  
  67.         case IDD_PATHSELECT:
  68.             PathSelect(hDlg, lParam);
  69.             break;
  70.  
  71.         default:
  72.             return FALSE;
  73.         }
  74.         break;
  75.  
  76.     default:
  77.         return FALSE;
  78.     }
  79.     return TRUE;
  80. }
  81.  
  82. /* support modules for send file dlg */
  83. static char* NEAR getnamestr(char *str, int len)
  84. {
  85.     char *tmp;
  86.  
  87.     tmp = str + len;
  88.  
  89.     while ((*tmp != ':') && (*tmp != '\\') && (tmp > str))
  90.     tmp--;
  91.     if (*tmp != ':' && *tmp != '\\')
  92.     return(tmp);
  93.     else
  94.     return(tmp + 1);
  95.  
  96. }
  97.  
  98. static void NEAR PathSelect(HWND hDlg, LONG lParam)
  99. {
  100.  
  101.     char buf[MAXPATHLEN];
  102.     char str[MAXPATHLEN];
  103.     int len;
  104.  
  105.     if (HIWORD(lParam) == LBN_SELCHANGE) {
  106.     len = GetDlgItemText(hDlg, IDD_EDITSELECT,(LPSTR)str,80);
  107.         DlgDirSelect(hDlg, (LPSTR)buf, IDD_PATHSELECT);
  108.     if (strchr(str,'*') || strchr(str,'?'))
  109.         strcat(buf,getnamestr(str,len));
  110.     else
  111.         strcat(buf, defstring);
  112.         SetDlgItemText(hDlg, IDD_EDITSELECT, (LPSTR)buf);
  113.     }
  114.     else if (HIWORD(lParam) == LBN_DBLCLK) {
  115.     GetDlgItemText(hDlg, IDD_EDITSELECT,(LPSTR)buf,80);
  116.     DlgDirList(hDlg, (LPSTR)buf, IDD_LISTSELECT,IDD_PATHDISPLAY, 0);
  117.     DlgDirList(hDlg, (LPSTR)buf, IDD_PATHSELECT,IDD_PATHDISPLAY, 0xC010);
  118.     SetDlgItemText(hDlg, IDD_EDITSELECT, (LPSTR)buf);
  119.     }
  120. }
  121.  
  122. static void NEAR ListBoxInit(HWND hDlg)
  123. {
  124.  
  125.     char buf[10];
  126.     strcpy(buf, defstring);
  127.  
  128.     DlgDirList(hDlg, (LPSTR)buf, IDD_LISTSELECT,IDD_PATHDISPLAY, 0);
  129.     DlgDirList(hDlg, (LPSTR)buf, IDD_PATHSELECT,IDD_PATHDISPLAY, 0xC010);
  130.     SetDlgItemText(hDlg, IDD_EDITSELECT, (LPSTR)buf);
  131.     SendDlgItemMessage(hDlg, IDD_EDITSELECT,EM_LIMITTEXT,128,0L);
  132. }
  133.  
  134. static int NEAR CheckSelect(HWND hDlg)
  135. {
  136.  
  137.     int num, i;
  138.     int count = 0;
  139.  
  140.     num = (int)SendDlgItemMessage(hDlg, IDD_LISTSELECT,LB_GETCOUNT,0,0L);
  141.  
  142.     for (i = 0; i < num; i++)
  143.         if (SendDlgItemMessage(hDlg,IDD_LISTSELECT,LB_GETSEL,(WORD)i,0L))
  144.         count += 1;
  145.  
  146.     return count;
  147. }
  148.  
  149. static void NEAR GetSendFileList(HWND hDlg, LONG lParam)
  150. {
  151.  
  152.     char buf[MAXPATHLEN];
  153.     int count, i, num;
  154.  
  155.     count = CheckSelect(hDlg);
  156.     if (count > 0) {
  157.         Kermit.hfilelist = LocalAlloc(LPTR, count * 13);
  158.     if (Kermit.hfilelist != NULL) {
  159.         Kermit.pfilelist = LocalLock(Kermit.hfilelist);
  160.         num = (int)SendDlgItemMessage(hDlg, IDD_LISTSELECT,
  161.                         LB_GETCOUNT,0,0L);
  162.         for (i = 0; i < num; i++) {
  163.             if (SendDlgItemMessage(hDlg,IDD_LISTSELECT,LB_GETSEL,(WORD)i,0L)) {
  164.                    SendDlgItemMessage(hDlg, IDD_LISTSELECT,LB_GETTEXT, 
  165.                         (WORD)i, (LONG)(LPSTR)buf);
  166.             strcat(Kermit.pfilelist, buf);
  167.             strcat(Kermit.pfilelist, " ");
  168.             }
  169.             }
  170.         }
  171.         else
  172.         count = 0;
  173.     }
  174.     if (count == 0) {
  175.     /* no files selected or local alloc error */
  176.     }
  177.     EndDialog(hDlg,count);
  178. }
  179.  
  180. static void NEAR ListBoxOK(HWND hDlg, LONG lParam)
  181. {
  182.  
  183.     char buf[MAXPATHLEN];
  184.     int validsspec, count;
  185.     OFSTRUCT myoff;
  186.     HANDLE hFile;
  187.     char oldpath[MAXPATHLEN];
  188.     char *ptr;
  189.  
  190. /* if nothing in the edit box */
  191.     if (GetDlgItemText(hDlg, IDD_EDITSELECT,(LPSTR)buf,sizeof(buf)) == 0)
  192. /* remove highlighting from file list box */
  193.         SendDlgItemMessage(hDlg,IDD_LISTSELECT,LB_SETSEL,FALSE,(LONG)(-1));
  194.     else {
  195. /* otherwise, save the path */
  196.         GetDlgItemText(hDlg, IDD_PATHDISPLAY,(LPSTR)oldpath,128);
  197. /* see if valid search spec */
  198.     SendDlgItemMessage(hDlg, IDD_LISTSELECT, WM_SETREDRAW, FALSE, 0L);
  199.     SendDlgItemMessage(hDlg, IDD_PATHSELECT, WM_SETREDRAW, FALSE, 0L);
  200.     validsspec = DlgDirList(hDlg,(LPSTR)buf,IDD_LISTSELECT,IDD_PATHDISPLAY,0);
  201.     SendDlgItemMessage(hDlg, IDD_LISTSELECT, WM_SETREDRAW, TRUE, 0L);
  202.     SendDlgItemMessage(hDlg, IDD_PATHSELECT, WM_SETREDRAW, TRUE, 0L);
  203. /* if valid, get the new path */
  204.     if (validsspec) {
  205. /* buf modified, so update it */
  206.         SetDlgItemText(hDlg, IDD_EDITSELECT, (LPSTR)buf);
  207.         GetDlgItemText(hDlg, IDD_PATHDISPLAY, (LPSTR)buf,128);
  208. /* if the same as before */
  209.         if (strcmp(oldpath, buf) == 0)
  210. /* select the whole list box */
  211.                 SendDlgItemMessage(hDlg,IDD_LISTSELECT,
  212.                    LB_SETSEL,TRUE,(LONG)(-1));
  213.         else {
  214. /* update the directory change box */
  215.                 SendDlgItemMessage(hDlg,IDD_LISTSELECT,
  216.                    LB_SETSEL,FALSE,(LONG)(-1));
  217.             DlgDirList(hDlg,(LPSTR)buf,IDD_PATHSELECT,
  218.                IDD_PATHDISPLAY, 0xC010);
  219.         }
  220. /* not a valid search spec */
  221.     } else {
  222. /* clear box */
  223.             SendDlgItemMessage(hDlg, IDD_LISTSELECT, LB_RESETCONTENT,0,0L);
  224. /* see if it exists */
  225.         hFile = OpenFile((LPSTR)buf, (OFSTRUCT FAR *)&myoff, OF_EXIST);
  226.         if (hFile != -1) {
  227. /* get the file name and add it to list box */
  228.             ptr = strrchr(myoff.szPathName,'\\') + 1;
  229.             SendDlgItemMessage(hDlg, IDD_LISTSELECT,LB_ADDSTRING,0,
  230.                             (LONG)(LPSTR)ptr);
  231. /* get the pathname */
  232.             strcpy(buf, myoff.szPathName);
  233.             count = strlen(buf) - strlen(ptr);
  234.             buf[count] = NUL;
  235. /* select it */
  236.                 SendDlgItemMessage(hDlg,IDD_LISTSELECT,LB_SETSEL,TRUE,-1L);
  237. /* update the path select box */
  238.             SetDlgItemText(hDlg, IDD_EDITSELECT, (LPSTR)ptr);
  239.             DlgDirList(hDlg, (LPSTR)buf, IDD_PATHSELECT,IDD_PATHDISPLAY, 0xC010);
  240.         }
  241.         }
  242.     }
  243.     SendDlgItemMessage(hDlg, IDD_EDITSELECT, EM_SETSEL, 0, MAKELONG(0,32767));
  244. }
  245.  
  246. /* set protocol parameters */
  247. BOOL FAR PASCAL SetMiscParams(HWND hDlg,
  248.                   unsigned message,WORD wParam,LONG lParam)
  249. {
  250.  
  251.     int i, *ptr;
  252.  
  253.     switch (message) {
  254.     case WM_INITDIALOG:
  255.         ptr = &Kermit.timer;
  256.         for (i = IDD_TIMER; i <= IDD_KRM_BELL; i++)
  257.             CheckDlgButton(hDlg,i, *ptr++);
  258.         CheckDlgButton(hDlg, IDD_KRM_SAVE,FALSE);
  259.         break;
  260.  
  261.     case WM_COMMAND:
  262.         switch(wParam) {
  263.             case IDOK:
  264.             ptr = &Kermit.timer;
  265.             for (i = IDD_TIMER; i <= IDD_KRM_BELL; i++)
  266.             *ptr++ = IsDlgButtonChecked(hDlg,i);
  267.             if (IsDlgButtonChecked(hDlg,IDD_KRM_SAVE))
  268.             SaveMiscParams(hDlg);
  269.             EndDialog(hDlg, TRUE);
  270.             break;
  271.             case IDCANCEL:
  272.             EndDialog(hDlg, FALSE);
  273.             break;
  274.         case IDD_TIMER:
  275.         case IDD_FILEWARN:
  276.         case IDD_FILEDISCARD:
  277.         case IDD_KRM_BELL:
  278.         case IDD_KRM_SAVE:
  279.             if (IsDlgButtonChecked(hDlg, wParam))
  280.                 CheckDlgButton(hDlg,wParam,FALSE);
  281.             else
  282.                 CheckDlgButton(hDlg,wParam,TRUE);
  283.             break;
  284.             default:
  285.             return FALSE;
  286.             }
  287.         break;
  288.  
  289.     default:
  290.         return FALSE;
  291.     }
  292.     return TRUE;
  293. }
  294.  
  295. /* support for set misc dlg */
  296. static void NEAR SaveMiscParams(HWND hDlg)
  297. {
  298.  
  299.     int i, *ptr;
  300.     char szAppStr[30];
  301.     char szKeyStr[30];
  302.     char szValStr[20];
  303.     HANDLE hInstance;
  304.  
  305.     hInstance = (HANDLE)GetWindowWord(hDlg, GWW_HINSTANCE);
  306.  
  307.     LoadString(hInstance,IDS_KRM_KERMIT, (LPSTR)szAppStr, sizeof(szAppStr));
  308.     ptr = &Kermit.timer;
  309.  
  310.     for (i = 0; i < 4; i++) {
  311.     LoadString(hInstance,IDS_KRM_TIMER+i,(LPSTR)szKeyStr, sizeof(szKeyStr));
  312.         itoa(*ptr++, szValStr, sizeof(szValStr));
  313.     WriteProfileString((LPSTR)szAppStr,(LPSTR)szKeyStr,(LPSTR)szValStr);
  314.     }
  315.     BroadcastWinIniChange();
  316. }
  317.  
  318. /* generic one-edit box dialog box */
  319. BOOL FAR PASCAL GetFileList(HWND hDlg,unsigned message,WORD wParam,LONG lParam)
  320. {
  321.  
  322.     int count;
  323.  
  324.     switch (message) {
  325.     case WM_INITDIALOG:
  326.         krmRemoteCommandInit(hDlg,Kermit.ids_title,0,IDD_GETNAME,0,
  327.                             Kermit.nullstrOK);
  328.         break;
  329.  
  330.     case WM_COMMAND:
  331.         switch(wParam) {
  332.             case IDOK:
  333.             count = (int)SendDlgItemMessage(hDlg,IDD_GETNAME,
  334.                     WM_GETTEXTLENGTH,0,0L);
  335.             Kermit.hfilelist = LocalAlloc(LPTR, count+1);
  336.             if (Kermit.hfilelist != NULL) {
  337.                 Kermit.pfilelist = LocalLock(Kermit.hfilelist);
  338.                 count = GetDlgItemText(hDlg,IDD_GETNAME,
  339.                     (LPSTR)Kermit.pfilelist,count + 1);
  340.             if (Kermit.remotecmd == 'R')
  341.                     AnsiUpper((LPSTR)Kermit.pfilelist);
  342.             }
  343.             else
  344.                 count = 0;
  345.             EndDialog(hDlg, count);
  346.             break;
  347.  
  348.             case IDCANCEL:
  349.             EndDialog(hDlg, FALSE);
  350.             break;
  351.  
  352.         case IDD_GETNAME:
  353.             if (!Kermit.nullstrOK)
  354.                 krmActivateOK(hDlg, wParam, lParam);
  355.             break;
  356.  
  357.             default:
  358.             return FALSE;
  359.             }
  360.         break;
  361.  
  362.     default:
  363.         return FALSE;
  364.     }
  365.     return TRUE;
  366. }
  367.  
  368. /* chdir, or two-edit box dialog box */
  369. BOOL FAR PASCAL krmRemoteChdir(HWND hDlg,
  370.                    unsigned message,WORD wParam,LONG lParam)
  371. {
  372.  
  373.     int count, count1;
  374.     static HBRUSH hbr = 0;
  375.  
  376.     switch (message) {
  377.  
  378.     case WM_CTLCOLOR:
  379.             if (LOWORD(lParam) == GetDlgItem(hDlg,IDD_GETTEXT2)) {
  380.         SetBkColor(wParam, GetSysColor(COLOR_WINDOWTEXT));
  381.         SetTextColor(wParam, GetSysColor(COLOR_WINDOWTEXT));
  382.         if (hbr)
  383.                 return (int)hbr;
  384.         else
  385.             return((int)GetStockObject(WHITE_BRUSH));
  386.         }
  387.         return FALSE;
  388.  
  389.     case WM_INITDIALOG:
  390.         krmRemoteCommandInit(hDlg,IDS_KRM_CWD,IDS_KRM_PASSWORD,
  391.                     IDD_GETTEXT1,IDD_GETTEXT2,TRUE); 
  392.         hbr = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  393.         break;
  394.  
  395.     case WM_COMMAND:
  396.         switch(wParam) {
  397.             case IDOK:
  398.             count = (int)SendDlgItemMessage(hDlg,IDD_GETTEXT1,
  399.                     WM_GETTEXTLENGTH,0,0L);
  400.             count1 = (int)SendDlgItemMessage(hDlg,IDD_GETTEXT2,
  401.                     WM_GETTEXTLENGTH,0,0L);
  402.             Kermit.hRemoteCommand = LocalAlloc(LPTR,count + count1 + 4);
  403.             if (Kermit.hRemoteCommand != NULL) {
  404.                 Kermit.pRemoteCommand = LocalLock(Kermit.hRemoteCommand);
  405.                *Kermit.pRemoteCommand = 'C';
  406.             *(Kermit.pRemoteCommand+1) = (BYTE)tochar(count);
  407.              if (count) {
  408.                     GetDlgItemText(hDlg,IDD_GETTEXT1,
  409.                     (LPSTR)(Kermit.pRemoteCommand+2), count+1);
  410.                 if (count1) {                
  411.                 *(Kermit.pRemoteCommand + count + 2) = 
  412.                             (BYTE)tochar(count1);            
  413.                     GetDlgItemText(hDlg,IDD_GETTEXT2,
  414.                     (LPSTR)(Kermit.pRemoteCommand+count+3), 
  415.                         count1 + 1);
  416.                 }
  417.             }
  418.             EndDialog(hDlg, TRUE);
  419.             }
  420.             else
  421.                 EndDialog(hDlg, FALSE);
  422.             if (hbr)
  423.                   DeleteObject(hbr);
  424.             break;
  425.  
  426.             case IDCANCEL:
  427.             EndDialog(hDlg, FALSE);
  428.               if (hbr)
  429.                 DeleteObject(hbr);
  430.             break;
  431.  
  432.             default:
  433.             return FALSE;
  434.             }
  435.         break;
  436.  
  437.     default:
  438.         return FALSE;
  439.     }
  440.     return TRUE;
  441. }
  442.  
  443. /* show transfer data */
  444. BOOL FAR PASCAL krmXferDlgBox(HWND hDlg,
  445.                   unsigned message,WORD wParam,LONG lParam)
  446. {
  447.  
  448.     char buf[40];
  449.     HANDLE hInst;
  450.  
  451.     switch (message) {
  452.  
  453.     case WM_INITDIALOG:
  454.         if (krmState == IDM_KRM_SEND) {
  455.         hInst = (HANDLE)GetWindowWord(hDlg, GWW_HINSTANCE);
  456.                 LoadString(hInst,IDS_KRM_SENDING, (LPSTR)buf, sizeof(buf));
  457.         SetDlgItemText(hDlg, IDD_OPERATION, (LPSTR)buf);
  458.         }
  459.         break;
  460.  
  461.     default:
  462.         return FALSE;
  463.     }
  464.     return TRUE;
  465. }
  466.  
  467. /* another 1-edit box dialog box */
  468. BOOL FAR PASCAL krmRemoteCmdDlgBox(HWND hDlg,
  469.                    unsigned message,WORD wParam,LONG lParam)
  470. {
  471.  
  472.     switch (message) {
  473.  
  474.     case WM_INITDIALOG:
  475.         krmRemoteCommandInit(hDlg,Kermit.ids_title,
  476.                  0,IDD_GETNAME,0,Kermit.nullstrOK); 
  477.         break;
  478.  
  479.     case WM_COMMAND:
  480.         switch(wParam) {
  481.             case IDOK:
  482.             EndDialog(hDlg,krmOneParamRemoteCmd(hDlg,
  483.                   IDD_GETNAME,Kermit.remotecmd));
  484.             break;
  485.  
  486.             case IDCANCEL:
  487.             EndDialog(hDlg, FALSE);
  488.             break;
  489.  
  490.         case IDD_GETNAME:
  491.             if (!Kermit.nullstrOK)
  492.                 krmActivateOK(hDlg, wParam, lParam);
  493.             break;
  494.  
  495.             default:
  496.             return FALSE;
  497.             }
  498.         break;
  499.  
  500.     default:
  501.         return FALSE;
  502.     }
  503.     return TRUE;
  504. }
  505.  
  506. /* support functions */
  507. static void NEAR krmRemoteCommandInit(HWND hDlg, WORD idd_string1, 
  508.         WORD idd_string2, WORD idd_limit1, WORD idd_limit2, BOOL enable)
  509. {
  510.     char buf[80];
  511.     HANDLE hInst;
  512.  
  513.     hInst = (HANDLE)GetWindowWord(hDlg, GWW_HINSTANCE);
  514.  
  515.     if (idd_string1) {
  516.         LoadString(hInst,idd_string1, (LPSTR)buf, sizeof(buf));
  517.     SetDlgItemText(hDlg, IDD_STATICTEXT1, (LPSTR)buf);
  518.     }
  519.     if (idd_string2) {
  520.         LoadString(hInst,idd_string2, (LPSTR)buf, sizeof(buf));
  521.     SetDlgItemText(hDlg, IDD_STATICTEXT2, (LPSTR)buf);
  522.     }
  523.  
  524.     if (idd_limit1)
  525.         SendDlgItemMessage(hDlg,idd_limit1,EM_LIMITTEXT,KRM_MAXPACKETSIZE,0L);
  526.     if (idd_limit2)
  527.         SendDlgItemMessage(hDlg,idd_limit2,EM_LIMITTEXT,KRM_MAXPACKETSIZE,0L);
  528.  
  529.     EnableWindow(GetDlgItem(hDlg, IDOK), enable);
  530. }
  531.  
  532. static void NEAR krmActivateOK(HWND hDlg, WORD id, LONG lParam)
  533. {
  534.  
  535.     int count;
  536.  
  537.     if (HIWORD(lParam) == EN_CHANGE) {
  538.         count = (int)SendDlgItemMessage(hDlg,id, WM_GETTEXTLENGTH,0,0L);
  539.     EnableWindow(GetDlgItem(hDlg, IDOK), count);
  540.     }
  541. }
  542.  
  543. static BOOL NEAR krmOneParamRemoteCmd(HWND hDlg, WORD id, char cmd)
  544. {
  545.     int count;
  546.  
  547.     count = (int)SendDlgItemMessage(hDlg,id,WM_GETTEXTLENGTH,0,0L);
  548.     Kermit.hRemoteCommand = LocalAlloc(LPTR,count + 3);
  549.     if (Kermit.hRemoteCommand != NULL) {
  550.         Kermit.pRemoteCommand = LocalLock(Kermit.hRemoteCommand);
  551.        *Kermit.pRemoteCommand = cmd;
  552.     *(Kermit.pRemoteCommand+1) = (BYTE)tochar(count);
  553.      if (count)
  554.             GetDlgItemText(hDlg,id,(LPSTR)(Kermit.pRemoteCommand+2), count+1);
  555.     return TRUE;
  556.     }
  557.     else
  558.     return FALSE;
  559. }
  560.