Waitwindowevent - catch user messages?

Just starting out? Need help? Post your questions and find answers here.
Osmdesat
New User
New User
Posts: 7
Joined: Wed Mar 28, 2018 6:13 pm

Waitwindowevent - catch user messages?

Post by Osmdesat »

Is it possible to catch user window messages using WaitWindowEvent function, i.e. messages with uMsg >= WM_USER posted to my window? In the list of possible events is no such kind of event.
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Waitwindowevent - catch user messages?

Post by RSBasic »

This is possible with SetWindowCallback(), if that's what you mean.
Image
Image
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Waitwindowevent - catch user messages?

Post by TI-994A »

Osmdesat wrote:Is it possible to catch user window messages using WaitWindowEvent function, i.e. messages with uMsg >= WM_USER posted to my window?
As RSBasic had pointed out, you'd have to implement a callback to intercept messages that are not supported by PureBasic natively; and that too would provide only an OS-specific solution. However, you might also consider using PureBasic's very own PostEvent() function, which is cross-platform:

https://www.purebasic.com/documentation ... event.html
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Osmdesat
New User
New User
Posts: 7
Joined: Wed Mar 28, 2018 6:13 pm

Re: Waitwindowevent - catch user messages?

Post by Osmdesat »

Thanks guys. Custom callback is what I wanted to avoid - that's why I asked my question. But PostEvent() with custom Event will work pretty well for me (:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Waitwindowevent - catch user messages?

Post by Dude »

Why is a callback needed anyway? It just seems weird that we have to have two separate sections of code to process all event messages. Is there some technical reason, or is it simply to keep the cross-platform messages separate from Windows-specific messages? I assume the answer is the latter reason, because PureBasic used to accept all Windows-specific messages in the past from WaitWindowEvent().
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Waitwindowevent - catch user messages?

Post by mk-soft »

It is only possible in callback to the message with another return value
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Osmdesat
New User
New User
Posts: 7
Joined: Wed Mar 28, 2018 6:13 pm

Re: Waitwindowevent - catch user messages?

Post by Osmdesat »

@Dude
Callback is not needed. I wrote I wanted to avoid it, if possible. I totally agree that it would be nonconsistent to have callback and WaitWindowEvent section.
My aim is to post a custom message from a thread to the main message loop, so that program knows that the thread has finished. I found no better way how to do it in non-blocking manner.
But PostEvent() function solves it without need of callback - there is even a thread message example there in documentation.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Waitwindowevent - catch user messages?

Post by Dude »

Osmdesat wrote:PostEvent() function solves it without need of callback
Maybe I'm misunderstanding, but how can PostEvent() let my app know that a #WM_NOTIFY message has occurred? AFAIK, only a callback can detect that sort of thing.
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Waitwindowevent - catch user messages?

Post by mk-soft »

Only over SetWindowCallback...

Here find some examples

Google 'WM_NOTIFY site:purebasic.fr'
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Osmdesat
New User
New User
Posts: 7
Joined: Wed Mar 28, 2018 6:13 pm

Re: Waitwindowevent - catch user messages?

Post by Osmdesat »

Why WM_NOTIFY? Originally I wanted to send WM_USER using PostMessage_ function.
But if it's possible to send #PB_Event_FirstCustomValue using PostEvent() function and WaitWindowEvent() will catch it, I can do it this way.
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Waitwindowevent - catch user messages?

Post by mk-soft »

Sorry,
I had misunderstood...

Small trick with SendEvent...

Code: Select all

;-TOP

; ***************************************************************************************

;-Begin Of SendEvent

; Comment : SendEvent
; Author  : mk-soft
; Version : v1.06
; Create  : unknown
; Update  : 07.08.2016

;- Structure
Structure udtSendEvent
  Signal.i
  Result.i
  *pData
EndStructure

; ---------------------------------------------------------------------------------------

