Re: [PB Cocoa] Methods, Tips & Tricks
Posted: Sat Apr 06, 2013 8:29 pm
Display an alert (MessageRequester with OK button) specifying a predefined icon:
Update 1: I have added the ReleaseIconRef() function to properly decrease the icon reference count after displaying the requester and thus closing a memory leak.
Update 2: Wilbert sent me a PM with a simplified solution which doesn't need anymore to import the outdated Carbon functions. Thank you for that nice hint, Wilbert!
Code: Select all
EnableExplicit
#kOnSystemDisk = -32768
#kSystemIconsCreator = $6D616373
; ----- Predefined alert icons
#kGenericApplicationIcon = $4150504C ; 'APPL'
#kAlertCautionIcon = $63617574 ; 'caut'
#kAlertNoteIcon = $6E6F7465 ; 'note'
#kAlertStopIcon = $73746F70 ; 'stop'
ImportC ""
GetIconRef(StartVolume.W, Creator.L, IconID.L, *IconRef)
ReleaseIconRef(IconRef.I)
EndImport
Procedure MessageRequesterEx(Title.S, Info.S, IconID.L)
Protected Alert.I
Protected IconRef.L
Protected Image.I
Alert = CocoaMessage(0, CocoaMessage(0, 0, "NSAlert new"), "autorelease")
CocoaMessage(0, Alert, "setMessageText:$", @Title)
CocoaMessage(0, Alert, "setInformativeText:$", @Info)
If GetIconRef(#kOnSystemDisk, #kSystemIconsCreator, IconID, @IconRef) = 0
Image = CocoaMessage(0, 0, "NSImage alloc")
CocoaMessage(0, Image, "initWithIconRef:", IconRef)
CocoaMessage(0, Alert, "setIcon:@", @Image)
CocoaMessage(0, Alert, "runModal")
CocoaMessage(0, Image, "release")
ReleaseIconRef(IconRef)
EndIf
EndProcedure
MessageRequesterEx("Icon demo 1", "Requester with app icon", #kGenericApplicationIcon)
MessageRequesterEx("Icon demo 2", "Requester with caution icon", #kAlertCautionIcon)
MessageRequesterEx("Icon demo 3", "Requester with note icon", #kAlertNoteIcon)
MessageRequesterEx("Icon demo 4", "Requester with stop icon", #kAlertStopIcon)
Update 2: Wilbert sent me a PM with a simplified solution which doesn't need anymore to import the outdated Carbon functions. Thank you for that nice hint, Wilbert!
Code: Select all
EnableExplicit
Global Workspace.i = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
Procedure MessageRequesterEx(Title.s, Info.s, Type.s)
Protected Alert.i = CocoaMessage(0, CocoaMessage(0, 0, "NSAlert new"), "autorelease")
CocoaMessage(0, Alert, "setMessageText:$", @Title)
CocoaMessage(0, Alert, "setInformativeText:$", @Info)
CocoaMessage(0, Alert, "setIcon:", CocoaMessage(0, Workspace, "iconForFileType:$", @Type))
CocoaMessage(0, Alert, "runModal")
EndProcedure
MessageRequesterEx("Icon demo 1", "Requester with app icon", "'APPL'")
MessageRequesterEx("Icon demo 2", "Requester with caution icon", "'caut'")
MessageRequesterEx("Icon demo 3", "Requester with note icon", "'note'")
MessageRequesterEx("Icon demo 4", "Requester with stop icon", "'stop'")