1 /*
2  *  SYSTEM.C
3  *
4  *  Written on 10-Jul-94 by John Dennis and released to the public domain.
5  *
6  *  Code that handles system events and passes them through MnuGetMsg()
7  *  back to the caller. Hotspots their location are also handled.
8  */
9 
10 #include <stdio.h>
11 #include "winsys.h"
12 
13 HotGroup *HotSpot[MAX_HOT_GROUP];
14 int (*KeyPreProc) (int ch) = NULL;
15 int NumHots = 0;
16 EVT e;
17 
18 int window_resized = 0;        /* signals a resize ... */
19 
PushHotGroup(HotGroup * New)20 void PushHotGroup(HotGroup * New)
21 {
22     if (NumHots == MAX_HOT_GROUP)
23     {
24         return;
25     }
26     HotSpot[NumHots] = New;
27     NumHots++;
28 }
29 
PopHotGroup(void)30 void PopHotGroup(void)
31 {
32     if (NumHots == 0)
33     {
34         return;
35     }
36     NumHots--;
37 }
38 
LocateHotItem(int x,int y,unsigned long wid)39 int LocateHotItem(int x, int y, unsigned long wid)
40 {
41     int gr, it;
42 
43     for (gr = 0; gr < NumHots; gr++)
44     {
45         if (!wid || HotSpot[gr]->wid == wid)
46         {
47             for (it = 0; it < HotSpot[gr]->num; it++)
48             {
49                 if (x >= HotSpot[gr]->harr[it].x1 &&
50                   x <= HotSpot[gr]->harr[it].x2 &&
51                   y >= HotSpot[gr]->harr[it].y1 &&
52                   y <= HotSpot[gr]->harr[it].y2)
53                 {
54                     return HotSpot[gr]->harr[it].id;
55                 }
56             }
57         }
58     }
59     return 0;
60 }
61 
62 /*
63  *  Returns a key or mouse press and defines the return type in p1.
64  *  Should be used over the TTGetMsg counterpart.
65  */
66 
MnuGetMsg(EVT * event,unsigned long wid)67 unsigned int MnuGetMsg(EVT * event, unsigned long wid)
68 {
69     unsigned int ch = 0;
70     int proc = 0;
71     int id;
72 
73     if (KeyPreProc != NULL)
74     {
75         ch = KeyPreProc(0);
76         if (ch)
77         {
78             proc = 1;
79             e.msgtype = WND_WM_CHAR;
80             e.msg = ch;
81         }
82     }
83     if (ch == 0)
84     {
85         ch = TTGetMsg(&e);
86     }
87     *event = e;
88 
89     switch (e.msgtype)
90     {
91     case WND_WM_MOUSE:
92         id = LocateHotItem(e.x, e.y, wid);
93         if (id != 0)
94         {
95             event->msgtype = WND_WM_COMMAND;
96             event->id = id;     /* id of screen object */
97         }
98         break;
99 
100     case WND_WM_CHAR:
101         if (!proc)
102         {
103             if (KeyPreProc != NULL)
104             {
105                 event->msg = KeyPreProc(ch);
106             }
107         }
108         break;
109 
110     case WND_WM_RESIZE:
111         window_resized = 1;
112         break;
113 
114     default:
115         break;
116     }
117     return (unsigned int)(event->msg);
118 }
119 
RegisterKeyProc(int (* fnc)(int ch))120 void RegisterKeyProc(int (*fnc) (int ch))
121 {
122     KeyPreProc = fnc;
123 }
124