[2] | 1 |
|
---|
[123] | 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: menu.c 1009 2008-05-10 07:51:58Z 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
|
---|
[985] | 15 | 29 Feb 08 GKY Use xfree where appropriate
|
---|
[123] | 16 |
|
---|
| 17 | ***********************************************************************/
|
---|
| 18 |
|
---|
[2] | 19 | #include <stdlib.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 | #include <share.h>
|
---|
[356] | 22 |
|
---|
[907] | 23 | #define INCL_WIN
|
---|
| 24 | #define INCL_LONGLONG // dircnrs.h
|
---|
| 25 |
|
---|
| 26 | #include "menu.h"
|
---|
| 27 | #include "errutil.h" // Dos_Error...
|
---|
[2] | 28 | #include "fm3dll.h"
|
---|
| 29 |
|
---|
| 30 | #pragma data_seg(DATA2)
|
---|
[356] | 31 |
|
---|
| 32 | static PSZ pszSrcFile = __FILE__;
|
---|
| 33 |
|
---|
[2] | 34 | MENU *menuhead = NULL;
|
---|
| 35 |
|
---|
[551] | 36 | INT tokenize(CHAR * str, INT max, CHAR ** tokens)
|
---|
[356] | 37 | {
|
---|
[551] | 38 | INT x = 0;
|
---|
[2] | 39 | CHAR *p;
|
---|
| 40 |
|
---|
[551] | 41 | if (str && max && tokens) {
|
---|
[2] | 42 | p = str;
|
---|
[551] | 43 | for (;;) {
|
---|
| 44 | p = skip_delim(p, " \t");
|
---|
| 45 | if (!p)
|
---|
| 46 | break;
|
---|
[2] | 47 | tokens[x++] = p;
|
---|
[551] | 48 | if (x == max)
|
---|
| 49 | break;
|
---|
| 50 | p = to_delim(p, " \t");
|
---|
| 51 | if (!p)
|
---|
| 52 | break;
|
---|
[2] | 53 | *p = 0;
|
---|
| 54 | p++;
|
---|
[356] | 55 | // saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"\"%s\"\r\r%d",tokens[x - 1],x);
|
---|
[551] | 56 | if (!*p)
|
---|
| 57 | break;
|
---|
[2] | 58 | }
|
---|
| 59 | }
|
---|
| 60 | return x;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[551] | 63 | VOID FreeMenuList(MENU * head)
|
---|
[356] | 64 | {
|
---|
[551] | 65 | MENU *info, *next;
|
---|
[2] | 66 |
|
---|
| 67 | info = head;
|
---|
[551] | 68 | while (info) {
|
---|
[2] | 69 | next = info->next;
|
---|
[1009] | 70 | xfree(info->text, pszSrcFile, __LINE__);
|
---|
| 71 | xfree(info, pszSrcFile, __LINE__);
|
---|
[2] | 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)
|
---|
[1009] | 109 | xfree(info, pszSrcFile, __LINE__);
|
---|
[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! */
|
---|
[1009] | 117 | xfree(info->text, pszSrcFile, __LINE__);
|
---|
| 118 | xfree(info, pszSrcFile, __LINE__);
|
---|
[551] | 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)
|
---|