| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: menu.c 356 2006-07-26 19:58:37Z root $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Custom menu support routines for FM/2
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   Copyright (c) 1996-98 M. Kimes
 | 
|---|
| 9 |   Copyright (c) 2004, 2006 Steven H.Levine
 | 
|---|
| 10 | 
 | 
|---|
| 11 |   01 Aug 04 SHL Rework lstrip/rstrip usage
 | 
|---|
| 12 |   22 Jul 06 SHL Check more run time errors
 | 
|---|
| 13 | 
 | 
|---|
| 14 | ***********************************************************************/
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #define INCL_DOS
 | 
|---|
| 17 | #define INCL_WIN
 | 
|---|
| 18 | #include <os2.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include <stdio.h>
 | 
|---|
| 21 | #include <stdlib.h>
 | 
|---|
| 22 | #include <string.h>
 | 
|---|
| 23 | #include <share.h>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "fm3dll.h"
 | 
|---|
| 26 | #include "menu.h"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #pragma data_seg(DATA2)
 | 
|---|
| 29 | 
 | 
|---|
| 30 | static PSZ pszSrcFile = __FILE__;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
 | 
|---|
| 33 | 
 | 
|---|
| 34 | MENU *menuhead = NULL;
 | 
|---|
| 35 | 
 | 
|---|
| 36 | INT tokenize (CHAR *str,INT max,CHAR **tokens)
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   INT   x = 0;
 | 
|---|
| 39 |   CHAR *p;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   if(str && max && tokens) {
 | 
|---|
| 42 |     p = str;
 | 
|---|
| 43 |     for(;;) {
 | 
|---|
| 44 |       p = skip_delim(p," \t");
 | 
|---|
| 45 |       if(!p)
 | 
|---|
| 46 |         break;
 | 
|---|
| 47 |       tokens[x++] = p;
 | 
|---|
| 48 |       if(x == max)
 | 
|---|
| 49 |         break;
 | 
|---|
| 50 |       p = to_delim(p," \t");
 | 
|---|
| 51 |       if(!p)
 | 
|---|
| 52 |         break;
 | 
|---|
| 53 |       *p = 0;
 | 
|---|
| 54 |       p++;
 | 
|---|
| 55 |       // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
 | 
|---|
| 56 |       if(!*p)
 | 
|---|
| 57 |         break;
 | 
|---|
| 58 |     }
 | 
|---|
| 59 |   }
 | 
|---|
| 60 |   return x;
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | 
 | 
|---|
| 64 | VOID FreeMenuList (MENU *head)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   MENU *info,*next;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   info = head;
 | 
|---|
| 69 |   while(info) {
 | 
|---|
| 70 |     next = info->next;
 | 
|---|
| 71 |     if(info->text)
 | 
|---|
| 72 |       free(info->text);
 | 
|---|
| 73 |     free(info);
 | 
|---|
| 74 |     info = next;
 | 
|---|
| 75 |   }
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | 
 | 
|---|
| 79 | BOOL AddToMenu (CHAR *filename,HWND hwndMenu)
 | 
