CounterGadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

CounterGadget

Post by Guimauve »

Hello everyone,

A little Counter Gadget. Have fun !

Edit 1 : Correction suggested by Demivec Added
Edit 2 : Modification/Simplification suggested by ebs

Best regards.
Guimauve

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : CounterGadget
; File Name : CounterGadget.pb
; File version: 1.1.1
; Programming : OK
; Programmed by : Guimauve
; Date : 03-04-2012
; Last Update : 04-04-2012
; PureBasic code : 4.60
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Enumeration
	
	#SEGMENT_DIRECTION_HORIZONTAL
	#SEGMENT_DIRECTION_VERTICAL
	#SEGMENT_DIRECTION_OBLIQUE
	
EndEnumeration

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<

Structure Segment
	
	X1.w
	Y1.w
	X2.w
	Y2.w
	
EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<

Macro GetSegmentX1(SegmentA)
	
	SegmentA\X1
	
EndMacro

Macro GetSegmentY1(SegmentA)
	
	SegmentA\Y1
	
EndMacro

Macro GetSegmentX2(SegmentA)
	
	SegmentA\X2
	
EndMacro

Macro GetSegmentY2(SegmentA)
	
	SegmentA\Y2
	
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<

Macro SetSegmentX1(SegmentA, P_X1)
	
	GetSegmentX1(SegmentA) = P_X1
	
EndMacro

Macro SetSegmentY1(SegmentA, P_Y1)
	
	GetSegmentY1(SegmentA) = P_Y1
	
EndMacro

Macro SetSegmentX2(SegmentA, P_X2)
	
	GetSegmentX2(SegmentA) = P_X2
	
EndMacro

Macro SetSegmentY2(SegmentA, P_Y2)
	
	GetSegmentY2(SegmentA) = P_Y2
	
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Update <<<<<

Macro UpdateSegment(SegmentA, P_X1, P_Y1, P_X2, P_Y2)
	
	SetSegmentX1(SegmentA, P_X1)
	SetSegmentY1(SegmentA, P_Y1)
	SetSegmentX2(SegmentA, P_X2)
	SetSegmentY2(SegmentA, P_Y2)
	
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Reset <<<<<

Macro ResetSegment(SegmentA)
	
	SetSegmentX1(SegmentA, 0)
	SetSegmentY1(SegmentA, 0)
	SetSegmentX2(SegmentA, 0)
	SetSegmentY2(SegmentA, 0)
	
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur ProductByScalar : R = A * Scalar <<<<<

Macro ProductByScalarSegment(SegmentR, SegmentA, Scalar)
	
	SetSegmentX1(SegmentR, GetSegmentX1(SegmentA) * Scalar)
	SetSegmentY1(SegmentR, GetSegmentY1(SegmentA) * Scalar)
	SetSegmentX2(SegmentR, GetSegmentX2(SegmentA) * Scalar)
	SetSegmentY2(SegmentR, GetSegmentY2(SegmentA) * Scalar)
	
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Draw Segment <<<<<

Procedure DrawSegment(*SegmentA.Segment, Radius.w, Color.l, Direction.b)
	
	Select Direction
			
		Case #SEGMENT_DIRECTION_HORIZONTAL
			For PosX = GetSegmentX1(*SegmentA) To GetSegmentX2(*SegmentA)
				Circle(PosX, GetSegmentY1(*SegmentA), Radius, Color)
			Next
			
		Case #SEGMENT_DIRECTION_VERTICAL
			For PosY = GetSegmentY1(*SegmentA) To GetSegmentY2(*SegmentA)
				Circle(GetSegmentX1(*SegmentA), PosY, Radius, Color)
			Next
			
		Case #SEGMENT_DIRECTION_OBLIQUE
			For PosXY = GetSegmentX1(*SegmentA) To GetSegmentX2(*SegmentA)
				Circle(PosXY, PosXY, Radius, Color)
			Next
			
	EndSelect
	
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<

Structure Counter

  Pattern.s
  DigitRadius.w
  DigitOnColor.l
  DigitOffColor.l
  BackColor.l
  LastSecond.l
  Width.w
  Height.w
  Numbers.l[16]

EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<

Macro GetCounterPattern(CounterA)

  CounterA\Pattern

EndMacro

Macro GetCounterDigitRadius(CounterA)

  CounterA\DigitRadius

EndMacro

Macro GetCounterDigitOnColor(CounterA)

  CounterA\DigitOnColor

EndMacro

Macro GetCounterDigitOffColor(CounterA)

  CounterA\DigitOffColor

EndMacro

Macro GetCounterBackColor(CounterA)

  CounterA\BackColor

EndMacro

Macro GetCounterLastSecond(CounterA)

  CounterA\LastSecond

EndMacro

Macro GetCounterWidth(CounterA)

  CounterA\Width

EndMacro

Macro GetCounterHeight(CounterA)

  CounterA\Height

EndMacro

Macro GetCounterNumbers(CounterA, NumbersID)

  CounterA\Numbers[NumbersID]

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<

Macro SetCounterPattern(CounterA, P_Pattern)

  GetCounterPattern(CounterA) = P_Pattern

EndMacro

