unit CarbonStuff; { this code has no warranty as to its correctness, usefulness or anything else } { it is to show how I did things } { some of this code assumes things are set up in certain ways in the code it was called } { from - see the explanation for ValidWindowRect } interface uses Types, Events, OSUtils, Memory, QuickDraw, QuickdrawText, Menus, Resources, Windows, Fonts, Packages, GestaltEqu, TextUtils, ToolUtils, Files, Aliases, AppleEvents, Controls, ControlDefinitions, Dialogs, StandardFile, TextEdit, SegLoad, Sound,Scrap, {$ifc TARGET_API_MAC_CARBON} MacHelp,Balloons {$elsec} Balloons {$endc} ; {$ifc TARGET_API_MAC_CARBON} FUNCTION HiWord(x: LONGINT): INTEGER; FUNCTION LoWord(x: LONGINT): INTEGER; {$endc} {$ifc TARGET_API_MAC_CARBON} procedure invalRect(r:Rect); procedure invalRgn(r:RgnHandle); {$endc} function GetScreenBounds:Rect; {$ifc TARGET_API_MAC_CARBON} function qdThePort:CGrafPtr; {$elsec} function qdThePort:GrafPtr; {$endc} {$ifc not ACCESSOR_CALLS_ARE_FUNCTIONS} FUNCTION GetWindowPort(window: WindowPtr): GrafPtr; FUNCTION GetDialogPort(dialog: DialogPtr): GrafPtr; FUNCTION GetDialogWindow(dialog: DialogPtr): WindowPtr; FUNCTION GetMenuID(menu: MenuRef): MenuID; FUNCTION GetWindowFromPort(port: univ GrafPtr): WindowPtr; FUNCTION GetWindowKind(window: WindowPtr): INTEGER; FUNCTION IsMenuItemEnabled(menu: MenuRef; item: MenuItemIndex): BOOLEAN; FUNCTION GetPortTextFont(port: GrafPtr): INTEGER; FUNCTION GetPortTextFace(port: GrafPtr): StyleField; FUNCTION GetPortTextMode(port: GrafPtr): INTEGER; FUNCTION GetPortTextSize(port: GrafPtr): INTEGER; FUNCTION GetDialogDefaultItem(dialog: DialogPtr): SInt16; FUNCTION GetDialogKeyboardFocusItem(dialog: DialogPtr): SInt16; FUNCTION GetDialogTextEditHandle(dialog: DialogPtr): TEHandle; FUNCTION GetMenuWidth(menu: MenuRef): SInt16; FUNCTION GetMenuHeight(menu: MenuRef): SInt16; FUNCTION GetWindowStandardState(window: WindowRef; VAR rect: Rect): RectPtr; FUNCTION GetWindowUserState(window: WindowRef; VAR rect: Rect): RectPtr; PROCEDURE SetWindowStandardState(window: WindowRef; {CONST}VAR rect: Rect); PROCEDURE SetWindowUserState(window: WindowRef; {CONST}VAR rect: Rect); FUNCTION GetControlHilite(control: ControlRef): UInt16; FUNCTION AEGetDescDataSize({CONST}VAR theAEDesc: AEDesc): Size; FUNCTION AEGetDescData({CONST}VAR theAEDesc: AEDesc; dataPtr: UNIV Ptr; maximumSize: Size): OSErr; PROCEDURE SetPortVisibleRegion(port: univ CGrafPtr; visRgn: RgnHandle); FUNCTION IsWindowVisible(window: WindowRef): BOOLEAN; {$endc} FUNCTION MyIsControlVisible(inControl: ControlRef): BOOLEAN; FUNCTION MyValidWindowRect(window: WindowRef; {CONST}VAR bounds: Rect): OSStatus; function MyGetRegionRect(theregion:RgnHandle):Rect; procedure GetDialogTextSelection(dlg:DialogPtr;var selStart,selEnd:integer); FUNCTION MyGetPortBounds(port: GrafPtr): Rect; {$ifc TARGET_API_MAC_CARBON} FUNCTION MyGetPortBitMapForCopyBits(port: CGrafPtr): BitMap; {$elsec} FUNCTION MyGetPortBitMapForCopyBits(port: GrafPtr): BitMap; {$endc} FUNCTION GetWindowSpareFlag(window: WindowPtr): BOOLEAN; {$ifc TARGET_API_MAC_CARBON} PROCEDURE IUDatePString(dateTime: LONGINT; longFlag: ByteParameter; VAR result: Str255; intlHandle: Handle); {$endc} implementation {$ifc TARGET_API_MAC_CARBON} FUNCTION HiWord(x: LONGINT): INTEGER; begin x := BSR(x, 16); HiWord := BitAnd(x, $FFFF); end; FUNCTION LoWord(x: LONGINT): INTEGER; begin LoWord := BitAnd(x, $FFFF); end; {$endc} {$ifc TARGET_API_MAC_CARBON} procedure invalRect(r:Rect); var err:OSErr; begin err := InvalWindowRect(GetWindowFromPort(qdThePort),r); end; procedure invalRgn(r:RgnHandle); var err:OSErr; begin err := InvalWindowRgn(GetWindowFromPort(qdThePort),r); end; {$endc} function GetScreenBounds:Rect; var theScreenBits:BitMap; ignore:BitMapPtr; begin {$ifc TARGET_API_MAC_CARBON} ignore := GetQDGlobalsScreenBits(theScreenBits); GetScreenBounds := theScreenBits.Bounds; {$elsec} GetScreenBounds := qd.ScreenBits.bounds; {$endc} end; {$ifc TARGET_API_MAC_CARBON} function qdThePort:CGrafPtr; begin qdThePort := GetQDGlobalsThePort; end; {$elsec} function qdThePort:GrafPtr; begin qdThePort := qd.thePort; end; {$endc} {$ifc not ACCESSOR_CALLS_ARE_FUNCTIONS} FUNCTION GetWindowPort(window: WindowPtr): GrafPtr; begin GetWindowPort := GrafPtr(window); end; FUNCTION GetDialogPort(dialog: DialogPtr): GrafPtr; begin GetDialogPort := GrafPtr(dialog); end; FUNCTION GetDialogWindow(dialog: DialogPtr): WindowPtr; begin GetDialogWindow := WindowPtr(dialog); end; FUNCTION GetMenuID(menu: MenuRef): MenuID; begin GetMenuID := menu^^.menuID; end; FUNCTION GetWindowFromPort(port: univ GrafPtr): WindowPtr; begin GetWindowFromPort := port; end; FUNCTION GetWindowKind(window: WindowPtr): INTEGER; begin GetWindowKind := windowPeek(window)^.windowkind end; FUNCTION IsMenuItemEnabled(menu: MenuRef; item: MenuItemIndex): BOOLEAN; begin IsMenuItemEnabled := BAND(BSR(menu^^.enableFlags, item), 1) <> 0 end; FUNCTION GetPortTextFont(port: GrafPtr): INTEGER; begin GetPortTextFont := port^.txFont; end; FUNCTION GetPortTextFace(port: GrafPtr): StyleField; begin GetPortTextFace := port^.txFace; end; FUNCTION GetPortTextMode(port: GrafPtr): INTEGER; begin GetPortTextMode := port^.txMode; end; FUNCTION GetPortTextSize(port: GrafPtr): INTEGER; begin GetPortTextSize := port^.txSize; end; FUNCTION GetDialogDefaultItem(dialog: DialogPtr): SInt16; begin GetDialogDefaultItem := DialogPeek(dialog)^.aDefItem; end; FUNCTION GetDialogKeyboardFocusItem(dialog: DialogPtr): SInt16; begin GetDialogKeyboardFocusItem := DialogPeek(dialog)^.editField; end; FUNCTION GetDialogTextEditHandle(dialog: DialogPtr): TEHandle; begin GetDialogTextEditHandle := DialogPeek(dialog)^.textH; end; FUNCTION GetMenuWidth(menu: MenuRef): SInt16; begin GetMenuWidth := menu^^.menuWidth end; FUNCTION GetMenuHeight(menu: MenuRef): SInt16; begin GetMenuHeight := menu^^.menuHeight end; FUNCTION GetWindowStandardState(window: WindowRef; VAR rect: Rect): RectPtr; var theWStateDataHandle:WStateDataHandle; begin theWStateDataHandle := WStateDataHandle(WindowPeek(window)^.dataHandle); rect := theWStateDataHandle^^.stdState; GetWindowStandardState := @rect; end; FUNCTION GetWindowUserState(window: WindowRef; VAR rect: Rect): RectPtr; var theWStateDataHandle:WStateDataHandle; begin theWStateDataHandle := WStateDataHandle(WindowPeek(window)^.dataHandle); rect := theWStateDataHandle^^.userState; GetWindowUserState := @rect; end; PROCEDURE SetWindowStandardState(window: WindowRef; {CONST}VAR rect: Rect); begin WStateDataHandle(WindowPeek(window)^.dataHandle)^^.stdState := rect end; PROCEDURE SetWindowUserState(window: WindowRef; {CONST}VAR rect: Rect); begin WStateDataHandle(WindowPeek(window)^.dataHandle)^^.userState := rect end; FUNCTION GetControlHilite(control: ControlRef): UInt16; begin GetControlHilite := control^^.contrlHilite end; FUNCTION AEGetDescDataSize({CONST}VAR theAEDesc: AEDesc): Size; begin AEGetDescDataSize := GetHandleSize(theAEDesc.dataHandle) end; FUNCTION AEGetDescData({CONST}VAR theAEDesc: AEDesc; dataPtr: UNIV Ptr; maximumSize: Size): OSErr; var theSize:longint; begin thesize := GetHandleSize(theAEDesc.dataHandle); if theSize > maximumSize then theSize := maximumSize; BlockMove(theAEDesc.datahandle^,dataPtr,theSize); AEGetDescData := noErr; end; PROCEDURE SetPortVisibleRegion(port: CGrafPtr; visRgn: RgnHandle); begin CopyRgn(visRgn, port^.visRgn); end; FUNCTION IsWindowVisible(window: WindowRef): BOOLEAN; begin IsWindowVisible := windowpeek(window)^.visible end; {$endc} FUNCTION MyIsControlVisible(inControl: ControlRef): BOOLEAN; begin { I do it this way to avoid any problems with multiple definition of} { IsControlVisible} {$ifc TARGET_API_MAC_CARBON} MyIsControlVisible := IsControlVisible(inControl); {$elsec} MyIsControlVisible := Boolean(inControl^^.contrlvis); {$endc} end; FUNCTION MyValidWindowRect(window: WindowRef; {CONST}VAR bounds: Rect): OSStatus; begin { I do it this way to avoid any problems with multiple definition of} { ValidWindowRect} {$ifc TARGET_API_MAC_CARBON} MyValidWindowRect := ValidWindowRect(window,bounds); {$elsec} { we really should switch the current graphics port to window} { but when we call ValidWindowRect we always have this set up correctly in my code} ValidRect(bounds); MyValidWindowRect := noErr; {$endc} end; function MyGetRegionRect(theregion:RgnHandle):Rect; var theRect:Rect; begin {$ifc TARGET_API_MAC_CARBON } MyGetRegionRect := GetRegionBounds(theregion,theRect)^; {$elsec} MyGetRegionRect := theregion^^.rgnBBox; {$endc} end; procedure GetDialogTextSelection(dlg:DialogPtr;var selStart,selEnd:integer); var theText:TEHandle; begin {$ifc TARGET_API_MAC_CARBON } theText := GetDialogTextEditHandle(dlg); selStart := theText^^.selStart; selEnd := theText^^.selEnd; {$elsec} selStart := DialogPeek(dlg)^.textH^^.selStart; selEnd := DialogPeek(dlg)^.textH^^.selEnd; {$endc} end; FUNCTION MyGetPortBounds(port: GrafPtr): Rect; var arect:Rect; ignore:RectPtr; begin {$ifc TARGET_API_MAC_CARBON } ignore := GetPortBounds(CGrafPtr(port),arect); MyGetPortBounds := arect; {$elsec} MyGetPortBounds := port^.portRect; {$endc} end; {$ifc TARGET_API_MAC_CARBON} FUNCTION MyGetPortBitMapForCopyBits(port: CGrafPtr): BitMap; begin MyGetPortBitMapForCopyBits := GetPortBitMapForCopyBits(port)^; end; {$elsec} FUNCTION MyGetPortBitMapForCopyBits(port: GrafPtr): BitMap; begin MyGetPortBitMapForCopyBits := port^.portBits; end; {$endc} FUNCTION GetWindowSpareFlag(window: WindowPtr): BOOLEAN; type pointPtr = ^point; var idealStandardState:Rect; BEGIN {$ifc TARGET_API_MAC_CARBON} GetWindowSpareFlag := IsWindowInStandardState(window,nil,@idealStandardState); {$elsec} GetWindowSpareFlag := WindowPeek(window)^.spareFlag {$endc} END; {$ifc TARGET_API_MAC_CARBON} PROCEDURE IUDatePString(dateTime: LONGINT; longFlag: ByteParameter; VAR result: Str255; intlHandle: Handle); begin DateString(dateTime, longFlag, result,intlHandle); end; {$endc} end.