Change button gadget during callback procedure

Just starting out? Need help? Post your questions and find answers here.
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Change button gadget during callback procedure

Post 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.
User avatar
T4r4ntul4
Posts: 132
Joined: Wed May 21, 2014 1:57 pm
Location: Netherlands
Contact:

Re: Change button gadget during callback procedure

Post 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.
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Re: Change button gadget during callback procedure

Post 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
User avatar
T4r4ntul4
Posts: 132
Joined: Wed May 21, 2014 1:57 pm
Location: Netherlands
Contact:

Re: Change button gadget during callback procedure

Post by T4r4ntul4 »

And if you change before the process starts?
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Re: Change button gadget during callback procedure

Post 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())
Last edited by KianV on Sun Mar 21, 2021 1:31 pm, edited 1 time in total.
munfraid
Posts: 104
Joined: Sat Mar 24, 2018 1:33 pm

Re: Change button gadget during callback procedure

Post 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.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Change button gadget during callback procedure

Post 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.
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Re: Change button gadget during callback procedure

Post 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.
Last edited by KianV on Sun Mar 21, 2021 1:35 pm, edited 6 times in total.
KianV
Posts: 21
Joined: Sun Nov 17, 2019 11:38 am

Re: Change button gadget during callback procedure

Post 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.
Post Reply