Macro SetCounterDigitRadius(CounterA, P_DigitRadius)

  GetCounterDigitRadius(CounterA) = P_DigitRadius

EndMacro

Macro SetCounterDigitOnColor(CounterA, P_DigitOnColor)

  GetCounterDigitOnColor(CounterA) = P_DigitOnColor

EndMacro

Macro SetCounterDigitOffColor(CounterA, P_DigitOffColor)

  GetCounterDigitOffColor(CounterA) = P_DigitOffColor

EndMacro

Macro SetCounterBackColor(CounterA, P_BackColor)

  GetCounterBackColor(CounterA) = P_BackColor

EndMacro

Macro SetCounterLastSecond(CounterA, P_LastSecond)

  GetCounterLastSecond(CounterA) = P_LastSecond

EndMacro

Macro SetCounterWidth(CounterA, P_Width)

  GetCounterWidth(CounterA) = P_Width

EndMacro

Macro SetCounterHeight(CounterA, P_Height)

  GetCounterHeight(CounterA) = P_Height

EndMacro

Macro SetCounterNumbers(CounterA, NumbersID, P_Numbers)

  GetCounterNumbers(CounterA, NumbersID) = P_Numbers

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Reset <<<<<

Macro ResetCounter(CounterA)
 
  SetCounterPattern(CounterA, "")
  SetCounterDigitRadius(CounterA, 0)
  SetCounterDigitOnColor(CounterA, 0)
  SetCounterDigitOffColor(CounterA, 0)
  SetCounterBackColor(CounterA, 0)
  SetCounterWidth(CounterA, 0)
  SetCounterHeight(CounterA, 0)
 
  For NumbersID = 0 To 15
    If IsImage(GetCounterNumbers(CounterA, NumbersID))
      FreeImage(GetCounterNumbers(CounterA, NumbersID))
      SetCounterNumbers(CounterA, NumbersID, 0)
    EndIf
  Next
 
  SetCounterLastSecond(CounterA, 0)

EndMacro


; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Le Constructeur <<<<<

Procedure.i CreateNewCounter()

  *CounterA.Counter = AllocateMemory(SizeOf(Counter))

  If *CounterA = #Null
    MessageRequester("Fatal Error", "CreateNewCounter() - Impossible to Allocate Memory !")
    End
  EndIf

  ProcedureReturn *CounterA
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code généré en : 00.008 secondes (21000.00 lignes/seconde) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure SetCounterGadgetValue(GadgetID, Value.s)

  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 15)))
        
        For DigitID = 0 To Len(Value)
          
          Number.l = FindString("0123456789-+.: |", Mid(Value, DigitID + 1, 1), 1)
          
          If Number
            
            Number - 1
            
            ; determine offset for specific digits/characters
            
            Select Number
                
              Case 12 ; "."
                Off.l = 3
                
              Case 13 ; ":"
                Off = 3
                
              Case 15 ; "|"
                Off = 3
                Number - 1
                
              Default
                Off = 11 
                
            EndSelect
            
            DrawImage(ImageID(GetCounterNumbers(*CounterA, Number)), PosX, 0)
            PosX + Off * GetCounterDigitRadius(*CounterA)
            
          EndIf
          
        Next
        
        StopDrawing()
        
        SetGadgetState(GadgetID, ImageID(GetCounterNumbers(*CounterA, 15)))
        
      EndIf
      
    EndIf
    
  EndIf
  
EndProcedure

