Jump to content

Countdown days


Rex
 Share

Recommended Posts

I needed a program that could count down to xx days.

I found lots of count downer's, but common for them all was that I needed to know the end date, but what I needed was one where I could just add the amount of days.

So I ended up creating my own count down, that could do just that.

Now I would like to share what I ended up with.

I did borrow some of the count down code from this nice count down Stylish Countdown GUI by @billthecreator

So what can this countdown do?

1. Countdown to a specific day, that can be chosen either by the amount of days to count down to, or by choosing a date.
2. Edit an existing event.
3. Delete an existing event.
4. On mouse over the Event, the event description is shown as a tip.
5. Play a sound when time is up (if enabled for the event).
6. Show a popup when time is up (if enabled for the event), the popup text is the event description.
7. Save an event as default, so it can be reused.
8. Can be pinned in place, so the gui is unmovable.
9. Can remember the last GUI position (If chosen in the settings)
10. Uses colors on days / time, Black, Green, Yellow and Red.
When Less than 5 days to event, the days will turn Red, 6 to 10 days Yellow, 11+ Green
The time will change to Green when 0 days and 23 to 13 hours left, Yellow when 12 to 7 hours and Red from 5 to 0, else it's Black
When the time is up, the Event name label background will change to Yellow and the text to Red

When adding/Editing/Deleting an event the GUI is updated, not by restarting the program, but by deleting and recreating the events.

When an event time is up, the event is set as disabled - but will still exist in the event ini file, but it won't be loaded into the GUI when the countdown'er is started.
The event can be edited, and restarted or deleted permanently.

Some screenshots:

PuqfH0B.pngfpbcYNH.png2IfvyYU.png

RUU127y.pngNE4VFMz.pngUQwVayZ.png

 

#include <Date.au3>
#include <ColorConstants.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <String.au3>
#include <GuiComboBox.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIDlg.au3>
#include <Sound.au3>
#include <GuiListBox.au3>
#include <WinAPISysWin.au3>

Opt("GUIOnEventMode", 1)
Opt('MustDeclareVars', 1)
Opt('GUIResizeMode', $GUI_DOCKALL) ; To prevent ctrls to move when we add a new event
OnAutoItExitRegister('MenuItem_ExitClick') ; To save pos at closedown, if savepos is enabled
Global $g_iSecs, $g_iMins, $g_iHours, $g_iDays
; Get settings
Global $g_sSetFile = @ScriptDir & '\Settings.ini'
Global $g_sEventFile = @ScriptDir & '\Events.ini'
Global $g_iMaxEvents = IniRead($g_sSetFile, 'Settings', 'MaxEvents', 6) ; Max Number of events
Global $g_bPin = StringToType(IniRead($g_sSetFile, 'Settings', 'Pin', False)) ; Pin in place, GUI Can't be moved
Global $g_bRemember = StringToType(IniRead($g_sSetFile, 'Settings', 'Remember', False)) ; Remember last position and start at that pos
Global $g_iGuiLeft = IniRead($g_sSetFile, 'settings', 'posx', -1) ; Gui Left pos (x)
Global $g_iGuiRight = IniRead($g_sSetFile, 'settings', 'posy', -1) ; Gui right pos (y)
Global $g_sSoundFile = IniRead($g_sSetFile, 'Settings', 'SoundFile', @WindowsDir & '\media\tada.wav')
Global $g_bSoundPlay = StringToType(IniRead($g_sSetFile, 'Settings', 'SoundPlay', True))
Global $g_bPopup = StringToType(IniRead($g_sSetFile, 'Settings', 'popup', True)) ; Do a popup window with event description
Global $g_bStartWithWin = StringToType(IniRead($g_sSetFile, 'Settings', 'StartWithWin', True)) ; If the prog should start with windows

; Check if the gui should start at last saved position
If $g_bRemember = False Then
    $g_iGuiRight = -1 ; Default Screen center
    $g_iGuiLeft = -1 ; Default Screen center
EndIf


; Creates an array to hold the end dates of the event(s), it's State (Enabled) and the SectionName of the event.
; this array we uses to calc the countdown, and to prevent the need to keep reopen the event ini file, to get the
; enddate of a given event.
Global $g_aEventData[1][3] = [['EndDate', 'Enabled', 'SectionName']]
; Get events
Global $g_aEvents = IniReadSectionNames($g_sEventFile)
; Check if we have any Events
If Not IsArray($g_aEvents) Then
    ; If we don't have any events, we creates the array with 1 and Null, 1 to be used to create an empty gui
    ; Null so we know that there isn't any events
    Global $g_aEvents[2] = [1, Null]
EndIf
; call the EventOnLoad function
EventOnLoad()

Global $g_idLbl_Days[$g_iMaxEvents + 1], _
        $g_idLbl_Hrs[$g_iMaxEvents + 1], _
        $g_idLbl_Min[$g_iMaxEvents + 1], _
        $g_idLbl_Sec[$g_iMaxEvents + 1], _
        $g_idLbl_Event[$g_iMaxEvents + 1]

