Sign in to follow this  
Followers 0
eignxing

AutoIt ToDo List

8 posts in this topic

#1 ·  Posted (edited)

Here is a ToDo List that is simple and easy to use. Thanks to a lot of people on the forums that I borrowed code from- namely the ones who posted about listview inline edit and color for the rows. It was good to learn from.

Add entries by pressing enter

delete entries by right clicking

double click to edit entries

closing saves the list

comments welcome, enjoy!

EDIT:

added multiple listview selection

new option for right click to add/remove marker

added delete button

#include <GuiConstants.au3>
#include <GuiEdit.au3>
#include <Date.au3>
#include <File.au3>
#include <GUIListView.au3>
#include <WinAPI.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <Misc.au3>


Opt("TrayIconHide", 1)
$dll = DllOpen("user32.dll")
Global $hEdit, $hDC, $hBrush, $Item = -1, $SubItem = 0
Global $Style = BitOR($WS_CHILD, $WS_VISIBLE, $ES_AUTOHSCROLL, $ES_LEFT)
Global $iLastItem = -1, $iLastsubitemNR = -1
Global $timestart = TimerInit()

$version = "1.3"
If WinExists("ToDo v"&$version) <> 0 Then
    WinActivate("ToDo v"&$version)
    Exit
EndIf

