home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvpdlg.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  25KB  |  802 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvpdlg.c */
  19. /* Dialog boxes for PM GSview */
  20. #include "gvpm.h"
  21.  
  22. BOOL get_string_busy;
  23. char get_string_prompt[MAXSTR];
  24. char get_string_answer[MAXSTR];
  25. void listbox_getpath(HWND hwnd, char *path, int listbox);
  26. void listbox_directory(HWND hwnd, char *path, int listbox, int stext);
  27.  
  28. MRESULT EXPENTRY 
  29. InputDlgProc(HWND hwnd, ULONG mess, MPARAM mp1, MPARAM mp2)
  30. {
  31.     switch(mess) {
  32.     case WM_INITDLG:
  33.         WinSendMsg( WinWindowFromID(hwnd, ID_ANSWER),
  34.             EM_SETTEXTLIMIT, MPFROM2SHORT(MAXSTR, 0), MPFROMLONG(0) );
  35.         WinSetWindowText( WinWindowFromID(hwnd, ID_ANSWER),
  36.             get_string_answer );
  37.         WinSetWindowText( WinWindowFromID(hwnd, ID_PROMPT),
  38.             get_string_prompt );
  39.         WinSendMsg( WinWindowFromID(hwnd, ID_ANSWER),
  40.             EM_SETSEL, MPFROM2SHORT(0, strlen(get_string_answer)), MPFROMLONG(0) );
  41.         WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, ID_ANSWER));
  42.             break;
  43.         case WM_COMMAND:
  44.             switch(SHORT1FROMMP(mp1)) {
  45.                 case DID_OK:
  46.                     WinEnableWindow(WinWindowFromID(hwnd, DID_OK), FALSE);
  47.                     WinQueryWindowText(WinWindowFromID(hwnd, ID_ANSWER),
  48.                         MAXSTR, get_string_answer);
  49.                     WinDismissDlg(hwnd, DID_OK);
  50.                     break;
  51.         case ID_HELP:
  52.             get_help();
  53.             return (MRESULT)TRUE;
  54.             }
  55.             break;
  56.     }
  57.     return WinDefDlgProc(hwnd, mess, mp1, mp2);
  58. }
  59.  
  60. BOOL
  61. get_string(char *prompt, char *answer)
  62. {
  63.     if (get_string_busy) {
  64.         message_box("get_string is busy",0);
  65.         return FALSE;
  66.     }
  67.     get_string_busy = TRUE;
  68.     strncpy(get_string_prompt, prompt, MAXSTR);
  69.     strncpy(get_string_answer, answer, MAXSTR);
  70.     
  71.     if (WinDlgBox(HWND_DESKTOP, hwnd_frame, InputDlgProc, 0, IDD_INPUT, NULL)
  72.        == DID_OK) {
  73.         strncpy(answer, get_string_answer, MAXSTR);
  74.         get_string_busy = FALSE;
  75.         return TRUE;
  76.     }
  77.     get_string_busy = FALSE;
  78.     return FALSE;
  79. }
  80.  
  81.  
  82. BOOL 
  83. get_filename(char *filename, BOOL save, int filter, int title, int help)
  84. {
  85. FILEDLG FileDlg;
  86. FILE *f;
  87. char szTitle[MAXSTR];
  88. char *p;
  89. int i;
  90.     memset(&FileDlg, 0, sizeof(FILEDLG));
  91.     FileDlg.cbSize = sizeof(FILEDLG);
  92.     if (save)
  93.         FileDlg.fl = FDS_CENTER | FDS_SAVEAS_DIALOG;
  94.     else
  95.         FileDlg.fl = FDS_CENTER | FDS_OPEN_DIALOG;
  96.     if (help) {
  97.         load_string(help, szHelpTopic, sizeof(szHelpTopic));
  98.         FileDlg.fl |= FDS_HELPBUTTON;
  99.     }
  100.     if (title) {
  101.         load_string(title, szTitle, sizeof(szTitle));
  102.         FileDlg.pszTitle = szTitle;
  103.     }
  104.     _getcwd(FileDlg.szFullFile, sizeof(FileDlg.szFullFile));
  105.     for (p=FileDlg.szFullFile; *p; p++) {
  106.         if (*p == '/')
  107.         *p = '\\';
  108.     }
  109.     i = strlen(FileDlg.szFullFile);
  110.     if (i && FileDlg.szFullFile[i-1]!='\\') {
  111.         strcat(FileDlg.szFullFile, "\\");
  112.         i++;
  113.     }
  114.     load_string(IDS_FILTER_BASE+filter, FileDlg.szFullFile+i, sizeof(FileDlg.szFullFile)-i);
  115.     WinFileDlg(HWND_DESKTOP, hwnd_frame, &FileDlg);
  116.     if (FileDlg.lReturn == DID_OK) {
  117.         f = (FILE *)NULL;
  118.         if ( !save && ((f = fopen(FileDlg.szFullFile, "rb")) == (FILE *)NULL) ) {
  119.         gserror(IDS_FILENOTFOUND, NULL, MB_ICONEXCLAMATION, SOUND_ERROR);
  120.         return FALSE;
  121.         }
  122.         if (f)
  123.             fclose(f);
  124.         strncpy(filename, FileDlg.szFullFile, MAXSTR);
  125.         p = strrchr(FileDlg.szFullFile, '\\');
  126.         if (p) {
  127.         *p = '\0';
  128.         _chdir(FileDlg.szFullFile);
  129.         }
  130.         return TRUE;
  131.     }
  132.     return FALSE;
  133. }
  134.  
  135. #ifdef NOTUSED
  136. /* Get page number from dialog box and store in ppage */
  137. BOOL
  138. get_page(int *ppage, BOOL multiple)
  139. {
  140.     char answer[MAXSTR];
  141.     if (doc == (PSDOC *)NULL)
  142.         return FALSE;
  143.     if (doc->numpages == 0) {
  144.         gserror(IDS_NOPAGE, NULL, MB_ICONEXCLAMATION, SOUND_NONUMBER);
  145.         return FALSE;
  146.     }
  147.     load_string(IDS_TOPICOPEN, szHelpTopic, sizeof(szHelpTopic));
  148.     sprintf(answer,"%d", psfile.pagenum);
  149.     if (!get_string("Select Page",answer) || atoi(answer)==0)
  150.         return FALSE;
  151.     *ppage = atoi(answer);
  152.     return TRUE;
  153. }
  154. #endif
  155.  
  156. /* information about document dialog box */
  157. MRESULT EXPENTRY 
  158. InfoDlgProc(HWND hwnd, ULONG mess, MPARAM mp1, MPARAM mp2)
  159. {
  160.     switch(mess) {
  161.         case WM_INITDLG:
  162.         info_init(hwnd);
  163.         break;
  164.         case WM_COMMAND:
  165.             switch(SHORT1FROMMP(mp1)) {
  166.                 case DID_OK:
  167.                     WinEnableWindow(WinWindowFromID(hwnd, DID_OK), FALSE);
  168.                     WinQueryWindowText(WinWindowFromID(hwnd, ID_ANSWER),
  169.                         MAXSTR, get_string_answer);
  170.                     WinDismissDlg(hwnd, DID_OK);
  171.                     break;
  172.             }
  173.             break;
  174.     }
  175.     return WinDefDlgProc(hwnd, mess, mp1, mp2);
  176. }
  177.  
  178.  
  179. /* show info about ps file */
  180. void
  181. show_info()
  182. {
  183.     WinDlgBox(HWND_DESKTOP, hwnd_frame, InfoDlgProc, 0, IDD_INFO, NULL);
  184. }
  185.  
  186.  
  187. /* About Dialog Box */
  188. MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  189. {
  190.   switch(msg) {
  191.     case WM_INITDLG:
  192.     WinSetWindowText( WinWindowFromID(hwnd, ABOUT_VERSION),
  193.             GSVIEW_VERSION );
  194.     break;
  195.     case WM_BUTTON1DBLCLK:
  196.     {POINTL pt;
  197.     RECTL rect;
  198.     HPS hps;
  199.     HBITMAP hbm;
  200.     rect.xLeft = 8; rect.xRight = rect.xLeft+216;
  201.     rect.yBottom = 10 /* 8 */; rect.yTop = rect.yBottom + 8;
  202.     pt.x = SHORT1FROMMP(mp1);  pt.y = SHORT2FROMMP(mp1);
  203.     WinMapDlgPoints(hwnd, &pt, 1, FALSE);
  204.     if (WinPtInRect(hab, &rect, &pt)) {
  205.         hps = WinGetPS(hwnd);
  206.         hbm = GpiLoadBitmap(hps, NULLHANDLE, IDM_MISC, 64, 64);
  207.         pt.x = 250; pt.y = 12;
  208.         WinMapDlgPoints(hwnd, &pt, 1, TRUE);
  209.         WinDrawBitmap(hps, hbm, NULL, &pt, CLR_BLACK, CLR_WHITE, DBM_NORMAL);
  210.         GpiDeleteBitmap(hbm);
  211.         WinReleasePS(hps);
  212.     }
  213.     }
  214.     break;
  215.     case WM_COMMAND:
  216.       switch(SHORT1FROMMP(mp1)) {
  217.         case DID_OK:
  218.           WinDismissDlg(hwnd, TRUE);
  219.           return (MRESULT)TRUE;
  220.       }
  221.       break;
  222.   }
  223.   return WinDefDlgProc(hwnd, msg, mp1, mp2);
  224. }    
  225.  
  226. void
  227. show_about()
  228. {
  229.         WinDlgBox(HWND_DESKTOP, hwnd_frame, AboutDlgProc, 0, IDD_ABOUT, 0);
  230. }
  231.  
  232.  
  233. MRESULT EXPENTRY PageDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  234. {
  235. char buf[40];
  236. int i;
  237. int notify_message;
  238.     switch(msg) {
  239.     case  WM_INITDLG:
  240.     if (page_list.multiple)
  241.         load_string(IDS_SELECTPAGES, buf, sizeof(buf));
  242.     else
  243.         load_string(IDS_SELECTPAGE, buf, sizeof(buf));
  244.     WinSetWindowText(hwnd, buf);
  245.     for (i=0; i<doc->numpages; i++) {
  246.         WinSendMsg( WinWindowFromID(hwnd, PAGE_LIST),
  247.             LM_INSERTITEM, MPFROMLONG(LIT_END), 
  248.         MPFROMP(doc->pages[map_page(i)].label) );
  249.     }
  250.     WinSendMsg( WinWindowFromID(hwnd, PAGE_LIST),
  251.             LM_SELECTITEM, MPFROMLONG(page_list.current), MPFROMLONG(TRUE) );
  252.     if (page_list.current > 5)
  253.         WinSendMsg( WinWindowFromID(hwnd, PAGE_LIST),
  254.         LM_SETTOPINDEX, MPFROMLONG(page_list.current - 5), (MPARAM)0 );
  255.     if (!page_list.multiple) {
  256.         WinEnableWindow(WinWindowFromID(hwnd, PAGE_ALL), FALSE);
  257.         WinEnableWindow(WinWindowFromID(hwnd, PAGE_ODD), FALSE);
  258.         WinEnableWindow(WinWindowFromID(hwnd, PAGE_EVEN), FALSE);
  259.     }
  260.     break;
  261.     case WM_CONTROL:
  262.     notify_message = SHORT2FROMMP(mp1);
  263.     switch (notify_message) {
  264.         case LN_ENTER:
  265.             if (SHORT1FROMMP(mp1) == PAGE_LIST)
  266.             WinPostMsg(hwnd, WM_COMMAND, (MPARAM)DID_OK, MPFROM2SHORT(CMDSRC_OTHER, TRUE));
  267.         break;
  268.     }
  269.     break;
  270.     case  WM_COMMAND:
  271.       switch(LOUSHORT(mp1)) {
  272.         case DID_OK:
  273.         i = (int)WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_QUERYSELECTION, (MPARAM)0, (MPARAM)0);
  274.         page_list.current = (i == LIT_NONE) ? -1 : i;
  275.         for (i=0; i<doc->numpages; i++) {
  276.             page_list.select[i] = 0;
  277.         }
  278.         if (page_list.multiple) {
  279.             i = LIT_FIRST;
  280.             while ( (i = (int)WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_QUERYSELECTION, (MPARAM)i, (MPARAM)0))
  281.             != LIT_NONE )
  282.             page_list.select[i] = TRUE;
  283.         }
  284.         else
  285.         page_list.select[page_list.current] = TRUE;
  286.             WinDismissDlg(hwnd, DID_OK);
  287.             return (MRESULT)TRUE;
  288.     case PAGE_ALL:
  289.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), FALSE);
  290.         for (i=(int)WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)-1; i>=0; i--)
  291.         WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_SELECTITEM, (MPARAM)i, (MPARAM)TRUE);
  292.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), TRUE);
  293.             return (MRESULT)TRUE;
  294.     case PAGE_ODD:
  295.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), FALSE);
  296.         for (i=(int)WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)-1; i>=0; i--)
  297.         WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_SELECTITEM, (MPARAM)i, (MPARAM)!(i&1));
  298.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), TRUE);
  299.             return (MRESULT)TRUE;
  300.     case PAGE_EVEN:
  301.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), FALSE);
  302.         for (i=(int)WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_QUERYITEMCOUNT, (MPARAM)0, (MPARAM)0)-1; i>=0; i--)
  303.         WinSendMsg(WinWindowFromID(hwnd, PAGE_LIST), LM_SELECTITEM, (MPARAM)i, (MPARAM)(i&1));
  304.         WinEnableWindowUpdate(WinWindowFromID(hwnd, PAGE_LIST), TRUE);
  305.             return (MRESULT)TRUE;
  306.     case ID_HELP:
  307.         get_help();
  308.         return (MRESULT)TRUE;
  309.       }
  310.       break;
  311.     }
  312.     return WinDefDlgProc(hwnd, msg, mp1, mp2);
  313. }
  314.  
  315. /* Get page number from dialog box and store in ppage */
  316. BOOL
  317. get_page(int *ppage, BOOL multiple)
  318. {
  319. int flag;
  320.     if (doc == (PSDOC *)NULL)
  321.         return FALSE;
  322.     if (doc->numpages == 0) {
  323.         gserror(IDS_NOPAGE, NULL, MB_ICONEXCLAMATION, SOUND_NONUMBER);
  324.         return FALSE;
  325.     }
  326.     page_list.current = *ppage - 1;
  327.     page_list.multiple = multiple;
  328.     if (page_list.select == (BOOL *)NULL)
  329.         return FALSE;
  330.     memset(page_list.select, 0, doc->numpages * sizeof(BOOL) );
  331.     if (page_list.multiple)
  332.         flag = WinDlgBox(HWND_DESKTOP, hwnd_frame, PageDlgProc, 0, IDD_MULTIPAGE, NULL);
  333.     else
  334.         flag = WinDlgBox(HWND_DESKTOP, hwnd_frame, PageDlgProc, 0, IDD_PAGE, NULL);
  335.     if ((flag == DID_OK) && (page_list.current >= 0))
  336.         *ppage = page_list.current + 1;
  337.     return (flag == DID_OK);
  338. }
  339.  
  340. MRESULT EXPENTRY BoundingBoxDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  341. {
  342. static int bboxindex;
  343. float x, y;
  344. char buf[MAXSTR];
  345.     switch(msg) {
  346.     case WM_INITDLG:
  347.         bboxindex = 0;
  348.         load_string(IDS_BBPROMPT, buf, sizeof(buf));
  349.         WinSetDlgItemText(hwnd, BB_PROMPT, buf);
  350.         break;
  351.     case WM_COMMAND:
  352.             switch(SHORT1FROMMP(mp1)) {
  353.         case BB_CLICK:
  354.             if (!get_cursorpos(&x, &y)) {
  355.             WinDestroyWindow(hwnd);
  356.             hwnd_modeless = 0;
  357.             }
  358.             switch(bboxindex) {
  359.             case 0:
  360.                 bbox.llx = x;
  361.                 break;
  362.             case 1:
  363.                 bbox.lly = y;
  364.                 break;
  365.             case 2:
  366.                 bbox.urx = x;
  367.                 break;
  368.             case 3:
  369.                 bbox.ury = y;
  370.                 bbox.valid = TRUE;
  371.                 break;
  372.             }
  373.             bboxindex++;
  374.             if (bboxindex <= 3) {
  375.                 load_string(IDS_BBPROMPT+bboxindex, buf, sizeof(buf));
  376.                     WinSetDlgItemText(hwnd, BB_PROMPT, buf);
  377.             WinSetFocus(HWND_DESKTOP, hwnd_modeless);
  378.             return (MRESULT)FALSE;
  379.             }
  380.             /* all corners have been obtained so close dialog box */
  381.             WinDestroyWindow(hwnd);
  382.             hwnd_modeless = 0;
  383.             return (MRESULT)TRUE;
  384.         }
  385.     case WM_CLOSE:
  386.         WinDestroyWindow(hwnd);
  387.         hwnd_modeless = 0;
  388.         return (MRESULT)TRUE;
  389.     }
  390.     return WinDefDlgProc(hwnd, msg, mp1, mp2);
  391. }
  392.  
  393. BOOL
  394. get_bbox(void)
  395. {
  396. QMSG q_mess;        /* queue message */
  397.     bbox.valid = FALSE;
  398.     bbox.llx = bbox.lly = bbox.urx = bbox.ury = 0;
  399.     if (!display.page) {
  400.         gserror(IDS_EPSNOBBOX, NULL, MB_ICONEXCLAMATION, SOUND_ERROR);
  401.         return FALSE;
  402.     }
  403.     hwnd_modeless = WinLoadDlg(HWND_DESKTOP, hwnd_frame, BoundingBoxDlgProc, (HMODULE)0, IDD_BBOX, NULL);
  404.     WinSetWindowPos(hwnd_modeless, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER | SWP_ACTIVATE);
  405.     while (hwnd_modeless) {
  406.         /* wait for bounding box to be obtained */
  407.           if (WinGetMsg(hab, &q_mess, 0L, 0, 0))
  408.             WinDispatchMsg(hab, &q_mess);
  409.     }
  410.     return bbox.valid;
  411. }
  412.  
  413. /* sounds stuff */
  414.  
  415. #define MAX_SYSTEM_SOUND 16
  416. char *system_sounds;
  417. char *sound_entry[MAX_SYSTEM_SOUND];
  418. char szNone[32];
  419. char szSpeaker[32];
  420. int system_num;
  421. MRESULT EXPENTRY SoundDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
  422.  
  423. int
  424. load_sounds(void)
  425. {
  426. char *p;
  427. int j;
  428. HINI hini;
  429.     /* add our two sounds */
  430.     sound_entry[0] = "";
  431.     sound_entry[1] = BEEP;
  432.     load_string(IDS_NONE, szNone, sizeof(szNone));
  433.     load_string(IDS_SPKR, szSpeaker, sizeof(szSpeaker));
  434.     /* get list of system sounds */
  435.     system_sounds = malloc(PROFILE_SIZE);
  436.     memset(system_sounds, 0, PROFILE_SIZE);
  437.     if (system_sounds != (char *)NULL) {
  438.          if ( (hini = PrfOpenProfile(hab, szMMini)) != NULLHANDLE ) {
  439.             PrfQueryProfileString(hini, "MMPM2_AlarmSounds", NULL, "\0\0", system_sounds, PROFILE_SIZE-1);
  440.             PrfCloseProfile(hini);
  441.         }
  442.     }
  443.     p = system_sounds;
  444.     for (j=2; p!=(char *)NULL && j<MAX_SYSTEM_SOUND && strlen(p)!=0; j++) {
  445.         if (atoi(p) > 100)
  446.             p += strlen(p) + 1;    /* ignore <Try it> */
  447.         if (strlen(p)==0)
  448.         j--;
  449.         else {
  450.             sound_entry[j] = p;    
  451.             p += strlen(p) + 1;
  452.         }
  453.     }
  454.     system_num = j;
  455.     return system_num;
  456. }
  457.  
  458. char *
  459. get_sound_entry(int index)
  460. {
  461.     return (sound_entry[index]);
  462. }
  463.  
  464. char *
  465. get_sound_name(int index)
  466. {
  467. static char buf[64];
  468. char *p, *q;
  469. HINI hini;
  470.     if (index==0)
  471.         return szNone;
  472.     if (index==1)
  473.         return szSpeaker;
  474.     /* get human readable name for system sound */
  475.     if ( (hini = PrfOpenProfile(hab, szMMini)) == NULLHANDLE )
  476.         return "";
  477.     PrfQueryProfileString(hini, "MMPM2_AlarmSounds", sound_entry[index], "##", buf, sizeof(buf));
  478.     PrfCloseProfile(hini);
  479.         p = strchr(buf,'#');
  480.         if (p != (char *)NULL) {
  481.             q = strchr(++p,'#');
  482.         if (q != (char *)NULL) {
  483.             *q = '\0';
  484.         return p;
  485.         }
  486.     }
  487.     return "";
  488. }
  489.  
  490. int 
  491. find_sound_name(char *entry)
  492. {
  493. int i;
  494.     for (i=0; i<system_num; i++) {
  495.         if (strcmp(entry, get_sound_entry(i))==0)
  496.             return i;
  497.     }
  498.     return -1;    /* no find */
  499. }
  500.  
  501. void
  502. add_sounds(HWND hwnd)
  503. {
  504. int ifile;
  505.     for (ifile=system_num-1; ifile>=0; ifile--)
  506.         WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_INSERTITEM, MPFROMSHORT(0),
  507.              MPFROMP(get_sound_name(ifile)));
  508. }
  509.  
  510. void
  511. free_sounds(void)
  512. {
  513.     if (system_sounds != (char *)NULL)
  514.         free(system_sounds);
  515. }
  516.  
  517. void
  518. change_sounds(void)
  519. {
  520.     load_string(IDS_TOPICSOUND, szHelpTopic, sizeof(szHelpTopic));
  521.     WinDlgBox(HWND_DESKTOP, hwnd_frame, SoundDlgProc, 0, IDD_SOUND, NULL);
  522. }
  523.  
  524. MRESULT EXPENTRY SoundDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  525. {
  526.     char buf[MAXSTR];
  527.     int ievent, ifile;
  528.     static struct sound_s dsound[NUMSOUND];    /* copy of sound[] */
  529.     char *szWaveFilter = "*.wav";
  530.     static char szPath[MAXSTR];
  531.     static int file_start;
  532.     char *p;
  533.  
  534.     switch (msg) {
  535.         case WM_INITDLG:
  536.         file_start = load_sounds();
  537.         for (ievent=0; ievent<NUMSOUND; ievent++) {
  538.             strcpy(dsound[ievent].file, sound[ievent].file);
  539.             load_string(sound[ievent].title, buf, sizeof(buf));
  540.             WinSendDlgItemMsg(hwnd, SOUND_EVENT, LM_INSERTITEM, 
  541.             MPFROMLONG(LIT_END), MPFROMP(buf));
  542.         }
  543.         ievent = 0;
  544.         WinSendDlgItemMsg(hwnd, SOUND_EVENT, LM_SELECTITEM, MPFROMLONG(ievent), MPFROMLONG(TRUE));
  545.         /* force update of SOUND_FILE */
  546.         WinSendMsg(WinWindowFromID(hwnd, SOUND_EVENT), WM_CONTROL,
  547.             MPFROM2SHORT(SOUND_EVENT, LN_SELECT),  MPFROMLONG(0));
  548.         break;
  549.         case WM_CONTROL:
  550.         if (mp1 == MPFROM2SHORT(SOUND_EVENT, LN_SELECT)) {
  551.             ievent = (int)WinSendDlgItemMsg(hwnd, SOUND_EVENT, LM_QUERYSELECTION,
  552.                 MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  553.             if (ievent == LIT_NONE) {
  554.                 WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), FALSE);
  555.                 break;
  556.             }
  557.             ifile = find_sound_name(dsound[ievent].file);
  558.             if (ifile >= 0) {
  559.                 strcpy(buf, get_sound_name(ifile));
  560.                 szPath[0] = '\0';
  561.                 WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), ifile!=0);
  562.             }
  563.             else {
  564.                 /* must be WAVE file */
  565.                 strcpy(szPath, dsound[ievent].file);
  566.                 p = strrchr(szPath, '\\');
  567.                 if (p != (char *)NULL) {
  568.                     strcpy(buf,++p);
  569.                     *p = '\0';
  570.                 }
  571.                 else {
  572.                     strcpy(buf, szPath);
  573.                 }
  574.                 WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), TRUE);
  575.             }
  576.             strcat(szPath, szWaveFilter);
  577.             WinEnableWindowUpdate(WinWindowFromID(hwnd, SOUND_FILE), FALSE);
  578.             WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_DELETEALL, MPFROMLONG(0), MPFROMLONG(0));
  579.             listbox_directory(hwnd, szPath, SOUND_FILE, SOUND_PATH);
  580.             add_sounds(hwnd);
  581.             WinEnableWindowUpdate(WinWindowFromID(hwnd, SOUND_FILE), TRUE);
  582.             WinShowWindow(WinWindowFromID(hwnd, SOUND_FILE), TRUE);
  583.             ifile = (int)WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_SEARCHSTRING, 
  584.                 MPFROM2SHORT(0,LIT_FIRST), MPFROMP(buf));
  585.             if (ifile != LIT_NONE)
  586.                 WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_SELECTITEM, MPFROMLONG(ifile), MPFROMLONG(TRUE));
  587.             else
  588.                 WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_SELECTITEM, MPFROMLONG(0), MPFROMLONG(TRUE));
  589.         }
  590.         if (mp1 == MPFROM2SHORT(SOUND_FILE, LN_SELECT)) {
  591.             ifile = (int)WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_QUERYSELECTION,
  592.             MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  593.             WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_QUERYITEMTEXT, 
  594.             MPFROM2SHORT(ifile,sizeof(buf)), MPFROMP(buf));
  595.             ievent = (int)WinSendDlgItemMsg(hwnd, SOUND_EVENT, LM_QUERYSELECTION,
  596.             MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  597.             if (ifile >= file_start) {
  598.             if (buf[0] == '[') { /* selected a directory */
  599.                 WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), FALSE);
  600.             }
  601.             else { /* selected a WAVE file */
  602.                 int i = WinQueryDlgItemText(hwnd, SOUND_PATH, MAXSTR, dsound[ievent].file);
  603.                 if (dsound[ievent].file[i-1] != '\\') {
  604.                 dsound[ievent].file[i++] = '\\';
  605.                 dsound[ievent].file[i] = '\0';
  606.                 }
  607.                 listbox_getpath(hwnd, dsound[ievent].file + i, SOUND_FILE);
  608.                 WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), TRUE);
  609.             }
  610.             }
  611.             else {
  612.             WinEnableWindow(WinWindowFromID(hwnd, SOUND_TEST), ifile!=0);
  613.             strcpy(dsound[ievent].file,get_sound_entry(ifile));
  614.             }
  615.         }
  616.         if (mp1 == MPFROM2SHORT(SOUND_FILE, LN_ENTER)) {
  617.             ifile = (int)WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_QUERYSELECTION,
  618.             MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  619.             WinSendDlgItemMsg(hwnd, SOUND_FILE, LM_QUERYITEMTEXT, 
  620.             MPFROM2SHORT(ifile,sizeof(buf)), MPFROMP(buf));
  621.             if (buf[0] == '[') {
  622.                 WinQueryDlgItemText(hwnd, SOUND_PATH, sizeof(szPath), szPath);
  623.             listbox_getpath(hwnd, szPath, SOUND_FILE);
  624.             strcat(szPath, szWaveFilter);
  625.             listbox_directory(hwnd, szPath, SOUND_FILE, SOUND_PATH);
  626.             add_sounds(hwnd);
  627.             }
  628.             else {
  629.                 WinSendMsg(hwnd, WM_COMMAND, MPFROMSHORT(SOUND_TEST), MPFROMLONG(0));
  630.             }
  631.         }
  632.         break;
  633.         case WM_COMMAND:
  634.         switch (SHORT1FROMMP(mp1)) {
  635.             case ID_HELP:
  636.             get_help();
  637.                 return (MRESULT)FALSE;
  638.             case SOUND_EVENT:
  639.             return (MRESULT)FALSE;
  640.             case SOUND_FILE:
  641.             return (MRESULT)FALSE;
  642.             case SOUND_TEST:
  643.                 ievent = (int)WinSendDlgItemMsg(hwnd, SOUND_EVENT, LM_QUERYSELECTION,
  644.                 MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  645.             if (strlen(dsound[ievent].file)==0)
  646.                 return FALSE;
  647.             if (!(pfnMciPlayFile) || strcmp(dsound[ievent].file,BEEP)==0) {
  648.                 DosBeep(200,200);
  649.                 return FALSE;
  650.             }
  651.             if (isdigit(*dsound[ievent].file))
  652.                 play_system_sound(dsound[ievent].file);
  653.             else {
  654.                 buf[0] = '\042';
  655.                 strcpy(buf+1, dsound[ievent].file);
  656.                 strcat(buf, "\042");
  657.                 if ((*pfnMciPlayFile)(hwnd_frame, buf, 0, 0, 0))
  658.                     DosBeep(200,200);
  659.             }
  660.             return (MRESULT)FALSE;
  661.             case IDOK:
  662.             for (ievent=0; ievent<NUMSOUND; ievent++)
  663.                 strcpy(sound[ievent].file, dsound[ievent].file);
  664.             free_sounds();
  665.             WinDismissDlg(hwnd, DID_OK);
  666.             return (MRESULT)TRUE;
  667.             case DID_CANCEL:
  668.             free_sounds();
  669.             WinDismissDlg(hwnd, DID_CANCEL);
  670.             return (MRESULT)TRUE;
  671.         }
  672.         break;
  673.     }
  674.         return WinDefDlgProc(hwnd, msg, mp1, mp2);
  675. }
  676.  
  677. /* update path with current selection */
  678. /* returned path has a trailing \ if it is a directory */
  679. void
  680. listbox_getpath(HWND hwnd, char *path, int listbox)
  681. {
  682. int i;
  683. char buf[MAXSTR];
  684. char *p;
  685. ULONG len;
  686.     i = (int)WinSendDlgItemMsg(hwnd, listbox, LM_QUERYSELECTION,
  687.         MPFROMSHORT(LIT_FIRST), MPFROMLONG(0));
  688.     WinSendDlgItemMsg(hwnd, listbox, LM_QUERYITEMTEXT, 
  689.         MPFROM2SHORT(i,sizeof(buf)), MPFROMP(buf));
  690.     if (buf[0] == '[') {
  691.         /* directory or drive */
  692.         if (buf[1] == '-') { /* drive */
  693.         len = MAXSTR;
  694.         path[0] = toupper(buf[2]);
  695.         path[1] = ':';
  696.         path[2] = '\\';
  697.         DosQueryCurrentDir(buf[2]+1-'a', path+3, &len);
  698.         }
  699.         else {
  700.         if (strcmp(buf, "[..]")==0) {
  701.             if ((p = strrchr(path,'\\')) != (char *)NULL)
  702.                 *(++p)='\0';
  703.         }
  704.         else {
  705.                 if (path[strlen(path)-1] != '\\')
  706.                 strcat(path,"\\");
  707.             if ((p = strchr(buf, ']')) != (char *)NULL)
  708.                 *p = '\0';
  709.             strcat(path, buf+1);
  710.             strcat(path, "\\");
  711.         }
  712.         }
  713.     }
  714.     else {
  715.         if (path[strlen(path)-1] != '\\')
  716.         strcat(path,"\\");
  717.         strcat(path, buf);
  718.     }
  719. }
  720.  
  721. /* empty list box, then fill it with files and subdirectories given */
  722. /* by path and stext */
  723. /* if path doesn't contain directory, use directory in stext */
  724. /* update stext to point to directory */
  725. void
  726. listbox_directory(HWND hwnd, char *path, int listbox, int stext)
  727. {
  728. APIRET rc;
  729. FILEFINDBUF3 findbuf;
  730. HDIR hdir;
  731. ULONG cFilenames = 1;
  732. char *p;
  733. char dpath[MAXSTR];
  734. char spath[MAXSTR];
  735. int i;
  736. ULONG drivenum, drivemap;
  737.     if (strrchr(path, '\\') == (char *)NULL) {
  738.         WinQueryWindowText(WinWindowFromID(hwnd, stext), MAXSTR, dpath);
  739.         if (strlen(dpath)==0)
  740.         strcpy(dpath, szExePath);
  741.         strcpy(spath, dpath);
  742.         if (spath[strlen(spath)-1] != '\\')
  743.         strcat(spath,"\\");
  744.         strcat(spath, path);
  745.     }
  746.     else {
  747.         strcpy(spath, path);
  748.         strcpy(dpath, path);
  749.         p = strrchr(dpath,'\\');
  750.         *(++p)='\0';
  751.         if (strlen(dpath) > 3)
  752.             *(--p)='\0';
  753.     }
  754.     if ( (strlen(dpath) != 3) &&(dpath[strlen(dpath)-1] == '\\') )
  755.             dpath[strlen(dpath)-1] = '\0';
  756.     WinSetWindowText(WinWindowFromID(hwnd, stext), dpath);
  757.     strcat(dpath, (strlen(dpath) > 3) ? "\\*" : "*");
  758.  
  759.     /* stuff in filenames */
  760.     WinSendDlgItemMsg(hwnd, listbox, LM_DELETEALL, MPFROMLONG(0), MPFROMLONG(0));
  761.     hdir = HDIR_CREATE;
  762.     cFilenames = 1;
  763.     rc = DosFindFirst(spath, &hdir, FILE_NORMAL,
  764.         &findbuf, sizeof(findbuf), &cFilenames, FIL_STANDARD);
  765.     while (!rc) {
  766.         WinSendDlgItemMsg(hwnd, listbox, LM_INSERTITEM, 
  767.         MPFROMLONG(LIT_SORTASCENDING), MPFROMP(findbuf.achName));
  768.         cFilenames = 1;
  769.         rc = DosFindNext(hdir, &findbuf, sizeof(findbuf), &cFilenames);
  770.     }
  771.     DosFindClose(hdir);
  772.  
  773.     /* stuff in subdirectory names */
  774.     hdir = HDIR_CREATE;
  775.     cFilenames = 1;
  776.     rc = DosFindFirst(dpath, &hdir, MUST_HAVE_DIRECTORY,
  777.         &findbuf, sizeof(findbuf), &cFilenames, FIL_STANDARD);
  778.     while (!rc) {
  779.         strcpy(spath,"[");
  780.         strcat(spath,findbuf.achName);
  781.         strcat(spath,"]");
  782.         if (strcmp(findbuf.achName, ".") != 0)
  783.           WinSendDlgItemMsg(hwnd, listbox, LM_INSERTITEM, 
  784.         MPFROMLONG(LIT_SORTASCENDING), MPFROMP(spath));
  785.         cFilenames = 1;
  786.         rc = DosFindNext(hdir, &findbuf, sizeof(findbuf), &cFilenames);
  787.     }
  788.     DosFindClose(hdir);
  789.  
  790.     DosQueryCurrentDisk(&drivenum, &drivemap); 
  791.     /* stuff in drive list */
  792.     for (i=1; i<=26; i++) {
  793.       if (drivemap & 1) {
  794.         sprintf(spath,"[-%c-]",i-1+'a');
  795.         WinSendDlgItemMsg(hwnd, listbox, LM_INSERTITEM, 
  796.         MPFROMLONG(LIT_END), MPFROMP(spath));
  797.       }
  798.       drivemap >>= 1;
  799.     }
  800. }
  801.  
  802.