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