Micro calendar

Share your advanced PureBasic knowledge/code with the community.
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Micro calendar

Post by WilliamL »

I was bored and made this 'micro' calendar with the calendargadget. Nothing special and should run on all platforms.

Code: Select all

EnableExplicit

;windows
#calendarwnd=1

;efids
Enumeration 1
    #leftbutton
    #rightbutton
    #calendar1
EndEnumeration

#maxcalendars=12 ; one year
Structure caldata
    Month.l
    Year.l
EndStructure
Global Dim Calendar.caldata(#maxcalendars)

Global totcalendars
Define event

Procedure stop(err$)
  ;Speak(#snduhoh)
  If MessageRequester("A stop is encountered...",err$+Chr(13)+"Do you want To quit?",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes:End:EndIf
EndProcedure

Procedure DrawWindow()
    ; resize window and fill in totcalendars calendars
    Define cnt,xtab,ytab,xwidth=30,yheight=22
    Define calendarx=138 ; size of calendar gadget
    Define calendary=149 ; size of calendar gadget
    Define xoffset=10
    Define yoffset=22
    Define wndx=((xoffset+calendarx)*totcalendars)+xoffset
    Define wndy=yoffset+calendary+xoffset
    Define thisyear=Year(Date())
    Define thismonth=Month(Date())
    Define thisday=Day(Date())
    
    If OpenWindow(#calendarwnd,0,0,wndx,wndy, "Micro Calendar", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
        Debug totcalendars
        xtab=xoffset : ytab=yoffset
        For cnt=1 To totcalendars
            CalendarGadget(#calendar1+cnt-1,xtab,ytab,calendarx,calendary)
            If thismonth=Calendar(cnt)\Month And thisyear=Calendar(cnt)\Year
                SetGadgetState(#calendar1+cnt-1,Date(Calendar(cnt)\Year,Calendar(cnt)\Month,thisday,12,0,0))
            Else
                SetGadgetState(#calendar1+cnt-1,Date(Calendar(cnt)\Year,Calendar(cnt)\Month,1,12,0,0))
            EndIf
            xtab+calendarx+xoffset
        Next
        
        xtab=xoffset : ytab=1
        ButtonGadget(#leftbutton, xtab, ytab, xWidth, yHeight,"<")
        
        xtab=WindowWidth(#calendarwnd)-xwidth-xoffset
        ButtonGadget(#rightbutton, xtab, ytab, xWidth, yHeight,">")
    EndIf
EndProcedure

Procedure AddMonth(pos)
    ; pos=#leftbutton or pos=#rightbutton
    Define cnt
    totcalendars+1
    If pos=#leftbutton
        For cnt=totcalendars To 2 Step -1
            Calendar(cnt)\Month=Calendar(cnt-1)\Month
            Calendar(cnt)\Year =Calendar(cnt-1)\Year
        Next
        Calendar(1)\Month=Calendar(2)\Month-1
        Calendar(1)\Year=Calendar(2)\Year
        If Calendar(1)\Month<1 : Calendar(1)\Month=12 : Calendar(1)\Year=Calendar(2)\Year-1 : EndIf
    Else ; pos=#rightbutton
        Calendar(totcalendars)\Month=Calendar(totcalendars-1)\Month+1
        Calendar(totcalendars)\Year=Calendar(totcalendars-1)\Year
        If Calendar(totcalendars)\Month>12 : Calendar(totcalendars)\Month=1 : Calendar(totcalendars)\Year=Calendar(totcalendars)\Year+1 : EndIf
    EndIf
    DrawWindow()
EndProcedure

totcalendars=1
Calendar(totcalendars)\Month=Month(Date())
Calendar(totcalendars)\Year =Year(Date())
DrawWindow()

Repeat
    Event = WaitWindowEvent()
    Select Event
    Case #PB_Event_CloseWindow
        end
        ;Totcalendars=1
       ; Calendar(1)\Month=Month(Date())
        ;DrawWindow()
    Case #PB_Event_Gadget
        Select EventGadget()
            Case #leftbutton ;  : Debug "Button #leftbutton clicked!"
                AddMonth(#leftbutton)
            Case #rightbutton ; : Debug "Button #rightbutton clicked!"
                AddMonth(#rightbutton)
        EndSelect
    EndSelect
ForEver
oh, and if anybody has an Inkbird Bluetooth thermometer, I wrote a little program to parse the data into months and graph them.

https://www.amazon.com/Inkbird-Wireless ... 218&sr=8-3
Last edited by WilliamL on Thu Mar 12, 2020 4:51 pm, edited 1 time in total.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Micro calendar

Post by Kwai chang caine »

Perhaps better when we can close the soft :wink:

Code: Select all

Case #PB_Event_CloseWindow
    
       If Totcalendars=1
        CloseWindow(#calendarwnd)
        Break
       EndIf
       Totcalendars=1
       Calendar(1)\Month=Month(Date())
       DrawWindow()
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Micro calendar

Post by WilliamL »

I guess that is what I get when I only run it from the editor. :oops:

Actually the close button was to re-set the window to the present month but I changed it to quit.

You can add your improvements...
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Micro calendar

Post by davido »

@WilliamL,
Looks great. Thank you for sharing.
DE AA EB
Post Reply