| [2] | 1 | 
 | 
|---|
| [123] | 2 | /***********************************************************************
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 |   $Id: menu.c 551 2007-02-28 01:33:51Z gyoung $
 | 
|---|
 | 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 | 
 | 
|---|
| [551] | 37 | INT tokenize(CHAR * str, INT max, CHAR ** tokens)
 | 
|---|
| [356] | 38 | {
 | 
|---|
| [551] | 39 |   INT x = 0;
 | 
|---|
| [2] | 40 |   CHAR *p;
 | 
|---|
 | 41 | 
 | 
|---|
| [551] | 42 |   if (str && max && tokens) {
 | 
|---|
| [2] | 43 |     p = str;
 | 
|---|
| [551] | 44 |     for (;;) {
 | 
|---|
 | 45 |       p = skip_delim(p, " \t");
 | 
|---|
 | 46 |       if (!p)
 | 
|---|
 | 47 |         break;
 | 
|---|
| [2] | 48 |       tokens[x++] = p;
 | 
|---|
| [551] | 49 |       if (x == max)
 | 
|---|
 | 50 |         break;
 | 
|---|
 | 51 |       p = to_delim(p, " \t");
 | 
|---|
 | 52 |       if (!p)
 | 
|---|
 | 53 |         break;
 | 
|---|
| [2] | 54 |       *p = 0;
 | 
|---|
 | 55 |       p++;
 | 
|---|
| [356] | 56 |       // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
 | 
|---|
| [551] | 57 |       if (!*p)
 | 
|---|
 | 58 |         break;
 | 
|---|
| [2] | 59 |     }
 | 
|---|
 | 60 |   }
 | 
|---|
 | 61 |   return x;
 | 
|---|
 | 62 | }
 | 
|---|
 | 63 | 
 | 
|---|
| [551] | 64 | VOID FreeMenuList(MENU * head)
 | 
|---|
| [356] | 65 | {
 | 
|---|
| [551] | 66 |   MENU *info, *next;
 | 
|---|
| [2] | 67 | 
 | 
|---|
 | 68 |   info = head;
 | 
|---|
| [551] | 69 |   while (info) {
 | 
|---|
| [2] | 70 |     next = info->next;
 | 
|---|
| [551] | 71 |     if (info->text)
 | 
|---|
| [2] | 72 |       free(info->text);
 | 
|---|
 | 73 |     free(info);
 | 
|---|
 | 74 |     info = next;
 | 
|---|
 | 75 |   }
 | 
|---|
 | 76 | }
 | 
|---|
 | 77 | 
 | 
|---|
| [551] | 78 | BOOL AddToMenu(CHAR * filename, HWND hwndMenu)
 | 
|---|
| [356] | 79 | {
 | 
|---|
| [2] | 80 |   FILE *fp;
 | 
|---|
| [551] | 81 |   CHAR s[256];
 | 
|---|
| [2] | 82 |   CHAR *tokens[3];
 | 
|---|
| [551] | 83 |   INT lines = 0;
 | 
|---|
 | 84 |   MENU *info, *last = NULL;
 | 
|---|
 | 85 |   BOOL ret = FALSE;
 | 
|---|
| [2] | 86 | 
 | 
|---|
| [356] | 87 |   // fixme to complain?
 | 
|---|
 | 88 |   if (!hwndMenu) {
 | 
|---|
 | 89 |     Runtime_Error(pszSrcFile, __LINE__, "no data");
 | 
|---|
| [2] | 90 |     return ret;
 | 
|---|
| [356] | 91 |   }
 | 
|---|
 | 92 |   if (!filename)
 | 
|---|
| [2] | 93 |     filename = "FM3MENU.DAT";
 | 
|---|
| [551] | 94 |   fp = _fsopen(filename, "r", SH_DENYWR);
 | 
|---|
| [356] | 95 |   if (!fp) {
 | 
|---|
 | 96 |     // else saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Couldn't open %s",filename);
 | 
|---|
 | 97 |   }
 | 
|---|
 | 98 |   else {
 | 
|---|
 | 99 |     while (!feof(fp)) {
 | 
|---|
| [551] | 100 |       if (!xfgets_bstripcr(s, sizeof(s), fp, pszSrcFile, __LINE__))
 | 
|---|
 | 101 |         break;
 | 
|---|
| [2] | 102 |       lines++;
 | 
|---|
| [551] | 103 |       if (!*s || *s == ';')
 | 
|---|
 | 104 |         continue;
 | 
|---|
 | 105 |       if (tokenize(s, 3, tokens) == 3 && (USHORT) atoi(tokens[1])) {
 | 
|---|
 | 106 |         info = xmallocz(sizeof(MENU), pszSrcFile, __LINE__);
 | 
|---|
 | 107 |         if (info) {
 | 
|---|
 | 108 |           info->size = sizeof(MENU);
 | 
|---|
 | 109 |           info->text = xstrdup(tokens[2], pszSrcFile, __LINE__);
 | 
|---|
 | 110 |           if (!info->text)
 | 
|---|
 | 111 |             free(info);
 | 
|---|
| [356] | 112 |           else {
 | 
|---|
| [551] | 113 |             if (!stricmp(tokens[0], "MENUITEM"))
 | 
|---|
 | 114 |               info->cmd = atoi(tokens[1]);
 | 
|---|
 | 115 |             else if (!stricmp(tokens[0], "SEPARATOR"))
 | 
|---|
 | 116 |               info->type = SEPARATOR;
 | 
|---|
 | 117 |             else {
 | 
|---|
| [356] | 118 |               /* error! */
 | 
