How to Refresh Image on ButtonImageGadget

Just starting out? Need help? Post your questions and find answers here.
Fips
User
User
Posts: 28
Joined: Sun Feb 20, 2022 1:03 pm

How to Refresh Image on ButtonImageGadget

Post by Fips »

Hi everyone,

I'm trying to implement a color picker. Therefore I use a ButtonImageGadget() to launch a color requester. The ButtonImageGadget is supposed to show the current selected color.
For that I draw a box in the image of the ButtonImageGadget. But the image on the button won't refresh until the i hover with my mouse over it. Tried to trigger a refreshing by resizing the button and giving the ButtonImageGadget the image-id again via SetGadgetAttribute but no succes so far.

In the example below I use two different 'styles' for which each a different color can be chosen.

So I would like to refresh the image on the ButtonImageGadget programmatically everytime the optiongadgets are changed and everytime a new color was picked.

Any ideas? I am on linux by the way, so no winapi please.

Code: Select all

EnableExplicit

Enumeration gadgets
  #opt_style1
  #opt_style2
  #btn_color
EndEnumeration

Global.i col_style1 = RGB(255,0,0), col_style2 = RGB(0,255,0)

Procedure oncloseWindoMain()
  CloseWindow(EventWindow())  
  End
EndProcedure

Procedure setImageButtonColor(ButtonNr.i,color.i)
  StartDrawing(ImageOutput(ButtonNr))
  Box(0,0,ImageWidth(ButtonNr), ImageHeight(ButtonNr), color)
  StopDrawing()
EndProcedure

Procedure onOptionGad_Change()
   If GetGadgetState(#opt_style1)
    setImageButtonColor(#btn_color,col_style1)
  ElseIf GetGadgetState(#opt_style2)
    setImageButtonColor(#btn_color,col_style2)
  EndIf    
EndProcedure

Procedure onclickButtonColor()
  Protected color.i
  
  If GetGadgetState(#opt_style1)
    color = col_style1 
  ElseIf GetGadgetState(#opt_style2)
    color = col_style2
  EndIf 
  
  color = ColorRequester(color)
  If  color <> -1
    setImageButtonColor(#btn_color,color)
    If GetGadgetState(#opt_style1)
      col_style1 = color
    ElseIf GetGadgetState(#opt_style2)
      col_style2 = color
    EndIf 
  EndIf
  
EndProcedure


  
Procedure MainWindow()
  Protected.i this_window
  Protected.i win_x, win_y, win_width, win_height, btn_width, btn_height
  Protected.i gad_width, gad_height, gad_X, gad_Y, frame, gad_dist

  
  btn_height = 40
  btn_width = 40
  frame = 10
  gad_dist = 5
  
  win_x = 100
  win_y = 300
  win_width = 250
  win_height = btn_height + 2 * frame

  CreateImage(#btn_color, btn_width * 0.75, btn_height * 0.75)


  this_window = OpenWindow(#PB_Any, win_x, win_y, win_width, win_height, "Test")
  gad_X = frame
  gad_Y = frame
  gad_width = (win_width - 2 * frame - 2 * gad_dist - btn_width) / 2
  gad_height = btn_height
  OptionGadget(#opt_style1,gad_x, gad_y, gad_width, gad_height, "Style 1")
  gad_x + gad_width + gad_dist
  OptionGadget(#opt_style2,gad_x, gad_y, gad_width, gad_height, "Style 2")
  gad_x + gad_width + gad_dist
  ButtonImageGadget(#btn_color,gad_x, gad_y, btn_width, btn_height, ImageID(#btn_color))
  onOptionGad_Change()
  
  BindEvent(#PB_Event_CloseWindow,@oncloseWindoMain(), this_window)
  BindEvent(#PB_Event_Gadget,@onOptionGad_Change(), this_window, #opt_style1)
  BindEvent(#PB_Event_Gadget,@onOptionGad_Change(), this_window, #opt_style2)
  BindEvent(#PB_Event_Gadget,@onclickButtonColor(), this_window, #btn_color)
  
  
EndProcedure


MainWindow()
Repeat 
  WaitWindowEvent()  
  
ForEver
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to Refresh Image on ButtonImageGadget

Post by RASHAD »

Hi
Change to

Code: Select all

Procedure onOptionGad_Change()
   If GetGadgetState(#opt_style1)
    setImageButtonColor(#btn_color,col_style1)
  ElseIf GetGadgetState(#opt_style2)
    setImageButtonColor(#btn_color,col_style2)
  EndIf
  SetGadgetAttribute(#btn_color,#PB_Button_Image,ImageID(#btn_color))      
EndProcedure
Egypt my love
Fips
User
User
Posts: 28
Joined: Sun Feb 20, 2022 1:03 pm

Re: How to Refresh Image on ButtonImageGadget

Post by Fips »

Hi Rashad,

thanks for your reply. I tried this before without any success.

After your reply I tried it again and I figured out that it seems that this does not work if you assign the same ID again. But it works somehow if you use a different image id. So I'm using a dummy image and change the image id twice:

Code: Select all

SetGadgetAttribute(#btn_color,#PB_Button_Image,ImageID(#dummy_image))
SetGadgetAttribute(#btn_color,#PB_Button_Image,ImageID(#btn_color))
Fips
Post Reply