|---|
| 80 | {
 | 
|---|
| 81 |   FILE *fp;
 | 
|---|
| 82 |   CHAR  s[256];
 | 
|---|
| 83 |   CHAR *tokens[3];
 | 
|---|
| 84 |   INT   lines = 0;
 | 
|---|
| 85 |   MENU *info,*last = NULL;
 | 
|---|
| 86 |   BOOL  ret = FALSE;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // fixme to complain?
 | 
|---|
| 89 |   if (!hwndMenu) {
 | 
|---|
| 90 |     Runtime_Error(pszSrcFile, __LINE__, "no data");
 | 
|---|
| 91 |     return ret;
 | 
|---|
| 92 |   }
 | 
|---|
| 93 |   if (!filename)
 | 
|---|
| 94 |     filename = "FM3MENU.DAT";
 | 
|---|
| 95 |   fp = _fsopen(filename,"r",SH_DENYWR);
 | 
|---|
| 96 |   if (!fp) {
 | 
|---|
| 97 |     // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 |   else {
 | 
|---|
| 100 |     while (!feof(fp)) {
 | 
|---|
| 101 |       if (!fgets(s,256,fp))
 | 
|---|
| 102 |         break;
 | 
|---|
| 103 |       lines++;
 | 
|---|
| 104 |       bstripcr(s);
 | 
|---|
| 105 |       if(!*s || *s == ';')
 | 
|---|
| 106 |         continue;
 | 
|---|
| 107 |       if (tokenize(s,3,tokens) == 3 && (USHORT)atoi(tokens[1])) {
 | 
|---|
| 108 |         info = xmallocz(sizeof(MENU),pszSrcFile,__LINE__);
 | 
|---|
| 109 |         if (info) {
 | 
|---|
| 110 |           info->size = sizeof(MENU);
 | 
|---|
| 111 |           info->text = xstrdup(tokens[2],pszSrcFile,__LINE__);
 | 
|---|
| 112 |           if (!info->text)
 | 
|---|
| 113 |             free(info);
 | 
|---|
| 114 |           else {
 | 
|---|
| 115 |             if (!stricmp(tokens[0],"MENUITEM"))
 | 
|---|
| 116 |               info->cmd = atoi(tokens[1]);
 | 
|---|
| 117 |             else if (!stricmp(tokens[0],"SEPARATOR"))
 | 
|---|
| 118 |               info->type = SEPARATOR;
 | 
|---|
| 119 |             else {
 | 
|---|
| 120 |               /* error! */
 | 
|---|
| 121 |               free(info->text);
 | 
|---|
| 122 |               free(info);
 | 
|---|
| 123 |               info = NULL;
 | 
|---|
| 124 |             }
 | 
|---|
| 125 |             if (info) {
 | 
|---|
| 126 |               if(!menuhead)
 | 
|---|
| 127 |                 menuhead = info;
 | 
|---|
| 128 |               else
 | 
|---|
| 129 |                 last->next = info;
 | 
|---|
| 130 |               info->next = NULL;
 | 
|---|
| 131 |               last = info;
 | 
|---|
| 132 |             }
 | 
|---|
| 133 |           }
 | 
|---|
| 134 |         }
 | 
|---|
| 135 |       }
 | 
|---|
| 136 |       else {
 | 
|---|
| 137 |         // fixme to complain?
 | 
|---|
| 138 |         // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
 | 
|---|
| 139 |       }
 | 
|---|
| 140 |     }
 | 
|---|
| 141 |     fclose(fp);
 | 
|---|
| 142 | 
 | 
|---|
| 143 |     if (menuhead) {
 | 
|---|
| 144 |       MENUITEM mi;
 | 
|---|
| 145 | 
 | 
|---|
| 146 |       memset(&mi,0,sizeof(mi));
 | 
|---|
| 147 |       info = menuhead;
 | 
|---|
| 148 |       WinEnableWindow(hwndMenu,FALSE);
 | 
|---|
| 149 |       while (info) {
 | 
|---|
| 150 |         mi.iPosition = MIT_END;
 | 
|---|
| 151 |         mi.id = info->cmd;
 | 
|---|
| 152 |         mi.afStyle = (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
 | 
|---|
| 153 |         if (WinSendMsg(hwndMenu, MM_INSERTITEM, MPFROMP(&mi),
 | 
|---|
| 154 |                        MPFROMP(info->text)))
 | 
|---|
| 155 |           ret = TRUE;
 | 
|---|
| 156 |         info = info->next;
 | 
|---|
| 157 |       }
 | 
|---|
| 158 |       WinEnableWindow(hwndMenu,TRUE);
 | 
|---|
| 159 |       FreeMenuList(menuhead);
 | 
|---|
| 160 |       menuhead = NULL;
 | 
|---|
| 161 |     }
 | 
|---|
| 162 |   }
 | 
|---|
| 163 |   return ret;
 | 
|---|
| 164 | }
 | 
|---|