event 8.1 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/************************************************************************/
/*  Acorn Computers Ltd, 1992.                                         */
/*                                                                      */
/* This file forms part of an unsupported source release of RISC_OSLib. */
/*                                                                      */
/* It may be freely used to create executable images for saleable       */
/* products but cannot be sold in source form or as an object library   */
/* without the prior written consent of Acorn Computers Ltd.            */
/*                                                                      */
/* If this file is re-distributed (even if modified) it should retain   */
/* this copyright notice.                                               */
/*                                                                      */
/************************************************************************/

/* Title: c.event
Robert Sprowson's avatar
Robert Sprowson committed
30
 * Purpose: central processing for window system events.
31 32
 * History: IDJ: 06-Feb-92: prepared for source release
 *
Neil Turton's avatar
Neil Turton committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 */

#define BOOL int
#define TRUE 1
#define FALSE 0

#include <stdlib.h>

#include "trace.h"
#include "os.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "menu.h"
#include "alarm.h"

#include "event.h"


/* --------- masking of events ---------- */

static wimp_emask event__mask = wimp_EMNULL;

void event_setmask (wimp_emask mask)
{
  event__mask = mask;
}


wimp_emask event_getmask (void)
{
  return (event__mask);
}


/* -------- attaching menus. -------- */

/* An event_w is in fact a wimp_w. */

typedef struct {
  menu m;
  event_menu_maker maker;
  event_menu_proc event;
  void *handle;
} mstr;

static BOOL event__attach(event_w w,
80 81
                          menu m, event_menu_maker menumaker,
                          event_menu_proc eventproc, void *handle)
Neil Turton's avatar
Neil Turton committed
82 83
{
  mstr *p = win_getmenuh(w);
84 85 86 87 88
  if (m == 0 && menumaker == 0)
  {
    /* cancelling */
    if (p != 0)
    { /* something to cancel */
Neil Turton's avatar
Neil Turton committed
89 90
      free(p);
      win_setmenuh(w, 0);
91 92 93 94
    }
  }
  else
  {
Neil Turton's avatar
Neil Turton committed
95 96 97 98 99 100 101
    if (p == 0) p = malloc(sizeof(mstr));
    if (p == 0) return FALSE;
    p->m = m;
    p->maker = menumaker;
    p->event = eventproc;
    p->handle = handle;
    win_setmenuh(w, p);
102
  }
Neil Turton's avatar
Neil Turton committed
103 104 105
  return TRUE;
}

106
BOOL event_attachmenu(event_w w, menu m, event_menu_proc eventproc, void* handle)
Neil Turton's avatar
Neil Turton committed
107 108 109 110
{
  return event__attach(w, m, 0, eventproc, handle);
}

111
BOOL event_attachmenumaker(event_w w, event_menu_maker menumaker, event_menu_proc eventproc, void *handle)
Neil Turton's avatar
Neil Turton committed
112 113 114 115 116 117 118 119
{
  return event__attach(w, 0, menumaker, eventproc, handle);
}

/* -------- Processing Events. -------- */

static wimp_w event__current_menu_window = 0;
  /* 0 if no menu currently visible */
120
static menu event__current_menu = NULL;
Neil Turton's avatar
Neil Turton committed
121 122 123 124
static BOOL event__current_destroy_after; /* set if we made the menu */
static int event__menux = 0;
static int event__menuy = 0;
static BOOL event__last_was_menu_hit = FALSE;
125
static BOOL event__recreation;
Neil Turton's avatar
Neil Turton committed
126 127 128 129 130

