home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / IOInterface / dialogDevice.icl < prev    next >
Encoding:
Modula Implementation  |  1997-04-23  |  27.2 KB  |  663 lines  |  [TEXT/3PRM]

  1. implementation module dialogDevice;
  2.  
  3. import StdClass,StdBool, StdInt, StdString, StdChar;
  4. import    OS_utilities, quickdraw, menus, events, windows, dialogs;
  5. import    dialogAccess, dialogInternal, dialogAbout, ioState;
  6. from    deltaTimer        import UWait;
  7. from    deltaMenu        import DisableMenuSystem, EnableMenuSystem;
  8. from    windowDevice    import WindowFunctions, IOStateSetCursorShape, IOStateGetGlobalCursor;
  9. from    menuDevice        import MenuFunctions;
  10. from    timerDevice        import TimerFunctions;
  11. from    dialogUpdate    import UpdateDialog;
  12.  
  13. ::    EventRecord        :== (!Int,!Int,!Int,!Int,!Int,!Int);
  14. ::    IOFunction *s    :== Event -> s -> *((IOState s) -> (Bool,s,IOState s));
  15.  
  16. DummyPtr        :== -1;
  17.  
  18. //DummyItem        ::    DialogItem s io;
  19. DummyItem        :== StaticText 0 Left "";
  20.  
  21. DialogDeviceError :: String String -> .x;
  22. DialogDeviceError f error = Error f "dialogDevice" error;
  23.  
  24.  
  25. DialogFunctions :: DeviceFunctions s;
  26. DialogFunctions = (    ChangeDialogs ShowWindow,
  27.                         DialogOpen,
  28.                             DialogIO,
  29.                         DialogClose,
  30.                     ChangeDialogs HideWindow    );
  31.  
  32.  
  33. ChangeDialogs :: !(!DialogPtr -> !Toolbox -> !Toolbox) !(IOState s) -> IOState s;
  34. ChangeDialogs f ioState
  35.     =    IOStateChangeToolbox (ChangeDialogs` f dHs) ioState1;
  36.     where {
  37.         (dHs, ioState1) = IOStateGetDialogs ioState;
  38.     };
  39.  
  40. ChangeDialogs` :: !(!DialogPtr -> !Toolbox -> !Toolbox) !(DialogHandles s) !Toolbox -> Toolbox;
  41. ChangeDialogs` f [dR : dRs] tb
  42. |    id < 0    = tb1;
  43.             = f ptr tb1;
  44.     where {
  45.         id            = DialogHandleGetId dH;
  46.         (dH, ptr)    = dR;
  47.         tb1            = ChangeDialogs` f dRs tb;
  48.     };
  49. ChangeDialogs` _ _ tb = tb;
  50.  
  51. DialogHandleGetId :: !(DialogHandle s io) -> DialogId;
  52. DialogHandleGetId (DialogH id _ _ _ _ _ _) = id;
  53.  
  54. //    Open all modeless dialogs in the DeviceSystem parameter.
  55.  
  56. DialogOpen :: !(DeviceSystem s (IOState s)) !(IOState s) -> IOState s;
  57. DialogOpen (DialogSystem dHs) ioState
  58.     =    IOStateSetDialogs (Concat dHs1 aboutHs) (IOStateSetToolbox tb1 ioState1);
  59.     where {
  60.         (tb, ioState1)        = IOStateGetToolbox ioState;
  61.         (aboutHs,dHs1,tb1)    = OpenModelessDialogs [] [] dHs tb;
  62.     };
  63. DialogOpen _ _
  64.     =    DialogDeviceError "DialogOpen" "argument is no DialogSystem";
  65.  
  66. OpenModelessDialogs :: ![DialogId] !(DialogHandles s) ![DialogDef s (IOState s)] !Toolbox
  67.     ->    (!DialogHandles s, !DialogHandles s, !Toolbox);
  68. OpenModelessDialogs ids aboutHs [aboutDef=:AboutDialog an pd fs ah : dDefs] tb
  69. |    IsEmptyList aboutHs
  70.     =    OpenModelessDialogs ids [(AboutDialogToDialogHandle aboutDef,DummyPtr)] dDefs tb;
  71.     =    OpenModelessDialogs ids aboutHs dDefs tb;
  72. OpenModelessDialogs ids aboutHs [dDef : dDefs] tb
  73. |    ContainsInt ids id    = OpenModelessDialogs ids aboutHs dDefs tb;
  74.                         = (aboutHs1, [dH : dHs1], tb2);
  75.     where {
  76.         (aboutHs1, dHs1, tb2)    = OpenModelessDialogs [id : ids] aboutHs dDefs tb1;
  77.         id                        = GetDialogDefId dDef;
  78.         (dH, tb1)                = OpenAnyDialog Modeless (-1) dDef tb;
  79.     };
  80. OpenModelessDialogs _ aboutHs dDefs tb = (aboutHs, [], tb);
  81.  
  82.  
  83. //    Close all open dialogs and remove the dialog device.
  84.  
  85. DialogClose :: !(IOState s) -> IOState s;
  86. DialogClose ioState
  87.     =    IOStateRemoveDevice (IOStateSetToolbox (CloseDialogs dHs tb) ioState2) DialogDevice;
  88.     where {
  89.         (dHs,ioState1) = IOStateGetDialogs ioState;
  90.         (tb, ioState2) = IOStateGetToolbox ioState1;
  91.     };
  92.  
  93. CloseDialogs :: !(DialogHandles *s) !Toolbox -> Toolbox;
  94. CloseDialogs [(_, dPtr) : dHs] tb
  95. |    dPtr == DummyPtr    = tb1;
  96.                         = DisposDialog dPtr tb1;
  97.     where {
  98.         tb1 = CloseDialogs dHs tb;
  99.     };
  100. CloseDialogs _ tb = tb;
  101.  
  102.  
  103. //    Event handling for dialogs.
  104.  
  105. DialogIO :: !Event !*s !(IOState *s) -> (!Bool, !*s, !IOState *s);
  106. DialogIO event=:(b,wt,ma,wn,h,v,ms) s io
  107. |    not isDialogEvent        = HandleSpecialDialogEvent event s io0;
  108. |    return_or_enter_still    = (True,s, io0);
  109. |    return_or_enter            = (True,s1,io2);
  110. |    command_period            = (True,s2,io3);
  111. |    mine                    = (True,s3,io4);
  112.                             = (True,s4,io5);
  113.     where {
  114.         eventrec            = (wt,ma,wn,h,v,ms);
  115.         (isDialogEvent,io0)    = IOStateAccessToolbox (IsDialogEvent eventrec) io;
  116.         (dSelect,io1)        = IOStateAccessToolbox (GetDialogSelect eventrec) io0;
  117.         (found,active,iowa)    = IOStateRemoveActiveDialog io0;
  118.         (s1,io2)            = PressDefaultButton` found active s iowa;
  119.         (s2,io3)            = PressCancelButton`  found active s iowa;
  120.         (s3,io4)            = HandleEvent drep itnr s iowd;
  121.         (s4,io5)            = HandleSpecialItemEvent eventrec s io1;
  122.         (drep,iowd)            = IOStateRemoveDialogOrAbort ptr io1;
  123.         (mine,ptr,itnr)        = dSelect;
  124.         (return_or_enter, return_or_enter_still, command_period) = KeyEventInfo eventrec;
  125.         
  126.         PressDefaultButton` b active s io
  127.         |    b                = PressDefaultButton active s io;
  128.                             = (s,io);
  129.         PressCancelButton` b active s io
  130.         |    b                = PressCancelButton active s io;
  131.                             = (s,io);
  132.     };
  133.  
  134. GetDialogSelect :: !EventRecord !Toolbox -> (!(!Bool,!DialogPtr,!Int),!Toolbox);
  135. GetDialogSelect eventrec tb
  136.     =    ((mine,ptr,itNr), tb1);
  137.     where {
  138.         (mine,ptr,itNr,tb1) = DialogSelect eventrec tb;
  139.     };
  140.  
  141. IOStateRemoveDialogOrAbort :: !DialogPtr !(IOState s) -> (!DialogRep s (IOState s), !IOState s);
  142. IOStateRemoveDialogOrAbort ptr ioState
  143. |    found    = (dRep, ioState1);
  144.             = DialogDeviceError "DialogIO" "Dialog not present in IOState";
  145.     where {
  146.         (found,dRep,ioState1) = IOStateRemoveDialogPtr ptr ioState;
  147.     };
  148.  
  149. DoModalDialog :: !(DialogDef *s (IOState *s)) !*s !(IOState *s) -> (!*s, !IOState *s);
  150. DoModalDialog dDef=:(CommandDialog id _ _ _ _) s ioState
  151. |    open    = (s, IOStateChangeToolbox (ActivateDialog dHs) ioState1);
  152.             = ModalIO menuIO timerIO windowIO mask dptr s (DisableMenuSystem (IOStateSetCursorShape cShape ioState3));
  153.     where {
  154.         (open,    ioState`)    = IOStateSetDialogInFront id ioState;
  155.         (dHs,    ioState1)    = IOStateGetDialogs ioState`;
  156.         (dRep,    ioState2)    = IOStateAccessToolbox (OpenAnyDialog Modal (-1) dDef) ioState`;
  157.         (_, dptr)            = dRep;
  158.         (cShape,ioState3)    = IOStateGetGlobalCursor (IOStateAddDialog dRep ioState2);
  159.         (_,_,menuIO, _,_)    = MenuFunctions;
  160.         (_,_,timerIO,_,_)    = TimerFunctions;
  161.         (_,_,windowIO,_,_)    = WindowFunctions;
  162.         mask                = UpdateMask        bitor
  163.                                 (ActivMask        bitor
  164.                                 (KeyboardMask    bitor
  165.                                 (MouseMask        bitor 1)));
  166.     };
  167. DoModalDialog dDef s ioState
  168. |    id == AboutDialogID    = (s, ioState);
  169. |    open                = (s, IOStateChangeToolbox (ActivateDialog dHs) ioState2);
  170.                         = (s, ioState1);
  171.     where {
  172.         id                = GetDialogDefId dDef;
  173.         (open,ioState1)    = IOStateSetDialogInFront id ioState;
  174.         (dHs, ioState2)    = IOStateGetDialogs ioState1;
  175.     };
  176.  
  177. ModalIO :: !(IOFunction *s) !(IOFunction *s) !(IOFunction *s) !Int !DialogPtr !*s !(IOState *s) -> (!*s, !IOState *s);
  178. ModalIO menuIO timerIO windowIO mask thisPtr s ioState
  179. |    quitted1    = (s,  ioState1);
  180. |    quitted2    = (s1, ioState4);
  181. |    active        = ModalIO menuIO timerIO windowIO mask thisPtr s2 ioState5;
  182. |    other        = (s2, ioState5);
  183. |    quitted3    = (s2, ioState6);
  184.                 = (s4, EnableMenuSystem ioState8);
  185.     where {
  186.         (quitted1,            ioState1) = IOStateClosed ioState;
  187.         (event,                ioState2) = IOStateAccessToolbox (GetEvent mask) ioState1;
  188.         (_,         s1,        ioState3) = timerIO event s ioState2;
  189.         (quitted2,            ioState4) = IOStateClosed ioState3;
  190.         (active,other,s2,    ioState5) = HandleModalEvent menuIO windowIO thisPtr event s1 ioState4;
  191.         (quitted3,            ioState6) = IOStateClosed ioState5;
  192.         (_,s3,                ioState7) = menuIO   event s2 ioState6;
  193.         (_,s4,                ioState8) = windowIO event s3 ioState7;
  194.     };
  195.  
  196. HandleModalEvent :: !(IOFunction *s) !(IOFunction *s) !DialogPtr !Event !*s !(IOState *s) -> (!Bool,!Bool,!*s,!IOState *s);
  197. HandleModalEvent menuIO windowIO thisPtr event=:(b,wt,ma,wn,h,v,ms) s io
  198. |    not found                                                = (False,False,s, io0);
  199. |    not modalActive                                            = (False,False,s, ioa);
  200. |    other                                                    = (False,other,s, ioa);
  201. |    noDialogEvent && mouseDown && region==InMenuBar            = (True, other,sm,iom);
  202. |    noDialogEvent && mouseDown                                = (True, other,s, iob);
  203. |    noDialogEvent && (wt==ActivateEvent || wt==UpdateEvent)    = (True, other,sw,iow);
  204. |    noDialogEvent || returnOrEnterStill                        = (True, other,s, ioa);
  205. |    returnOrEnter                                            = (True, other,s1,io1);
  206. |    commandPeriod                                            = (True, other,s2,io2);
  207. |    mine && mPtr <> ptr`                                    = (True, other,s, IOStateAddDialog active iowa`);
  208. |    mine                                                    = (True, other,s3,io3);
  209.                                                             = (True, other,s4,io4);
  210.     where {
  211.         (found,active,io0)    = IOStateRemoveActiveDialog io;
  212.         (modalActive,mPtr)    = IsModalDialog active;
  213.         other                = thisPtr <> mPtr;
  214.         eventrec            = (wt,ma,wn,h,v,ms);
  215.         (eventInfo, iowa)    = IOStateAccessToolbox (GetEventInfo eventrec) io0;
  216.         (dialogEvent,region)= eventInfo;
  217.         noDialogEvent        = not dialogEvent;
  218.         (eventInfo2, iowa`)    = IOStateAccessToolbox (GetDialogSelect eventrec) iowa;
  219.         (mine, ptr`, itnr)    = eventInfo2;
  220.         ioa                    = IOStateAddDialog active iowa;
  221.         iob                    = IOStateChangeToolbox (SysBeep 1) ioa;
  222.         (b,sm,iom)            = menuIO event s ioa;
  223.         (_,sw,iow)            = windowIO event s ioa;
  224.         (s1,io1)            = PressDefaultButton active s iowa;
  225.         (s2,io2)            = PressCancelButton  active s iowa;
  226.         (s3,io3)            = HandleEvent         active itnr s iowa`;
  227.         (s4,io4)            = HandleSpecialItemEvent eventrec s (IOStateAddDialog active iowa`);
  228.         mouseDown            = wt == MouseDownEvent;
  229.         (returnOrEnter, returnOrEnterStill, commandPeriod)    = KeyEventInfo eventrec;
  230.     };
  231.  
  232. GetEventInfo :: !EventRecord !Toolbox -> (!(!Bool,!Int), !Toolbox);
  233. GetEventInfo eventrec=:(_,_,_,h,v,_) tb
  234.     =    ((dialogEvent,region), tb2);
  235.     where {
  236.         (dialogEvent,  tb1)    = IsDialogEvent eventrec tb;
  237.         (region, _,    tb2)    = FindWindow    h v tb1;
  238.     };
  239.  
  240. KeyEventInfo :: !EventRecord -> (!Bool, !Bool, !Bool);
  241. KeyEventInfo (what,message,when,h,v,mods)
  242. |    what == KeyDownEvent    = (return_or_enter, False, command_period);
  243. |    what == AutoKeyEvent    = (False, return_or_enter, False);
  244.                             = (False, False, False);
  245.     where {
  246.         return_or_enter    = ('\015' == key || '\003' == key) && ms + cmd == 0;
  247.         command_period    = '.' == key && (ms == 0 && cmd <> 0);
  248.         ms                = (mods bitand 512) + ((mods bitand 2048) + (mods bitand 4096));
  249.         cmd                = mods bitand 256;
  250.         key                = toChar (message bitand 255);
  251.     };
  252.  
  253. PressDefaultButton :: !(DialogRep *s (IOState *s)) !*s !(IOState *s) -> (!*s, !IOState *s);
  254. PressDefaultButton dRep=:(dH=:DialogH _ _ _ _ _ items (DialogRest _ _ defid),ptr) s ioState
  255. |    not found    = (s, IOStateAddDialog dRep ioState);
  256.                 = buttonF (DialogHandleToDialogInfo dH1) s (IOStateAddDialog dRep1 ioState2);
  257.     where {
  258.         (found,isIcon,rect,buttonF)    = GetDialogButtonInfo defid items;
  259.         (tb, ioState1)                = IOStateGetToolbox ioState;
  260.         tb1                            = HiliteDefaultButton isIcon rect ptr tb;
  261.         (dRep1, tb2)                = RetrieveEditTexts (dRep,tb1);
  262.         ioState2                    = IOStateSetToolbox tb2 ioState1;
  263.         (dH1,_)                        = dRep1;
  264.     };
  265.  
  266. PressCancelButton :: !(DialogRep *s (IOState *s)) !*s !(IOState *s) -> (!*s, !IOState *s);
  267. PressCancelButton dRep=:(dH=:DialogH _ _ _ _ _ items _,ptr) s ioState
  268. |    not found    = (s, IOStateAddDialog dRep ioState);
  269.                 = buttonF (DialogHandleToDialogInfo dH1) s (IOStateAddDialog dRep1 ioState2);
  270.     where {
  271.         (found, rect, buttonF)    = GetCancelButtonInfo items;
  272.         (tb, ioState1)            = IOStateGetToolbox ioState;
  273.         tb1                        = HiliteDefaultButton False rect ptr tb;
  274.         (dRep1,tb2)                = RetrieveEditTexts (dRep,tb1);
  275.         ioState2                = IOStateSetToolbox tb2 ioState1;
  276.         (dH1,_)                    = dRep1;
  277.     };
  278.  
  279. HandleSpecialDialogEvent :: !Event !*s !(IOState *s) -> (!Bool, !*s, !IOState *s);
  280. HandleSpecialDialogEvent event=:(b,MouseDownEvent,ma,wn,h,v,ms) s ioState
  281. |    NoDialogs dHs            = (False, s,ioState1);
  282. |    not mine                = (False, s,ioState3);
  283. |    region == InContent        = ActivateTheDialog dRep event s ioState4;
  284. |    region == InDrag        = (True,  s,DragTheDialog  h v dRep ioState4);
  285. |    region == InGoAway        = (closed,s,ioState5);
  286.                             = (False, s,ioState3);
  287.     where {
  288.         (dHs, ioState1)        = IOStateGetDialogs ioState;
  289.         (tb,  ioState2)        = IOStateGetToolbox ioState1;
  290.         (region, ptr, tb1)    = FindWindow h v tb;
  291.         ioState3            = IOStateSetToolbox tb1 ioState2;
  292.         (mine, dRep, dHs1)    = RemoveDialogPtr ptr dHs;
  293.         ioState4            = IOStateSetDialogs dHs1 ioState3;
  294.         (closed, ioState5)    = CloseTheDialog h v dRep ioState4;
  295.     };
  296. HandleSpecialDialogEvent _ s io = (False,s,io);
  297.  
  298. NoDialogs :: !(DialogHandles s) -> Bool;
  299. NoDialogs [] = True;
  300. NoDialogs [(dH,ptr) : _] = (DialogHandleGetId dH) <= 0 && ptr == DummyPtr;
  301.  
  302. ActivateTheDialog :: !(DialogRep *s (IOState *s)) !Event !*s !(IOState *s)  -> (!Bool,!*s,!IOState *s);
  303. ActivateTheDialog dRep=:(dH,ptr) event s ioState
  304. |    DialogIsStandBy dH
  305.     =    DialogIO event s (IOStateSetToolbox tb1 ioState2);
  306.     =    (True, s, IOStateSetToolbox (RemoveMouseUp tb1) ioState2);
  307.     where {
  308.         ioState1        = IOStateAddDialog dRep ioState;
  309.         (tb, ioState2)    = IOStateGetToolbox ioState1;
  310.         tb1                = SelectWindow ptr tb;
  311.     };
  312.  
  313.  
  314. DialogIsStandBy :: !(DialogHandle s (IOState s)) -> Bool;
  315. DialogIsStandBy (DialogH _ _ _ _ _ _ (DialogRest _ attrs _)) = ContainsStandByAttribute attrs;
  316.  
  317. ContainsStandByAttribute :: [DialogAttribute] -> Bool;
  318. ContainsStandByAttribute [StandByDialog    : _]    = True;
  319. ContainsStandByAttribute [_                : rest] = ContainsStandByAttribute rest;
  320. ContainsStandByAttribute _                        = False;
  321.  
  322. RemoveMouseUp :: !Toolbox -> Toolbox;
  323. RemoveMouseUp tb
  324. |    notUpYet    = RemoveMouseUp tb1;
  325.                 = tb1;
  326.     where {
  327.         (notUpYet,tb1) = WaitMouseUp tb
  328.     };
  329.  
  330. DragTheDialog :: !Int !Int !(DialogRep s (IOState s)) !(IOState s)  -> IOState s;
  331. DragTheDialog h v dRep=:(dH,ptr) ioState
  332.     =    IOStateAddDialog dRep (IOStateSetToolbox tb2 ioState1);
  333.     where {
  334.         (tb, ioState1)        = IOStateGetToolbox ioState;
  335.         (sl,st,sr,sb,tb1)    = QScreenRect tb;
  336.         tb2                    = DragWindow ptr h v (sl,st,dec sr,dec sb) tb1;
  337.     };
  338.  
  339. CloseTheDialog :: !Int !Int !(DialogRep s (IOState s)) !(IOState s)  -> (!Bool,!IOState s);
  340. CloseTheDialog h v dRep=:(_,ptr) ioState
  341. |    goAway    = (True, IOStateSetToolbox (DisposDialog ptr tb1) ioState1);
  342.             = (False,IOStateAddDialog dRep (IOStateSetToolbox tb1 ioState1));
  343.     where {
  344.         (tb, ioState1)    = IOStateGetToolbox ioState;
  345.         (goAway, tb1)    = TrackGoAway ptr h v tb;
  346.     };
  347.  
  348. HandleEvent :: !(DialogRep *s (IOState *s)) !Int !*s !(IOState *s) -> (!*s, !IOState *s);
  349. HandleEvent (dH,ptr) itemNr s ioState
  350.     =    HandleResponse resp dRep s (IOStateSetToolbox tb1 ioState1);
  351.     where {
  352.         (tb, ioState1)    = IOStateGetToolbox ioState;
  353.         (dRep,resp,tb1) = HandleDialogEvent dH ptr tb itemNr;
  354.     };
  355.  
  356. HandleResponse :: !(Response *s) !(DialogRep *s (IOState *s)) !*s !(IOState *s) -> (!*s, !IOState *s);
  357. HandleResponse (RadioBox dFunc) dRep s ioState
  358.     =    (s, IOStateAddDialog dRep2 (IOStateSetToolbox tb2 ioState1));
  359.     where {
  360.         (tb, ioState1)    = IOStateGetToolbox ioState;
  361.         (dRep1,tb1)        = RetrieveEditTexts (dRep,tb);
  362.         (dH,_)            = dRep1;
  363.         dInfo            = DialogHandleToDialogInfo dH;
  364.         (dRep2,tb2)        = UnpackDialogState (dFunc dInfo (CreateDialogState (dRep1,tb1)));
  365.     };
  366. HandleResponse (Final bFunc) dRep s ioState
  367.     =    bFunc dInfo s (IOStateAddDialog dRep1 (IOStateSetToolbox tb1 ioState1));
  368.     where {
  369.         (tb, ioState1)    = IOStateGetToolbox ioState;
  370.         (dRep1,tb1)        = RetrieveEditTexts (dRep,tb);
  371.         (dH,_)            = dRep1;
  372.         dInfo            = DialogHandleToDialogInfo dH;
  373.     };
  374. HandleResponse Void_new dRep s ioState = (s,IOStateAddDialog dRep ioState);
  375.  
  376. HandleSpecialItemEvent :: !EventRecord !*s !(IOState *s) -> (!*s, !IOState *s);
  377. HandleSpecialItemEvent (UpdateEvent,ptr,w,h,v,m) s ioState
  378. |    not mine    = (s,ioState1);
  379.                 = (s,IOStateChangeToolbox (UpdateDialog dRep) ioState1);
  380.     where {
  381.         (mine,dRep,ioState1) = IOStateGetDialogPtr ptr ioState;
  382.     };
  383. HandleSpecialItemEvent (MouseDownEvent,_,when,h,v,mods) s ioState
  384. |    region <> InContent    = (s, IOStateSetToolbox tb1 ioState1);
  385. |    not mine            = (s, IOStateSetToolbox tb1 ioState`);
  386. |    not found            = (s, IOStateSetToolbox tb2 (IOStateAddDialog dRep ioState`));
  387.                         = HandleMouseEvent item (localPos, bstate, modifiers) dRep1 s ioState``;
  388.     where {
  389.         (tb, ioState1)        = IOStateGetToolbox ioState;
  390.         (region, ptr, tb1)    = FindWindow h v tb;
  391.         (mine,dRep,ioState`)= IOStateRemoveActiveDialogPtr ptr ioState1;
  392.         (localh,localv)        = localPos;
  393.         (localPos, tb2)        = InGrafport ptr (GlobalToLocal (h,v)) tb1;
  394.         (found,item)        = GetItemPointedTo localPos dRep;
  395.         modifiers            = INTToModifiers mods;
  396.         (dRep1, tb3)        = RetrieveEditTexts (dRep,tb2);
  397.         (bstate, ioState``)    = IOStateButtonFreq when (localh,localv) ptr (IOStateSetToolbox tb3 ioState`);
  398.     };
  399. HandleSpecialItemEvent _ s ioState = (s,ioState);
  400.  
  401. GetItemPointedTo :: !(!Int,!Int) !(DialogRep s (IOState s)) -> (!Bool,!DialogItem s (IOState s));
  402. GetItemPointedTo localPos (DialogH id tt md rc ps items rs,ptr) =  GetItemPtedTo localPos items;
  403.  
  404. GetItemPtedTo :: !(!Int,!Int) ![DialogItem s (IOState s)] -> (!Bool,!DialogItem s (IOState s));
  405. GetItemPtedTo pos=:(x,y) [pop=:DialogPopUp _ (ItemBox l t w h) Able _ _ : rest]
  406. |    IsBetween x l (l+w) && IsBetween y t (t+h)    = (True,pop);
  407.                                                 = GetItemPtedTo pos rest;
  408. GetItemPtedTo pos=:(x,y) [but=:DialogIconButton _ (ItemBox l t w h) _ _ Able _ : rest]
  409. |    IsBetween x l (l+w) && IsBetween y t (t+h)    = (True,but);
  410.                                                 = GetItemPtedTo pos rest;
  411. GetItemPtedTo pos=:(x,y) [ctl=:Control _ (ItemBox l t w h) _ Able _ _ _ _ : rest]
  412. |    IsBetween x l (l+w) && IsBetween y t (t+h)    = (True,ctl);
  413.                                                 = GetItemPtedTo pos rest;
  414. GetItemPtedTo pos [_ : rest]    = GetItemPtedTo pos rest;
  415. GetItemPtedTo _ _                = (False, DummyItem);
  416.  
  417.  
  418. //    Handle mouse events in dialogs.
  419.  
  420. HandleMouseEvent :: !(DialogItem *s (IOState *s)) !MouseState !(DialogRep *s (IOState *s)) !*s !(IOState *s)
  421.     ->    (!*s,!IOState *s);
  422. HandleMouseEvent item=:(DialogPopUp id pos=:(ItemBox l t w h) Able _ radios) ((x,_),_,_)
  423.                  dRep=:(dH,dPtr) s ioState
  424. |    x < l                            = (s, IOStateAddDialog dRep  ioState );
  425. |    itemNr == 0 || itemNr == oldNr    = (s, IOStateAddDialog dRep  ioState1);
  426.                                     = (s, IOStateAddDialog dRep2 ioState3);
  427.     where {
  428.         (oldNr,itemNr,menu,ioState1)= HandlePopUpMenuEvent item dRep ioState;
  429.         (tb, ioState2)                = IOStateGetToolbox ioState1;
  430.         tb1                            = CheckItem menu oldNr False tb;
  431.         tb2                            = CheckItem menu itemNr True tb1;
  432.         tb3                            = RedrawPopUpItemText pos text dPtr tb2;
  433.         dialogH1                    = ReplacePopUpInDialogHandle id item1 dH;
  434.         item1                        = DialogPopUp id pos Able ndi radios;
  435.         dRep1                        = (dialogH1, dPtr);
  436.         dState                        = CreateDialogState (dRep1,tb3);
  437.         (ndi, text, ab, dialogF)    = GetIndexRadioButtonInfo itemNr radios;
  438.         dState1                        = dialogF (DialogHandleToDialogInfo dH) dState;
  439.         (dRep2, tb4)                = UnpackDialogState dState1;
  440.         ioState3                    = IOStateSetToolbox tb4 ioState2;
  441.     };
  442. HandleMouseEvent (Control id pos=:(ItemBox l t w h) dom=:((dL,dT),_) ab cs look feel dialogF)
  443.                  ((x,y), button, mods) dRep=:(dialogH, dPtr) s ioState
  444.     =    (s, TrackCleanControl port tb4 dH dV id dRep2 ioState1);
  445.     where {
  446.         (tb, ioState1)    = IOStateGetToolbox ioState;
  447.         (port, tb1)        = QGetPort tb;
  448.         tb2                = QSetPort dPtr tb1;
  449.         tb3                = DrawIconOrControl pos dom draws dPtr tb2;
  450.         (cs1, draws)    = feel ((x+dH, y+dV), button, mods) cs;
  451.         control1        = Control id pos dom ab cs1 look feel dialogF;
  452.         dialogH1        = ReplaceControlInDialogHandle id control1 dialogH;
  453.         dRep1            = (dialogH1, dPtr);
  454.         dState            = CreateDialogState (dRep1,tb3);
  455.         dState1            = dialogF (DialogHandleToDialogInfo dialogH1) dState;
  456.         (dRep2, tb4)    = UnpackDialogState dState1;
  457.         dH                = dL-l;
  458.         dV                = dT-t;
  459.     };
  460. HandleMouseEvent (DialogIconButton id pos=:(ItemBox l t w h) dom look ab buttonF) _ (dH,dPtr) s ioState
  461.     =    TrackIconButton True port tb3 dPtr rect buttonF dH s ioState1;
  462.     where {
  463.         (tb, ioState1)    = IOStateGetToolbox ioState;
  464.         (port, tb1)        = QGetPort tb;
  465.         tb2                = QSetPort dPtr tb1;
  466.         tb3                = QInvertRect rect tb2;
  467.         rect            = (l,t, l+w,t+h);
  468.     };
  469.  
  470. HandlePopUpMenuEvent :: !(DialogItem s (IOState s)) !(DialogRep s (IOState s)) !(IOState s)
  471.     ->    (!Int, !Int, !MacMenuHandle, !IOState s);
  472. HandlePopUpMenuEvent (DialogPopUp id (ItemBox l t w h) _ di radios) (dH, dPtr) ioState
  473.     =    (oldNr, itemNr, menu, ioState2);
  474.     where {
  475.         (tb, ioState1)        = IOStateGetToolbox ioState;
  476.         tb1                    = InsertMenu menu (-1) tb;
  477.         (globalPos, tb2)    = InGrafport dPtr (LocalToGlobal (l,t)) tb1;
  478.         (globalX, globalY)    = globalPos;
  479.         (i, menu)            = DialogHandleGetPopUpHandle id dH;
  480.         oldNr                = RadioButtonsIdToNr di radios;
  481.         (menuId,itemNr,tb3)    = PopUpMenuSelect menu globalY globalX oldNr tb2;
  482.         tb4                    = DeleteMenu PopUpMenuID tb3;
  483.         ioState2            = IOStateSetToolbox tb4 ioState1;
  484.     };
  485.  
  486. TrackCleanControl :: !GrafPtr !Toolbox !Int !Int !DialogItemId !(DialogRep s (IOState s)) !(IOState s)
  487.     ->    IOState s;
  488. TrackCleanControl port tb dH dV id dRep ioState
  489. |    down    = TrackCleanControl port tb2 dH dV id dRep1 ioState;
  490.             = IOStateAddDialog dRep1 (IOStateSetToolbox tb2 ioState);
  491.     where {
  492.         (down,  tb1) = WaitMouseUp tb;
  493.         (dRep1, tb2) = TrackCleanControlWithMouse port tb1 dH dV id down dRep;
  494.     };
  495.  
  496. TrackCleanControlWithMouse :: !GrafPtr !Toolbox !Int !Int !DialogItemId !Bool !(DialogRep s (IOState s))
  497.     ->    (!DialogRep s (IOState s), !Toolbox);
  498. TrackCleanControlWithMouse port tb dH dV id buttonDown (dialogH, dPtr)
  499.     =    UnpackDialogState dState1;
  500.     where {
  501.         (pos,dom,feel,cs,dialogF)    = GetControlInfo control;
  502.         control                        = GetControl id dialogH;
  503.         (x, y,         tb1)            = GetMouse tb;
  504.         (k1,k2,k3,k4,tb2)            = GetKeys tb1;
  505.         mods                        = KeyMapToModifiers (k1,k2,k3,k4);
  506.         (cs1, draws1)                = feel ((x+dH,y+dV), If buttonDown ButtonStillDown ButtonUp, mods) cs;
  507.         control1                    = ReplaceControlState cs1 control;
  508.         dialogH1                    = ReplaceControlInDialogHandle id control1 dialogH;
  509.         dRep1                        = (dialogH1, dPtr);
  510.         tb3                            = DrawIconOrControl pos dom draws1 dPtr tb2;
  511.         tb4                            = SetGrafportWhenUp buttonDown port tb3;
  512.         dState                        = CreateDialogState (dRep1,tb4);
  513.         dState1                        = dialogF (DialogHandleToDialogInfo dialogH1) dState;
  514.     };
  515.  
  516. SetGrafportWhenUp :: !Bool !GrafPtr !Toolbox -> Toolbox;
  517. SetGrafportWhenUp buttonDown port tb
  518. |    buttonDown    = tb;
  519.                 = QSetPort port tb;
  520.  
  521. ReplaceControlState    :: !ControlState !(DialogItem s (IOState s)) -> DialogItem s (IOState s);
  522. ReplaceControlState state (Control id ps dm ab _ lk fl df) = Control id ps dm ab state lk fl df;
  523.  
  524. GetControlInfo :: !(DialogItem s (IOState s))
  525.     -> (ItemPos,PictureDomain,ControlFeel,ControlState,DialogFunction s (IOState s));
  526. GetControlInfo (Control _ pos dom _ state _ feel dfunc) = (pos,dom,feel,state,dfunc);
  527.  
  528. GetControl :: !DialogItemId !(DialogHandle s (IOState s)) -> DialogItem s (IOState s);
  529. GetControl cid (DialogH _ _ _ _ _ items _) = GetControlInItemList cid items;
  530.  
  531. GetControlInItemList :: !DialogItemId ![DialogItem s (IOState s)] -> DialogItem s (IOState s);
  532. GetControlInItemList cid [item=:Control id _ _ _ _ _ _ _ : items]
  533. |    cid == id    = item;
  534.                 = GetControlInItemList cid items;
  535. GetControlInItemList cid [_ : items] = GetControlInItemList cid items;
  536.  
  537. TrackIconButton ::    !Bool !GrafPtr !Toolbox !DialogPtr !Rect !(ButtonFunction *s (IOState *s))
  538.                     !(DialogHandle *s (IOState *s)) !*s !(IOState *s)
  539.                 ->    (!*s, !IOState *s);
  540. TrackIconButton inside port tb dPtr rect=:(l,t,r,b) buttonF dialog s ioState
  541. |    inside && stillIn    = TrackIconButton` stillDown stillIn port tb2 dPtr rect buttonF dialog s ioState;
  542. |    inside || stillIn    = TrackIconButton` stillDown stillIn port tb3 dPtr rect buttonF dialog s ioState;
  543.                         = TrackIconButton` stillDown stillIn port tb2 dPtr rect buttonF dialog s ioState;
  544.     where {
  545.         stillIn                = x >= l && y >= t && x <= r && y <= b;
  546.         (x, y,        tb1)    = GetMouse        tb;
  547.         (stillDown, tb2)    = WaitMouseUp    tb1;
  548.         tb3                    = QInvertRect rect tb2;
  549.     };
  550.  
  551. TrackIconButton` ::    !Bool !Bool !GrafPtr !Toolbox !DialogPtr !Rect !(ButtonFunction *s (IOState *s))
  552.                     !(DialogHandle *s (IOState *s)) !*s !(IOState *s)
  553.                  ->    (!*s, !IOState *s);
  554. TrackIconButton` down inside port tb dPtr rect buttonF dialog s ioState
  555. |    down        = TrackIconButton inside port tb dPtr rect buttonF dialog s ioState;
  556. |    inside        = buttonF (DialogHandleToDialogInfo dialog) s ioState2;
  557.                 = (s, ioState3);
  558.     where {
  559.         dRep        = (dialog, dPtr);
  560.         ioState1    = IOStateAddDialog dRep ioState;
  561.         ioState2    = IOStateSetToolbox (QSetPort port (QInvertRect rect tb))    ioState1;
  562.         ioState3    = IOStateSetToolbox (QSetPort port tb)                        ioState1;
  563.     };
  564.  
  565. HiliteDefaultButton :: !Bool !Rect !DialogPtr !Toolbox -> Toolbox;
  566. HiliteDefaultButton True rect dPtr tb
  567.     =    tb7;
  568.     where {
  569.         (port, tb1)    = QGetPort tb;
  570.         tb2            = QSetPort dPtr tb1;
  571.         tb3            = QInvertRect rect tb2;
  572.         tb5            = UWait 8 tb3;
  573.         tb6            = QInvertRect rect tb5;
  574.         tb7            = QSetPort port tb6;
  575.     };
  576. HiliteDefaultButton noIcon rect dPtr tb
  577.     =    tb9;
  578.     where {
  579.         (port, tb1)    = QGetPort tb;
  580.         tb2            = QSetPort dPtr tb1;
  581.         tb3            = QInvertRoundRect rect 10 10 tb2;
  582.         tb4            = QFrameRoundRect rect 10 10 tb3;
  583.         tb6            = UWait 8 tb4;
  584.         tb7            = QInvertRoundRect rect 10 10 tb6;
  585.         tb8            = QFrameRoundRect rect 10 10 tb7;
  586.         tb9            = QSetPort port tb8;
  587.     };
  588.  
  589.  
  590. //    Acces rules on dialogs and dialog items.
  591.  
  592. GetDialogButtonInfo    :: !DialogItemId ![DialogItem s (IOState s)]
  593.     ->    (!Bool, !Bool, !Rect, !ButtonFunction s (IOState s));
  594. GetDialogButtonInfo bid [DialogButton id (ItemBox l t w h) tt Able bf : rest]
  595. |    bid == id    = (True, False, (l,t,l + w,t + h), bf);
  596.                 = GetDialogButtonInfo bid rest;
  597. GetDialogButtonInfo bid [DialogIconButton id (ItemBox l t w h) pd il Able bf : rest]
  598. |    bid == id    = (True, True , (l,t,l + w,t + h), bf);
  599.                 = GetDialogButtonInfo bid rest;
  600. GetDialogButtonInfo bid [item : rest]    = GetDialogButtonInfo bid rest;
  601. GetDialogButtonInfo bid _                = (False,False,(0,0,0,0),DummyButFunc);
  602.  
  603. GetCancelButtonInfo :: ![DialogItem s (IOState s)] -> (!Bool, !Rect, !ButtonFunction s (IOState s));
  604. GetCancelButtonInfo [DialogButton id (ItemBox l t w h) title Able bfunc : rest]
  605. |    title == "Cancel"    = (True, (l,t,l + w,t + h), bfunc);
  606.                         = GetCancelButtonInfo rest;
  607. GetCancelButtonInfo [_ : rest]    = GetCancelButtonInfo rest;
  608. GetCancelButtonInfo _            = (False,(0,0,0,0),DummyButFunc);
  609.  
  610. // DummyButFunc    :: !DialogInfo !*s !(IOState *s) -> (!*s, !IOState *s);
  611. DummyButFunc _ s io = (s,io);
  612.  
  613. GetIndexRadioButtonInfo :: !Int ![RadioItemDef s (IOState s)]
  614.     -> (!DialogItemId,!ItemTitle, !SelectState, !DialogFunction s (IOState s));
  615. GetIndexRadioButtonInfo itemnr [RadioItem id title ab df : rest]
  616. |    itemnr == 1    = (id,title,ab,df);
  617.                 = GetIndexRadioButtonInfo (dec itemnr) rest;
  618. GetIndexRadioButtonInfo _ _
  619.     =    DialogDeviceError "GetIndexRadioButtonInfo" "Index out of range";
  620.  
  621. RadioButtonsIdToNr :: !DialogItemId ![RadioItemDef s (IOState s)] -> Int;
  622. RadioButtonsIdToNr bid [RadioItem id title ab df : rest]
  623. |    bid == id    = 1;
  624.                 = inc (RadioButtonsIdToNr bid rest);
  625. RadioButtonsIdToNr bid _
  626.     =    DialogDeviceError "RadioButtonsIdToNr" ("Unknown item id: " +++ toString bid);
  627.  
  628.  
  629. //    Access rule(s) on DialogHandles.
  630.  
  631. DialogHandleGetPopUpHandle :: !DialogItemId !(DialogHandle s (IOState s)) -> PopUpHandle;
  632. DialogHandleGetPopUpHandle pid (DialogH _ _ _ _ popups _ _) = GetPopUpHandle pid popups;
  633.  
  634. ReplacePopUpInDialogHandle :: !DialogId !(DialogItem s (IOState s)) !(DialogHandle s (IOState s))
  635.     -> DialogHandle s (IOState s);
  636. ReplacePopUpInDialogHandle pid popup (DialogH id tt md rc ps items rt)
  637.     =    DialogH id tt md rc ps (ReplacePopUpInItemList pid popup items) rt;
  638.  
  639. ReplacePopUpInItemList :: !DialogId !(DialogItem s (IOState s)) ![DialogItem s (IOState s)]
  640.     -> [DialogItem s (IOState s)];
  641. ReplacePopUpInItemList pid popup [item=:DialogPopUp id ps ab di bs : rest]
  642. |    pid == id    = [popup : rest];
  643.                 = [item  : ReplacePopUpInItemList pid popup rest];
  644. ReplacePopUpInItemList pid popup [item : rest]
  645.     =    [item : ReplacePopUpInItemList pid popup rest];
  646. ReplacePopUpInItemList pid _ _
  647.     =    DialogDeviceError "ReplacePopUpInDialogHandle" ("No PopUp with same id: " +++ toString pid);
  648.  
  649. ReplaceControlInDialogHandle :: !DialogId !(DialogItem s (IOState s)) !(DialogHandle s (IOState s))
  650.     -> DialogHandle s (IOState s);
  651. ReplaceControlInDialogHandle cid control (DialogH id tt md rc ps items rt)
  652.     =    DialogH id tt md rc ps (ReplaceControlInItemList cid control items) rt;
  653.  
  654. ReplaceControlInItemList :: !DialogId !(DialogItem s (IOState s)) ![DialogItem s (IOState s)]
  655.     -> [DialogItem s (IOState s)];
  656. ReplaceControlInItemList cid control [item=:Control id ps pd ab cs cl cf df : rest]
  657. |    cid == id    = [control    : rest];
  658.                 = [item        : ReplaceControlInItemList cid control rest];
  659. ReplaceControlInItemList cid control [item : rest]
  660.     =    [item : ReplaceControlInItemList cid control rest];
  661. ReplaceControlInItemList cid _ _
  662.     =    DialogDeviceError "ReplaceControlInDialogHandle" ("No Control with same id: " +++ toString cid);
  663.