Procedure DrawCounterGadgetDigits(GadgetID)
  
	Dim Segments.Segment(10)
	
	UpdateSegment(Segments(0), 3, 1, 7, 1)    ; SEGMENT CENTRAL HAUT
	UpdateSegment(Segments(1), 3, 9, 7, 9)    ; SEGMENT CENTRAL CENTRE (MOINS ET PLUS HORIZONTAL)
	UpdateSegment(Segments(2), 3, 17, 7, 17)  ; SEGMENT CENTRAL BAS
	UpdateSegment(Segments(3), 1, 3, 1, 7)    ; SEGMENT GAUCHE HAUT
	UpdateSegment(Segments(4), 1, 11, 1, 15)  ; SEGMENT GAUCHE BAS
	UpdateSegment(Segments(5), 9, 3, 9, 7)    ; SEGMENT DROIT HAUT
	UpdateSegment(Segments(6), 9, 11, 9, 15)  ; SEGMENT DROIT BAS
	
	UpdateSegment(Segments(7), 1, 5, 1, 5)    ; DEUX POINT HAUT
	UpdateSegment(Segments(8), 1, 13, 1, 13)  ; DEUX POINT BAS
	UpdateSegment(Segments(9), 5, 7, 5, 11)   ; PLUS VERTICAL
	UpdateSegment(Segments(10), 1, 17, 1, 17) ; DÉCIMAL
	
	If IsGadget(GadgetID)
		
		*CounterA.Counter = GetGadgetData(GadgetID)
		
		If *CounterA <> #Null
			
			For index = 0 To 10
				ProductByScalarSegment(Segments(index), Segments(index), GetCounterDigitRadius(*CounterA))
			Next
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "0" OK
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 0)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 4, 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 1
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "1" OK
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 1)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 4
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "2" OK
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 2)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 4, 5
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 3, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "3" OK
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 3)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 3, 4
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "4" OK
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 4)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 1
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 0, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 4
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "5"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 5)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 4, 5
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "6"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 6)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 4, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 5
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "7"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 7)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 5, 6, 3 ; --> If you want the 7 number draw like a normal Digital display, just remove the 3 from this "Case"
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 4
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "8"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 8)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 4, 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "9"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 9)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					
					For SegmentID = 0 To 6
						
						Select SegmentID
								
							Case 0, 1, 2
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
								
							Case 3, 5, 6
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
							Case 4
								DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
								
						EndSelect
						
					Next
					
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "-"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 10)))
					
					Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
					DrawSegment(Segments(1), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
				StopDrawing()
				
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "+"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 11)))
			  
			  Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
			  DrawSegment(Segments(1), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  DrawSegment(Segments(9), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
			  StopDrawing()
			  
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole "."
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 12)))
			  
			  Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
			  DrawSegment(Segments(10), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  StopDrawing()
			  
			EndIf
			
			; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			; Dessin du symbole ":"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 13)))
			  
			  Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
			  DrawSegment(Segments(7), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  DrawSegment(Segments(8), GetCounterDigitRadius(*CounterA), GetCounterDigitOnColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  StopDrawing()
			  
			EndIf
			
			; Dessin du symbole "|"
			
			If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 14)))
			  
			  Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
			  DrawSegment(Segments(7), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  DrawSegment(Segments(8), GetCounterDigitRadius(*CounterA), GetCounterDigitOffColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
			  StopDrawing()
			  
			EndIf
			
		EndIf
		
	EndIf
	
EndProcedure

Procedure ChangeCounterGadgetColors(GadgetID, DigitOnColor.l, DigitOffColor.l, BackColor.l = 0)
	
	If IsGadget(GadgetID)
		
		*CounterA.Counter = GetGadgetData(GadgetID)
		
		If *CounterA <> #Null
		  
		  If DigitOnColor <> -1
		    SetCounterDigitOnColor(*CounterA, DigitOnColor)
		  EndIf
		  
		  If DigitOffColor <> -1
		    SetCounterDigitOffColor(*CounterA, DigitOffColor)
		  EndIf
		  
		  SetCounterBackColor(*CounterA, BackColor)
		  
		EndIf
		
	EndIf
	
	DrawCounterGadgetDigits(GadgetID)
	
EndProcedure

Procedure CounterGadget(GadgetID, PosX.w, PosY.w, DigitPattern.s, DigitRadius.w, DigitOnColor.l, DigitOffColor.l, BackColor.l = 0, Option = -1)

	*CounterA.Counter = CreateNewCounter()
	
	SetCounterPattern(*CounterA, DigitPattern)
	
	Point_TwoPoint = CountString(DigitPattern, ".") + CountString(DigitPattern, ":")
	DigitCount = Len(DigitPattern) - Point_TwoPoint
	
	SetCounterDigitRadius(*CounterA, DigitRadius)
	SetCounterWidth(*CounterA, DigitCount * DigitRadius * 11 + Point_TwoPoint * DigitRadius * 3 - DigitRadius)
	SetCounterHeight(*CounterA, DigitRadius * 18)
	
	If Option <= 0
		GadgetHandle = ImageGadget(GadgetID, PosX, PosY, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), 0)
	ElseIf Option >= 1
		GadgetHandle = ImageGadget(GadgetID, PosX, PosY, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), 0, #PB_Image_Border)
	EndIf
	
	If GadgetID = #PB_Any
		GadgetID = GadgetHandle
	EndIf
	
	SetGadgetData(GadgetID, *CounterA)
	
	For NumbersID = 0 To 14
		SetCounterNumbers(*CounterA, NumbersID, CreateImage(#PB_Any, DigitRadius * 10, DigitRadius * 18))
	Next
	
	SetCounterNumbers(*CounterA, 15, CreateImage(#PB_Any, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA)))
	
	If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 15)))
	  Box(0, 0, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), BackColor)
	  StopDrawing()
	EndIf
	
	ChangeCounterGadgetColors(GadgetID, DigitOnColor, DigitOffColor, BackColor)
	
EndProcedure

Procedure ClockCounterGadget(GadgetID)
  
	time = Date()
	CurrentSecond = Second(time)
	
	If IsGadget(GadgetID)
		
		*CounterA.Counter = GetGadgetData(GadgetID)
		
		If *CounterA <> #Null
			
			If GetCounterLastSecond(*CounterA) <> CurrentSecond
				
				If CurrentSecond % 2 = 0
					SetCounterGadgetValue(GadgetID, FormatDate("%hh:%ii:%ss", time))
				Else
					SetCounterGadgetValue(GadgetID, FormatDate("%hh|%ii|%ss", time))
				EndIf
				
				SetCounterLastSecond(*CounterA, CurrentSecond)
				
			EndIf
			
		EndIf
		
	EndIf
	
EndProcedure

Procedure FreeCounterGadget(GadgetID)
	
	If IsGadget(GadgetID)
		
		*CounterA.Counter = GetGadgetData(GadgetID)
		
		If *CounterA <> #Null
			ResetCounter(*CounterA)
		EndIf
		
		FreeGadget(GadgetID)   
		
	EndIf
	
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Macro GadgetDown(GadgetID)
  
  GadgetY(GadgetID) + GadgetHeight(GadgetID)
  
EndMacro

Enumeration
  
  #Counter_01
  #Counter_02
  #Counter_03
  #Counter_04
  #Counter_05
  
EndEnumeration

If OpenWindow(0, 300, 300, 500, 375, "Counter Gadget", #PB_Window_SystemMenu) 
  
  CounterGadget(#Counter_01, 5, 5, "88:88:88", 3, RGB(000, 255, 000), RGB(000, 64, 000), 0, 0)
  CounterGadget(#Counter_02, 5, GadgetDown(#Counter_01) + 10, "88:88:88", 2, RGB(255, 100, 000), RGB(064, 010, 000), 0, 1)
  CounterGadget(#Counter_03, 5, GadgetDown(#Counter_02) + 10, "+8888888", 2, RGB(255, 255, 000), RGB(064, 064, 000), 0, 1)
  CounterGadget(#Counter_04, 5, GadgetDown(#Counter_03) + 10, "888.888.888.888", 2, RGB(255, 000, 125), RGB(064, 000, 012), 0, 1)
  CounterGadget(#Counter_05, 5, GadgetDown(#Counter_04) + 10, "88-88-8888", 2, RGB(100, 100, 255),RGB(020, 020, 96), 0, 1)

  SetCounterGadgetValue(#Counter_03, "+01234567")
  SetCounterGadgetValue(#Counter_04, "192.223.145.123")
  SetCounterGadgetValue(#Counter_05, FormatDate("%dd-%mm-%yyyy", Date()))

  Repeat
    
    ClockCounterGadget(#Counter_01)
    ClockCounterGadget(#Counter_02)
    
    EventID = WaitWindowEvent(250)
    
    Select EventID
        
      Case #PB_Event_Menu
        
        Select EventMenu()
            
        EndSelect
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
            
        EndSelect
        
    EndSelect
    
  Until EventID = #PB_Event_CloseWindow
  
EndIf

FreeCounterGadget(#Counter_01)
FreeCounterGadget(#Counter_02)
FreeCounterGadget(#Counter_03)
FreeCounterGadget(#Counter_04)
FreeCounterGadget(#Counter_05)

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Last edited by Guimauve on Thu Apr 05, 2012 12:05 am, edited 5 times in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: CounterGadget

Post by ts-soft »

thx, nice work! :D

I miss the declaration of variables for EnableExplicit.

Code: Select all

Procedure ClockCounterGadget(GadgetID)
 
  Time = Date()
  CurrentSecond = Second(Time)
 
  If LastSecond <> CurrentSecond
   
    If CurrentSecond % 2 = 0
      SetCounterGadgetValue(GadgetID, FormatDate("%hh:%ii:%ss", Time))
    Else
      SetCounterGadgetValue(GadgetID, FormatDate("%hh|%ii|%ss", Time))
    EndIf
   
    LastSecond = CurrentSecond
   
  EndIf
 
EndProcedure
I think LastSecond is always zero :wink:

Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: CounterGadget

Post by STARGÅTE »

ts-soft wrote:I think LastSecond is always zero
Therefore, the gadget also don't update itself exactly at x:x:00
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
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: CounterGadget

Post by Guimauve »

STARGÅTE wrote:
ts-soft wrote:I think LastSecond is always zero
Therefore, the gadget also don't update itself exactly at x:x:00
Maybe reducing the WaitWindowEvent() timeout from 500 to 250 can help. The other solution is to run the ClockCounterGadget() instruction has a thread instead of running it inside the event loop.

Best regards.
Guimauve
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CounterGadget

Post by Kwai chang caine »

Therefore, the gadget also don't update itself exactly at x:x:00
+1
Maybe reducing the WaitWindowEvent() timeout from 500 to 250 can help
I have try on VISTA 250/50 and 1 with no success :(

But nice effect, thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CounterGadget

Post by Demivec »

Guimauve wrote:
STARGÅTE wrote:
ts-soft wrote:I think LastSecond is always zero
Therefore, the gadget also don't update itself exactly at x:x:00
Maybe reducing the WaitWindowEvent() timeout from 500 to 250 can help. The other solution is to run the ClockCounterGadget() instruction has a thread instead of running it inside the event loop.
Using a separate thread for gadgets may run into problems.

You can also use modifications to store the last second, such as these:

Code: Select all

;change
Structure counter

  pattern.s
  digitRadius.w
  digitFrontColor.l
  digitBackColor.l
  width.w
  height.w
  numbers.l[16]
  lastSecond.i      ;<< add

EndStructure

;add
Macro getCounterLastSecond(counterA): counterA\lastSecond: EndMacro

Macro setCounterLastSecond(counterA, p_LastSecond): getCounterLastSecond(counterA) = p_LastSecond: EndMacro

;change
Procedure clockCounterGadget(gadgetID)

  time = Date()
  currentSecond = Second(time)
  
  If IsGadget(gadgetID)
      
    *counterA.counter = GetGadgetData(gadgetID)
    
    If getCounterLastSecond(*counterA) <> currentSecond
      
      If currentSecond % 2 = 0
        setCounterGadgetValue(gadgetID, FormatDate("%hh:%ii:%ss", time))
      Else
        setCounterGadgetValue(gadgetID, FormatDate("%hh|%ii|%ss", time))
      EndIf
      
      setCounterLastSecond(*counterA, currentSecond)
      
    EndIf 
    
  EndIf

EndProcedure
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: CounterGadget

Post by Guimauve »

Hello everyone,

@Demivec I have apply your correction thanks.

Something I can't figure out, here on Linux Mint 12 + Gnome-Shell everything work fine. Both V1.0.0 and V1.0.1
Very strange because I just use PureBasic Internal command and no platform specific code.

I have create this gadget to display number, floating number, dates, IP address and so on, the clock is an experimental display system.

If someone find the solution for Windows feel free to report the solution here.

Best regards
Guimauve
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CounterGadget

Post by Demivec »

Guimauve wrote:If someone find the solution for Windows feel free to report the solution here.
Solution to what? I have tested it with Windows XP x86. So far I haven't found any troubles.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: CounterGadget

Post by Guimauve »

Demivec wrote:
Guimauve wrote:If someone find the solution for Windows feel free to report the solution here.
Solution to what? I have tested it with Windows XP x86. So far I haven't found any troubles.
Kwaï chang caïne reported a problem about the clock display, the second value refresh.

Best regards.
Guimauve
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: CounterGadget

Post by dobro »

Nice , Thanks ;)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
juror
Enthusiast
Enthusiast
Posts: 232
Joined: Mon Jul 09, 2007 4:47 pm
Location: Courthouse

Re: CounterGadget

Post by juror »

A little Counter Gadget. Have fun !
Very nice indeed! Thanks for sharing it with us.
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: CounterGadget

Post by ebs »

Guimauve,

Thank you for your fine counter gadget code!
The clock counter works fine for me on Windows XP and Windows 7.

I can't resist tinkering, so I added the ability to set a color for the segments when they are OFF.
This lets you make the display look more like a real LED, if you like that effect.
I set the segment off color to a lighter shade of green in the top display.

I also simplified (hopefully) the "SetCounterGadgetValue" routine.

I hope you don't mind me messing around with your code!

Regards,
Eric

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : CounterGadget
; File Name : CounterGadget.pb
; File version: 1.0.1
; Programming : OK
; Programmed by : Guimauve
; Date : 03-04-2012
; Last Update : 04-04-2012
; PureBasic code : 4.60
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Enumeration
  
  #SEGMENT_DIRECTION_HORIZONTAL
  #SEGMENT_DIRECTION_VERTICAL
  #SEGMENT_DIRECTION_OBLIQUE
  
EndEnumeration

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<

Structure Segment
  
  X1.w
  Y1.w
  X2.w
  Y2.w
  
EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<

Macro GetSegmentX1(SegmentA)
  
  SegmentA\X1
  
EndMacro

Macro GetSegmentY1(SegmentA)
  
  SegmentA\Y1
  
EndMacro

Macro GetSegmentX2(SegmentA)
  
  SegmentA\X2
  
EndMacro

Macro GetSegmentY2(SegmentA)
  
  SegmentA\Y2
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<

Macro SetSegmentX1(SegmentA, P_X1)
  
  GetSegmentX1(SegmentA) = P_X1
  
EndMacro

Macro SetSegmentY1(SegmentA, P_Y1)
  
  GetSegmentY1(SegmentA) = P_Y1
  
EndMacro

Macro SetSegmentX2(SegmentA, P_X2)
  
  GetSegmentX2(SegmentA) = P_X2
  
EndMacro

Macro SetSegmentY2(SegmentA, P_Y2)
  
  GetSegmentY2(SegmentA) = P_Y2
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Update <<<<<

Macro UpdateSegment(SegmentA, P_X1, P_Y1, P_X2, P_Y2)
  
  SetSegmentX1(SegmentA, P_X1)
  SetSegmentY1(SegmentA, P_Y1)
  SetSegmentX2(SegmentA, P_X2)
  SetSegmentY2(SegmentA, P_Y2)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Reset <<<<<

Macro ResetSegment(SegmentA)
  
  SetSegmentX1(SegmentA, 0)
  SetSegmentY1(SegmentA, 0)
  SetSegmentX2(SegmentA, 0)
  SetSegmentY2(SegmentA, 0)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur ProductByScalar : R = A * Scalar <<<<<

Macro ProductByScalarSegment(SegmentR, SegmentA, Scalar)
  
  SetSegmentX1(SegmentR, GetSegmentX1(SegmentA) * Scalar)
  SetSegmentY1(SegmentR, GetSegmentY1(SegmentA) * Scalar)
  SetSegmentX2(SegmentR, GetSegmentX2(SegmentA) * Scalar)
  SetSegmentY2(SegmentR, GetSegmentY2(SegmentA) * Scalar)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Draw Segment <<<<<

Procedure DrawSegment(*SegmentA.Segment, Radius.w, Color.l, Direction.b)
  
  Select Direction
      
    Case #SEGMENT_DIRECTION_HORIZONTAL
      For PosX = GetSegmentX1(*SegmentA) To GetSegmentX2(*SegmentA)
        Circle(PosX, GetSegmentY1(*SegmentA), Radius, Color)
      Next
      
    Case #SEGMENT_DIRECTION_VERTICAL
      For PosY = GetSegmentY1(*SegmentA) To GetSegmentY2(*SegmentA)
        Circle(GetSegmentX1(*SegmentA), PosY, Radius, Color)
      Next
      
    Case #SEGMENT_DIRECTION_OBLIQUE
      For PosXY = GetSegmentX1(*SegmentA) To GetSegmentX2(*SegmentA)
        Circle(PosXY, PosXY, Radius, Color)
      Next
      
  EndSelect
  
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Déclaration de la Structure <<<<<

Structure Counter
  
  Pattern.s
  DigitRadius.w
  DigitFrontColor.l   ; <- color of segments when ON
  DigitBackColor.l    ; <- color of segments when OFF
  CounterBackColor.l  ; <- background color of display (not segments)
  Width.w
  Height.w
  Numbers.l[16]
  LastSecond.l
  
EndStructure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les observateurs <<<<<

Macro GetCounterPattern(CounterA)
  
  CounterA\Pattern
  
EndMacro

Macro GetCounterDigitRadius(CounterA)
  
  CounterA\DigitRadius
  
EndMacro

Macro GetCounterDigitFrontColor(CounterA)
  
  CounterA\DigitFrontColor
  
EndMacro

Macro GetCounterDigitBackColor(CounterA)
  
  CounterA\DigitBackColor
  
EndMacro

Macro GetCounterBackColor(CounterA)
  
  CounterA\CounterBackColor
  
EndMacro

Macro GetCounterWidth(CounterA)
  
  CounterA\Width
  
EndMacro

Macro GetCounterHeight(CounterA)
  
  CounterA\Height
  
EndMacro

Macro GetCounterNumbers(CounterA, NumbersID)
  
  CounterA\Numbers[NumbersID]
  
EndMacro

Macro GetCounterLastSecond(CounterA)
  
  CounterA\LastSecond
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Les mutateurs <<<<<

Macro SetCounterPattern(CounterA, P_Pattern)
  
  GetCounterPattern(CounterA) = P_Pattern
  
EndMacro

Macro SetCounterDigitRadius(CounterA, P_DigitRadius)
  
  GetCounterDigitRadius(CounterA) = P_DigitRadius
  
EndMacro

Macro SetCounterDigitFrontColor(CounterA, P_DigitFrontColor)
  
  GetCounterDigitFrontColor(CounterA) = P_DigitFrontColor
  
EndMacro

Macro SetCounterDigitBackColor(CounterA, P_DigitBackColor)
  
  GetCounterDigitBackColor(CounterA) = P_DigitBackColor
  
EndMacro

Macro SetCounterBackColor(CounterA, P_CounterBackColor)
  
  GetCounterBackColor(CounterA) = P_CounterBackColor
  
EndMacro

Macro SetCounterWidth(CounterA, P_Width)
  
  GetCounterWidth(CounterA) = P_Width
  
EndMacro

Macro SetCounterHeight(CounterA, P_Height)
  
  GetCounterHeight(CounterA) = P_Height
  
EndMacro

Macro SetCounterNumbers(CounterA, NumbersID, P_Numbers)
  
  GetCounterNumbers(CounterA, NumbersID) = P_Numbers
  
EndMacro

Macro SetCounterLastSecond(CounterA, P_LastSecond)
  
  GetCounterLastSecond(CounterA) = P_LastSecond
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< L'opérateur Reset <<<<<

Macro ResetCounter(CounterA)
  
  SetCounterPattern(CounterA, "")
  SetCounterDigitRadius(CounterA, 0)
  SetCounterDigitFrontColor(CounterA, 0)
  SetCounterDigitBackColor(CounterA, 0)
  SetCounterBackColor(CounterA, 0)
  SetCounterWidth(CounterA, 0)
  SetCounterHeight(CounterA, 0)
  
  For NumbersID = 0 To 15
    SetCounterNumbers(CounterA, NumbersID, 0)
  Next
  
  SetCounterLastSecond(CounterA, 0)
  
  ; ClearStructure(CounterA, Counter)
  
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Le Constructeur <<<<<

Procedure.i CreateNewCounter()
  
  *CounterA.Counter = AllocateMemory(SizeOf(Counter))
  
  If *CounterA = #Null
    MessageRequester("Fatal Error", "CreateNewCounter() - Impossible to Allocate Memory !")
    End
  EndIf
  
  ProcedureReturn *CounterA
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code généré en : 00.017 secondes (9117.65 lignes/seconde) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure SetCounterGadgetValue(GadgetID, Value.s)
  
  #Digits = "0123456789-+.: |"
  
  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 15)))
          
          For DigitID = 0 To Len(Value)
            
            Number.l = FindString(#Digits, Mid(Value, DigitID + 1, 1), 1)
            If Number
              Number - 1
              ; determine offset for specific digits/characters
              Select Number
                Case 12 ; "."
                  Off.l = 3
                Case 13 ; ":"
                  Off = 3
                Case 15 ; "|"
                  Off = 3
                  Number - 1
                Default
                  Off = 11 
              EndSelect
              
              DrawImage(ImageID(GetCounterNumbers(*CounterA, Number)), PosX, 0)
              offset = Off * GetCounterDigitRadius(*CounterA)
              
              PosX + offset
              
            EndIf
          Next
          
        StopDrawing()
        
        SetGadgetState(GadgetID, ImageID(GetCounterNumbers(*CounterA, 15)))
        
      EndIf
      
    EndIf
    
  EndIf
  
EndProcedure

Procedure DrawCounterGadgetDigits(GadgetID)
  ;      0
  ;   ------
  ;   |    |
  ; 3 |    | 5
  ;   ------
  ;   |  1 |
  ; 4 |    | 6
  ;   ------
  ;      2
  Dim Segments.Segment(10)
  
  UpdateSegment(Segments(0), 3, 1, 7, 1)   ; SEGMENT CENTRAL BAS
  UpdateSegment(Segments(1), 3, 9, 7, 9)   ; SEGMENT CENTRAL CENTRE (MOINS ET PLUS HORIZONTAL)
  UpdateSegment(Segments(2), 3, 17, 7, 17) ; SEGMENT CENTRAL HAUT
  UpdateSegment(Segments(3), 1, 3, 1, 7)   ; SEGMENT GAUCHE BAS
  UpdateSegment(Segments(4), 1, 11, 1, 15) ; SEGMENT GAUCHE HAUT
  UpdateSegment(Segments(5), 9, 3, 9, 7)   ; SEGMENT DROIT BAS
  UpdateSegment(Segments(6), 9, 11, 9, 15) ; SEGMENT DROIT HAUT
  
  UpdateSegment(Segments(7), 1, 5, 1, 5)   ; DEUX POINT BAS
  UpdateSegment(Segments(8), 1, 13, 1, 13) ; DEUX POINT HAUT
  UpdateSegment(Segments(9), 5, 7, 5, 11)  ; PLUS VERTICAL
  UpdateSegment(Segments(10), 1, 17, 1, 17)  ; DÉCIMAL
  
  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      
      For index = 0 To 10
        ProductByScalarSegment(Segments(index), Segments(index), GetCounterDigitRadius(*CounterA))
      Next
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "0" OK
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 0)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 4, 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 1  ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "1" OK
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 1)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 0, 1, 2  ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 4     ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "2" OK
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 2)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 4, 5
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 3, 6 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "3" OK
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 3)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 3, 4 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "4" OK
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 4)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 1
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 0, 2 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 4    ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "5"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 5)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 4, 5 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "6"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 6)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 4, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 5  ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "7"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 7)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 5, 6
              ;Case 3, 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 1, 2 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 4 ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "8"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 8)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 4, 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "9"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 9)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            
            Select SegmentID
                
              Case 0, 1, 2
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
                
              Case 3, 5, 6
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
              Case 4  ; off
                DrawSegment(Segments(SegmentID), GetCounterDigitRadius(*CounterA), GetCounterDigitBackColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
                
            EndSelect
            
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "-"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 10)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          DrawSegment(Segments(1), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "+"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 11)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            DrawSegment(Segments(1), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
            DrawSegment(Segments(9), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole "."
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 12)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            DrawSegment(Segments(10), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
          Next
          
        StopDrawing()
        
      EndIf
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Dessin du symbole ":"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 13)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
          For SegmentID = 0 To 6
            DrawSegment(Segments(7), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_HORIZONTAL)
            DrawSegment(Segments(8), GetCounterDigitRadius(*CounterA), GetCounterDigitFrontColor(*CounterA), #SEGMENT_DIRECTION_VERTICAL)
          Next
          
        StopDrawing()
        
      EndIf
      
      ; Dessin du symbole "|"
      
      If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 14)))
          
          Box(0, 0, GetCounterDigitRadius(*CounterA) * 10, GetCounterDigitRadius(*CounterA) * 18, GetCounterBackColor(*CounterA))
          
        StopDrawing()
        
      EndIf
      
    EndIf
    
  EndIf
  
EndProcedure

Procedure ChangeCounterGadgetColors(GadgetID, DigitFrontColor.l, DigitBackColor.l)
  
  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      
      SetCounterDigitFrontColor(*CounterA, DigitFrontColor)
      SetCounterDigitBackColor(*CounterA, DigitBackColor)
      
    EndIf
    
  EndIf
  
  DrawCounterGadgetDigits(GadgetID)
  
EndProcedure

Procedure CounterGadget(GadgetID, PosX.w, PosY.w, DigitPattern.s, DigitRadius.w, DigitFrontColor.l, DigitBackColor.l, CounterBackColor.l, Option = -1)
;Procedure CounterGadget(GadgetID, PosX.w, PosY.w, DigitPattern.s, DigitRadius.w, DigitFrontColor.l, DigitBackColor.l, Option = -1)
  
  *CounterA.Counter = CreateNewCounter()
  
  SetCounterPattern(*CounterA, DigitPattern)
  
  Point_TwoPoint = CountString(DigitPattern, ".") + CountString(DigitPattern, ":")
  DigitCount = Len(DigitPattern) - Point_TwoPoint
  
  SetCounterDigitRadius(*CounterA, DigitRadius)
  SetCounterWidth(*CounterA, DigitCount * DigitRadius * 11 + Point_TwoPoint * DigitRadius * 3 - DigitRadius)
  SetCounterHeight(*CounterA, DigitRadius * 18)
  
  If Option = -1
    GadgetHandle = ImageGadget(GadgetID, PosX, PosY, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), 0)
  Else
    GadgetHandle = ImageGadget(GadgetID, PosX, PosY, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), 0, #PB_Image_Border)
  EndIf
  
  If GadgetID = #PB_Any
    GadgetID = GadgetHandle
  EndIf
  
  SetGadgetData(GadgetID, *CounterA)
  
  For NumbersID = 0 To 14
    SetCounterNumbers(*CounterA, NumbersID, CreateImage(#PB_Any, DigitRadius * 10, DigitRadius * 18))
  Next
  
  SetCounterNumbers(*CounterA, 15, CreateImage(#PB_Any, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA)))
  
  If StartDrawing(ImageOutput(GetCounterNumbers(*CounterA, 15)))
      Box(0, 0, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), CounterBackColor)
;      Box(0, 0, GetCounterWidth(*CounterA), GetCounterHeight(*CounterA), DigitBackColor)
    StopDrawing()
  EndIf
  
  ChangeCounterGadgetColors(GadgetID, DigitFrontColor, DigitBackColor)
  
EndProcedure

Procedure ClockCounterGadget(GadgetID)
  
  time = Date()
  CurrentSecond = Second(time)
  
  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      
      If GetCounterLastSecond(*CounterA) <> CurrentSecond
        
        If CurrentSecond % 2 = 0
          SetCounterGadgetValue(GadgetID, FormatDate("%hh:%ii:%ss", time))
        Else
          SetCounterGadgetValue(GadgetID, FormatDate("%hh|%ii|%ss", time))
        EndIf
        
        SetCounterLastSecond(*CounterA, CurrentSecond)
        
      EndIf
      
    EndIf
    
  EndIf
  
EndProcedure

Procedure FreeCounterGadget(GadgetID)
  
  If IsGadget(GadgetID)
    
    *CounterA.Counter = GetGadgetData(GadgetID)
    
    If *CounterA <> #Null
      ResetCounter(*CounterA)
    EndIf
    
    FreeGadget(GadgetID)   
    
  EndIf
  
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<<
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<<
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Macro GadgetDown(GadgetID)
  
  GadgetY(GadgetID) + GadgetHeight(GadgetID)
  
EndMacro

Enumeration
  
  #Counter_01
  #Counter_02
  #Counter_03
  #Counter_04
  #Counter_05
  
EndEnumeration

If OpenWindow(0, 300, 300, 500, 375, "Counter Gadget", #PB_Window_SystemMenu)
  
  CounterGadget(#Counter_01, 5, 5, "88:88:88", 3, RGB(000, 255, 000), RGB(000, 64, 000), 0, -1)
  CounterGadget(#Counter_02, 5, GadgetDown(#Counter_01) + 10, "88:88:88", 2, RGB(255, 100, 000), 0, 0, 1)
  CounterGadget(#Counter_03, 5, GadgetDown(#Counter_02) + 10, "+8888888", 2, RGB(255, 255, 000), 0, 0, 1)
  CounterGadget(#Counter_04, 5, GadgetDown(#Counter_03) + 10, "888.888.888.888", 2, RGB(255, 000, 125), 0, 0, 1)
  CounterGadget(#Counter_05, 5, GadgetDown(#Counter_04) + 10, "88-88-8888", 2, RGB(100, 100, 255), 0, 0, 1)
  
  SetCounterGadgetValue(#Counter_03, "+01234567")
  SetCounterGadgetValue(#Counter_04, "192.223.145.123")
  SetCounterGadgetValue(#Counter_05, FormatDate("%dd-%mm-%yyyy", Date()))
  
  Repeat
    
    ClockCounterGadget(#Counter_01)
    ClockCounterGadget(#Counter_02)
    
    EventID = WaitWindowEvent(500)
    
    Select EventID
        
      Case #PB_Event_Menu
        
        Select EventMenu()
            
        EndSelect
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
            
        EndSelect
        
    EndSelect
    
  Until EventID = #PB_Event_CloseWindow
  
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CounterGadget

Post by Kwai chang caine »

Now i am in my home and test the first code on XP
And that works good, the Zeo appears at the end of the first counter
Again thanks for your nice sharing code 8)
ImageThe happiness is a road...
Not a destination
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: CounterGadget

Post by Guimauve »

ebs wrote:I hope you don't mind me messing around with your code!
Of course not. This is why this code has been released on the forum.

I have updated the code in the first post to make sure the ResetCounter() also free all created images to avoid a memory leak.
But I prefer to have the number seven draw with 4 segments instead of 3 but the code can be change as anyone wish.

Best regards.
Guimauve
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: CounterGadget

Post by Kwai chang caine »

Tested today on VISTA and that's works fine too, the 00 appears
Thanks 8)
ImageThe happiness is a road...
Not a destination
Post Reply