|---|
| [551] | 119 |               free(info->text);
 | 
|---|
 | 120 |               free(info);
 | 
|---|
 | 121 |               info = NULL;
 | 
|---|
 | 122 |             }
 | 
|---|
 | 123 |             if (info) {
 | 
|---|
 | 124 |               if (!menuhead)
 | 
|---|
 | 125 |                 menuhead = info;
 | 
|---|
 | 126 |               else
 | 
|---|
 | 127 |                 last->next = info;
 | 
|---|
 | 128 |               info->next = NULL;
 | 
|---|
 | 129 |               last = info;
 | 
|---|
 | 130 |             }
 | 
|---|
 | 131 |           }
 | 
|---|
 | 132 |         }
 | 
|---|
| [2] | 133 |       }
 | 
|---|
| [356] | 134 |       else {
 | 
|---|
 | 135 |         // fixme to complain?
 | 
|---|
| [551] | 136 |         // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Tokenization failed");
 | 
|---|
| [2] | 137 |       }
 | 
|---|
 | 138 |     }
 | 
|---|
 | 139 |     fclose(fp);
 | 
|---|
 | 140 | 
 | 
|---|
| [356] | 141 |     if (menuhead) {
 | 
|---|
| [2] | 142 |       MENUITEM mi;
 | 
|---|
 | 143 | 
 | 
|---|
| [551] | 144 |       memset(&mi, 0, sizeof(mi));
 | 
|---|
| [2] | 145 |       info = menuhead;
 | 
|---|
| [551] | 146 |       WinEnableWindow(hwndMenu, FALSE);
 | 
|---|
| [356] | 147 |       while (info) {
 | 
|---|
| [551] | 148 |         mi.iPosition = MIT_END;
 | 
|---|
 | 149 |         mi.id = info->cmd;
 | 
|---|
 | 150 |         mi.afStyle =
 | 
|---|
 | 151 |           (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
 | 
|---|
 | 152 |         if (WinSendMsg
 | 
|---|
 | 153 |             (hwndMenu, MM_INSERTITEM, MPFROMP(&mi), MPFROMP(info->text)))
 | 
|---|
 | 154 |           ret = TRUE;
 | 
|---|
 | 155 |         info = info->next;
 | 
|---|
| [2] | 156 |       }
 | 
|---|
| [551] | 157 |       WinEnableWindow(hwndMenu, TRUE);
 | 
|---|
| [2] | 158 |       FreeMenuList(menuhead);
 | 
|---|
 | 159 |       menuhead = NULL;
 | 
|---|
 | 160 |     }
 | 
|---|
 | 161 |   }
 | 
|---|
 | 162 |   return ret;
 | 
|---|
 | 163 | }
 | 
|---|