summaryrefslogtreecommitdiff
path: root/maccaml/modalfilter.c
blob: 1075b8cb2b2b42eaca869616b7bc1981a6a225cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
80
81
82
/***********************************************************************/
/*                                                                     */
/*                           Objective Caml                            */
/*                                                                     */
/*             Damien Doligez, projet Para, INRIA Rocquencourt         */
/*                                                                     */
/*  Copyright 1998 Institut National de Recherche en Informatique et   */
/*  en Automatique.  All rights reserved.  This file is distributed    */
/*  under the terms of the GNU Library General Public License.         */
/*                                                                     */
/***********************************************************************/

/* $Id$ */

#include "main.h"

/* See ocaml.r before modifying this. */
typedef struct {
  char mod;
  char chr;
  char item;
  char filler;
} KeyEquRecord, **KeyEquHandle;

short modalkeys;
ModalFilterUPP myModalFilterUPP;

/* Before calling ModalDialog with myModalFilter, set the dialog
   window's refcon to the resource number of the key equivalence
   list for the dialog.
*/
static pascal Boolean myModalFilter (DialogPtr d, EventRecord *evt,
                                     DialogItemIndex *item)
{
  Boolean result = false;
  char key;
  int cmdflag;
  KeyEquHandle equivlist;
  int equivcount, i;
  short itemtype;
  Handle itemhandle;
  Rect itemrect;
  unsigned long ticks;

  switch (evt->what){
  case updateEvt:
    if ((WindowPtr) evt->message != d) WinUpdate ((WindowPtr) evt->message);
    break;
  case activateEvt:
    if ((WindowPtr) evt->message != d){
      WinActivateDeactivate (evt->modifiers & activeFlag,
                             (WindowPtr) evt->message);
    }
    break;
  case keyDown: case autoKey:
    key = evt->message & charCodeMask;
    cmdflag = !!(evt->modifiers & cmdKey);
    equivlist = (KeyEquHandle) GetResource ('Kequ', modalkeys);
    if (equivlist != NULL){
      equivcount = GetHandleSize ((Handle) equivlist) / sizeof (KeyEquRecord);
      for (i = 0; i < equivcount; i++){
        if ((*equivlist)[i].chr == key && (!(*equivlist)[i].mod || cmdflag)){
          result = true;
          *item = (*equivlist)[i].item;
          GetDialogItem (d, *item, &itemtype, &itemhandle, &itemrect);
          HiliteControl ((ControlHandle) itemhandle, kControlButtonPart);
          Delay (kVisualDelay, &ticks);
          HiliteControl ((ControlHandle) itemhandle, 0);
        }
      }
    }
    break;
  default: break;
  }
  return result;
}

OSErr InitialiseModalFilter (void)
{
  myModalFilterUPP = NewModalFilterProc (myModalFilter);
  return noErr;
}