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