Example sheet NSAlert with options

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Example sheet NSAlert with options

Post by Wolfram »

Code: Select all


; NSBlock module by Wilbert  July 17, 2015

DeclareModule NSBlock
  
  Structure NSBlock
    *isa
    flags.l
    reserved.l
    *invoke
    *descriptor
  EndStructure
  
  Structure NSBlockWithPtr
    *block.NSBlock
    _block.NSBlock
  EndStructure
  
  Declare InitNSBlockWithPtr(*NSBlockWithPtr.NSBlockWithPtr, *Invoke, IsGlobal = #True)
  
EndDeclareModule

Module NSBlock
  
  Global.i NSConcreteGlobalBlock, NSConcreteStackBlock, DyLib
  
  DyLib = OpenLibrary(#PB_Any, "libSystem.dylib")
  If DyLib
    NSConcreteGlobalBlock = GetFunction(DyLib, "_NSConcreteGlobalBlock")
    NSConcreteStackBlock = GetFunction(DyLib, "_NSConcreteStackBlock")    
    CloseLibrary(DyLib)
  EndIf
  
  If Not NSConcreteGlobalBlock
    MessageRequester("Error", "Unable to access _NSConcreteGlobalBlock symbol")
    End
  EndIf
  
  DataSection
    NSBlockDescriptor:
    Data.i 0, SizeOf(NSBlock), 0, 0
  EndDataSection
  
  Procedure InitNSBlockWithPtr(*NSBlockWithPtr.NSBlockWithPtr, *Invoke, IsGlobal = #True)
    *NSBlockWithPtr\block = @*NSBlockWithPtr\_block
    With *NSBlockWithPtr\_block
      If IsGlobal
        \isa = NSConcreteGlobalBlock
        \flags = $30000000
      Else
        \isa = NSConcreteStackBlock
        \flags = $20000000
      EndIf
      \invoke = *Invoke
      \descriptor = ?NSBlockDescriptor
    EndWith
  EndProcedure
  
EndModule

; *** End of NSBlock module ***


UseModule NSBlock

#NSCriticalAlertStyle = 2
#NSInformationalAlertStyle = 1 
#NSWarningAlertStyle = 0

Enumeration NSAlertButton
  #firstButton =1000
  #secondButton
  #thirdButton
EndEnumeration

#NSShiftKeyMask      = 1 << 17
#NSControlKeyMask    = 1 << 18
#NSAlternateKeyMask  = 1 << 19
#NSCommandKeyMask    = 1 << 20

Global MyBlockWithPtr.NSBlockWithPtr, NSAlert

;//App Preferences
Global defaults = CocoaMessage(0, 0, "NSUserDefaults standardUserDefaults")


;//Custom Icon for Alert
Global myIcon = CreateImage(#PB_Any, 64, 64, 32, $FFF26A0B)

Global mainWindow, strGadget, button

ProcedureC ModalSheetCallback(*Block.NSBlock, returnCode)
 
  suppressionButton = CocoaMessage(0, NSAlert, "suppressionButton")
  If CocoaMessage(0, suppressionButton, "state")
    CocoaMessage(0, defaults, "setBool:", #YES, "forKey:$", @"alertSuppressionKey")
  EndIf
  

  Select returnCode
    Case #firstButton
      Debug "OK"
    Case #secondButton
      Debug "Not OK"
    Case #thirdButton
      Debug "its coold"
  EndSelect
  
  If IsGadget(strGadget)
    FreeGadget(strGadget)
  EndIf
  
EndProcedure


InitNSBlockWithPtr(@MyBlockWithPtr, @ModalSheetCallback())



Procedure sheetAlert(message.s = "", informativeText.s= "", hasAccessory = #NO, icon = #Null)
  
  If CocoaMessage(0, defaults, "boolForKey:$", @"alertSuppressionKey") = #NO
    NSAlert = CocoaMessage(0,CocoaMessage(0, 0, "NSAlert alloc"), "init")
    
    CocoaMessage(0, NSAlert, "setMessageText:$", @message)
    CocoaMessage(0, NSAlert, "setInformativeText:$", @informativeText)
    
    
    defaultButton =CocoaMessage(0, NSAlert, "addButtonWithTitle:$", @"OK")
    
    noButton =CocoaMessage(0, NSAlert, "addButtonWithTitle:$", @"not OK")
    CocoaMessage(0, noButton, "setKeyEquivalent:$", @"n")
    CocoaMessage(0, noButton, "setKeyEquivalentModifierMask:", #NSCommandKeyMask)
    
    iButton =CocoaMessage(0, NSAlert, "addButtonWithTitle:$", @"I'm freezing")
    CocoaMessage(0, iButton, "setKeyEquivalent:$", @"i")
    CocoaMessage(0, iButton, "setKeyEquivalentModifierMask:", #NSCommandKeyMask)
    
;     CocoaMessage(0, NSAlert, "setDelegate:", NSAlert)
    
    CocoaMessage(0, NSAlert, "setAlertStyle:", #NSInformationalAlertStyle)
    
    If icon
      CocoaMessage(0, NSAlert, "setIcon:", ImageID(icon))
    EndIf
    
    
    
    
    CocoaMessage(0, NSAlert, "setShowsSuppressionButton:", #YES)
    suppressionButton = CocoaMessage(0, NSAlert, "suppressionButton")
    CocoaMessage(0, suppressionButton, "setTitle:$", @"Do Not show this warning again")
    
    
    If hasAccessory
      strGadget = StringGadget(#PB_Any, 20, 68, 327, 20, "...you answare")
      
      strGadgetID = GadgetID(strGadget)
      CocoaMessage(0, NSAlert, "setAccessoryView:", strGadgetID)
      
      
      CocoaMessage(0, strGadgetID, "currentEditor")
      
      ;//Set defaultButton action while StringGadget has focus
      CocoaMessage(0, strGadgetID, "setTarget:", defaultButton)
      CocoaMessage(0, strGadgetID, "setAction:", sel_registerName_("performClick:"))
      
      CocoaMessage(0, NSAlert, "layout")
      
      CocoaMessage(0, strGadgetID, "becomeFirstResponder")
;     selectText = CocoaMessage(0, strGadgetID, "selectText:")
      
    EndIf
    
    CocoaMessage(0, NSAlert, "beginSheetModalForWindow:", WindowID(mainWindow), "completionHandler:@", @MyBlockWithPtr)
  EndIf
EndProcedure


Procedure mainWindowEvents(event)
  
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
      
    Case #PB_Event_Menu
      Select EventMenu()
          
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case button
          sheetAlert("My Alert", "Global warming", #YES, myIcon)
          
        Case strGadget
          If EventType() = #PB_EventType_Change
            Debug GetGadgetText(strGadget)
          EndIf
          
      EndSelect
  EndSelect
  
  ProcedureReturn #True
EndProcedure




mainWindow = OpenWindow(#PB_Any, 0, 0, 400, 120, "MAIN WINDOW", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
button = ButtonGadget(#PB_Any, 160, 30, 80, 25, "show")


  ;//just to rest the default value for alertSuppressionKey
  ;CocoaMessage(0, defaults, "setBool:", #NO, "forKey:$", @"alertSuppressionKey")
      


Repeat
  event = WaitWindowEvent()
 
Until mainWindowEvents(event) = #False
macOS Catalina 10.15.7