$main_form = GUICreate("ToDo v"&$version, 585, 345, 235, 193)
$filemenu = GUICtrlCreateMenu("&File")
$menu_options = GUICtrlCreateMenuItem("Options", $filemenu)
$MenuItem1 = GUICtrlCreateMenuItem("", $filemenu)
$menu_exit = GUICtrlCreateMenuItem("Exit", $filemenu)
$listview = GUICtrlCreateListView("No.|ToDo Items|Time Created", 8, 8, 568, 265, $LVS_REPORT)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 30)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 390)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 144)
$add_input = GUICtrlCreateInput("", 8, 288, 329, 21)
$add_button = GUICtrlCreateButton("Add", 352, 288, 46, 25, BitOR($BS_DEFPUSHBUTTON,$WS_GROUP))
$exit_button = GUICtrlCreateButton("Exit", 477, 288, 98, 25, $WS_GROUP)
$delete_button = GUICtrlCreateButton("Delete", 408, 288, 59, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

$hListView = GUICtrlGetHandle($listview)

$working_dir = @WorkingDir

Local $aFontBold = DllCall("gdi32.dll", "int", "CreateFont", "int", 14, "int", 0, "int", 0, "int", 0, "int", 700, _
        "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, _
        "dword", 0, "str", "")

Local $aFontItalic = DllCall("gdi32.dll", "int", "CreateFont", "int", 14, "int", 0, "int", 0, "int", 0, "int", 400, _
        "dword", 1, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, "dword", 0, _
        "dword", 0, "str", "")

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()

$options_form = GUICreate("Options", 312, 170, 273, 259)
$arrangebytime_cb = GUICtrlCreateCheckbox("Right click deletes items", 16, 16, 217, 17)
;GUICtrlSetState(-1, $GUI_DISABLE)
$usecolors_cb = GUICtrlCreateCheckbox("Use colors (experimental)", 16, 48, 257, 17)
$clearcomplete_cb = GUICtrlCreateCheckbox("Option 3", 16, 80, 273, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$done_button = GUICtrlCreateButton("Done", 16, 128, 75, 25, $WS_GROUP)
$cancel_button = GUICtrlCreateButton("Cancel", 112, 128, 75, 25, $WS_GROUP)
GUISetState(@SW_HIDE)

$settingsarray = _readsettings() ;3 settings to read

If FileExists($working_dir & "\todolist.ini") = 1 Then
    $todolist_file = FileOpen($working_dir & "\todolist.ini", 0)
    $todolist_lines = _FileCountLines($working_dir & "\todolist.ini")

    GUICtrlSetData($listview, "No.|ToDo Items: ("&$todolist_lines&")|Time Created")

    If $todolist_lines <> 0 Then
        Dim $todo_ar[$todolist_lines][3]
    Else
        Dim $todo_ar[1][3]
    EndIf
    If $todolist_lines >= 1 Then
        $i = 1
        Do
            $divider1 = StringInStr(FileReadLine($todolist_file, $i), "¥")
            $divider2 = StringInStr(FileReadLine($todolist_file, $i), "§")
            $date = StringMid(FileReadLine($todolist_file, $i), $divider1 + 1, 19)
            $color = 0
            $memoitem = StringLeft(FileReadLine($todolist_file, $i), $divider1 - 1)
            $todo_ar[$i - 1][0] = $memoitem
            $todo_ar[$i - 1][1] = $date
            $todo_ar[$i - 1][2] = $color
            GUICtrlCreateListViewItem($i & "|" & $memoitem & "|" & $date, $listview)
            $i = $i + 1
        Until $i = $todolist_lines + 1
        _GUICtrlListView_EnsureVisible($hListView, $todolist_lines-1)
    EndIf
    FileClose($todolist_file)
Else
    $todolist_file = FileOpen($working_dir & "\todolist.ini", 2)
    Dim $todo_ar[1][3]
    FileClose($todolist_file)
EndIf

$todo_ar = _constructcolors()

_WinAPI_RedrawWindow($hListView)
_GUICtrlListView_RegisterSortCallBack($hListView)

_GUICtrlListView_SortItems($hListView, 2)

GUICtrlSetState($add_input, $GUI_FOCUS)

While 1
    $nMsg = GUIGetMsg()

    If _IsPressed("2E", $dll) Then
        _GUICtrlListView_DeleteItemsSelected($hListView)
        ;GUICtrlSetState($add_input, $GUI_FOCUS)
    EndIf

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            If WinGetState("Options") <> 15 Then
                _exit()
            Else
                GUISetState(@SW_HIDE, "Options")
                _readsettings()
            EndIf
        Case $menu_exit
            _exit()
        Case $menu_options
            _readsettings()
            GUISetState(@SW_SHOW, "Options")
        Case $cancel_button
            GUISetState(@SW_HIDE, "Options")
            _readsettings()
        Case $delete_button
        ;   $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
            ;_GUICtrlListView_DeleteItem($hListView, DllStructGetData($tInfo, "Index"))
            _GUICtrlListView_DeleteItemsSelected($hListView)
            GUICtrlSetData($listview, "No.|ToDo Items ("&_GUICtrlListView_GetItemCount($listview)&")|Time Created")
        Case $done_button
            _writesettings()
            GUISetState(@SW_HIDE, "Options")
        Case $add_button

            If GUICtrlRead($add_input) = "" Then
            Else
                $stamp = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
                $addnum = _GUICtrlListView_GetItemCount($listview)
                _GUICtrlListView_AddItem($hListView, $addnum+1, 0)
                _GUICtrlListView_AddSubItem($hListView, $addnum, GUICtrlRead($add_input), 1, 1)
                _GUICtrlListView_AddSubItem($hListView, $addnum, $stamp, 2, 1)
                _GUICtrlListView_EnsureVisible($hListView, $addnum)
                GUICtrlSetData($listview, "No.|ToDo Items: ("&_GUICtrlListView_GetItemCount($listview)&")|Time Created")
                GUICtrlSetData($add_input, "")
                GUICtrlSetState($add_input, $GUI_FOCUS)
            EndIf
        Case $exit_button
            _exit()
    EndSwitch
WEnd


Func _exit()
    FileClose($todolist_file)
    If _GUICtrlListView_GetItemCount($listview) <> 0 Then
        FileOpen($working_dir & "\todolist.ini", 2)
        $l = 0
        Do
            FileWriteLine($working_dir & "\todolist.ini", _GUICtrlListView_GetItemText($hListView, $l, 1) & "¥" & _GUICtrlListView_GetItemText($hListView, $l, 2) & "§Color")
            $l = $l + 1
        Until $l = _GUICtrlListView_GetItemCount($listview)
        FileClose($working_dir & "\todolist.ini")
    Else
        FileDelete($working_dir & "\todolist.ini")
    EndIf
    _GUICtrlListView_UnRegisterSortCallBack($listview)
    DllClose($dll)
    GUIDelete()
    Exit
EndFunc   ;==>_exit

Func _readsettings()
    Dim $settings_ar[3]
    If FileExists($working_dir & "\settings.ini") = 1 Then
        $settings_file = FileOpen($working_dir & "\settings.ini", 0)
        If StringInStr(FileReadLine($settings_file, 1), "arrangebytime_cb=0") <> 0 Then
            GUICtrlSetState($arrangebytime_cb, 4)
            $settings_ar[0] = 4
        Else
            GUICtrlSetState($arrangebytime_cb, 1)
            $settings_ar[0] = 1
        EndIf

        If StringInStr(FileReadLine($settings_file, 2), "usecolors_cb=0") <> 0 Then
            GUICtrlSetState($usecolors_cb, 4)
            $settings_ar[1] = 4
        Else
            GUICtrlSetState($usecolors_cb, 1)
            $settings_ar[1] = 1
        EndIf

        If StringInStr(FileReadLine($settings_file, 3), "clearcomplete_cb=0") <> 0 Then
            GUICtrlSetState($clearcomplete_cb, 4)
            $settings_ar[2] = 4
        Else
            GUICtrlSetState($clearcomplete_cb, 1)
            $settings_ar[2] = 1
        EndIf
        FileClose($settings_file)
    Else
        $settings_file = FileOpen($working_dir & "\settings.ini", 2)
        FileWriteLine($settings_file, "arrangebytime_cb=0")
        GUICtrlSetState($arrangebytime_cb, 4)
        $settings_ar[0] = 4
        FileWriteLine($settings_file, "usecolors_cb=0")
        GUICtrlSetState($usecolors_cb, 4)
        $settings_ar[1] = 4
        FileWriteLine($settings_file, "clearcomplete_cb=0")
        GUICtrlSetState($clearcomplete_cb, 4)
        $settings_ar[2] = 4
        FileClose($settings_file)
    EndIf
    Return $settings_ar
EndFunc   ;==>_readsettings

Func _writesettings()
    $settings_file = FileOpen($working_dir & "\settings.ini", 2)
    If GUICtrlRead($arrangebytime_cb) = 4 Then
        FileWriteLine($settings_file, "arrangebytime_cb=0")
    Else
        FileWriteLine($settings_file, "arrangebytime_cb=1")
    EndIf

    If GUICtrlRead($usecolors_cb) = 4 Then
        FileWriteLine($settings_file, "usecolors_cb=0")
        _WinAPI_RedrawWindow($hListView)
    Else
        FileWriteLine($settings_file, "usecolors_cb=1")
        _WinAPI_RedrawWindow($hListView)
    EndIf

    If GUICtrlRead($clearcomplete_cb) = 4 Then
        FileWriteLine($settings_file, "clearcomplete_cb=0")
    Else
        FileWriteLine($settings_file, "clearcomplete_cb=1")
    EndIf
    FileClose($settings_file)
EndFunc   ;==>_writesettings

Func _constructcolors()
    _ArraySort($todo_ar, 1, 0, 0, 1) ;most recent to oldest times
    $diff = _DateDiff('s', $todo_ar[UBound($todo_ar) - 1][1], $todo_ar[0][1])
    $colorperc = ($diff / UBound($todo_ar)) / $diff

    $j = 0
    Do
        $tmp = 0 & ";" & (UBound($todo_ar) + 1 - $j) * (200 * $colorperc) & ";" & 0
        $nRGB = _ColorSwitch($tmp)
        $todo_ar[$j][2] = $nRGB
        $j = $j + 1
    Until $j = UBound($todo_ar)

    Return ($todo_ar)

EndFunc   ;==>_constructcolors

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CUSTOMDRAW
                    If GUICtrlRead($usecolors_cb) <> 4 Then
                        If Not _GUICtrlListView_GetViewDetails($hWndFrom) Then Return $GUI_RUNDEFMSG
                        Local $tCustDraw = DllStructCreate('hwnd hwndFrom;int idFrom;int code;' & _
                                'dword DrawStage;hwnd hdc;long rect[4];dword ItemSpec;int ItemState;dword Itemlparam;' & _
                                'dword clrText;dword clrTextBk;int SubItem;dword ItemType;dword clrFace;int IconEffect;' & _
                                'int IconPhase;int PartID;int StateID;long rectText[4];int Align', _;winxp or later
                                $ilParam)
                        Local $iDrawStage, $iItem, $iSubitem, $hDC, $iColor1, $iColor2, $iColor3
                        $iDrawStage = DllStructGetData($tCustDraw, 'DrawStage')

                        If $iDrawStage = $CDDS_PREPAINT Then Return $CDRF_NOTIFYITEMDRAW;request custom drawing of items
                        If $iDrawStage = $CDDS_ITEMPREPAINT Then Return $CDRF_NOTIFYSUBITEMDRAW;request drawing each cell separately
                        If Not BitAND($iDrawStage, $CDDS_SUBITEM) Then Return $CDRF_DODEFAULT
                        $iItem = DllStructGetData($tCustDraw, 'ItemSpec')

                        Dim $avArray[1]
                        For $i = 1 To _GUICtrlListView_GetItemCount($hListView)
                            _ArrayAdd($avArray, $i)
                        Next

                        $iIndex = _ArrayToString($avArray, "|") & "|"
                        $color2 = "0xFFFFFF"
                        If $iIndex = -1 Then Return $CDRF_NEWFONT

                        Switch $iItem
                            Case 0 To UBound($todo_ar)
                                DllStructSetData($tCustDraw, 'clrTextBk', _color($iItem))
                                DllStructSetData($tCustDraw, 'clrText', $color2)
                        EndSwitch
                        Return $CDRF_NEWFONT
                    EndIf

                Case $LVN_COLUMNCLICK ; A column was clicked
                    $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
                    _GUICtrlListView_SortItems($hWndFrom, DllStructGetData($tInfo, "SubItem"))
                Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
                    Local $aRect
                    Local $aHit = _GUICtrlListView_SubItemHitTest($hListView)
                    If ($aHit[0] <> -1) And ($aHit[1] = 0) Then
                        $Item = $aHit[0]
                        $SubItem = 0
                        $aRect = _GUICtrlListView_GetItemRect($hListView, $Item)
                    ElseIf ($aHit[0] <> -1) And ($aHit[1] > 0) Then
                        $Item = $aHit[0]
                        $SubItem = $aHit[1]
                        $aRect = _GUICtrlListView_GetSubItemRect($hListView, $Item, $SubItem)
                    Else
                        Return $GUI_RUNDEFMSG
                    EndIf

                    Local $iItemText = _GUICtrlListView_GetItemText($hListView, $Item, $SubItem)

                    If $SubItem = 1 Then
                        Local $iLen = 377
                    Else
                        Local $iLen = 131
                    EndIf

                    Local $aPos = ControlGetPos($main_form, "", $hListView)
                    Local $iX = $aPos[0] + $aRect[0] + 5
                    Local $iY = $aPos[1] + $aRect[1] + 2

                    Global $hEdit = _GUICtrlEdit_Create($main_form, $iItemText, $iX, $iY, $iLen + 10, 17, $Style)
                    _GUICtrlEdit_SetSel($hEdit, 0, -1)
                    _WinAPI_SetFocus($hEdit)

                    $hDC = _WinAPI_GetWindowDC($hEdit)
                    $hBrush = _WinAPI_CreateSolidBrush(0x000000)
                    FrameRect($hDC, 0, 0, $iLen + 10, 17, $hBrush)
                Case $NM_RCLICK
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
                    If GUICtrlRead($arrangebytime_cb) <> 4 Then
                        _GUICtrlListView_DeleteItem($hListView, DllStructGetData($tInfo, "Index"))
                        GUICtrlSetData($listview, "No.|ToDo Items: ("&_GUICtrlListView_GetItemCount($listview)&")|Time Created")
                    Else
                        $striketxt = _GUICtrlListView_GetItemText($hListView, DllStructGetData($tInfo, "Index"), 1)
                        If StringInStr($striketxt, Chr(149)&" ") <> 0 Then
                            $striketxt = StringReplace($striketxt, Chr(149)&""&Chr(149)&" ", "")
                            _GUICtrlListView_SetItemText($hListView, DllStructGetData($tInfo, "Index"), $striketxt, 1)
                        Else
                            _GUICtrlListView_SetItemText($hListView, DllStructGetData($tInfo, "Index"), Chr(149)&""&Chr(149)&" "&$striketxt, 1)
                        EndIf
                    EndIf
                Case $LVN_HOTTRACK ;future tooltip implementation
                    ;If GUICtrlGetState($hListView) = $GUI_FOCUS Then
;~                      Local $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
;~                      Local $iItem = DllStructGetData($tInfo, "Item")
;~                      Local $subitemNR = DllStructGetData($tInfo, "SubItem")

;~                      ; if no cell change return without doing anything
;~                      If $iLastItem = $iItem And $iLastsubitemNR = $subitemNR Then Return 0
;~                      $iLastItem = $iItem
;~                      $iLastsubitemNR = $subitemNR

;~                      If TimerDiff($timestart) >= 1000 Then
;~                          Local $sToolTipData = _GUICtrlListView_GetItemText($hListView, $iItem,$subitemNR)
;~                          $Mypos = StringInStr ( $sToolTipData, @CRLF )
;~                          ToolTip($sToolTipData, MouseGetPos(0) + 20, MouseGetPos(1) + 20)
;~                          ConsoleWrite("R" & $iItem & "C" & $iLastsubitemNR & " No tip" & @CR)
;~                          $timestart = TimerInit()
;~                      EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func RGB2BGR($iColor)
    Return BitAND(BitShift(String(Binary($iColor)), 8), 0xFFFFFF)
EndFunc   ;==>RGB2BGR

Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
    Local $stRect = DllStructCreate("int;int;int;int")
    DllStructSetData($stRect, 1, $nLeft)
    DllStructSetData($stRect, 2, $nTop)
    DllStructSetData($stRect, 3, $nRight)
    DllStructSetData($stRect, 4, $nBottom)
    DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
EndFunc   ;==>FrameRect

Func _color($var)

    $iIt_date = _GUICtrlListView_GetItemText($hListView, $var, 2) ;0 index on lv
    $todate = _ArraySearch($todo_ar, $iIt_date, 0, 0, 0, 1, 1, 1)

    If $todate <> -1 Then
        Return $todo_ar[$todate][2]
    Else
        Global $color2 = "0x000000"
        Return "0xFFFFFF"
    EndIf
EndFunc   ;==>_color


Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    #forceref $hWnd, $Msg
    Local $hWndFrom, $iIDFrom, $hWndEdit, $iCode
    ; Local $iCode = BitShift($wParam, 16)
    If Not IsHWnd($hEdit) Then $hWndEdit = GUICtrlGetHandle($hEdit)
    $hWndFrom = $lParam
    $iIDFrom = _WinAPI_LoWord($wParam)
    $iCode = _WinAPI_HiWord($wParam)

    If _IsPressed("0D", $dll) Then
        GUICtrlSetState($add_input, $GUI_FOCUS)
    EndIf

    Switch $lParam
        Case $hEdit, $hWndEdit
            Switch $iCode
                Case $EN_KILLFOCUS
                    Local $iText = _GUICtrlEdit_GetText($hEdit)
                    _GUICtrlListView_SetItemText($hListView, $Item, $iText, $SubItem)
                    _WinAPI_DeleteObject($hBrush)
                    _WinAPI_ReleaseDC($hEdit, $hDC)
                    _WinAPI_DestroyWindow($hEdit)
                    $Item = -1
                    $SubItem = 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _ColorSwitch($ccode)
    $Symbol = "0x"
    $ccode = StringReplace($ccode, "0x", "")
    $ccode = StringReplace($ccode, "#", "")
    $tCode = StringSplit($ccode, ";")
    If IsArray($tCode) Then
        $1 = Hex(Int($tCode[1]), 2)
        $2 = Hex(Int($tCode[2]), 2)
        $3 = Hex(Int($tCode[3]), 2)
        Return $Symbol & $1 & $2 & $3
    Else
        SetError(1)
        Return -1
    EndIf
EndFunc   ;==>_ColorSwitch
Edited by Jon8763

Share this post


Link to post
Share on other sites



very nice ... good job!

though cant sort the items in list by no. , name, date etc.

Share this post


Link to post
Share on other sites

Good little app.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

very nice ... good job!

though cant sort the items in list by no. , name, date etc.

Thanks, you should be able to sort by clicking the column headers

Share this post


Link to post
Share on other sites

Nice.

Some ideas:

Editing functionality,

Todo status (started, in progress, completed)

Priority

Thanks, you can edit the text by double clicking. I added a simple marker option on right-click to show completed or not (updated in post). I don't think I will add levels of completion though. Thanks for the ideas.

Share this post


Link to post
Share on other sites

I can't change manually todolist.ini => it get bad colored gradients.

If I change manually todolist.ini, my added lines are of the same green color, while lines created under todo are gradient colored.

Strange!

Any idea?

post-22877-12646678168406_thumb.jpg

Share this post


Link to post
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
Sign in to follow this  
Followers 0