Processing WM_SYSCOMMAND without callback?

Windows specific forum
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Processing WM_SYSCOMMAND without callback?

Post by bbanelli »

Greetings to all,

here's the code:

Code: Select all

Procedure WinProc(hWnd, msg, wParam, lParam) 
  Protected.i result
  result = #PB_ProcessPureBasicEvents 
  If msg = #WM_SYSCOMMAND
    If wparam = #SC_MINIMIZE
      Debug "Minimized"
      result = 0
    ElseIf wParam = #SC_CLOSE
      Debug "Closed"
      result = 0
    ElseIf wparam = #SC_RESTORE
      Debug "Restored"
      result = 0
    EndIf 
  EndIf 
  ProcedureReturn result 
EndProcedure 

If OpenWindow(0, 0, 0, 320, 240, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
  ;SetWindowCallback(@WinProc())
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
      Case #WM_SYSCOMMAND
        Select EventwParam()
          Case #SC_CLOSE
            Debug "Close"
          Case #SC_MINIMIZE
            Debug "Minimize"
          Case #SC_RESTORE
            Debug "Restore"
        EndSelect
    EndSelect
    
  Until Event = #PB_Event_CloseWindow
EndIf
Unfortunately, only triggered event appears to be SC_RESTORE. All others are not reported in the loop. Naturally, callback works perfectly, but I was wandering, what's wrong with the approach in WaitWindowEvent()?

With my best,

Bruno
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Processing WM_SYSCOMMAND without callback?

Post by luis »

The fact WindowEvent() is not legally bound to return Windows messages.
Can replace them with corresponding #PB events and return those instead, can ignore some of them, etc.
It has only to return the documented #PB events, for everything else OS specific you have to use a callback.

Also it may return some Windows messages today, and stop to do it tomorrow -> http://www.purebasic.fr/english/viewtop ... 13&t=54124
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by bbanelli »

luis wrote:The fact WindowEvent() is not legally bound to return Windows messages.
Can replace them with corresponding #PB events and return those instead, can ignore some of them, etc.
It has only to return the documented #PB events, for everything else OS specific you have to use a callback.

Also it may return some Windows messages today, and stop to do it tomorrow -> http://www.purebasic.fr/english/viewtop ... 13&t=54124
Thank you, luis, for, as usual, precise and informative answers! :)

I can only guess that there is no mutual exclusivity in using callback and WaitWindowEvent(), that is, there should be no issues there if both are used (for different purposes, naturally!)? Basically, callback should be "the right way", right?

BTW, I was really not aware that Eventl/w were deprecated functions. Why are they not reported by compiler as CreateGadgetList(), for example?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Processing WM_SYSCOMMAND without callback?

Post by luis »

bbanelli wrote: I can only guess that there is no mutual exclusivity in using callback and WaitWindowEvent(), that is, there should be no issues there if both are used (for different purposes, naturally!)? Basically, callback should be "the right way", right?
You can mix them. You can process the #PB events in the main loop, and the extra OS specific events in the callback.
In the callback you can also "transform" the Windows messages in your own custom user messages and PostEvent them in the original main loop to keep all the event processing there in one place, if you like.
bbanelli wrote: BTW, I was really not aware that Eventl/w were deprecated functions. Why are they not reported by compiler as CreateGadgetList(), for example?
I don't know :?
"Have you tried turning it off and on again ?"
A little PureBasic review
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Processing WM_SYSCOMMAND without callback?

Post by Little John »

bbanelli wrote:BTW, I was really not aware that Eventl/w were deprecated functions. Why are they not reported by compiler as CreateGadgetList(), for example?
I also don't know why they are not reported by the compiler.
However, it is documented:
Help for EventwParam() wrote:This function is not supported anymore and shouldn't used in new project. Use a callback to get full control over Windows message with SetWindowCallback().
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by TI-994A »

bbanelli wrote:...I can only guess that there is no mutual exclusivity in using callback and WaitWindowEvent(), that is, there should be no issues there if both are used (for different purposes, naturally!)? Basically, callback should be "the right way", right?
Hi bbanelli. Yes; you can safely use them both together, but you can't use callbacks exclusively without the PureBasic event loop. Callbacks are only meant as handlers of additional, low-level messages.
bbanelli wrote:...I was really not aware that Eventl/w were deprecated functions. Why are they not reported by compiler as CreateGadgetList(), for example?
Perhaps because they were never official functions to begin with (according to Fred).
Fred wrote:...these two commands aren't 'officials' but it would probably hurt if we remove it as we can't replace it with something else. So it's still here..
I read somewhere (can't seem to find that post though) that this function pair was originally implemented to assist the development team, but their use unintentionally proliferated among the early adopters.
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
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by bbanelli »

Hi guys,

thanks for the heads up about the functions and callback! :) It's all much clearer now. While we are at it, I guess it is only legal to post events to call for requesters from callback, right? Just the way it is supposed to be done from threads?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by TI-994A »

