Page 1 of 1

Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 8:04 am
by KianV
I have a button gadget whose callback generates a fairly large image, so the process is quite lengthy.
I would like to change the text of the button while the process is underway, for example, to say 'Processing', but can find no way of doing so.
Any changes I make during the callback procedure are not effected until the procedure ends.
In PB (which I appreciate runs in a different way) I can add 'While WindowEvent():Wend' to counteract similar behaviour.
I would be grateful if anyone knows of a way to produce the desired behaviour.

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 9:04 am
by T4r4ntul4
Use SetGadgetText(#Gadget, Text$) when you start the process, and again SetGadgetText(#Gadget, Text$) when it finishes. Maybe use DisableGadget(#Gadget, State) too, because you dont want users press that button again, when its processing data.

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 9:11 am
by KianV
Hi T4r4ntul4,
The problem is that using SetGadgetText() doesn't do anything when the procedure is called.
Once the callback procedure is finished, then the button text will change, but this slightly defeats the object.
I do also intend to disable the gadget, but this also won't occur until the callback is finished
Kian

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 9:15 am
by T4r4ntul4
And if you change before the process starts?

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 9:18 am
by KianV
I'm not sure how I would do that, since clicking on the button just calls the callback procedure.
The SetGadgetText() function was the first instruction in the callback, but nothing happens until the procedure ends.
I also tried calling a different procedure to change the gadget text, which in turn calls the image generating procedure, but the same effect still persists.
This is also true if I bind 2 events (one of which changes the gadget text) to the button.

Example code:

Code: Select all

OpenWindow(0, 0, 0, 200, 100, "Button Callback",#PB_Window_ScreenCentered) 

Procedure ButtonCallback()
  SetGadgetText(1,"processing")
  For f=1 To 2000 ; this just stalls for a while to simulate pdf generation time
                  ; for, I expect, similar reasons nothing appears on the debug window until the end
    Debug f
  Next f
  Debug "callback finished"
EndProcedure



ButtonGadget(1,25,40,150,20,"Generate")
BindGadgetEvent(1,@buttoncallback())

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 11:46 am
by munfraid
I have no explanation for this behaviour and no solution how to change it. When I had a similar problem I helped myself with a timer solution. According to your example code it would be something like this:

Code: Select all

Global processing = #False

Procedure TimerEvents()
  If EventTimer()=0 And processing
    For f=1 To 2000 
      Debug f
    Next
    Debug "...done"
    processing = #False   
    SetGadgetText(1,"Generate")
  EndIf   
EndProcedure


Procedure ButtonCallback()
  processing = #True
  SetGadgetText(1,"processing")
  Debug "processing..."
EndProcedure


OpenWindow(0, 0, 0, 200, 100, "Button Callback",#PB_Window_ScreenCentered)
AddWindowTimer(0, 0, 500)
BindEvent(#PB_Event_Timer, @TimerEvents())
ButtonGadget(1,25,40,150,20,"Generate")
BindGadgetEvent(1,@buttoncallback())
Wish there was a command to manually update/redraw gadgets or windows.

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 12:13 pm
by Peter
@KianV: You can use setTimeout():

Code: Select all

OpenWindow(0, 0, 0, 200, 100, "Button Callback",#PB_Window_ScreenCentered)

Procedure ButtonCallback()
  
  SetGadgetText(1,"processing")
  
  ! setTimeout(function() {
  
  For f=1 To 1000 ; this just stalls for a while
    Debug f
  Next f
  
  SetGadgetText(1,"Generate")
  
  ! }, 0);  
  
EndProcedure

ButtonGadget(1,25,40,150,20,"Generate")
BindGadgetEvent(1,@buttoncallback())
But in general, it is not recommended to block the browser with long-running functions.

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 1:20 pm
by KianV
Thanks munfraid.
I never thought of trying that. Nice lateral thinking. :D
It still seems possible to trigger the generating function twice if you double click, even if I change the length of the window timer,but that's not too much of a problem.
The effect is seen here:
http://www.fractalevitreuse.fr/Boites/
on the 'Générer' button.

Re: Change button gadget during callback procedure

Posted: Sun Mar 21, 2021 1:24 pm
by KianV
@Peter
Thanks for that Peter.
The program is not deliberately trying to slow things down. The loop was only there as a stand-in for the generating process to make it obvious what was happening.
I am only too aware of the horrors that can ensue when trying to artificially hold things up.