ok I've just finished a sort of include for creating NSAlert sheets on the fly and a test code for testing its implementation.
File: NSAlert.pbi
Code: Select all
EnableExplicit
;--- Custom NSAlert Event Definition
Enumeration 1000
#NSAlert_Button_OK
#NSAlert_Button_Cancel
EndEnumeration
#NSAlertFirstButtonReturn = 1000
#NSAlertSecondButtonReturn = 1001
ImportC ""
sel_registerName(MethodName.S)
class_addMethod(Class.I, Selector.I, Implementation.I, Types.S)
EndImport
Define Alert.I
Define AppDelegate.I
Define Selector.I
Procedure.S ConvertToUTF8(String.S)
Protected UTF8String.S = Space(StringByteLength(String))
PokeS(@UTF8String, String, -1, #PB_UTF8)
ProcedureReturn UTF8String
EndProcedure
ProcedureC AlertCallback(Object.I, Selector.I, Alert.I, ButtonType.I, ContextInfo.I)
Select ButtonType
Case #NSAlertFirstButtonReturn
PostEvent(#NSAlert_Button_OK) ; Post the OK button event
Case #NSAlertSecondButtonReturn
PostEvent(#NSAlert_Button_Cancel) ; Post the Cancel button event
EndSelect
EndProcedure
Procedure NSAlertBeginSheet(ParentWindow.I, Message.S, InformativeText.S)
Shared Alert.I
Shared Selector.I
Shared AppDelegate.I
Alert = CocoaMessage(0, 0, "NSAlert new")
AppDelegate = CocoaMessage(0,CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Selector = sel_registerName(ConvertToUTF8("alert:returnCode:contextInfo"))
class_addMethod(CocoaMessage(0, AppDelegate, "class"), Selector, @AlertCallback(), ConvertToUTF8("v@:@@@"))
CocoaMessage(0, Alert, "setDelegate:", AppDelegate)
CocoaMessage(0, Alert, "addButtonWithTitle:$", @"OK")
CocoaMessage(0, Alert, "addButtonWithTitle:$", @"Cancel")
CocoaMessage(0, Alert, "setMessageText:$", @Message)
CocoaMessage(0, Alert, "setInformativeText:$", @InformativeText)
CocoaMessage(0, Alert,
"beginSheetModalForWindow:", WindowID(ParentWindow),
"modalDelegate:", AppDelegate,
"didEndSelector:", Selector,
"contextInfo:", 0)
EndProcedure
File: test.pb
Code: Select all
IncludeFile("NSAlert.pbi")
Define MessageID.I
OpenWindow(0, 240, 100, 380, 40, "AlertSheet Test")
ButtonGadget(0, 10, 10, 180, 25, "First message")
ButtonGadget(1, 190, 10, 180, 25, "Second message")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 0
;-Setting up the first alert
MessageID = 0
NSAlertBeginSheet(0, "First message", "First informative text")
Case 1
;-Setting up the second alert
MessageID = 1
NSAlertBeginSheet(0, "Second message", "Second informative text")
EndSelect
Case #NSAlert_Button_OK ;-Was the OK button pressed?
Select MessageID
Case 0
Debug "You answered OK to the first message"
Case 1
Debug "You answered OK to the second message"
EndSelect
Case #NSAlert_Button_Cancel ;-Was the Cancel button pressed?
Select MessageID
Case 0
Debug "You answered Cancel to the first message"
Case 1
Debug "You answered Cancel to the second message"
EndSelect
EndSelect
ForEver
Tell me what you think and feel free to enhance it...
