{ This unit is meant to handle the data structures of multiple windows } UNIT ControlsAndWindows; INTERFACE USES MacTypes,Controls,MacWindows,files; TYPE MyWindowRec = record TheWindow : WindowPtr; { the window itself } Storage : ptr; { pointer to the window data } WindowChanged : boolean; { The window changed } HasFile : boolean; { has a file associated with the window } TheFSSpec : FSSpec; { the FSSpec associated with the window } Controls : MyControlRecPtr; { all controls assiciated WITH this window } ScrollH : INTEGER; { current horizontal scroll value } ScrollV : INTEGER; { current vertical scroll value } SetWindowTitle : PROCEDURE (windowname,s:STRING); { set the title of a window } NextWindow : MyWindowRecPtr; { for scrolling through the windows } PreviousWindow : MyWindowRecPtr; { for scrolling through the windows } WindowTitle : STRING; { current title of the window (read only) } WindowName : STRING; { unique name for a window separate from the window title } END; MyWindowRecPtr = ^MyWindowRec; MyControlRec = record TheControl : ControlHandle; { the control itself } NextControl : MyControlRecPtr; { for scrolling through the controls } PreviousControl : MyControlRecPtr; { for scrolling through the controls } ControlName : str255; { what are we calling this control? } ControlID : INTEGER; { unique ID number for this control } { I don't think you'll have more than 65,536 controls in one window } END; MyControlRecPtr = ^MyControlRec; VAR WindowList : MyWindowRecPtr; { Initialize the UNIT } PROCEDURE InitControlsAndWindows; {--------------- Window stuff --------------} { Create a new window entry } FUNCTION CreateNewWindowEntry(base:MyWindowRecPtr):MyWindowRecPtr; { Given a window name AND the base, locate the window you need } FUNCTION LocateWindowByName(windowname:STRING; base:MyWindowRecPtr):MyWindowRecPtr; { Given a window AND the base, locate the window you need } FUNCTION LocateWindowByWindow(window:windowptr; base:MyWindowRecPtr):MyWindowRecPtr; { Remove the window entry AND all controls associated WITH it AND dispose of all it's memory } PROCEDURE RemoveWindowEntry(Window: WindowPtr); { Set the title of the window } PROCEDURE SetTheWindowTitle(windowname,s:STRING); {--------------- Control stuff --------------} { Create a new control entry } FUNCTION CreateNewControlEntry(base:MyControlRecPtr):MyControlRecPtr; { Given a control name AND the base, locate the control you need } FUNCTION LocateControlByName(ControlName:STRING; base:MyControlRecPtr):MyControlRecPtr; { Given a control handle AND the base, locate the control you need } FUNCTION LocateControlByControl(Control:ControlHandle; base:MyControlRecPtr):MyControlRecPtr; { Given a control ID AND the base, locate the control you need } FUNCTION LocateControlByControlID(ID:INTEGER; base:MyControlRecPtr):MyControlRecPtr; { Remove the control entry AND dispose of it's memory } PROCEDURE RemoveControlEntry(Control:ControlHandle; base:MyWindowRecPtr); IMPLEMENTATION VAR TempWindowList : MyWindowRecPtr; PROCEDURE InitControlsAndWindows; BEGIN TempWindowList := NIL; WindowList := NIL; END; FUNCTION LocateControlByName(ControlName:STRING; base:MyControlRecPtr):MyControlRecPtr; VAR temp : MyControlRecPtr; BEGIN IF base<>NIL THEN IF base^.ControlName<>ControlName THEN temp := LocateControlByName(ControlName,base^.NextControl) ELSE temp := base ELSE temp := NIL; LocateControlByName := temp; END; FUNCTION LocateControlByControl(Control:ControlHandle; base:MyControlRecPtr):MyControlRecPtr; VAR temp : MyControlRecPtr; BEGIN IF base<>NIL THEN IF base^.TheControl<>Control THEN temp := LocateControlByControl(Control,base^.NextControl) ELSE temp := base ELSE temp := NIL; LocateControlByControl := temp; END; FUNCTION LocateControlByControlID(ID:INTEGER; base:MyControlRecPtr):MyControlRecPtr; VAR temp : MyControlRecPtr; BEGIN IF base <> NIL THEN IF base^.ControlID <> ID THEN temp := LocateControlByControlID(ID,base^.NextControl) ELSE temp := base ELSE temp := NIL; LocateControlByControlID := temp; END; FUNCTION CreateNewControlEntry(base:MyControlRecPtr):MyControlRecPtr; VAR temp : MyControlRecPtr; BEGIN temp := base; IF temp<>NIL THEN BEGIN { Passed a real pointer so handle it appropiately } WHILE temp^.NextControl<>NIL DO temp := temp^.NextControl; temp^.NextControl := MyControlRecPtr(NewPtr(sizeof(MyControlRec))); temp^.NextControl^.PreviousControl := temp; temp := temp^.NextControl; WITH temp^ DO BEGIN TheControl := NIL; NextControl := NIL; ControlName := ''; ControlID := 0; END; END ELSE BEGIN { passed NIL so create a new item } temp := MyControlRecPtr(NewPtr(sizeof(MyControlRec))); WITH temp^ DO BEGIN TheControl := NIL; NextControl := NIL; PreviousControl := NIL; ControlName := ''; ControlID := 0; END; END; CreateNewControlEntry := temp; END; PROCEDURE RemoveControlEntry(Control:ControlHandle; base:MyWindowRecPtr); VAR temp : MyControlRecPtr; BEGIN IF base <> NIL THEN BEGIN temp := LocateControlByControl(Control,base^.Controls); IF temp <> NIL THEN BEGIN IF temp^.NextControl <> NIL THEN IF temp^.PreviousControl <> NIL THEN BEGIN temp^.NextControl^.PreviousControl := temp^.PreviousControl; temp^.PreviousControl^.NextControl := temp^.NextControl; END ELSE BEGIN temp^.NextControl^.PreviousControl := NIL; base^.Controls := temp^.NextControl; END ELSE IF temp^.PreviousControl <> NIL THEN BEGIN temp^.PreviousControl^.NextControl := NIL; END ELSE BEGIN base^.Controls := NIL; END; DisposeControl(temp^.TheControl); DisposePtr(ptr(temp)); END END; END; FUNCTION LocateWindowByName(windowname:STRING; base:MyWindowRecPtr):MyWindowRecPtr; VAR temp : MyWindowRecPtr; BEGIN IF base<>NIL THEN IF base^.WindowName<>windowname THEN temp := LocateWindowByName(windowname,base^.NextWindow) ELSE temp := base ELSE temp := NIL; LocateWindowByName := temp; END; FUNCTION LocateWindowByWindow(window:windowptr; base:MyWindowRecPtr):MyWindowRecPtr; VAR temp : MyWindowRecPtr; BEGIN IF base<>NIL THEN IF base^.TheWindow<>window THEN temp := LocateWindowByWindow(window,base^.NextWindow) ELSE temp := base ELSE temp := NIL; LocateWindowByWindow := temp; END; PROCEDURE RemoveWindowEntry(Window: WindowPtr); VAR temp : MyWindowRecPtr; BEGIN temp := LocateWindowByWindow(Window,WindowList); IF temp <> NIL THEN BEGIN WHILE temp^.Controls <> NIL DO RemoveControlEntry(temp^.Controls^.TheControl,temp); IF temp^.NextWindow <> NIL THEN IF temp^.PreviousWindow <> NIL THEN BEGIN { NextWindow <> nil, PreviousWindow <> nil } temp^.NextWindow^.PreviousWindow := temp^.PreviousWindow; temp^.PreviousWindow^.NextWindow := temp^.NextWindow; END ELSE BEGIN { NextWindow <> nil, PreviousWindow = nil } temp^.NextWindow^.PreviousWindow := NIL; WindowList := temp^.NextWindow; END ELSE IF temp^.PreviousWindow <> NIL THEN BEGIN { NextWindow = nil, PreviousWindow <> nil } temp^.PreviousWindow^.NextWindow := NIL; END ELSE BEGIN { NextWindow = nil, PreviousWindow = nil } WindowList := NIL; END; DisposePtr(ptr(temp)); END; END; PROCEDURE SetTheWindowTitle(windowname,s:STRING); BEGIN TempWindowList := WindowList; IF TempWindowList<>NIL THEN BEGIN WHILE ((TempWindowList^.WindowName<>windowname) AND (TempWindowList^.NextWindow<>NIL)) DO TempWindowList := TempWindowList^.NextWindow; IF TempWindowList^.WindowName = windowname THEN SetWTitle(TempWindowList^.TheWindow,s); TempWindowList^.WindowTitle := s; END; END; FUNCTION CreateNewWindowEntry(base:MyWindowRecPtr):MyWindowRecPtr; VAR temp : MyWindowRecPtr; BEGIN temp := base; IF temp<>NIL THEN BEGIN WHILE temp^.NextWindow<>NIL DO temp := temp^.NextWindow; temp^.NextWindow := MyWindowRecPtr(NewPtr(sizeof(MyWindowRec))); temp^.NextWindow^.PreviousWindow := temp; temp := temp^.NextWindow; WITH temp^ DO BEGIN TheWindow := NIL; Storage := NIL; Controls := NIL; WindowChanged := false; HasFile := false; TheFSSpec.vRefNum := 0; TheFSSpec.parID := 0; TheFSSpec.name := ''; ScrollH := 0; ScrollV := 0; NextWindow := NIL; SetWindowTitle := SetTheWindowTitle; WindowTitle := ''; WindowName := ''; END; END ELSE BEGIN temp := MyWindowRecPtr(NewPtr(sizeof(MyWindowRec))); WITH temp^ DO BEGIN TheWindow := NIL; Storage := NIL; Controls := NIL; WindowChanged := false; HasFile := false; TheFSSpec.vRefNum := 0; TheFSSpec.parID := 0; TheFSSpec.name := ''; ScrollH := 0; ScrollV := 0; NextWindow := NIL; PreviousWindow := NIL; SetWindowTitle := SetTheWindowTitle; WindowTitle := ''; WindowName := ''; END; END; CreateNewWindowEntry := temp; END; END.