Advice on Binding Custom Events

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Advice on Binding Custom Events

Post by davido »

Could someone, please, explain how to Bind Custom Events.
I've read the manual but I don't fully understand what to enter in the optional entries.

Thank you in advance.
DE AA EB
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

Here's one I've slapped together for demonstrating the custom event binding.

Code: Select all

#Window = 0
#My_Custom_Event = 10

Procedure SayHello()
  Debug "My Custom event on window #" + EventWindow()
  Debug "Hello"  
EndProcedure

OpenWindow(#Window, 100, 100, 200, 200, "Binding To custom event", #PB_Window_SystemMenu)
EditorGadget(0, 10, 10, 180, 180)

BindEvent(#My_Custom_Event, @SayHello())

AddWindowTimer(#Window, 1, 2000)

Repeater.b = 3

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Timer And EventTimer() = 1
    If Repeater
      Repeater-1
      PostEvent(#My_Custom_Event)
    Else
      RemoveWindowTimer(#Window, 1) : Debug " ~~~~ FIN ~~~~~~"
    EndIf  
  EndIf
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Advice on Binding Custom Events

Post by Demivec »

@Thunder93: Your example doesn't address his question about the optional parameters.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

True enough.. I'm not trying to be funny here, however I don't know how to explain It any better than what's already in the PB Help file.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

Code: Select all

BindEvent(Event, @Callback() [, Window [, Object [, EventType]]])
Event or our custom event in this case and the Callback procedure is mandatory fields.

Window Parameter is the Window identifier number. Optional Field unless you wanting to use Object field and also EvenType parameter. The Window parameter could use #PB_All to be used to apply for all running windows.

Object Parameter is is the gadget, menu-item or systray identifier number. Optional Field unless wanting to use EventType parameter. #PB_All to be used to apply for all gadget, menu-item or systray objects.

EventType Parameter to bind to specific event type like (e.g: #PB_EventType_LeftClick, #PB_EventType_Focus.) #PB_All to be used to apply to all event types.

... clear as mud? :mrgreen:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Advice on Binding Custom Events

Post by nco2k »

@Thunder93
wow such a horrible example. your custom event overlapps with WM_ENABLE. custom events have to start from #PB_Event_FirstCustomValue!

@davido
well, custom events are custom events. you enter whatever you want/need to enter. it depends on your application and what you are trying to achieve. its just a way of communicating within your application. if you dont need the object and type parameters, its good practice to simply use -1, since that value is outside of purebasics range.

Code: Select all

Enumeration
  #Window_0 = 0
  #Event_0 = #PB_Event_FirstCustomValue
  #Event_1
EndEnumeration

OpenWindow(#Window_0, #PB_Ignore, #PB_Ignore, 400, 300, "Test", #PB_Window_SystemMenu)
PostEvent(#Event_0, #Window_0, 123, 456, 789)
PostEvent(#Event_1, #Window_0, -1, -1, 123456789)

Repeat
  Select WaitWindowEvent()
    Case #Event_0
      Debug "Event_0:"
      Debug "EventGadget: "+Str(EventGadget())
      Debug "EventType: "+Str(EventType())
      Debug "EventData: "+Str(EventData())
      Debug ""
    Case #Event_1
      Debug "Event_1:"
      Debug "EventGadget: "+Str(EventGadget())
      Debug "EventType: "+Str(EventType())
      Debug "EventData: "+Str(EventData())
      Debug ""
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

If you wanting to be technical.. I'm sure he knows better, heck I even know better. The way to do the custom event wasn't important, it wasn't what the focus for my demonstration. Also in the future, don't be so crude. There is more friendly manner in pointing out incompleteness and such than the way you just done.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Advice on Binding Custom Events

Post by nco2k »

Thunder93 wrote:If you wanting to be technical.. I'm sure he knows better, heck I even know better. The way to do the custom event wasn't important, it wasn't what the focus for my demonstration.
it doesnt matter. a beginner searches this forum on how to use custom events, finds this thread and copy+pastes the first code he can find. you shouldnt post codes with such basic mistakes, because they set up a bad example for beginners.
Thunder93 wrote:Also in the future, don't be so crude. There is more friendly manner in pointing out incompleteness and such than the way you just done.
the world isnt a big liberal arts college. if you got offended by what i just said, well.. sucks to be you then. :P

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

You worrying about how code examples should look.. to your standards and assumptions. However you should also be worrying about the manner in which you communicating on here. You setting crude example, and perhaps beginners seeing this thinks this is normal way on PureBasic forums. However to be perfectly honest, you cannot do no harm in being little more respectable with your communications on here. This is often why people are in a feud on PureBasic board.

I've stepped in, because I've seen no responses to this person's post. Trying to be considered, and I get this annoyance from a person.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Advice on Binding Custom Events

Post by Demivec »

@nco2k: Thanks for the example you posted. Turnabout is fair play though; to use a custom event type you should start enumerating with #PB_EventType_FirstCustomValue. :mrgreen:

@Thunder93: Thanks for your example. It also gave an opportunity to mention as a side note of how to come up with the custom event number itself.


@davido: Here's some odds and ends points from my point of view.

For BindEvent(), the syntax is BindEvent(Event, @Callback() [, Window [, Object [, EventType]]]) .

By specifying a window, object, or eventtype you can finetune the callback to handle more specific situations, or group them to be handled in a common way even when they may seem different. You can use different BindEvent()'s that all use the same callback. This means you can use a BindEvent() that handles an event for everything from a given window and another BindEvent() specifying the same callback so that it also handles the event of a specific object and also eventype from a separate window.

For the optional parameters you can specify #PB_All, but if you do that all of the parameters after that one have to also be #PB_All. I think the default for leaving the optional parameters unspecified is that they each equal #PB_All.

If a callback handles an event (or events) from more than one source it can use EventWindow(), EventGadget(), EventType(), EventMenu(), EventTimer(), and Event() (as of v5.60) to help sort out things a bit. This is highly likely if you use #PB_All in the BindEvent() for any of the optional parameters or you use more than one BindEvent() with the same callback.

For PostEvent(), the syntax is PostEvent(Event [, Window, Object [, Type [, Data]]]) .

The important thing to note here is that any custom events or custom types be first enumerated starting with #PB_Event_FirstCustomValue, or #PB_EventType_FirstCustomValue respectively. You can also use values that PureBasic already defines but these won't be posted to the operating systems event loop, just PureBasic's event loop. This means you can't trigger the operating system to do something by posting a custom event.

You have the option to add a data value which is an integer and more importantly can also be a pointer to a larger structure or object. It's value can be retrieved with EventData() in your callback procedure. If you use the data parameter but not the other preceding optional parameters then settle on an unused value such as -1 (as demonstrated by nco2k.).
Last edited by Demivec on Wed Sep 27, 2017 12:24 am, edited 1 time in total.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

@Demivec: Nicely put. :wink:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Advice on Binding Custom Events

Post by nco2k »

@Demivec
in my defense, #PB_EventType_FirstCustomValue was added in a later version :D but you dont need to use it for your own custom messages (non-pb objects) because they wont produce any #PB_* events and therefor cant really clash with purebasics internals. PostEvent() + Type/Data is basically just PostMessage_() + wParam/lParam. so you can use whatever you want/need. but yes, if you plan on extending purebasic object events, then you should use it. #PB_Event_FirstCustomValue is mandatory though, cause it could clash with windows itself. :)

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Advice on Binding Custom Events

Post by Demivec »

nco2k wrote:in my defense, #PB_EventType_FirstCustomValue was added in a later version :D but you dont need to use it for your own custom messages (non-pb objects) because they wont produce any #PB_* events and therefor cant really clash with purebasics internals. PostEvent() + Type/Data is basically just PostMessage_() + wParam/lParam. so you can use whatever you want/need. but yes, if you plan on extending purebasic object events, then you should use it. #PB_Event_FirstCustomValue is mandatory though, cause it could clash with windows itself. :)
Thanks for the additional explanation. :)

I hadn't noticed it either until just today.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

nco2k wrote:@Thunder93
wow such a horrible example. your custom event overlapps with WM_ENABLE. custom events have to start from #PB_Event_FirstCustomValue!
Where's the overlaps / clashing that makes my earlier demonstration horrible example?

Code: Select all

#My_Custom_Event = 10

Procedure WinCallback(hWnd, uMsg, wParam, lParam) 
  
  If uMsg = #WM_ENABLE
    If wParam
      Debug "Window Enabled (#WM_ENABLE message) ID: "+Str(uMsg)
    Else
      Debug "Window Disabled (#WM_ENABLE message) ID: "+Str(uMsg)
    EndIf
  EndIf
  
  ProcedureReturn #PB_ProcessPureBasicEvents 
EndProcedure

Procedure SayHello()
  Debug "Hello...!  My Custom event on window #" + EventWindow() 
EndProcedure

OpenWindow(0, 100, 100, 200, 200, "Binding to custom event", #PB_Window_SizeGadget | #PB_Window_SystemMenu)

SetWindowCallback(@WinCallback())

BindEvent(#My_Custom_Event, @SayHello())

Debug "Disabling Window"
DisableWindow(0, #True)

Delay(1000)
Debug #LF$+"Enabling Window"
DisableWindow(0, #False)

Debug #LF$+"Performing PostEvent() - #1"
PostEvent(#My_Custom_Event)

Debug #LF$+"Performing PostEvent() - #2"
PostEvent(#My_Custom_Event)

Debug #LF$+"Performing PostEvent() - #3"
PostEvent(#My_Custom_Event)

Debug #LF$+"Performing PostEvent() - #4"
PostEvent(#My_Custom_Event)


Repeat
  Event = WaitWindowEvent()  
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Advice on Binding Custom Events

Post by Thunder93 »

Demivec wrote:For the optional parameters you can specify #PB_All, but if you do that all of the parameters after that one have to also be #PB_All. I think the default for leaving the optional parameters unspecified is that they each equal #PB_All.
Exactly how you think.. :)

Code: Select all

Enumeration Windows
  #Window_1 = 0
  #Window_2
EndEnumeration

Enumeration #PB_Event_FirstCustomValue
  #My_Custom_Event
EndEnumeration

Procedure SayHello()
  Debug "Hello!... My Custom event on window #" + EventWindow()
EndProcedure

OpenWindow(#Window_1, 100, 100, 200, 200, "Binding to custom event", #PB_Window_SystemMenu)

OpenWindow(#Window_2, 302, 100, 200, 200, "Binding to custom event", #PB_Window_SystemMenu)

BindEvent(#My_Custom_Event, @SayHello())

PostEvent(#My_Custom_Event, #Window_1, #PB_Any)
PostEvent(#My_Custom_Event, #Window_2, #PB_Any)


Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply