EventClose on Dock

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: EventClose on Dock

Post by Rinzwind »

Sorry, I was talking in the context of logout. It does work nicely from context menu close, but not from logout.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: EventClose on Dock

Post by Rinzwind »

And cannot use WaitEvent/WaitWindowEvent in the callback procedure to keep things responsive too...
WindowEvent() and WaitWindowEvent() can not be called from a 'binded' event callback.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: EventClose on Dock

Post by Rinzwind »

Seems that BindEvent keeps working. Good.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: EventClose on Dock

Post by Rinzwind »

Why appWillTerminate is not called in below case?

Code: Select all

EnableExplicit

Prototype Proto_AppNotification(Object, Selector, Sender)
DeclareC AppShouldTerminate(Object, Selector, Sender)
DeclareC AppWillTerminate(Object, Selector, Sender)

Global AppDelegate, AppShouldTerminate_.Proto_AppNotification, AppWillTerminate_.Proto_AppNotification

AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
AppShouldTerminate_ = class_replaceMethod_(CocoaMessage(0, AppDelegate, "class"),
                                           sel_registerName_("applicationShouldTerminate:"),
                                           @AppShouldTerminate(), "v@:@")
AppWillTerminate_ = class_replaceMethod_(CocoaMessage(0, AppDelegate, "class"),
                                         sel_registerName_("applicationWillTerminate:"),
                                         @AppWillTerminate(), "v@:@")
; class_addMethod_(CocoaMessage(0, AppDelegate, "class"),
;                                            sel_registerName_("applicationWillTerminate:"),
;                                            @AppWillTerminate(), "v@:@")

ProcedureC AppShouldTerminate(Object, Selector, Sender)
  Debug "AppShouldTerminate"
  If AppShouldTerminate_
    AppShouldTerminate_(Object, Selector, Sender)
  EndIf
  If MessageRequester("Confirmation", "Do you want to terminate the application?",
                      #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
    End
  Else
    ProcedureReturn #NO
  EndIf
EndProcedure

ProcedureC AppWillTerminate(Object, Selector, Sender)
  Debug "AppWillTerminate"
  End
EndProcedure

OpenWindow(0, 270, 100, 200, 170, "Test", #PB_Window_MinimizeGadget)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
And is there anything wrong with the way to do things like this instead or does this break stuff?:

Code: Select all

ProcedureC AppShouldTerminate(Object, Selector, Sender)

   Debug "AppShouldTerminate"

   ProcedureReturn #YES
EndProcedure

ProcedureC AppWillTerminate(Object, Selector, Sender)
  Debug "AppWillTerminate" 
  End
EndProcedure

w = OpenWindow(0, 0, 0, 300, 200, "Test")
CocoaMessage(@app, 0, "NSApplication sharedApplication")

myAppDelegateClass = objc_allocateClassPair_(objc_getClass_("NSObject"), "myAppDelegate", 0)
class_addProtocol_(myAppDelegateClass, objc_getProtocol_("NSApplicationDelegate"))

class_addMethod_(myAppDelegateClass, sel_registerName_("applicationShouldTerminate:"), @appShouldTerminate(), "L@:@")
class_addMethod_(myAppDelegateClass, sel_registerName_("applicationWillTerminate:"), @appWillTerminate(), "v@:@")

objc_registerClassPair_(myAppDelegateClass)
myAppDelegate = class_createInstance_(myAppDelegateClass, 0)
CocoaMessage(0, app, "setDelegate:", myAppDelegate)
Repeat
  e = WaitWindowEvent(10)
Until e = #PB_Event_CloseWindow
All code from elsewhere in forum slightly adjusted to fit the case.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: EventClose on Dock

Post by wilbert »

Rinzwind wrote:Why appWillTerminate is not called in below case?
When you place End within the AppShouldTerminate procedure, it already ends the application so AppWillTerminate will never be called.
For some reason when I remove End, it still isn't being called. I have no clue why :?
Rinzwind wrote:And is there anything wrong with the way to do things like this instead or does this break stuff?:
Yes, this is wrong. Creating you own app delegate is only safe when you create everything yourself (including windows, event loop etc.).
PureBasic already uses an app delegate so if you replace it like this with your own app delegate, it will break things.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply