SetGadgetState problem with CanvasGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

SetGadgetState problem with CanvasGadget

Post by charvista »

Hello :)

CanvasGadget() is still new for me, and would like to use it.
I do not know what to put at the SetGadgetState() so the canvas gadget is updated with the new text.
For images, I know that I need to use ImageID() as second parameter, but there is no CanvasID(), so I am puzzled.

Here an example: (the text of the clock should be cleared, that's where is my problem)

Code: Select all

OpenWindow(0,0,0,300,200,"Clock",#PB_Window_ScreenCentered)

F1=LoadFont(#PB_Any,"Tahoma",8,#PB_Font_Bold)

Can2=CanvasGadget(#PB_Any,0,0,180,30)
StartDrawing(CanvasOutput(Can2))
FillArea(0,0,-1,$46596E)
StopDrawing()

c=0
Repeat
    Text.s=FormatDate("%hh : %ii : %ss",Date())
    StartDrawing(CanvasOutput(Can2))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(F1)
    DrawText(12+1,8+1,Text.s,$000000); shadows always black
    DrawText(12,8,Text,$FFFFFF)
    StopDrawing()
    SetGadgetState(Can2,0); how to update this? <<<<<<<<<<<<<<
    Delay(50)
    C+1
Until C>=60; about 3 seconds
Thanks.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Lothar Schirm
User
User
Posts: 54
Joined: Mon Nov 26, 2012 4:57 pm

Re: SetGadgetState problem with CanvasGadget

Post by Lothar Schirm »

According to the PB Help, SetGadgetState cannot be used for a CanvasGadget. Perhaps this is a solution:

Code: Select all

OpenWindow(0,0,0,300,200,"Clock",#PB_Window_ScreenCentered)

F1=LoadFont(#PB_Any,"Tahoma",8,#PB_Font_Bold)

Can2=CanvasGadget(#PB_Any,0,0,180,30)
StartDrawing(CanvasOutput(Can2))
FillArea(0,0,-1,$46596E)
StopDrawing()

c=0
Repeat
    Text.s=FormatDate("%hh : %ii : %ss",Date())
    StartDrawing(CanvasOutput(Can2))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(F1)
    Box(0, 0, 180, 30, $46596E)           ; <<<<<<<< delete the contents of the CanvasGadget
    DrawText(12+1,8+1,Text.s,$000000); shadows always black
    DrawText(12,8,Text,$FFFFFF)
    StopDrawing()
    ;SetGadgetState(Can2,0); how to update this? <<<<<<<<<<<<<<  this will not work
    Delay(50)
    C+1
Until C>=60; about 3 seconds
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: SetGadgetState problem with CanvasGadget

Post by RASHAD »

Graaeh Ya Gedaan :mrgreen:
Too much coffee and no sleep I think

Code: Select all

OpenWindow(0,0,0,300,200,"Clock",#PB_Window_ScreenCentered)

  F1=LoadFont(#PB_Any,"Tahoma",12,#PB_Font_Bold)
  Text.s=FormatDate("%hh : %ii : %ss",Date())
  Can2=CanvasGadget(#PB_Any,0,0,180,30)
  StartDrawing(CanvasOutput(Can2))
  FillArea(0,0,-1,$46596E)
  StopDrawing()
  c=0
Repeat
  Text.s=FormatDate("%hh : %ii : %ss",Date())
  StartDrawing(CanvasOutput(Can2))
  DrawingFont(FontID(F1))
  DrawText(12+1,8+1,Text.s,$000000,$46596E); shadows always black
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(12,8,Text,$FFFFFF);,$46596E)
  StopDrawing()
  Delay(50)
  C+1
ForEver
Edit :Updated again
Last edited by RASHAD on Mon Mar 18, 2013 6:06 pm, edited 2 times in total.
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: SetGadgetState problem with CanvasGadget

Post by charvista »

Very nice workaround for this case, Lothar! Thank you :D Good point!

However, I am still wondering if there is no way to use SetGadgetState to refresh the contents of the canvas gadget?....
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: SetGadgetState problem with CanvasGadget

Post by charvista »

Hi RASHAD :mrgreen:
Too much sleep and no coffee at all :mrgreen:
You are a magician, it works without SetGadgetState :shock:
Thank you for revealing your secret :mrgreen:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: SetGadgetState problem with CanvasGadget

Post by RASHAD »

Hi charvista
Updated again for DrawingFont(FontID(F1))
Egypt my love
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: SetGadgetState problem with CanvasGadget

Post by charvista »

Thank you again my friend! I also noticed that the shadow was no longer there.
You edited the code before I had the chance to complain! :D

Conclusion:
- if the canvas gadget has an image as background, we must use SetGadgetState(#Gadget,ImageID(#Image)) to update the gadget.
- if the canvas gadget has plain color, do as RASHAD did.

Excellent.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: SetGadgetState problem with CanvasGadget

Post by STARGÅTE »

this is a wrong way zu upate the gadget, you need a WindowEvent(), else freeze the window!

here a right way:

Code: Select all

OpenWindow(0,0,0,300,200,"Clock",#PB_Window_ScreenCentered)

F1=LoadFont(#PB_Any,"Tahoma",8,#PB_Font_Bold)

Can2=CanvasGadget(#PB_Any,0,0,180,30)

AddWindowTimer(0, 1, 50)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_Timer
			Text.s=FormatDate("%hh : %ii : %ss",Date())
			If StartDrawing(CanvasOutput(Can2))
				Box(0,0,OutputWidth(), OutputHeight(),$46596E)
				DrawingMode(#PB_2DDrawing_Transparent)
				DrawingFont(F1)
				DrawText(12+1,8+1,Text.s,$000000); shadows always black
				DrawText(12,8,Text,$FFFFFF)
				StopDrawing()
			EndIf
	EndSelect
ForEver
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: SetGadgetState problem with CanvasGadget

Post by charvista »

you need a WindowEvent(), else freeze the window!
Stargåte,
You are correct. I tried the code from Rashad again, and when I want to move the window with the mouse, it hangs. Sorry Rashad...
With your code, when we move the window, the clock is suspended, and continues when the user has released the mouse. Yup, Windows is like a cat we hold above his neck :mrgreen:
The truth is, in my program, the clock would run in a thread, so I think it would never need a WindowEvent(). Or am I wrong here too?

And you changed as well the Delay(50) to a AddWindowTimer(0,1,50). Is it better? Is it the same as WaitWindowEvent(50)?

Cheers!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: SetGadgetState problem with CanvasGadget

Post by STARGÅTE »

Delay(50) stop the program for 50ms, not good.

AddWindowTimer() is the safe way, to run a code all 50ms.
With a Thread you can use Delay(50) and it looks like this one:

Enable: Compiler: Thread-Safe!

Code: Select all

OpenWindow(0,0,0,300,200,"Clock",#PB_Window_ScreenCentered)

Global F1=LoadFont(#PB_Any,"Tahoma",8,#PB_Font_Bold)

Can2=CanvasGadget(#PB_Any,0,0,180,30)

Procedure Update(Gadget)
	Protected Text.s
	Repeat
		Text = FormatDate("%hh : %ii : %ss",Date())
		If StartDrawing(CanvasOutput(Gadget))
			Box(0,0,OutputWidth(), OutputHeight(),$46596E)
			DrawingMode(#PB_2DDrawing_Transparent)
			DrawingFont(F1)
			DrawText(12+1,8+1,Text.s,$000000)
			DrawText(12,8,Text,$FFFFFF)
			StopDrawing()
		EndIf
		Delay(50)
	ForEver
EndProcedure

CreateThread(@Update(), Can2)

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         End
   EndSelect
ForEver
then you can move the window and update runs
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: SetGadgetState problem with CanvasGadget

Post by charvista »

Super.
Thread is really a good invention.
Runs fine, window can be moved safely and clock continues to run, no hang-up and nothing can stop the time :o
Thanks Stargåte for your time, I have documented it so I hope to always remember.
Live long and prosper!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: SetGadgetState problem with CanvasGadget

Post by Joris »

What is the difference between the PB-Timer AddWindowTimer(0, 1, 50) and the windows api-timer (timeGetDevCaps etc.) ?
They look (act) quit the same imo, as they both seem to create a thread no ?
If the same, AddWindowTimer is always the best solution here, imo.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
Post Reply