bbanelli wrote:... it is only legal to post events to call for requesters from callback, right?
Hello again. It should be pretty safe to implement requesters from just about anywhere within the main thread; and that includes procedures and callbacks:

Code: Select all

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  If uMsg = #WM_LBUTTONDOWN
    Debug (OpenFileRequester("Opened from Callback:", "", "", 0))
  EndIf
  ProcedureReturn result
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
OpenWindow(0, #PB_Any, #PB_Any, 300, 150, "Requesters in Callbacks", wFlags)
SetWindowCallback(@WndProc())
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
The only known exception is calling them from threads.

Please, correct me if I'm wrong.
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
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by bbanelli »

TI-994A wrote:The only known exception is calling them from threads.

Please, correct me if I'm wrong.
I was just unsure whether callback counts as a thread, but according to ProcessExplorer, there are equal amount of threads with or without SetWindowCallback() function.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by TI-994A »

bbanelli wrote:...just unsure whether callback counts as a thread...
Callbacks are nothing more than procedures that execute within the same thread.

However, it bears noting that some languages do have the capability of asynchronous callbacks, which technically run in separate threads.

Again, please correct me if I'm wrong. :lol:
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
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Processing WM_SYSCOMMAND without callback?

Post by luis »

bbanelli wrote:I was just unsure whether callback counts as a thread, but according to ProcessExplorer, there are equal amount of threads with or without SetWindowCallback() function.
Sure, it's part of the same thread. That's why generally is important to not spend too much time inside a callback.

TI-994A wrote: It's so childish to get defensive about principles that are clearly beyond you.
Ahahahahah ! Yeah ! Right ! It's plain to see.

:lol:
Last edited by luis on Thu Feb 25, 2016 11:11 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by TI-994A »

luis wrote:That's why (it is) generally important (not to) spend too much time inside a callback.
There's simply no logic to support that. Being procedures themselves, callbacks function in the same manner, and are accordingly subject to the same rules of execution.


It's so childish to get defensive about principles that are clearly beyond you. And throwing tantrums in your signatures isn't helping either. :lol:
Last edited by TI-994A on Fri Sep 11, 2015 8:11 pm, edited 1 time in total.
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
fromVB
User
User
Posts: 81
Joined: Sun Jul 29, 2012 2:27 am

Re: Processing WM_SYSCOMMAND without callback?

Post by fromVB »

TI-994A wrote:
luis wrote:That's why (it is) generally important (not to) spend too much time inside a callback.
There's simply no logic to support that. Being procedures themselves, callbacks function in the same manner, and are accordingly subject to the same rules of execution.
Are you sure?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Processing WM_SYSCOMMAND without callback?

Post by TI-994A »

fromVB wrote:Are you sure?
Yes; pretty much. :wink:
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
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Processing WM_SYSCOMMAND without callback?

Post by Danilo »

TI-994A wrote:
luis wrote:That's why (it is) generally important (not to) spend too much time inside a callback.
There's simply no logic to support that. Being procedures themselves, callbacks function in the same manner, and are accordingly subject to the same rules of execution.
There is no more event processing, while Windows is within your callback.

Code: Select all

Procedure MyWindowCallback(WindowID, Message, WParam, LParam)
    Delay(50)
    ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Procedure Timer()
    Static Value : Value + 1
    SetGadgetText(0,StrU(Value,#PB_Integer))
EndProcedure

If OpenWindow(0, 0, 0, 400, 100, "Timer Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0,10,10,100,25,"")
    AddWindowTimer(0, 123, 50)
    BindEvent(#PB_Event_Timer,@Timer())

    ;
    ; ACTIVATE THE CALLBACK FOR TESTING, THEN MOVE THE WINDOW AROUND
    ;    
    ;SetWindowCallback(@MyWindowCallback())
    
    Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
The event processing is blocked as long as you are within the callback, that's why luis said
codes within callbacks should be as fast as possible.
Of course it's the same with all other procedures within the same thread. If your procedures
process something for 10 seconds, there is no event handling for 10 seconds - if you don't continue
to call WindowEvent() from time to time.

The main GUI thread should be non-blocking, and procedures that take long time to process
should be better executed in other threads.

That's meant to be a general thing to have in mind, not specific to your OpenFileRequester() example.
Post Reply