Procedure SendEvent(Event, Window = 0, Object = 0, EventType = 0, pData = 0, Semaphore = 0)
  
  Protected MyEvent.udtSendEvent, result
  
  With MyEvent
    If Semaphore
      \Signal = Semaphore
    Else
      \Signal = CreateSemaphore()
    EndIf
    \pData = pData
    PostEvent(Event, Window, Object, EventType, @MyEvent)
    WaitSemaphore(\Signal)
    result = \Result
    If Semaphore = 0
      FreeSemaphore(\Signal)
    EndIf
  EndWith
  
  ProcedureReturn result
  
EndProcedure

; ---------------------------------------------------------------------------------------

Procedure SendEventData(*MyEvent.udtSendEvent)
  ProcedureReturn *MyEvent\pData
EndProcedure

; ---------------------------------------------------------------------------------------

Procedure DispatchEvent(*MyEvent.udtSendEvent, result)
  *MyEvent\Result = result
  SignalSemaphore(*MyEvent\Signal)
EndProcedure

;- End Of SendEvent

; ***************************************************************************************

;- Example

CompilerIf #PB_Compiler_IsMainFile
  
  EnableExplicit
  
  ; ***************************************************************************************
  
  ;- Memory string helper
  
  Procedure AllocateString(String.s)
    Protected *mem.string
    *mem = AllocateStructure(string)
    If *mem
      *mem\s = String
    EndIf
    ProcedureReturn *mem
  EndProcedure
  
  ; ---------------------------------------------------------------------------------------
  
  Procedure.s FreeString(*mem.string)
    Protected result.s
    If *mem
      result = *mem\s
      FreeStructure(*mem)
    EndIf
    ProcedureReturn result
  EndProcedure
  
  ; ***************************************************************************************

  Enumeration
    #Window
  EndEnumeration
  
  ;- Constants
  Enumeration #PB_Event_FirstCustomValue
    #My_Event_Messagebox
    #My_Event_Inputbox
  EndEnumeration
  
  Procedure Thread1(Null)
    
    Debug "Init Thread 1"
    Protected result
    
    Repeat
      Delay(500)
      result = SendEvent(#My_Event_Messagebox, 0, 0, 0, Random(100))
      Select result
        Case #PB_MessageRequester_Yes
          Debug "#PB_MessageRequester_Yes"
        Case #PB_MessageRequester_No
          Debug "#PB_MessageRequester_No"
        Case #PB_MessageRequester_Cancel
          Debug "#PB_MessageRequester_Cancel"
      EndSelect
    Until result = #PB_MessageRequester_Cancel
    
    Debug "Exit Thread 1"
    
  EndProcedure
  
  Procedure Thread2(Null)
    
    Debug "Init Thread 2"
    Protected result, text.s
    
    Repeat
      Delay(500)
      result = SendEvent(#My_Event_Inputbox, 0, 0, 0, Random(100))
      text = FreeString(result)
      Debug text
    Until text = ""
    
    Debug "Exit Thread 2"
    
  EndProcedure
  
  Procedure Main()
    Protected exit, th1, th2
    Protected MyEvent, value, result, text.s 
    
    If OpenWindow(#Window, 0, 0, 800, 600, "WindowTitle", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
      th1 = CreateThread(@Thread1(), #Null)
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Break
            
          Case #PB_Event_Gadget
            
          Case #My_Event_Messagebox
            MyEvent = EventData()
            value = SendEventData(MyEvent)
            result = MessageRequester("Thread 1", "What happens next?" + #LF$ + "(Code " + value + ")", #PB_MessageRequester_YesNoCancel)
            DispatchEvent(MyEvent, result)
            If result = #PB_MessageRequester_Cancel
              th2 = CreateThread(@Thread2(), #Null)
            EndIf
            
          Case #My_Event_Inputbox
            MyEvent = EventData()
            text = InputRequester("Thread 2", "Name:", "")
            result = AllocateString(text)
            DispatchEvent(MyEvent, result)
            
        EndSelect
      ForEver
      
      If IsThread(th1)
        KillThread(th1)
      EndIf
      If IsThread(th2)
        KillThread(th2)
      EndIf
      
    EndIf
  EndProcedure : Main()
  
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Osmdesat
New User
New User
Posts: 7
Joined: Wed Mar 28, 2018 6:13 pm

Re: Waitwindowevent - catch user messages?

Post by Osmdesat »

Thanks mk-soft.
Post Reply