TimeBoxGadget - Like DateGadget..

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

TimeBoxGadget - Like DateGadget..

Post by minimy »

This is a gadget what i create for my project because dategadget no have seconds. Work using canvasgadget methods.
Have two principal lenguages, change global variable 'Lenguaje'.

I hope be interesting for somebody.

Code: Select all

;{
  Structure ColorEstructura
    ti.l
    pa.l
    tion.l
    paon.l
    fold.l
  EndStructure
  Global AppColor.ColorEstructura
    AppColor\pa    = $332211
    AppColor\paon  = $332211
    AppColor\ti    = $777777
    AppColor\tion  = $FF9944
    AppColor\fold  = $443322
    
    ;AppColor\pa    = $223344
    ;AppColor\paon  = $223344
    ;AppColor\ti    = $BBBBBB
    ;AppColor\tion  = $6677FF
    ;AppColor\fold  = $112233

;}
  
Global Lenguaje = 0 ;1=English, 0=Español

;-----------------------------------------------
Global Dim DiaDeLaSemana.s(1,6)
DiaDeLaSemana(0,0)="Domingo" 
DiaDeLaSemana(0,1)="Lunes"
DiaDeLaSemana(0,2)="Martes"
DiaDeLaSemana(0,3)="Miércoles"
DiaDeLaSemana(0,4)="Jueves"
DiaDeLaSemana(0,5)="Viernes"
DiaDeLaSemana(0,6)="Sábado"
DiaDeLaSemana(1,0)="Sunday" 
DiaDeLaSemana(1,1)="Monday"
DiaDeLaSemana(1,2)="Tuesday"
DiaDeLaSemana(1,3)="Wednesday"
DiaDeLaSemana(1,4)="Thursday"
DiaDeLaSemana(1,5)="Friday"
DiaDeLaSemana(1,6)="Saturday"
Structure estructuratimebox
  Gadget.l
  time.l
  font.l
  fontlt.l
  titu.s
EndStructure
Global NewList Tim.estructuratimebox()