static BOOL event__process(wimp_eventstr *e)
{
  /* Look for submenu requests, and if found turn them into menu hits. */
  /* People wishing to respond can pick up the original from wimpt. */
131 132
  if (e->e == wimp_ESEND && e->data.msg.hdr.action == wimp_MMENUWARN)
  {
Neil Turton's avatar
Neil Turton committed
133 134 135 136 137 138
    int i;
    e->e = wimp_EMENU;
    i = 0;
    while ((e->data.menu[i] = e->data.msg.data.words[i+3]) != -1) i++;
    e->data.menu[i++] = 0;
    e->data.menu[i++] = -1;
139
  }
Neil Turton's avatar
Neil Turton committed
140 141

  /* Look for menu events */
142 143
  if (e->e == wimp_EBUT && (wimp_BMID & e->data.but.m.bbits) != 0)
  {
Neil Turton's avatar
Neil Turton committed
144 145 146 147
    /* set up a menu! */
    mstr *p;
    if (e->data.but.m.w <= -1) e->data.but.m.w = win_ICONBAR;
    p = win_getmenuh(e->data.but.m.w);
148 149
    if (p != 0)
    {
Neil Turton's avatar
Neil Turton committed
150 151 152 153
      wimp_menuhdr *m;
      event__current_menu_window = e->data.but.m.w;
      event__current_menu = p->m;
      event__current_destroy_after = FALSE;
154 155
      if (p->m != 0)
      {
Neil Turton's avatar
Neil Turton committed
156
        m = (wimp_menuhdr*) menu_syshandle(p->m);
157 158 159
      }
      else if (p->maker != 0)
      {
Neil Turton's avatar
Neil Turton committed
160 161 162 163
        event__current_destroy_after = FALSE;
        event__recreation = 0 ;
        event__current_menu = p->maker(p->handle);
        m = (wimp_menuhdr*) menu_syshandle(event__current_menu);
164 165 166
      }
      else
      {
Ben Avison's avatar
Ben Avison committed
167
        m = (wimp_menuhdr *) -1;
168 169 170
      }
      if (event__current_menu_window == win_ICONBAR)
      {
Neil Turton's avatar
Neil Turton committed
171 172 173 174 175 176 177 178 179
        /* move icon bar menus up a bit. */
        e->data.but.m.x -= 16 /* m->width/2 */;
        e->data.but.m.y = 96;
        {
          wimp_menuitem *mi = (wimp_menuitem*) (m + 1);
          while (1) {
            e->data.but.m.y += m->height + m->gap;
            if ((mi->flags & wimp_MLAST) != 0) break;
            mi++;
180 181 182
          }
        }
      }
Neil Turton's avatar
Neil Turton committed
183 184
      event__menux = e->data.but.m.x;
      event__menuy = e->data.but.m.y;
Robert Sprowson's avatar
Robert Sprowson committed
185
      wimpt_complain(wimp_create_menu((wimp_menustr*) m, e->data.but.m.x - 64, e->data.but.m.y));
Neil Turton's avatar
Neil Turton committed
186
      return FALSE;
187 188
    }
  }
189
  else if (e->e == wimp_EMENU && event__current_menu != NULL)
190
  {
Neil Turton's avatar
Neil Turton committed
191 192
    mstr *p;
    p = win_getmenuh(event__current_menu_window);
193 194
    if (p == 0)
    {
Neil Turton's avatar
Neil Turton committed
195 196
      /* if the menu registration has been cancelled,
      we hand the event on. */
197 198 199
    }
    else
    {
Neil Turton's avatar
Neil Turton committed
200 201
      int i;
      char a[20];
202
      for (i = 0; e->data.menu[i] != -1; i++) {a[i] = 1 + e->data.menu[i];}
Neil Turton's avatar
Neil Turton committed
203 204 205 206
      a[i] = 0;
      p->event(p->handle, a);
      event__last_was_menu_hit = TRUE; /* menu has still to be deleted. */
      return FALSE;
207 208
    }
  }
Neil Turton's avatar
Neil Turton committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

  if (e->e == wimp_ENULL)
  {
    int dummy_time;
    BOOL pending_alarm = alarm_next(&dummy_time);
    tracef0("Got a null\n");
    if (pending_alarm != 0 && dummy_time <= alarm_timenow())
    {
      tracef1("Calling alarm at %d\n", alarm_timenow());
      alarm_callnext();
    }

    if ((event_getmask() & wimp_EMNULL) != 0)
       return TRUE;
  }

  /* now process the event */
226 227
  if (win_processevent(e))
  {
Neil Turton's avatar
Neil Turton committed
228
    /* all is well, it was claimed */
229 230 231
  }
  else if (e->e == wimp_ENULL)
  {
Neil Turton's avatar
Neil Turton committed
232 233
    /* machine idle: say so */
    return TRUE;
234 235 236
  }
  else if (e->e == wimp_EOPEN)
  {
Neil Turton's avatar
Neil Turton committed
237 238
    /* Assume it's a menu being moved */
    wimpt_complain(wimp_open_wind(&e->data.o));
239 240
  }

Neil Turton's avatar
Neil Turton committed
241 242 243 244 245
  return FALSE;
}

static void event__poll(wimp_emask mask, wimp_eventstr *result)
{
246 247
  if (event__last_was_menu_hit && wimpt_last_event()->e == wimp_EMENU)
  {
Neil Turton's avatar
Neil Turton committed
248 249 250
    wimp_mousestr m;
    event__last_was_menu_hit = FALSE;
    wimpt_noerr(wimp_get_point_info(&m));
251 252
    if (0 != (wimp_BRIGHT & m.bbits))
    {
Neil Turton's avatar
Neil Turton committed
253 254
      /* An ADJ-hit. The menu should be recreated. */
      mstr *p = win_getmenuh(event__current_menu_window);
255 256
      if (p != 0)
      {
Neil Turton's avatar
Neil Turton committed
257 258 259
        wimp_menustr *m;
        event__current_menu = p->m;
        event__current_destroy_after = FALSE;
260 261
        if (p->m != 0)
        {
Neil Turton's avatar
Neil Turton committed
262 263 264 265 266
          m = (wimp_menustr*) menu_syshandle(p->m);
        } else if (p->maker != 0) {
          event__recreation = 1 ;
          event__current_menu = p->maker(p->handle);
          m = (wimp_menustr*) menu_syshandle(event__current_menu);
267 268 269
        }
        else
        {
Ben Avison's avatar
Ben Avison committed
270
          m = (wimp_menustr *) -1;
271
        }
Neil Turton's avatar
Neil Turton committed
272
        wimpt_complain(wimp_create_menu(m, event__menux, event__menuy));
273 274 275
      }
    }
  }
Neil Turton's avatar
Neil Turton committed
276 277 278 279 280 281 282 283 284 285 286 287 288
  tracef0("doing poll.\n");
  wimpt_complain(wimpt_poll(mask, result));
  tracef0("poll done.\n");
}

void event_process(void)
{
  tracef0("event_process.\n");
  if (win_activeno() == 0) exit(0); /* stop program */
  {
    wimp_eventstr e;
    event__poll(event_getmask(), &e);
    (void) event__process(&e);
289
  }
Neil Turton's avatar
Neil Turton committed
290 291 292 293 294 295 296 297 298 299
}

#ifndef UROM
BOOL event_anywindows()
{
  return(win_activeno() != 0);
}
#endif

void event_clear_current_menu(void) {
300 301
  event__current_menu = NULL;
  event__current_menu_window = 0;
Neil Turton's avatar
Neil Turton committed
302 303 304 305 306
  wimpt_noerr(wimp_create_menu((wimp_menustr*) -1, 0, 0));
}

BOOL event_is_menu_being_recreated(void)
{
307
 return event__recreation;
Neil Turton's avatar
Neil Turton committed
308 309 310
}

/* end */