Global $g_iSpace = 80
Global $g_iGuiHeight = ($g_iSpace * $g_aEvents[0]) + 8
#Region ### START Koda GUI section ### Form=D:\Profil\Rex\ISN AutoIt Studio\Projects\Countdown Days\GUI\hCountdownDays.kxf
Global $hCountdownDays = GUICreate("CountDown Days", 136, $g_iGuiHeight, $g_iGuiLeft, $g_iGuiRight, $WS_POPUP, $WS_EX_TOOLWINDOW)
; Gui menu
Global $hCountdownDayscontext = GUICtrlCreateContextMenu()
Global $MenuItem_EventHandling = GUICtrlCreateMenu("Event(s)", $hCountdownDayscontext)
Global $MenuItem_AddEvent = GUICtrlCreateMenuItem("Add", $MenuItem_EventHandling)
GUICtrlSetOnEvent(-1, "MenuItem_AddEventClick")
Global $MenuItem_EditEvent = GUICtrlCreateMenuItem("Edit", $MenuItem_EventHandling)
GUICtrlSetOnEvent(-1, "MenuItem_EditEventClick")
Global $MenuItem_DeleteEvent = GUICtrlCreateMenuItem("Delete", $MenuItem_EventHandling)
GUICtrlSetOnEvent(-1, "MenuItem_DeleteEventClick")
Global $MenuItem_Settings = GUICtrlCreateMenuItem("Settings", $hCountdownDayscontext)
GUICtrlSetOnEvent(-1, "MenuItem_SettingsClick")
Global $MenuItem_Pin = GUICtrlCreateMenuItem("Pin in Place", $hCountdownDayscontext)
GUICtrlSetOnEvent(-1, "MenuItem_PinClick")
Global $MenuItem_Exit = GUICtrlCreateMenuItem("Exit", $hCountdownDayscontext)
GUICtrlSetOnEvent(-1, "MenuItem_ExitClick")
GUISetBkColor(0x4E4E4E)
Global $idLbl_Move = GUICtrlCreateLabel("", 1, 1, 134, 12, $SS_CENTER, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetFont(-1, 6, 400, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0x808080)

; Create a label to frame the gui, so it don't look to flat
Global $idFrame = GUICtrlCreateLabel('', 0, 0, 136, $g_iGuiHeight, BitOR($SS_SUNKEN, $WS_DISABLED, $SS_SIMPLE))

If $g_aEvents[1] = Null Then
    UpdateGui(False) ; Update Countdown GUI
Else
    UpdateGui(True) ; Update Countdown GUI
EndIf
; check if we needs to tick the pin, and disable the Move label
If $g_bPin = True Then
    GUICtrlSetState($MenuItem_Pin, $GUI_CHECKED)
    GUICtrlSetState($idLbl_Move, $GUI_DISABLE)
EndIf
; Check if we should disable the add event MenuItem
If $g_aEvents[0] >= $g_iMaxEvents Then GUICtrlSetState($MenuItem_AddEvent, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

#Region ### START Koda GUI section ### Form=D:\Profil\Rex\ISN AutoIt Studio\Projects\Countdown Days\GUI\hAddEvent.kxf
Global $hAddEvent = GUICreate("Add Event", 206, 407, -1, -1, BitOR($WS_SYSMENU, $WS_POPUP, $WS_CAPTION))
GUISetOnEvent($GUI_EVENT_CLOSE, "hWnd_Close")
Global $idAddEvent = GUICtrlCreateDummy() ; Create a dumme, we use this to calculate the CtrlID of the Controls created after the dummy
GUICtrlCreateLabel("Default Events:", 8, 8, 77, 17) ; This would be dummy + 1 in ID
GUICtrlCreateCombo("", 8, 24, 185, 25, $CBS_DROPDOWNLIST)
GUICtrlSetTip(-1, "Saved default events")
GUICtrlSetOnEvent(-1, "idCombo_EventsChange")
GUICtrlCreateLabel("Event Name:", 8, 56, 66, 17)
GUICtrlCreateInput("", 8, 72, 185, 21)
GUICtrlSetLimit(-1, 24)
GUICtrlSetTip(-1, "Name of event." & @CRLF & "This is the text that will be shown at the gui." & @CRLF _
         & "Max length is 24 chars")
GUICtrlCreateLabel("Event description:", 8, 104, 89, 17)
GUICtrlCreateEdit("", 8, 120, 185, 89, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetTip(-1, "Event description." & @CRLF _
         & "This wil be shown as a tip upon hovering, and also in the popup (if popup is enabled)")

GUICtrlCreateCheckbox("Choose Event date by calendar", 8, 216, 170, 17)
GUICtrlSetOnEvent(-1, "idCb_ChoseDateClick")
GUICtrlCreateLabel("Days to event:", 8, 240, 73, 17)
GUICtrlCreateInput("", 8, 256, 73, 21, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetTip(-1, "Days to the event")
GUICtrlCreateLabel("Time of event", 112, 240, 69, 17)
GUICtrlCreateDate('', 112, 256, 82, 21, BitOR($DTS_UPDOWN, $DTS_TIMEFORMAT))
GUICtrlSendMsg(-1, $DTM_SETFORMATW, 0, 'HH:mm:ss') ; Set time style
GUICtrlCreateLabel("Event date:", 8, 240, 59, 17)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlCreateDate('', 8, 256, 186, 21, 0)
GUICtrlSendMsg(-1, $DTM_SETFORMATW, 0, 'yyyy/MM/dd HH:mm:ss') ; Set Date and Time style
GUICtrlSetTip(-1, "The end date of the event")
GUICtrlSetState(-1, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Diasbled and hidden yy default
GUICtrlCreateLabel("Sound to play:", 8, 280, 72, 17)
GUICtrlCreateInput('', 8, 296, 160, 21, $ES_READONLY) ; Default sound to play at event end
GUICtrlCreateButton("...", 174, 296, 21, 21)
GUICtrlSetOnEvent(-1, "idBtn_SoundBrowseClick")
GUICtrlCreateCheckbox("Play sound", 8, 328, 73, 17)
GUICtrlCreateCheckbox("Popup", 88, 328, 49, 17)
GUICtrlCreateCheckbox("Save as default event", 8, 352, 124, 17)
GUICtrlSetTip(-1, "Saves the event as a default event, that later can be choosen from the dropdown")
GUICtrlCreateButton("&Add event", 8, 376, 75, 21)
GUICtrlSetTip(-1, "Adds the events and closes the add event gui")
GUICtrlSetOnEvent(-1, "idBtn_AddEventClick")
GUICtrlCreateButton("&Close", 120, 376, 75, 21)
GUICtrlSetOnEvent(-1, "hWnd_Close")
GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###

#Region ### START Koda GUI section ### Form=D:\Profil\Rex\ISN AutoIt Studio\Projects\Countdown Days\GUI\hEditEvent.kxf
Global $hEditEvent = GUICreate("Edit Event", 206, 407, -1, -1, BitOR($WS_SYSMENU, $WS_POPUP, $WS_CAPTION))
GUISetOnEvent($GUI_EVENT_CLOSE, "hWnd_Close")
Global $idEditEvent = GUICtrlCreateDummy()
GUICtrlCreateLabel("Choose events to edit:", 8, 8, 110, 17)
GUICtrlCreateCombo("", 8, 24, 185, 25, $CBS_DROPDOWNLIST)
GUICtrlSetOnEvent(-1, "idCombo_EventsChange")
GUICtrlCreateLabel("Event Name:", 8, 56, 66, 17)
GUICtrlCreateInput("", 8, 72, 185, 21)
GUICtrlSetLimit(-1, 24)
GUICtrlSetTip(-1, "Name of event. This is the text that will be shown at the gui. Max length is 24 chars")
GUICtrlCreateLabel("Event description:", 8, 104, 89, 17)
GUICtrlCreateEdit("", 8, 120, 185, 89, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL))
GUICtrlSetTip(-1, "Event description. This will be shown as a tip upon hovering, and also in the popup (if popup is enabled)")
GUICtrlCreateCheckbox("Choose event date by calender", 8, 216, 170, 17)
GUICtrlSetOnEvent(-1, "idCb_ChoseDateClick")
GUICtrlCreateLabel("Days to event:", 8, 240, 73, 17)
GUICtrlCreateInput("", 8, 256, 73, 21, BitOR($ES_CENTER, $ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetTip(-1, "Days to the event, time will be from the time the event is added")
GUICtrlCreateLabel("Time of event", 112, 240, 69, 17)
GUICtrlCreateDate('', 112, 256, 82, 21, BitOR($DTS_UPDOWN, $DTS_TIMEFORMAT))
GUICtrlSendMsg(-1, $DTM_SETFORMATW, 0, 'HH:mm:ss')
GUICtrlSetTip(-1, "Time of the event")
GUICtrlCreateLabel("Event date:", 8, 240, 59, 17)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlCreateDate('', 8, 256, 186, 21, 0)
GUICtrlSendMsg(-1, $DTM_SETFORMATW, 0, 'yyyy/MM/dd HH:mm:ss')
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetState(-1, $GUI_HIDE)
GUICtrlSetTip(-1, "Choose the end date of the event")
GUICtrlCreateLabel("Sound to play:", 8, 280, 72, 17)
GUICtrlCreateInput("", 8, 296, 160, 21, $ES_READONLY)
GUICtrlCreateButton("...", 174, 296, 21, 21)
GUICtrlSetOnEvent(-1, "idBtn_SoundBrowseClick")
GUICtrlCreateCheckbox("Play sound", 8, 328, 73, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlCreateCheckbox("Popup", 88, 328, 49, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlCreateCheckbox("Enabled", 8, 352, 60, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetTip(-1, "Enables or disables the event, if disabled it don't show in the countdown gui")
GUICtrlCreateButton("&Update", 8, 376, 75, 21)
GUICtrlSetTip(-1, "Updates the selected event")
GUICtrlSetOnEvent(-1, "idBtn_UpdateEventClick")
GUICtrlCreateButton("&Close", 120, 376, 75, 21)
GUICtrlSetOnEvent(-1, "hWnd_Close")
GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###

#Region ### START Koda GUI section ### Form=D:\Profil\Rex\ISN AutoIt Studio\Projects\Countdown Days\GUI\hEditEvent.kxf
Global $hDeleteEvent = GUICreate("Delete Event", 206, 318, Default, Default, BitOR($WS_SYSMENU, $WS_POPUP, $WS_CAPTION))
GUISetOnEvent($GUI_EVENT_CLOSE, "hWnd_Close")
Global $idDelEvent = GUICtrlCreateDummy()
GUICtrlCreateLabel("Choose event to delete:", 8, 8, 122, 16)
GUICtrlCreateCombo("", 8, 24, 185, 25, $CBS_DROPDOWNLIST)
GUICtrlSetTip(-1, "Event to delete")
GUICtrlSetOnEvent(-1, "idCombo_EventsChange")
GUICtrlCreateLabel("Event Name:", 8, 56, 66, 16)
GUICtrlCreateInput("", 8, 72, 185, 21, $ES_READONLY)
GUICtrlCreateLabel("Event description:", 8, 104, 89, 16)
GUICtrlCreateEdit("", 8, 120, 185, 89, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL, $ES_READONLY))
GUICtrlCreateLabel("Event date:", 8, 216, 59, 17)
GUICtrlCreateInput("", 8, 232, 186, 21, $ES_READONLY)
GUICtrlCreateCheckbox("Enabled", 8, 264, 60, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateButton("&Delete", 8, 288, 75, 21)
GUICtrlSetTip(-1, "Deletes selected event")
GUICtrlSetOnEvent(-1, "idBtn_EventDeleteClick")
GUICtrlCreateButton("&Close", 120, 288, 75, 21)
GUICtrlSetTip(-1, "Closes the add event, without saving the event.")
GUICtrlSetOnEvent(-1, "hWnd_Close")
GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###

#Region ### START Koda GUI section ### Form=D:\Profil\Rex\ISN AutoIt Studio\Projects\Countdown Days\GUI\hSettings.kxf
Global $hSettings = GUICreate("Settings", 203, 327, Default, Default, BitOR($WS_SYSMENU, $WS_POPUP, $WS_CAPTION))
GUISetOnEvent($GUI_EVENT_CLOSE, "hWnd_Close")
GUICtrlCreateLabel("Max Number of events:", 8, 12, 114, 17)
GUICtrlSetTip(-1, "Max number of events." & @CRLF & "On a 1920x1080 Screen the max would be aprox 13 events.")
Global $idInp_Sett_MaxEvents = GUICtrlCreateInput("", 122, 8, 21, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_NUMBER))
GUICtrlSetLimit(-1, 2)
Global $idCb_Sett_Remember = GUICtrlCreateCheckbox("Remember last position.", 8, 32, 130, 17)
GUICtrlSetTip(-1, "When countdown is closed, it saves the position it was placed, and starts at that position.")
Global $idCb_Sett_PlaySound = GUICtrlCreateCheckbox("Play Sound", 8, 56, 74, 17)
GUICtrlSetTip(-1, "Plays a sound when the event time is up.")
Global $idCb_Sett_Pop = GUICtrlCreateCheckbox("Show popup", 88, 56, 80, 17)
GUICtrlSetTip(-1, "Shows a popup windows, when the event time is up." & @CRLF & "The content of the popup windows will be the event description.")
Global $idInp_Sett_Sound = GUICtrlCreateInput("", 8, 104, 153, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
GUICtrlSetTip(-1, "Default sound to play when the event time is up.")
Global $idCb_Sett_StartWithWin = GUICtrlCreateCheckbox("Start with windows", 8, 80, 110, 17)
GUICtrlSetTip(-1, "Start the program with windows")
Global $idBtn_Sett_SoundBrowse = GUICtrlCreateButton("...", 168, 104, 21, 21)
GUICtrlSetOnEvent(-1, "idBtn_SoundBrowseClick")
GUICtrlCreateLabel("Default events:", 8, 136, 76, 17)
Global $idList_Sett_DefEvents = GUICtrlCreateList("", 8, 152, 185, 97)
GUICtrlSetTip(-1, "Default events that, can be picked from add event.")
GUICtrlCreateButton("&Remove", 8, 256, 75, 21)
GUICtrlSetTip(-1, "Deletes the selected Default event")
GUICtrlSetOnEvent(-1, "idBtn_Sett_RemEventClick")
GUICtrlCreateButton("&OK", 8, 296, 75, 21)
GUICtrlSetOnEvent(-1, "idBtn_Sett_OKClick")
GUICtrlCreateButton("&Close", 120, 296, 75, 21)
GUICtrlSetOnEvent(-1, "hWnd_Close")

GUISetState(@SW_HIDE)
#EndRegion ### END Koda GUI section ###
;~ If $cmdline[0] = 1 Then RelHandle($cmdline[1])
Countdown()

AdlibRegister("Countdown", 1000)

While 1
    Sleep(100)
WEnd

Func EventOnLoad($bMsg = True)
    ; This function is used to prevent array subscript dimension range exceeded, and to remove events that is currently disabled.
    ; Also it checks if there is more events enabled that max events allows, and throws a msg about it - and trims the max events
    ; to match the MaxEvents allowed
    
    ; First we removes the events that is currently disabled
    Local $sDelRange ; To save the array index where the disabled event is stored
    For $i = 1 To $g_aEvents[0]
        ; Check if the event is disabled, using StringToType to convert string to Bool
        If StringToType(IniRead($g_sEventFile, $g_aEvents[$i], 'Enabled', True)) = False Then
            ; If the event is disabled we add the index to our save index var and adds a ";" in case that more than one is disabled
            $sDelRange &= $i & ';'
        EndIf
    Next
    If $sDelRange <> '' Then
        ; If we had some Disabled events, we remove them from the array
        _ArrayDelete($g_aEvents, StringTrimRight($sDelRange, 1))
        ; And then we update the value of $g_aEvents[0]
        $g_aEvents[0] = UBound($g_aEvents) - 1
        If $g_aEvents[0] = 0 Then ; If there is no enabled events
            Dim $g_aEvents[2] = [1, Null] ; We updates the array, so we know that there is't any enabled events
        EndIf
    EndIf
    
    ; Then we check if there is more events that allowed
    If $g_aEvents[0] > $g_iMaxEvents And $bMsg = True Then ; If there is more events that allowed, we throw a warn - and trims the array to max
        MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Error', _
                'The amount of events exceeds the max nr of events' & @CRLF & _
                'Only the first ' & $g_iMaxEvents & ' events will be loaded!')
        $g_aEvents[0] = $g_iMaxEvents
    EndIf
    
    ; Updates the EventData array
    ReDim $g_aEventData[UBound($g_aEvents)][3]
    ; Add enddate to the array
    For $i = 1 To $g_aEvents[0]
        $g_aEventData[$i][0] = IniRead($g_sEventFile, $g_aEvents[$i], 'EndDate', _NowCalc())
        $g_aEventData[$i][1] = StringToType(IniRead($g_sEventFile, $g_aEvents[$i], 'Enabled', True))
        $g_aEventData[$i][2] = $g_aEvents[$i]
        
    Next
EndFunc   ;==>EventOnLoad

Func MenuItem_AddEventClick()
    ; Add default evnets to dropdown
    Local $aDefEvents = IniReadSectionNames($g_sSetFile)
    If IsArray($aDefEvents) Then
        For $i = 1 To $aDefEvents[0]
            If $aDefEvents[$i] <> 'Settings' Then ; If the name is settings we ignore it
                ; Add the defaults to the combo and revert the name from hex to string.
                GUICtrlSetData($idAddEvent + 2, _HexToString($aDefEvents[$i]))
            EndIf
        Next
    EndIf
    ; Set default sound to play
    GUICtrlSetData($idAddEvent + 15, $g_sSoundFile)
    GUICtrlSetTip($idAddEvent + 15, $g_sSoundFile)
    ; Check if the playsound and popup is enabled in settings
    CbSetState($idAddEvent + 17, $g_bSoundPlay)
    CbSetState($idAddEvent + 18, $g_bPopup)
    ; Set focus to the Event Name input
    GUICtrlSetState($idAddEvent + 4, $GUI_FOCUS)
    ; Show the gui
    GUISetState(@SW_SHOW, $hAddEvent)
EndFunc   ;==>MenuItem_AddEventClick

Func MenuItem_DeleteEventClick()
    Local $aEvents = IniReadSectionNames($g_sEventFile)
    If IsArray($aEvents) Then
        For $i = 1 To $aEvents[0]
            GUICtrlSetData($idDelEvent + 2, IniRead($g_sEventFile, $aEvents[$i], 'Name', 'Error Reading Name'))
        Next
    EndIf
    GUISetState(@SW_SHOW, $hDeleteEvent)
EndFunc   ;==>MenuItem_DeleteEventClick

Func MenuItem_EditEventClick()
    ; First we get events from the inifile
    Local $aEvents = IniReadSectionNames($g_sEventFile)
    If IsArray($aEvents) Then
        For $i = 1 To $aEvents[0]
            GUICtrlSetData($idEditEvent + 2, IniRead($g_sEventFile, $aEvents[$i], 'Name', 'Error Reading Name'))
        Next
    EndIf
    
    ; Then we ticks of the date checkbox so it shows date picker
    GUICtrlSetState($idEditEvent + 7, $GUI_CHECKED)
    
    ; And hides/disables the Days to evnet / event time input and labels
    GUICtrlSetState($idEditEvent + 8, $GUI_HIDE) ; Hide the label Days to event
    GUICtrlSetState($idEditEvent + 9, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Hide the input Days to event
    GUICtrlSetData($idEditEvent + 9, '') ; Clear the input Days to event
    GUICtrlSetState($idEditEvent + 10, $GUI_HIDE) ; Hides the label Time to event
    GUICtrlSetState($idEditEvent + 11, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Hide the TimePicker Time to event

    ; Show date picker and lable
    GUICtrlSetState($idEditEvent + 12, $GUI_SHOW) ; Show the label Event date
    GUICtrlSetState($idEditEvent + 13, BitOR($GUI_ENABLE, $GUI_SHOW)) ; Show the Date time picker

    ; Then we show the GUI
    GUISetState(@SW_SHOW, $hEditEvent)
EndFunc   ;==>MenuItem_EditEventClick

Func MenuItem_SettingsClick()
    ; Update the gui with info's from the settings file, or if no settings file exists, with defaults
    ; Add default evnets to Listbox (The events that is saved, and can be loaded in add event)
    Local $aDefEvents = IniReadSectionNames($g_sSetFile)
    
    If IsArray($aDefEvents) Then ; If it's an array we continue
        
        For $i = 1 To $aDefEvents[0]
            If $aDefEvents[$i] <> 'Settings' Then ; If the name is settings we ignore it
                ; Add the defaults to the listbox and convert the name from hex to string.
                _GUICtrlListBox_AddString($idList_Sett_DefEvents, _HexToString($aDefEvents[$i]))
            EndIf
        Next
    EndIf

    ; Check if the Remember, playsound, popup and Start with windows is enabled in settings
    CbSetState($idCb_Sett_Remember, $g_bRemember)
    CbSetState($idCb_Sett_PlaySound, $g_bSoundPlay)
    CbSetState($idCb_Sett_Pop, $g_bPopup)
    CbSetState($idCb_Sett_StartWithWin, $g_bStartWithWin)
    
    ; Set default sound
    GUICtrlSetData($idInp_Sett_Sound, IniRead($g_sSetFile, 'Settings', 'SoundFile', @WindowsDir & '\media\Tada.wav'))
    
    ; Set max events
    GUICtrlSetData($idInp_Sett_MaxEvents, IniRead($g_sSetFile, 'Settings', 'MaxEvents', 6))
    
    ; Show the gui
    GUISetState(@SW_SHOW, $hSettings)
EndFunc   ;==>MenuItem_SettingsClick

Func MenuItem_ExitClick()
    ; Check if the program should save it's poition
    If $g_bRemember = True Then PosSave()
    Exit
EndFunc   ;==>MenuItem_ExitClick

Func MenuItem_PinClick()
    ; Get state of MenuItem Pin in place
    ; If the pin is enabled, the gui can't be moved
    If BitAND(GUICtrlRead($MenuItem_Pin), $GUI_CHECKED) = $GUI_CHECKED Then
        GUICtrlSetState($MenuItem_Pin, $GUI_UNCHECKED)
        $g_bPin = False ; Update the var
        IniWrite($g_sSetFile, 'Settings', 'Pin', False) ; Update the ini file
        GUICtrlSetState($idLbl_Move, $GUI_ENABLE) ; Enable the label, this allows the gui to be moved
    Else
        GUICtrlSetState($MenuItem_Pin, $GUI_CHECKED)
        $g_bPin = True ; Update the var
        IniWrite($g_sSetFile, 'Settings', 'Pin', True) ; Update the ini file
        GUICtrlSetState($idLbl_Move, $GUI_DISABLE) ; Disable the label, preventing the gui to be moved
    EndIf
    
EndFunc   ;==>MenuItem_PinClick

Func hWnd_Close()
    ; First we check if it's Add, Edit or Delete event gui that send the msg
    Switch WinGetHandle('[ACTIVE]') ; The handle of the current gui
        Case WinGetHandle($hAddEvent) ; If its Add event
            Local $iCtrlID = $idAddEvent
            ; hide the addevent gui
            GUISetState(@SW_HIDE, $hAddEvent)
        Case WinGetHandle($hEditEvent) ; If its Edit event
            Local $iCtrlID = $idEditEvent
            ; hide the editevent gui
            GUISetState(@SW_HIDE, $hEditEvent)
        Case WinGetHandle($hDeleteEvent) ; If its Delete event
            Local $iCtrlID = $idDelEvent
            ; hide the Delete event gui
            GUISetState(@SW_HIDE, $hDeleteEvent)
        Case WinGetHandle($hSettings) ; If its Settings
            ; Hide the gui
            GUISetState(@SW_HIDE, $hSettings)
            Return
    EndSwitch
    
    ; Clear all
    ; The first 3 ctrls is the same for all 3 gui's
    _GUICtrlComboBox_ResetContent($iCtrlID + 2) ; Clear the combo
    GUICtrlSetData($iCtrlID + 4, '') ; Clear the input Event name
    GUICtrlSetData($iCtrlID + 6, '') ; Clear the edit event Description
    
    ; Now we need to check if we have the edit or update gui
    If WinGetHandle($hAddEvent) Or WinGetHandle($hEditEvent) = WinGetHandle('[ACTIVE]') Then
        GUICtrlSetState($iCtrlID + 7, $GUI_UNCHECKED) ; Unchecks the Choose event date by calender checkbox
        GUICtrlSetData($iCtrlID + 9, '') ; Clear the input Days to event
        GUICtrlSetState($iCtrlID + 19, $GUI_UNCHECKED) ; Uncheck Save as default / Enabled checkbox
    Else
        ; Uncheck the Enabled checkbox at the delete gui
        GUICtrlSetState($iCtrlID + 19, $GUI_UNCHECKED) ; Uncheck Enabled checkbox
    EndIf
EndFunc   ;==>hWnd_Close

Func idBtn_Sett_OKClick()
    ; Read infos and update the settings.
    ; Get maxEvents
    Local $iMaxEvents = GUICtrlRead($idInp_Sett_MaxEvents)
    If $iMaxEvents = '' Then
        MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Max number of events', _
                "The input Max number of events is empty, but it shouldn't be" & @CRLF & 'Please type number of max allowed events!!')
        GUICtrlSetState($idInp_Sett_MaxEvents, $GUI_FOCUS)
    Else
        IniWrite($g_sSetFile, 'Settings', 'MaxEvents', $iMaxEvents)
    EndIf
    ; Write the settings to ini file
    IniWrite($g_sSetFile, 'Settings', 'Remember', (GUICtrlRead($idCb_Sett_Remember) = $GUI_CHECKED) ? True : False)
    IniWrite($g_sSetFile, 'Settings', 'SoundPlay', (GUICtrlRead($idCb_Sett_PlaySound) = $GUI_CHECKED) ? True : False)
    IniWrite($g_sSetFile, 'Settings', 'Popup', (GUICtrlRead($idCb_Sett_Pop) = $GUI_CHECKED) ? True : False)
    IniWrite($g_sSetFile, 'Settings', 'StartWithWin', (GUICtrlRead($idCb_Sett_StartWithWin) = $GUI_CHECKED) ? True : False)
    IniWrite($g_sSetFile, 'Settings', 'SoundFile', GUICtrlRead($idInp_Sett_Sound))
    ; Check if the program should rember it's last position
    If GUICtrlRead($idCb_Sett_Remember) = $GUI_CHECKED Then PosSave()
    ; Check if the program should start with windows
    If GUICtrlRead($idCb_Sett_StartWithWin) = $GUI_CHECKED Then
        RegWrite('HKCU\Software\Microsoft\Windows\CurrentVersion\Run', 'CCD', 'REG_SZ', '"' & @ScriptFullPath & '"')
    Else
        RegDelete('HKCU\Software\Microsoft\Windows\CurrentVersion\Run', 'CCD')
    EndIf
    ; Update the global vars
    $g_bRemember = (GUICtrlRead($idCb_Sett_Remember) = $GUI_CHECKED) ? True : False
    $g_bSoundPlay = (GUICtrlRead($idCb_Sett_PlaySound) = $GUI_CHECKED) ? True : False
    $g_bPopup = (GUICtrlRead($idCb_Sett_Pop) = $GUI_CHECKED) ? True : False
    $g_bStartWithWin = (GUICtrlRead($idCb_Sett_StartWithWin) = $GUI_CHECKED) ? True : False
    $g_sSoundFile = GUICtrlRead($idInp_Sett_Sound)
    GUISetState(@SW_HIDE, $hSettings)
EndFunc   ;==>idBtn_Sett_OKClick

Func idBtn_Sett_RemEventClick()
    ; First we warn about the removal
    Local $iStyle = BitOR($MB_YESNO, $MB_SETFOREGROUND, $MB_TOPMOST, $MB_ICONWARNING)
    Local $sEvent = GUICtrlRead($idList_Sett_DefEvents)
    Local $iMsgBoxAnswer = MsgBox($iStyle, 'Delete event', 'The event "' & $sEvent & '" will be deletede!' & @CRLF & "Do you want to continue?")
    
    Switch $iMsgBoxAnswer
        Case $IDYES
            ; Remove the evetn from ini file
            IniDelete($g_sSetFile, _StringToHex($sEvent))
            ; Remove selected from the list box
            _GUICtrlListBox_DeleteString($idList_Sett_DefEvents, _GUICtrlListBox_FindString($idList_Sett_DefEvents, $sEvent))
        Case $IDNO
            Return
    EndSwitch
EndFunc   ;==>idBtn_Sett_RemEventClick

Func idCb_ChoseDateClick()
    ; Handel the Choose by date checkbox for either Add or Edit event gui
    ; First we check if it's Add or Edit event gui that is asking
    Switch WinGetHandle('[ACTIVE]') ; The handle of the current gui
        Case WinGetHandle($hAddEvent) ; If its Add event
            Local $iCtrlID = $idAddEvent
        Case WinGetHandle($hEditEvent) ; If its Edit event
            Local $iCtrlID = $idEditEvent
    EndSwitch
    
    ; Get state of checkbox
    Local $bChoseDate = (GUICtrlRead($iCtrlID + 7) = $GUI_CHECKED) ? True : False
    
    ; Handel state of checkbox
    Switch $bChoseDate
        Case True
            
            ; Hide, disable and clear days and time input / label
            GUICtrlSetState($iCtrlID + 8, $GUI_HIDE) ; Hide the label Days to event
            GUICtrlSetState($iCtrlID + 9, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Hide the input Days to event
            GUICtrlSetData($iCtrlID + 9, '') ; Clear the input Days to event
            GUICtrlSetState($iCtrlID + 10, $GUI_HIDE) ; Hides the label Time to event
            GUICtrlSetState($iCtrlID + 11, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Hide the TimePicker Time to event

            ; Show date picker and lable
            GUICtrlSetState($iCtrlID + 12, $GUI_SHOW) ; Show the label Event date
            GUICtrlSetState($iCtrlID + 13, BitOR($GUI_ENABLE, $GUI_SHOW)) ; Show the Date time picker

        Case False
            
            ; Hide, disable and clear date picker and label
            GUICtrlSetState($iCtrlID + 12, $GUI_HIDE) ; Hide the label Event date
            GUICtrlSetState($iCtrlID + 13, BitOR($GUI_DISABLE, $GUI_HIDE)) ; Hide the Date time picker

            ; Enable, show and set time input / labels
            GUICtrlSetState($iCtrlID + 8, $GUI_SHOW) ; Show the label Days to event
            GUICtrlSetState($iCtrlID + 9, BitOR($GUI_ENABLE, $GUI_SHOW)) ; Shows the Input Days to event
            GUICtrlSetState($iCtrlID + 10, $GUI_SHOW) ; Shows the label Time to event
            GUICtrlSetState($iCtrlID + 11, BitOR($GUI_ENABLE, $GUI_SHOW)) ; Show time picker
            
    EndSwitch
    
EndFunc   ;==>idCb_ChoseDateClick


Func idBtn_AddEventClick()
    ; First we handle the inputs etc.
    ; Read data from gui, and add it to an array
    Local $sReadName = GUICtrlRead($idAddEvent + 4) ; Read name from Event name input
    If $sReadName = '' Then
        MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Event Name Error', 'The input "Event name" was empty!' & _
                @CRLF & 'Please enter type an Event name!!')
        GUICtrlSetState($idAddEvent + 4, $GUI_FOCUS)
        Return
    EndIf

    ; Get state of checkbox
    Local $bChoseDate = (GUICtrlRead($idAddEvent + 7) = $GUI_CHECKED) ? True : False
    ; Handel state of checkbox
    Switch $bChoseDate
        Case True
            ; Get DTS from date picker
            Local $sDTS = GUICtrlRead($idAddEvent + 13)
        Case False
            ; Check that days is not empty
            Local $sReadDays = GUICtrlRead($idAddEvent + 9)
            If $sReadDays <> '' Then
                Local $sDTS = CalcEndDate($sReadDays, GUICtrlRead($idAddEvent + 11))
            Else
                MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Days Error', 'The input "Days to event" was empty!' & _
                        @CRLF & 'Please enter number of days!!')
                GUICtrlSetState($idAddEvent + 9, $GUI_FOCUS)
                Return
            EndIf
    EndSwitch
    Local $sEvent = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC ; We use this to prevent events with same SectionNames in the ini file.
    ; Create an array to hold the Event values
    Local $aValues[8][2]
    $aValues[0][0] = 7 ; Number of elements in the array
    $aValues[0][1] = ''
    $aValues[1][0] = 'Name'
    $aValues[1][1] = $sReadName ; Event Name
    $aValues[2][0] = 'Description'
    $aValues[2][1] = StringReplace(GUICtrlRead($idAddEvent + 6), @CRLF, ' ') ; Get data from edit
    $aValues[3][0] = 'EndDate'
    $aValues[3][1] = $sDTS ; Event end date
    $aValues[4][0] = 'Sound'
    $aValues[4][1] = GUICtrlRead($idAddEvent + 15) ; Get sound to play from input sound to play
    $aValues[5][0] = 'PlaySound'
    $aValues[5][1] = (GUICtrlRead($idAddEvent + 17) = $GUI_CHECKED) ? True : False ; To play or not play sound at event end
    $aValues[6][0] = 'Popup'
    $aValues[6][1] = (GUICtrlRead($idAddEvent + 18) = $GUI_CHECKED) ? True : False ; To show or not to show popup at event end
    $aValues[7][0] = 'Enabled'
    $aValues[7][1] = 'True' ; The Event is Enabled by default
    IniWriteSection($g_sEventFile, $sEvent, $aValues) ; Write data to ini file
    
    ; Get state of save as default checkbox
    If GUICtrlRead($idAddEvent + 19) = $GUI_CHECKED Then
        ; We dont save all the info, only Days to Event, Sound to play, To play o not play the sound and to show or not to show the popup
        ; Trim the array from unneeded data
        _ArrayDelete($aValues, '1;7') ; Remove Name and Enabled
        $aValues[0][0] = 5 ; Update the number of values left in the array
        $aValues[2][0] = 'Days' ; Change EndDate to Days
        ; Get the amount of days to event +1 course the cal is only returing whole days and the event will be in x days and 23:59:xx sec
        ; There for we needs to ad +1, so it will correspond to the amount of days th euser might have entered in Days to Event
        $aValues[2][1] = _DateDiff('D', _NowCalc(), $sDTS) + 1
        IniWriteSection($g_sSetFile, _StringToHex(GUICtrlRead($idAddEvent + 4)), $aValues) ; Write the info to Settings file
    EndIf
    hWnd_Close() ; Close the gui
    
    If $g_aEvents[1] = Null Then
        UpdateGui(True) ; Update Countdown GUI with Msg
    Else
        UpdateGui(False) ; Update Countdown GUI without msg
    EndIf

EndFunc   ;==>idBtn_AddEventClick

Func idBtn_UpdateEventClick()
    ; First we read the combo to get the index of the selected event
    Local $iIndex = _GUICtrlComboBox_GetCurSel($idEditEvent + 2) + 1
    ; Now we get the data from the inifile
    Local $aEvents = IniReadSectionNames($g_sEventFile)

    ; First we handle the inputs etc.
    ; Read data from gui, and add it to an array
    Local $sReadName = GUICtrlRead($idEditEvent + 4) ; Read name from Event name input
    If $sReadName = '' Then
        MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Event Name Error', 'The input "Event name" was empty!' & _
                @CRLF & 'Please enter type an Event name!!')
        GUICtrlSetState($idEditEvent + 4, $GUI_FOCUS)
        Return
    EndIf

    ; Get state of checkbox
    Local $bChoseDate = (GUICtrlRead($idEditEvent + 7) = $GUI_CHECKED) ? True : False
    ; Handel state of checkbox
    Switch $bChoseDate
        Case True
            ; Get DTS from date picker
            Local $sDTS = GUICtrlRead($idEditEvent + 13)
        Case False
            ; Check that days is not empty
            Local $sReadDays = GUICtrlRead($idEditEvent + 9)
            If $sReadDays <> '' Then
                Local $sDTS = CalcEndDate($sReadDays, GUICtrlRead($idEditEvent + 11))
            Else
                MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Days Error', 'The input "Days to event" was empty!' & _
                        @CRLF & 'Please enter number of days!!')
                GUICtrlSetState($idEditEvent + 9, $GUI_FOCUS)
                Return
            EndIf
    EndSwitch
    
    ; Create an array to hold the Event values
    Local $aValues[8][2]
    $aValues[0][0] = 7 ; Number of elements in the array
    $aValues[0][1] = ''
    $aValues[1][0] = 'Name'
    $aValues[1][1] = $sReadName ; Event Name
    $aValues[2][0] = 'Description'
    $aValues[2][1] = StringReplace(GUICtrlRead($idEditEvent + 6), @CRLF, ' ') ; Get data from edit
    $aValues[3][0] = 'EndDate'
    $aValues[3][1] = $sDTS ; Event end date
    $aValues[4][0] = 'Sound'
    $aValues[4][1] = GUICtrlRead($idEditEvent + 15) ; Get sound to play from input sound to play
    $aValues[5][0] = 'PlaySound'
    $aValues[5][1] = (GUICtrlRead($idEditEvent + 17) = $GUI_CHECKED) ? True : False ; To play or not play sound at event end
    $aValues[6][0] = 'Popup'
    $aValues[6][1] = (GUICtrlRead($idEditEvent + 18) = $GUI_CHECKED) ? True : False ; To show or not to show popup at event end
    $aValues[7][0] = 'Enabled'
    $aValues[7][1] = (GUICtrlRead($idEditEvent + 19) = $GUI_CHECKED) ? True : False ; If enabled or not enabled
    IniWriteSection($g_sEventFile, $aEvents[$iIndex], $aValues) ; Write data to ini file
    
    hWnd_Close() ; Close the gui
    
    UpdateGui(False) ; Update Countdown GUI
    
EndFunc   ;==>idBtn_UpdateEventClick

Func idBtn_EventDeleteClick()
    ; Deletes the selected Event from the Events.ini file and if it's current active in the CountDown GUI
    ; refreshes the gui to remove it from that also
    
    ; First we warn the user that the event will be deletede
    If MsgBox(BitOR($MB_YESNO, $MB_TOPMOST, $MB_SETFOREGROUND, $MB_ICONWARNING), 'Delete Event', 'The Event ' & GUICtrlRead($idDelEvent + 2) & _
            ' will be deletede!' & @CRLF & 'Do you want to continue?') = $IDNO Then Return
    
    ; Read the combo, to get the index of the selected Event
    Local $iIndex = _GUICtrlComboBox_GetCurSel($idDelEvent + 2) + 1 ; Add +1 to the index
    ; Now we get the sectionnames from the ini file
    Local $aEvents = IniReadSectionNames($g_sEventFile)
    
    ; Saves the sectionname that we need to delete
    Local $sEventToDelete = $aEvents[$iIndex]
    ; We deletes the event from the ini file
    IniDelete($g_sEventFile, $aEvents[$iIndex])
    
    ; Closes the Delete event gui
    GUISwitch($hDeleteEvent)
    hWnd_Close()
    ; Check if the event is active in the CountDownGUI, by checking if it's located in the EventData array.
    ; We search for the "SectionName" in the aEventData array, at colum 3, sins Col 1 holds the enddate and col 2 holds Enabled
    If _ArraySearch($g_aEventData, $sEventToDelete, 0, 0, 0, 0, 1, 3) <> -1 Then UpdateGui(False)
    ; Check if Max events is lessere that current enabled events
    EventOnLoad(False)
    If $g_aEvents[0] < $g_iMaxEvents Then GUICtrlSetState($MenuItem_AddEvent, $GUI_ENABLE)
    
EndFunc   ;==>idBtn_EventDeleteClick

Func idBtn_SoundBrowseClick()
    ; First we check if it's Add, Edit Event or Settings gui that is asking
    Switch WinGetHandle('[ACTIVE]')
        Case WinGetHandle($hAddEvent) ; If its Add event
            Local $iCtrlID = $idAddEvent
            
        Case WinGetHandle($hEditEvent) ; If its Edit event
            Local $iCtrlID = $idEditEvent
            
        Case WinGetHandle($hSettings) ; If it Settings
            Local $iCtrlID = $idInp_Sett_Sound - 15 ; We have a ctrlid for this, so we subtracts 15 from the id to match our input.
    EndSwitch
    
    ; Display an open dialog to select a list of file(s).
    Local $iExStyle = BitOR($OFN_PATHMUSTEXIST, $OFN_FILEMUSTEXIST)
    Local $sAudioFile = _WinAPI_OpenFileDlg('', @WorkingDir, 'Audio (*.wav;*.mp3)', 1, '', '', $iExStyle)
    
    ; We only add if there is something to add
    If $sAudioFile <> '' Then GUICtrlSetData($iCtrlID + 15, $sAudioFile)

EndFunc   ;==>idBtn_SoundBrowseClick


Func idCombo_EventsChange()
    ; Here we handle the combo changes for Add,Edit and Delete events
    
    ; First we check if it's Add or Edit event gui that is asking
    Switch WinGetHandle('[ACTIVE]') ; Get winhandl from active GUI
        Case WinGetHandle($hAddEvent) ; If it's Add event
            ; If we have the Add Event gui
            ; First we read the combo
            Local $sRead = GUICtrlRead($idAddEvent + 2)
            ; Now we get the default event data from the settings ini file
            Local $aData = IniReadSection($g_sSetFile, _StringToHex($sRead))

            ; Add the data to the inputs
            GUICtrlSetData($idAddEvent + 4, $sRead) ; Add Name of event to input
            GUICtrlSetData($idAddEvent + 6, $aData[1][1]) ; Add Description to the edit
            GUICtrlSetData($idAddEvent + 9, $aData[2][1]) ; Add number of days to input
            GUICtrlSetData($idAddEvent + 15, $aData[3][1]) ; Add sound to input
            ; Set the state of Soundplay
            CbSetState($idAddEvent + 17, StringToType($aData[4][1]))
            ; Set the state of popup checkbox
            CbSetState($idAddEvent + 18, StringToType($aData[5][1]))
            
        Case WinGetHandle($hEditEvent) ; If it's Edit event
            ; We have the edit event gui
            ; First we read the combo
            Local $iIndex = _GUICtrlComboBox_GetCurSel($idEditEvent + 2) + 1
            ; Now we get the data from the inifile
            Local $aEvents = IniReadSectionNames($g_sEventFile)
            Local $aData = IniReadSection($g_sEventFile, $aEvents[$iIndex])

            ; Add the data to the inputs
            GUICtrlSetData($idEditEvent + 4, $aData[1][1]) ; Add the name to Event Name input
            GUICtrlSetData($idEditEvent + 6, $aData[2][1]) ; Add Description
            GUICtrlSetData($idEditEvent + 13, $aData[3][1]) ; Add end date of event
            GUICtrlSetData($idEditEvent + 15, $aData[4][1]) ; Add the sound
            GUICtrlSetTip($idEditEvent + 15, $aData[4][1])
            
            ;Update the state of the checkboxes
            CbSetState($idEditEvent + 17, StringToType($aData[5][1])) ; PlaySound checkbox
            CbSetState($idEditEvent + 18, StringToType($aData[6][1])) ; Popup Checkbox
            CbSetState($idEditEvent + 19, StringToType($aData[7][1])) ; Enabled checkbox
            
        Case WinGetHandle($hDeleteEvent) ; If it's Delete event
            ; We have the edit event gui
            ; First we read the combo
            Local $iIndex = _GUICtrlComboBox_GetCurSel($idDelEvent + 2) + 1
            ; Now we get the data from the inifile
            Local $aEvents = IniReadSectionNames($g_sEventFile)
            Local $aData = IniReadSection($g_sEventFile, $aEvents[$iIndex])

            ; Add the data to the inputs
            GUICtrlSetData($idDelEvent + 4, $aData[1][1]) ; Add the name to Event Name input
            GUICtrlSetData($idDelEvent + 6, $aData[2][1]) ; Add Description
            GUICtrlSetData($idDelEvent + 8, $aData[3][1]) ; Add end date of event
            CbSetState($idDelEvent + 9, StringToType($aData[7][1])) ; Enabled checkbox
    EndSwitch
    
EndFunc   ;==>idCombo_EventsChange


Func UpdateGui($bStart = True)
    ; Adds events to the main gui
    ; Check if we have an event to add to the gui
    If $g_aEvents[1] = Null And $bStart = False Then Return ; If we don't have any events
    ; If the function is called with False, we deletes all controls
    If $bStart = False Then
        ; Delete existing controls, so we can recreate them
        For $i = 1 To $g_aEvents[0]
            GUICtrlDelete($g_idLbl_Event[$i]) ; Delete the Event Name Label
            GUICtrlDelete($g_idLbl_Days[$i]) ; Deletes the label Days Counter
            GUICtrlDelete($g_idLbl_Days[$i] + 1) ; Deletes the label Days
            GUICtrlDelete($g_idLbl_Hrs[$i]) ; Deletes the label Hrs counter
            GUICtrlDelete($g_idLbl_Hrs[$i] + 1) ; Deletes the label : after hrs
            GUICtrlDelete($g_idLbl_Min[$i]) ; Deletes the label min counter
            GUICtrlDelete($g_idLbl_Min[$i] + 1) ; Deletes the label : after min
            GUICtrlDelete($g_idLbl_Sec[$i]) ; Deletes the label sec counter
            GUICtrlDelete($g_idLbl_Sec[$i] + 1) ; Deletes the label we use as frame around the time counter
            GUICtrlDelete($g_idLbl_Sec[$i] + 2) ; Deletes the Group we use as spacer
            GUICtrlDelete($g_idLbl_Sec[$i] + 3) ; Deletes the label we use as a frame for the gui
        Next
        
    EndIf

    $g_aEvents = IniReadSectionNames($g_sEventFile) ; reload the array

    ; Check if there is any events
    If Not IsArray($g_aEvents) Then
        Dim $g_aEvents[2] = [1, Null]
        Return
    EndIf
    ; Trims disabled events
    EventOnLoad(False) ; We call the function with false to prevent the msg box about too many events.
    ; Just to be shure
    If $g_aEvents[1] = Null Then Return
    ; Updates the global GUIHeight
    $g_iGuiHeight = ($g_iSpace * $g_aEvents[0]) + 8
    ; Switch to the CCD gui, else the labels would be created on the last active gui
    GUISwitch($hCountdownDays)
    For $i = 1 To $g_aEvents[0]
        
        ; Adds the events to the Countdown gui
        Local $sEventName = StringLeft(IniRead($g_sEventFile, $g_aEvents[$i], 'Name', ''), 24)
        $g_idLbl_Event[$i] = GUICtrlCreateLabel($sEventName, 8, 14 + (($i - 1) * $g_iSpace), 119, 20, $SS_CENTER)
        GUICtrlSetFont(-1, 12, 400, 0, "Arial Narrow")
        GUICtrlSetColor(-1, 0xFFFFFF) ; Navy blue
        GUICtrlSetTip(-1, SplitText(IniRead($g_sEventFile, $g_aEvents[$i], 'Description', ''), 45))
        $g_idLbl_Days[$i] = GUICtrlCreateLabel("", 31, 34 + (($i - 1) * $g_iSpace), 32, 24, $SS_RIGHT)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        GUICtrlCreateLabel("Days", 65, 34 + (($i - 1) * $g_iSpace), 34, 24)
        GUICtrlSetFont(-1, 14, 400, 0, "Arial Narrow")
        $g_idLbl_Hrs[$i] = GUICtrlCreateLabel("", 22, 56 + (($i - 1) * $g_iSpace), 18, 24, $SS_CENTER)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        GUICtrlCreateLabel(":", 46, 56 + (($i - 1) * $g_iSpace), 8, 24)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        $g_idLbl_Min[$i] = GUICtrlCreateLabel("", 56, 56 + (($i - 1) * $g_iSpace), 18, 24, $SS_CENTER)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        GUICtrlCreateLabel(":", 80, 56 + (($i - 1) * $g_iSpace), 8, 24)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        $g_idLbl_Sec[$i] = GUICtrlCreateLabel("", 90, 56 + (($i - 1) * $g_iSpace), 18, 24, $SS_CENTER)
        GUICtrlSetFont(-1, 14, 800, 0, "Arial Narrow")
        ; Create a static lable to frame the Time countdown
        GUICtrlCreateLabel("", 16, 56 + (($i - 1) * $g_iSpace), 102, 24, BitOR($SS_GRAYFRAME, $WS_DISABLED, $SS_SUNKEN))
        ; We add a horizontal line as a spacer between the events, but only if we have more that one. But we don't add one after the last
        If $i >= 1 And $i < $g_aEvents[0] And $g_aEvents[0] > 1 Then
            GUICtrlCreateGroup("", 0, 84 + (($i - 1) * $g_iSpace), 135, 9)
            GUICtrlCreateGroup("", -99, -99, 1, 1)
        EndIf
        $g_idLbl_Event[0] = $i
    Next
    ; Resizes the Frame label
    GUICtrlSetPos($idFrame, 0, 0, 136, $g_iGuiHeight)
    ; Get the position of the Countdown gui
    Local $aWinPos = WinGetPos($hCountdownDays)

    ; Updates the height of the Countdown gui, using the pos we got, and the new height using the updated global guiheight
    _WinAPI_SetWindowPos($hCountdownDays, $HWND_NOTOPMOST, $aWinPos[0], $aWinPos[1], 136, $g_iGuiHeight, $SWP_NOMOVE)
    
    ; Finaly we checks if we have exceeded or maxed out the allowed amounts of Events.
    ; If we have we disables the add event MenuItem, thereby preventing the user to add more events, until
    ; an Event is either disabled or deleted, making up space for a new one.
    If $g_aEvents[0] >= $g_iMaxEvents Then GUICtrlSetState($MenuItem_AddEvent, $GUI_DISABLE)
    
EndFunc   ;==>UpdateGui

Func CbSetState($iCtrlID, $bEnabled)
    Switch $bEnabled ; Popup checkbox
        Case True
            GUICtrlSetState($iCtrlID, $GUI_CHECKED)
        Case False
            GUICtrlSetState($iCtrlID, $GUI_UNCHECKED)
    EndSwitch
EndFunc   ;==>CbSetState



Func PosSave()
    Local $aPos = WinGetPos($hCountdownDays)
    IniWrite($g_sSetFile, 'Settings', 'Posx', $aPos[0])
    IniWrite($g_sSetFile, 'Settings', 'Posy', $aPos[1])
EndFunc   ;==>PosSave


Func Countdown()
    If $g_aEvents[1] = Null Then Return ; If we don't have any events
    For $i = 1 To $g_aEvents[0]
        ; cycle through events
        
        Local $iGetSec = _DateDiff('s', _NowCalc(), $g_aEventData[$i][0])
        ; grab seconds of difference from now to future date

        If $iGetSec <= 0 And $g_aEventData[$i][1] = True Then
            EventHandle($g_aEvents[$i], $i)
        EndIf
        Ticks_To_Time($iGetSec, $g_iDays, $g_iHours, $g_iMins, $g_iSecs)
        ; convert seconds to days/hours/minutes/seconds
        
        ; set color and data
        Switch $g_iDays
            Case 0 To 5
                GUICtrlSetDataAndColor($g_idLbl_Days[$i], $g_iDays, 1) ; Red
            Case 6 To 10
                GUICtrlSetDataAndColor($g_idLbl_Days[$i], $g_iDays, 2) ; Yellow
            Case Else
                GUICtrlSetDataAndColor($g_idLbl_Days[$i], $g_iDays, 3) ; Green
        EndSwitch
        ; If days = 0 we want to change the timer colors
        If $g_iDays = 0 Then
            Switch $g_iHours
                Case 0 To 6 
                    GUICtrlSetDataAndColor($g_idLbl_Hrs[$i], $g_iHours, 1) ; Red
                    GUICtrlSetDataAndColor($g_idLbl_Min[$i], $g_iMins, 1) ; Red
                    GUICtrlSetDataAndColor($g_idLbl_Sec[$i], $g_iSecs, 1) ; Red
                Case 7 To 12
                    GUICtrlSetDataAndColor($g_idLbl_Hrs[$i], $g_iHours, 2) ; Yellow
                    GUICtrlSetDataAndColor($g_idLbl_Min[$i], $g_iMins, 2) ; Yellow
                    GUICtrlSetDataAndColor($g_idLbl_Sec[$i], $g_iSecs, 2) ; Yellow
                Case 13 To 23
                    GUICtrlSetDataAndColor($g_idLbl_Hrs[$i], $g_iHours, 3) ; Green
                    GUICtrlSetDataAndColor($g_idLbl_Min[$i], $g_iMins, 3) ; Green
                    GUICtrlSetDataAndColor($g_idLbl_Sec[$i], $g_iSecs, 3) ; Green
            EndSwitch
        Else ; If not we just keep them black
            GUICtrlSetDataAndColor($g_idLbl_Hrs[$i], $g_iHours) ; Black
            GUICtrlSetDataAndColor($g_idLbl_Min[$i], $g_iMins) ; Black
            GUICtrlSetDataAndColor($g_idLbl_Sec[$i], $g_iSecs) ; Black
        EndIf
    Next
EndFunc   ;==>Countdown

Func EventHandle($sEvent, $iIndex)
    ; Here we handles the event when it's time is up o_O
    
    ; First we disable the event, to prevent it from caling this function more than once.
    IniWrite($g_sEventFile, $sEvent, 'Enabled', False) ; Set False in the ini file
    $g_aEventData[$iIndex][1] = False ; Update the EventData array from True to false
    ; We need to check if the we should play a sound and / or throw a popup
    Local $aData = IniReadSection($g_sEventFile, $sEvent)
    ; Error checker
    If @error Then
        MsgBox(BitOR($MB_ICONERROR, $MB_SETFOREGROUND, $MB_TOPMOST), 'Error reading event data', 'An error happend when trying to read the event data!' _
                 & @CRLF & 'The program will now close!!')
        Exit
    EndIf
    ; Check if a sound should be played
    If StringToType($aData[5][1]) = True Then SoundPlay($aData[4][1])
    
    ; Check if a popup should be displayed
    If StringToType($aData[6][1]) = True Then
        ; Disable the on event mode, else the GuiGetMsg won't work
        Opt("GUIOnEventMode", 0)
        ; Create the popup window
        Local $iStyle = BitOR($WS_SYSMENU, $WS_POPUP, $DS_SETFOREGROUND, $WS_CAPTION)
        Local $iExStyle = BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)
        Local $hPopup = GUICreate($aData[1][1], 405, 285, Default, Default, $iStyle, $iExStyle)
        Local $idEdit_Popup = GUICtrlCreateEdit("", 11, 8, 385, 233, BitOR($ES_CENTER, $ES_READONLY, $ES_WANTRETURN, $WS_VSCROLL))
        ; Set the Event description test to the edit
        GUICtrlSetData(-1, @CRLF & SplitText($aData[2][1]), 45)
;~      GUICtrlSetBkColor(-1, 0x4E4E4E)
        GUICtrlSetFont(-1, 18, 800, 0, "Arial Narrow") ; Boost the font size
        Local $idBtn_OK = GUICtrlCreateButton("OK", 165, 256, 75, 21, $BS_DEFPUSHBUTTON)
        GUISetState(@SW_SHOW)

        While 1
            Local $nMsg = GUIGetMsg()
            Switch $nMsg
                Case $GUI_EVENT_CLOSE
                    SoundPlay('') ; Kill the sound if any
                    GUIDelete($hPopup) ; Delete the gui
                    ExitLoop ; And exit loop
                Case $idBtn_OK
                    SoundPlay('') ; Kill the sound if any
                    GUIDelete($hPopup) ; Delete the gui
                    ExitLoop ; And exit loop
            EndSwitch
        WEnd
        Opt("GUIOnEventMode", 1) ; Reenable OnEventMode
    EndIf
    ; Set background label of the event to Yellow and text to Red - Indicate that the event is up
    GUICtrlSetColor($g_idLbl_Event[$iIndex], $COLOR_RED) ; Red
    GUICtrlSetBkColor($g_idLbl_Event[$iIndex], $COLOR_YELLOW) ; Yellow
    GUICtrlSetData($g_idLbl_Sec[$iIndex], '00') ; And set sec to 00 just becorse

EndFunc   ;==>EventHandle


Func Ticks_To_Time($iSeconds, ByRef $idays, ByRef $iHours, ByRef $iMins, ByRef $iSecs)
    ; second conversion.

    ; modified: added days. added format style, to use 00 for time
    If Number($iSeconds) > 0 Then
        $idays = Int($iSeconds / 86400)

        $iSeconds = Mod($iSeconds, 86400)
        $iHours = StringFormat("%02d", Int($iSeconds / 3600))

        $iSeconds = Mod($iSeconds, 3600)
        $iMins = StringFormat("%02d", Int($iSeconds / 60))

        $iSecs = StringFormat("%02d", Mod($iSeconds, 60))
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>Ticks_To_Time

Func GUICtrlSetDataAndColor($hWnd, $iCount, $iColor = 0)
    ; Set Day(s), Hour(s), Min(s), Sec(s) and color to the countdown labels
    Local $dRed, $dYellow, $dGreen, $dBlack

    Switch $iColor ; Check if we chould use a color
        Case 0 ; Black <- Default color
            $dBlack = 0x000000
        Case 1 ; Red
            $dRed = 0xFF0000
        Case 2 ; Yellow
            $dYellow = 0xFFFF00
        Case 3 ; Green
            $dGreen = 0x008000
    EndSwitch

    If GUICtrlRead($hWnd) <> $iCount Or GUICtrlRead($hWnd) = '' Then
        ; if data is different then change. helps with flickering
        GUICtrlSetData($hWnd, $iCount)
        GUICtrlSetColor($hWnd, $dRed & $dYellow & $dGreen & $dBlack)
    EndIf

EndFunc   ;==>GUICtrlSetDataAndColor

Func CalcEndDate($idays, $sTime)
    ; Calculates a new date by adding a specified number of Days to an initial date
    Return _DateAdd('D', $idays, @YEAR & '/' & @MON & '/' & @MDAY & ' ' & $sTime)
EndFunc   ;==>CalcEndDate


Func StringToType($vData)
    ; Converts a string to bool, Number or KeyWord
    Local $aData = StringRegExp($vData, "(?i)\bTrue\b|\bFalse\b|\bNull\b|\bDefault\b|\d{1,}", 3)
    If Not IsArray($aData) Then Return $vData
    Switch $aData[0]
        Case 'True'
            Return True
        Case 'False'
            Return False
        Case 'Null'
            Return Null
        Case 'Default'
            Return Default
        Case Else
            Return Number($aData[0])
    EndSwitch
EndFunc   ;==>StringToType

Func SplitText($sString, $iLenght = 45)
    #cs
        This splits a large text up into cunks at N lengt, at the last space within the nLength specified, and seperated with a @CRLF
        eg. 'A child asked his father, "How were people born?" So his father said, "Adam and Eve made babies, then their babies became adults and made babies, and so on."
        Would be formated as this:
        A child asked his father, "How were people born?" So his father said, "Adam and Eve
        made babies, then their babies became adults and made babies, and so on."
    #ce
    Local $sResult = StringRegExpReplace($sString, ".{1," & $iLenght - 1 & "}\K(\s{1,2}|$)", @CRLF)
    Return $sResult
EndFunc   ;==>SplitText

 

Edited by Rex
Adde description and scr shots
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...