Procedure TimeBoxDraw(gadget.l)
  Protected ch.s
  ForEach Tim()
    If Tim()\Gadget=gadget
      StartDrawing(CanvasOutput(Tim()\Gadget))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(Tim()\font))
        Box(0,0,OutputWidth(),OutputHeight(),AppColor\ti)
        Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
        ch.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
        DrawText(((OutputWidth()/2)-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2,ch,AppColor\ti)
        ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
        DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
        Box(OutputWidth()/2,2,(OutputWidth()/2)-2,OutputHeight()/2,AppColor\ti)
        DrawingFont(FontID(Tim()\fontlt))
        DrawText(((OutputWidth()/2)-TextWidth(Tim()\titu))/2+(OutputWidth()/2),((OutputHeight()/2)-TextHeight(Tim()\titu))/2,Tim()\titu,AppColor\pa)
      StopDrawing()
      Break
    EndIf
  Next
EndProcedure
Procedure TimeBoxSet(gadget.l,date.l)
  ForEach Tim()
    If Tim()\Gadget=gadget
      Tim()\time=date
      TimeBoxDraw(Tim()\Gadget)
      Break
    EndIf
  Next
EndProcedure
Procedure TimeBoxGet(gadget.l)
  Protected valor.l=-1
  ForEach Tim()
    If Tim()\Gadget=gadget
      valor=Tim()\time
      Break
    EndIf
  Next
  ProcedureReturn valor
EndProcedure
Procedure TimeBoxAdd(x.i,y.i,w.i,h.i,date.l,font.s="Arial",txt.s="")
  Protected size = (w+h)/20
  AddElement(Tim())
  Tim()\Gadget  = CanvasGadget(#PB_Any,x,y,w,h,#PB_Canvas_Keyboard)
  Tim()\time    = date
  Tim()\titu    = txt
  Tim()\font    = LoadFont(#PB_Any,font,size,#PB_Font_HighQuality)
  Tim()\fontlt  = LoadFont(#PB_Any,font,size*0.75,#PB_Font_HighQuality)
  TimeBoxDraw(Tim()\Gadget)
  ProcedureReturn Tim()\Gadget
EndProcedure
Procedure TimEvent(EventGadget,EventType)
  Static Pulsado.b
  Protected gadget=-1
  ForEach Tim()
    If EventGadget=Tim()\gadget
      StartDrawing(CanvasOutput(Tim()\Gadget))
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(FontID(Tim()\font))
      Protected x.i=GetGadgetAttribute(Tim()\gadget,#PB_Canvas_MouseX)
      Protected y.i=GetGadgetAttribute(Tim()\gadget,#PB_Canvas_MouseY)
      ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
      chf.s=FormatDate("%dd-%mm-%yyyy",Tim()\time)
      chh.s=FormatDate("%hh:%ii:%ss",Tim()\time)
      yy = (OutputHeight()-TextHeight(ch))/2;+(OutputWidth()/2)
      xf0 = (OutputWidth()-TextWidth(ch))/2
      xf1= xf0+TextWidth("00-")
      xf2= xf1+TextWidth("00-")
      xf3= xf2+TextWidth("0000")
      xh0= xf3+TextWidth(" ")
      xh1= xh0+TextWidth("00:")
      xh2= xh1+TextWidth("00:")
;       Protected LogW.i = Rule3(Tim()\scrw,GadgetWidth(Tim()\gadget),Tim()\boxw)* Tim()\boxs)
;       Protected LogH.i = Rule3(Tim()\scrw,GadgetWidth(Tim()\gadget),Tim()\boxh)* Tim()\boxs)
      If EventType=#PB_EventType_MouseEnter
        SetGadgetAttribute(EventGadget,#PB_Canvas_Cursor,#PB_Cursor_Hand)

      ElseIf EventType=#PB_EventType_MouseLeave
        SetGadgetAttribute(EventGadget,#PB_Canvas_Cursor,#PB_Cursor_Default)
        Box(0,0,OutputWidth(),OutputHeight(),AppColor\ti)
        Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
        dia.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
        DrawingFont(FontID(Tim()\font))
        DrawText(((OutputWidth()/2)-TextWidth(dia))/2,((OutputHeight()/2)-TextHeight(dia))/2,dia,AppColor\ti)
        DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
        Box(OutputWidth()/2,2,(OutputWidth()/2)-2,OutputHeight()/2,AppColor\ti)
        DrawingFont(FontID(Tim()\fontlt))
        DrawText(((OutputWidth()/2)-TextWidth(Tim()\titu))/2+(OutputWidth()/2),((OutputHeight()/2)-TextHeight(Tim()\titu))/2,Tim()\titu,AppColor\pa)
        
      ElseIf EventType=#PB_EventType_MouseMove        
        Box(0,0,OutputWidth(),OutputHeight(),AppColor\tion)
          Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
          ch.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
          DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2,ch,AppColor\ti)
          ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
          DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
          If (x>xf0 And x<xf1) Or y<OutputHeight()/2; dia
            dia.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
            DrawText((OutputWidth()-TextWidth(dia))/2,((OutputHeight()/2)-TextHeight(dia))/2,dia,AppColor\tion)
            DrawText(xf0,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chf,1,2),AppColor\tion)
          ElseIf x>xf1 And x<xf2 And y>OutputHeight()/2; mes
            DrawText(xf1,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chf,4,2),AppColor\tion)
          ElseIf x>xf2 And x<xf3 And y>OutputHeight()/2; año
            me=Val(Mid(chf,7,4)) + 1 ;: If me>12:me=1:EndIf
            DrawText(xf2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chf,7,4),AppColor\tion)
          ElseIf x>xh0 And x<xh1 And y>OutputHeight()/2; hora
            DrawText(xh0,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chh,1,2),AppColor\tion)
          ElseIf x>xh1 And x<xh2 And y>OutputHeight()/2; min
            DrawText(xh1,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chh,4,2),AppColor\tion)
          ElseIf x>xh2 And y>OutputHeight()/2;And x<xh3 ; seg
            DrawText(xh2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),Mid(chh,7,2),AppColor\tion)
          EndIf
        
      ElseIf EventType=#PB_EventType_LeftButtonUp
        gadget=Tim()\gadget
      ElseIf EventType=#PB_EventType_RightButtonUp
        gadget=Tim()\gadget
        
      ElseIf EventType=#PB_EventType_MouseWheel
        wheel = GetGadgetAttribute(EventGadget,#PB_Canvas_WheelDelta)
          If (x>xf0 And x<xf1) Or y<OutputHeight()/2; dia
            Tim()\time=AddDate(Tim()\time, #PB_Date_Day, wheel)
          ElseIf x>xf1 And x<xf2 And y>OutputHeight()/2; mes
            Tim()\time=AddDate(Tim()\time, #PB_Date_Month, wheel)
          ElseIf x>xf2 And x<xf3 And y>OutputHeight()/2; año
            Tim()\time=AddDate(Tim()\time, #PB_Date_Year, wheel)
          ElseIf x>xh0 And x<xh1 And y>OutputHeight()/2; hora
            Tim()\time=AddDate(Tim()\time, #PB_Date_Hour, wheel)
          ElseIf x>xh1 And x<xh2 And y>OutputHeight()/2; min
            Tim()\time=AddDate(Tim()\time, #PB_Date_Minute, wheel)
          ElseIf x>xh2 And y>OutputHeight()/2;And x<xh3 ; seg
            Tim()\time=AddDate(Tim()\time, #PB_Date_Second, wheel)
          EndIf
          Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
          dia.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
          DrawText((OutputWidth()-TextWidth(dia))/2,((OutputHeight()/2)-TextHeight(dia))/2,dia,AppColor\ti)
          ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
          DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
          gadget=Tim()\gadget
          
      ElseIf EventType=#PB_EventType_LeftButtonDown
          If (x>xf0 And x<xf1) Or y<OutputHeight()/2; dia
            Tim()\time=AddDate(Tim()\time, #PB_Date_Day, 1)
          ElseIf x>xf1 And x<xf2 And y>OutputHeight()/2; mes
            Tim()\time=AddDate(Tim()\time, #PB_Date_Month, 1)
          ElseIf x>xf2 And x<xf3 And y>OutputHeight()/2; año
            Tim()\time=AddDate(Tim()\time, #PB_Date_Year, 1)
          ElseIf x>xh0 And x<xh1 And y>OutputHeight()/2; hora
            Tim()\time=AddDate(Tim()\time, #PB_Date_Hour, 1)
          ElseIf x>xh1 And x<xh2 And y>OutputHeight()/2; min
            Tim()\time=AddDate(Tim()\time, #PB_Date_Minute, 1)
          ElseIf x>xh2 And y>OutputHeight()/2;And x<xh3 ; seg
            Tim()\time=AddDate(Tim()\time, #PB_Date_Second, 1)
          EndIf
          Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
          dia.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
          DrawText((OutputWidth()-TextWidth(dia))/2,((OutputHeight()/2)-TextHeight(dia))/2,dia,AppColor\ti)
          ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
          DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
          gadget=Tim()\gadget
      ElseIf EventType=#PB_EventType_RightButtonDown
          If (x>xf0 And x<xf1) Or y<OutputHeight()/2; dia
            Tim()\time=AddDate(Tim()\time, #PB_Date_Day, -1)
          ElseIf x>xf1 And x<xf2 And y>OutputHeight()/2; mes
            Tim()\time=AddDate(Tim()\time, #PB_Date_Month, -1)
          ElseIf x>xf2 And x<xf3 And y>OutputHeight()/2; año
            Tim()\time=AddDate(Tim()\time, #PB_Date_Year, -1)
          ElseIf x>xh0 And x<xh1 And y>OutputHeight()/2; hora
            Tim()\time=AddDate(Tim()\time, #PB_Date_Hour, -1)
          ElseIf x>xh1 And x<xh2 And y>OutputHeight()/2; min
            Tim()\time=AddDate(Tim()\time, #PB_Date_Minute, -1)
          ElseIf x>xh2 And y>OutputHeight()/2;And x<xh3 ; seg
            Tim()\time=AddDate(Tim()\time, #PB_Date_Second, -1)
          EndIf
          Box(1,1,OutputWidth()-2,OutputHeight()-2,AppColor\pa)
          dia.s=DiaDeLaSemana(Lenguaje,DayOfWeek(Tim()\time))
          DrawText((OutputWidth()-TextWidth(dia))/2,((OutputHeight()/2)-TextHeight(dia))/2,dia,AppColor\ti)
          ch.s=FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",Tim()\time)
          DrawText((OutputWidth()-TextWidth(ch))/2,((OutputHeight()/2)-TextHeight(ch))/2+(OutputHeight()/2),ch,AppColor\ti)
          gadget=Tim()\gadget
      EndIf

      StopDrawing()
      Break
    EndIf
  Next
  ProcedureReturn Gadget
EndProcedure
CompilerIf Not #PB_Compiler_IsIncludeFile
  

Global W_Int = OpenWindow(#PB_Any, 183, 58,1300, 600, "HORUSEYE", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_TitleBar)
SetWindowColor(W_Int,AppColor\pa)

TimPrueba  = TimeBoxAdd(1050, 20,200,50,Date(),"Arial","Time")
TimPrueba2 = TimeBoxAdd(10, 10,600,150,Date(),"Arial","Wake")


Repeat
  Event = WaitWindowEvent(1)
  
  Select Event
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      
      ; ------ TimeBox FECHA y HORA (date & time)
      TimGadget = TimEvent(EventGadget,EventType)
      
      
      If TimGadget = TimPrueba
        Debug TimeBoxGet(TimGadget)
      EndIf
      
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = W_Int
        CloseWindow(W_Int)
        W_Int = 0
        Break
      EndIf
  EndSelect
ForEver

CompilerEndIf
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: TimeBoxGadget - Like DateGadget..

Post by Demivec »

Very nice. Thanks for posting this.

I appreciate that the time and date information can be changed with right and left clicking of the mouse.

I did notice that the date information can only be formatted in one way and that the code has to be modified each time a change is needed. It would be a nice effect to have this accessible through some additional procedures.

Perhaps adding a new fields like dateformat to the 'estructuratimebox' structure. Maybe a field like 'timeformat' might be useful as well for 24/12 hour time formats.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: TimeBoxGadget - Like DateGadget..

Post by davido »

@minimy,

Thank you for sharing, excellent. :D

Froze on my MacBook Pro, but managed to get it working by changing all 'longs' to 'integers'
DE AA EB
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: TimeBoxGadget - Like DateGadget..

Post by Vera »

Dito ~ thanks minimy

It runs fine on Linux :-)

ps: exchanging 'long' by 'integers' works the same
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: TimeBoxGadget - Like DateGadget..

Post by RSBasic »

Very nice, thanks. :)
Image
Image
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: TimeBoxGadget - Like DateGadget..

Post by minimy »

Hi PB coders! ..Yes, this work on Linux, Mac and Windows.
Im working now in a 'global timeline' to move between more distant dates, like year 0 or before.. I think this can be very usefull for educational or historyc projects. Gregorian calendar only work with relative modern history information. PB date is from 1970, we can put the man on the moon for example because this was in 1969.
When study other cultures, in a lot of cases more advanced (only see TV and say me.. :mrgreen: ) need other rules to size this.
Example Summerians, Nubians or Egyptians, thousends years before of the year 0.
If any body have any idea to improve this proyect, please put here.
When project finish I will post it. Thanks friends!
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
glomph
User
User
Posts: 48
Joined: Tue Apr 27, 2010 1:43 am
Location: St. Elsewhere / Germany
Contact:

Re: TimeBoxGadget - Like DateGadget..

Post by glomph »

Hi,
search in the forum for julianday
and you find what you need.
Sincerly
glomph
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: TimeBoxGadget - Like DateGadget..

Post by Vera »

Some tips you might like to check out:

* DayOfWeek(Datum) (My_Date.pbi, gregorian)
* Date Calculations (julian)
* DateAndTimeA.pbi - advanced date-functionality (1601 - 9999, WIN only)
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: TimeBoxGadget - Like DateGadget..

Post by minimy »

Thanks a lot friends for your quick and explicit help. Sound very interesting! Go to invetigate your recomendations.
PB community is always the best! :D
If translation=Error: reply="Sorry, Im Spanish": Endif
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: TimeBoxGadget - Like DateGadget..

Post by infratec »

Hi,

with agreement of minimy I publish a revised version.

1. No list (speed improvement)
2. Added the posibilty for a clock
3. Added change event
4. Simplified the code (hopefully)
5. No need for main loop action

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


;{
  Structure ColorEstructura
    ti.i
    pa.i
    tion.i
    paon.i
    fold.i
  EndStructure
 
  Global AppColor.ColorEstructura
    AppColor\pa    = $332211
    AppColor\paon  = $332211
    AppColor\ti    = $777777
    AppColor\tion  = $FF9944
    AppColor\fold  = $443322
   
    ;AppColor\pa    = $223344
    ;AppColor\paon  = $223344
    ;AppColor\ti    = $BBBBBB
    ;AppColor\tion  = $6677FF
    ;AppColor\fold  = $112233

;}
 
Global Lenguaje = 1 ;1=English, 0=Español


Global Dim DiaDeLaSemana.s(1,6)
DiaDeLaSemana(0,0)="Domingo"
DiaDeLaSemana(0,1)="Lunes"
DiaDeLaSemana(0,2)="Martes"
DiaDeLaSemana(0,3)="Miércoles"
DiaDeLaSemana(0,4)="Jueves"
DiaDeLaSemana(0,5)="Viernes"
DiaDeLaSemana(0,6)="Sábado"
DiaDeLaSemana(1,0)="Sunday"
DiaDeLaSemana(1,1)="Monday"
DiaDeLaSemana(1,2)="Tuesday"
DiaDeLaSemana(1,3)="Wednesday"
DiaDeLaSemana(1,4)="Thursday"
DiaDeLaSemana(1,5)="Friday"
DiaDeLaSemana(1,6)="Saturday"

Structure estructuratimebox
  gadget.i
  width.i
  height.i
  time.i
  font.i
  fontlt.i
  title.s
  window.i
  auto.i
  seconds.i
  active.i
  xf0.i
  xf1.i
  xf2.i
  xf3.i
  xh0.i
  xh1.i
  xh2.i
  mousex.i
  mousey.i
EndStructure




Procedure TimeBoxGadgetDraw(*Time.estructuratimebox, Init.i=#False)
  
  Protected OWHalf.i, OHHalf.i, THeight.i, ch$
  
  
  StartDrawing(CanvasOutput(*Time\Gadget))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawingFont(FontID(*Time\font))
  
  OWHalf = *Time\width >> 1
  OHHalf = *Time\height >> 1
  THeight = TextHeight(" ")
  
  Box(0, 0, *Time\width, *Time\height, AppColor\ti)
  Box(1, 1, *Time\width - 2, *Time\height - 2, AppColor\pa)
  ch$ = DiaDeLaSemana(Lenguaje, DayOfWeek(*Time\time))
  DrawText((OWHalf - TextWidth(ch$)) / 2,(OHHalf - THeight) / 2, ch$, AppColor\ti)
  ch$ = FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss", *Time\time)
  
  If Init
    *Time\xf0 = (*Time\width - TextWidth(ch$)) / 2
    *Time\xf1 = *Time\xf0 + TextWidth("00-")
    *Time\xf2 = *Time\xf1 + TextWidth("00-")
    *Time\xf3 = *Time\xf2 + TextWidth("0000")
    *Time\xh0 = *Time\xf3 + TextWidth(" ")
    *Time\xh1 = *Time\xh0 + TextWidth("00:")
    *Time\xh2 = *Time\xh1 + TextWidth("00:")
  EndIf
  
  DrawText((*Time\width - TextWidth(ch$)) / 2,(OHHalf - THeight) / 2 + OHHalf, ch$, AppColor\ti)
  Box(OWHalf,2, OWHalf - 2, OHHalf, AppColor\ti)
  DrawingFont(FontID(*Time\fontlt))
  DrawText((OWHalf - TextWidth(*Time\title)) / 2 + OWHalf,(OHHalf - THeight) / 2, *Time\title, AppColor\pa)
  StopDrawing()
  
EndProcedure




Procedure TimeBoxGadgetDrawTime(*Time.estructuratimebox)
  
  Protected ch.s, dia.s
  
  
  ch = FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss", *Time\time)
  
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawingFont(FontID(*Time\font))
  
  Box(1, ((*Time\height/2) - TextHeight(ch))/2+(*Time\height/2), *Time\width - 2, TextHeight(ch), AppColor\pa)
  
  DrawText((*Time\width - TextWidth(ch)) / 2,((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), ch, AppColor\ti)
  
  If *Time\active
    If (*Time\mousex > *Time\xf0 And *Time\mousex < *Time\xf1) Or *Time\mousey < *Time\height / 2; dia
      dia = DiaDeLaSemana(Lenguaje,DayOfWeek(*Time\time))
      DrawText((*Time\width - TextWidth(dia)) / 2,((*Time\height / 2) - TextHeight(dia)) / 2, dia, AppColor\tion)
      DrawText(*Time\xf0, ((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), Mid(ch, 1, 2), AppColor\tion)
    ElseIf *Time\mousex > *Time\xf1 And *Time\mousex < *Time\xf2 And *Time\mousey > *Time\height / 2; mes
      DrawText(*Time\xf1, ((*Time\height / 2) - TextHeight(ch))/2+(*Time\height / 2), Mid(ch, 4, 2), AppColor\tion)
    ElseIf *Time\mousex > *Time\xf2 And *Time\mousex < *Time\xf3 And *Time\mousey > *Time\height / 2; año
      ;me=Val(Mid(chf,7,4)) + 1 ;: If me>12:me=1:EndIf
      DrawText(*Time\xf2, ((*time\height / 2)- TextHeight(ch)) / 2 + (*Time\height / 2), Mid(ch, 7, 4), AppColor\tion)
    ElseIf *Time\mousex > *Time\xh0 And *Time\mousex < *Time\xh1 And *Time\mousey > *Time\height / 2; hora
      DrawText(*Time\xh0, ((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), Mid(ch, 12, 2), AppColor\tion)
    ElseIf *Time\mousex > *Time\xh1 And *Time\mousex < *Time\xh2 And *Time\mousey > *Time\height / 2; min
      DrawText(*Time\xh1, ((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), Mid(ch, 15, 2), AppColor\tion)
    ElseIf *Time\mousex > *Time\xh2 And *Time\mousey> *Time\height / 2;And x<xh3 ; seg
      DrawText(*Time\xh2, ((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), Mid(ch, 18, 2), AppColor\tion)
    EndIf
  EndIf
  
EndProcedure




Procedure TimeBoxGadgetSet(Gadget.i, Value.i)
  
  Protected *Time.estructuratimebox
  
  *Time = GetGadgetData(Gadget)
  
  *Time\time = Value
  
  StartDrawing(CanvasOutput(*Time\Gadget))
  TimeBoxGadgetDrawTime(*Time)
  StopDrawing()
  
EndProcedure




Procedure.i TimeBoxGadgetGet(Gadget.i)
  
  Protected *Time.estructuratimebox
  
  *Time = GetGadgetData(Gadget)
  
  ProcedureReturn *Time\time
  
EndProcedure




Procedure TimeBoxGadgetAddChange(*Time.estructuratimebox, Delta.i)
  
  If (*Time\mousex > *Time\xf0 And *Time\mousex < *Time\xf1) Or *Time\mousey < *Time\height / 2; dia
    *Time\time = AddDate(*Time\time, #PB_Date_Day, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  ElseIf *Time\mousex > *Time\xf1 And *Time\mousex < *Time\xf2 And *Time\mousey > *Time\height / 2; mes
    *Time\time = AddDate(*Time\time, #PB_Date_Month, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  ElseIf *Time\mousex > *Time\xf2 And *Time\mousex < *Time\xf3 And *Time\mousey > *Time\height / 2; año
    *Time\time=AddDate(*Time\time, #PB_Date_Year, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  ElseIf *Time\mousex > *Time\xh0 And *Time\mousex < *Time\xh1 And *Time\mousey > *Time\height / 2; hora
    *Time\time = AddDate(*Time\time, #PB_Date_Hour, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  ElseIf *Time\mousex > *Time\xh1 And *Time\mousex < *Time\xh2 And *Time\mousey > *Time\height / 2; min
    *Time\time = AddDate(*Time\time, #PB_Date_Minute, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  ElseIf *Time\mousex > *Time\xh2 And *Time\mousey > *Time\height / 2;And x<xh3 ; seg
    *Time\time = AddDate(*Time\time, #PB_Date_Second, Delta)
    PostEvent(#PB_Event_Gadget, EventWindow(), *Time\Gadget, #PB_EventType_Change, *Time\time)
  EndIf
  
EndProcedure




Procedure TimeBoxGadgetEvent()
  
  Protected.i EventType, me, DrawFlag
  Protected.s ch, dia
  Protected *Time.estructuratimebox
  
  
  EventType = EventType()
  
  If EventType <> #PB_EventType_Change
    
    *Time = GetGadgetData(EventGadget())
    
    StartDrawing(CanvasOutput(*Time\Gadget))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(FontID(*Time\font))
    *Time\mousex = GetGadgetAttribute(*Time\gadget,#PB_Canvas_MouseX)
    *Time\mousey = GetGadgetAttribute(*Time\gadget,#PB_Canvas_MouseY)
    
    
    
    If EventType=#PB_EventType_MouseEnter
      SetGadgetAttribute(*Time\Gadget,#PB_Canvas_Cursor,#PB_Cursor_Hand)
      *Time\active = #True
      
    ElseIf EventType=#PB_EventType_MouseLeave
      SetGadgetAttribute(*Time\Gadget,#PB_Canvas_Cursor,#PB_Cursor_Default)
      Box(0, 0, *Time\width, *Time\height, AppColor\ti)
      Box(1, 1, *Time\width - 2, *Time\height - 2, AppColor\pa)
      dia = DiaDeLaSemana(Lenguaje, DayOfWeek(*Time\time))
      DrawingFont(FontID(*Time\font))
      DrawText(((*Time\width / 2) - TextWidth(dia)) / 2, ((*Time\height / 2) - TextHeight(dia)) / 2, dia,AppColor\ti)
      
      ch = FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss",*Time\time)
      DrawText((*Time\width - TextWidth(ch)) / 2, ((*Time\height / 2) - TextHeight(ch)) / 2 + (*Time\height / 2), ch, AppColor\ti)
      Box(*Time\width / 2, 2, (*Time\width / 2) - 2, *Time\height / 2, AppColor\ti)
      DrawingFont(FontID(*Time\fontlt))
      DrawText(((*Time\width / 2) - TextWidth(*Time\title)) / 2 + (*Time\width / 2), ((*Time\height / 2) - TextHeight(*Time\title)) / 2, *Time\title, AppColor\pa)
      *Time\active = #False
      
    ElseIf EventType = #PB_EventType_MouseMove
      Box(0, 0, *Time\width, *Time\height, AppColor\tion)
      DrawFlag = #True
      
    ElseIf EventType = #PB_EventType_MouseWheel
      TimeBoxGadgetAddChange(*Time, GetGadgetAttribute(*Time\gadget,#PB_Canvas_WheelDelta))
      DrawFlag = #True
      
    ElseIf EventType = #PB_EventType_LeftButtonDown
      TimeBoxGadgetAddChange(*Time, 1)
      DrawFlag = #True
      
    ElseIf EventType = #PB_EventType_RightButtonDown
      TimeBoxGadgetAddChange(*Time, -1)
      DrawFlag = #True
      
    EndIf
    
    If DrawFlag
      Box(1, 1, *Time\width - 2, *Time\height - 2, AppColor\pa)
      dia = DiaDeLaSemana(Lenguaje,DayOfWeek(*Time\time))
      DrawText((*time\width - TextWidth(dia)) / 2, ((*Time\height / 2) - TextHeight(dia)) / 2, dia, AppColor\ti)
      TimeBoxGadgetDrawTime(*Time)
    EndIf
    
    StopDrawing()
    
  EndIf
  
EndProcedure




Procedure TimeBoxGadgetAuto()
  
  Protected *Time.estructuratimebox
  
  *Time = GetWindowData(EventWindow())
  If *Time\seconds <> Date()
    *Time\time + 1
    StartDrawing(CanvasOutput(*Time\Gadget))
    TimeBoxGadgetDrawTime(*Time)
    StopDrawing()
    *Time\seconds = Date()
  EndIf
  
EndProcedure




Procedure.i TimeBoxGadget(Gadget.i,x.i,y.i,w.i,h.i,date.i,font.s="Arial",txt.s="",auto.i=#False)
  
  Protected size = (w+h)/20
  Protected *Time.estructuratimebox, Result.i
  
  *Time = AllocateMemory(SizeOf(estructuratimebox))
  
  Result = CanvasGadget(Gadget,x,y,w,h,#PB_Canvas_Keyboard)
  If Gadget = #PB_Any
    *Time\Gadget = Result
  Else
    *Time\Gadget = Gadget
  EndIf
  *Time\time    = date
  *Time\title   = txt
  *Time\font    = LoadFont(#PB_Any,font,size,#PB_Font_HighQuality)
  *Time\fontlt  = LoadFont(#PB_Any,font,size*0.75,#PB_Font_HighQuality)
  *Time\auto    = auto
  *Time\width   = w
  *Time\height  = h
  
  SetGadgetData(*Time\Gadget, *Time)
  
  TimeBoxGadgetDraw(*Time, #True)
  
  BindGadgetEvent(*Time\Gadget, @TimeBoxGadgetEvent(), #PB_All)
  
  If auto
    *Time\seconds = Date()
    *Time\window = OpenWindow(#PB_Any, 0, 0, 1, 1, "", #PB_Window_Invisible)
    SetWindowData(*Time\window, *Time)
    AddWindowTimer(*Time\window, 1, 100)
    BindEvent(#PB_Event_Timer, @TimeBoxGadgetAuto(), *Time\window)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure




CompilerIf #PB_Compiler_IsMainFile
  
Define.i W_Int, TimPrueba, Event

W_Int = OpenWindow(#PB_Any, 183, 58,1000, 600, "HORUSEYE", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_TitleBar)
SetWindowColor(W_Int,AppColor\pa)

TimeBoxGadget(0, 780, 20,200,50,Date(),"Arial","Time")
TimPrueba = TimeBoxGadget(#PB_Any, 10, 10,600,150,Date(),"Arial","Wake", #True)


PostEvent(#PB_Event_Gadget, W_Int, 0, #PB_EventType_Change, 123)


Repeat
  Event = WaitWindowEvent()
 
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_Change
            Debug "0: " + Str(TimeBoxGadgetGet(0))
          EndIf
        Case TimPrueba
          If EventType() = #PB_EventType_Change
            Debug "TimePrueba: " + Str(TimeBoxGadgetGet(TimPrueba))
          EndIf
      EndSelect
    Case #PB_Event_CloseWindow
  EndSelect
 
Until Event = #PB_Event_CloseWindow

CompilerEndIf
Bernd
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: TimeBoxGadget - Like DateGadget..

Post by minimy »

Thanks Infratec, good job!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply