#include "thief.h"

LPBUTTONS  LpButtons ;
FONTRECORD ButFont ;

BOOL CALLBACK ButtonBoxWndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    static int nSel ;
    static HWND hFunction ;
    int nI, nF ;
    char cTmp [2048] ;
    RECT rc ;

    switch (iMsg)
        {
        case WM_INITDIALOG :
            if (! STATE_EnterDialogBox ())
                {
                EndDialog (hwnd, FALSE) ;
                return FALSE ;
                }

            hFunction = GetDlgItem (hwnd, IDD_BUTTON_FUNCTION) ;
            LpButtons = (LPBUTTONS) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof (BUTTONS)) ;

            for (nI = 0 ; nI < MAX_BUTTON ; nI++)
                {
                LpButtons->nType [nI] = Buttons.nType [nI] ;
                strcpy (LpButtons->cText   [nI], Buttons.cText   [nI]) ;
                strcpy (LpButtons->cAction [nI], Buttons.cAction [nI]) ;
                }

            SendDlgItemMessage (hwnd, IDD_BUTTON_EXTENDED, BM_SETCHECK, User.bExtendedButton ? BST_CHECKED : BST_UNCHECKED, 0) ;

            SendDlgItemMessage (hwnd, IDD_BUTTON, LB_RESETCONTENT, 0, 0) ;
            for (nI = 0 ; nI < MAX_BUTTON ; nI++)
                {
                sprintf (cTmp, "Button %02d", nI + 1) ;
                SendDlgItemMessage (hwnd, IDD_BUTTON, LB_ADDSTRING, 0, (LPARAM) cTmp) ;
                }

            SendDlgItemMessage (hwnd, IDD_BUTTON, LB_SETCURSEL, 0, 0) ;
            SendDlgItemMessage (hwnd, IDD_BUTTON_LABEL,   WM_SETTEXT, 0, (LPARAM) LpButtons->cText   [0]) ;

            SendMessage (hFunction, CB_RESETCONTENT, 0, 0) ;
            for (nI = 0 ; nI < MAX_FMENU ; nI++)
                {
                strcpy (cTmp, FString [nI]) ;
                SendMessage (hFunction, CB_ADDSTRING, 0, (LPARAM) cTmp) ;
                }

            strcpy (cTmp, FString [LpButtons->nType [0]]) ;
            if (SendMessage (hFunction, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) cTmp) == CB_ERR)
                {
                SendMessage (hFunction, CB_SETCURSEL, (WPARAM) -1, (LPARAM) 0) ;
                SendMessage (hFunction, WM_SETTEXT, (WPARAM) 0, (LPARAM) cTmp) ;
                }

            SendDlgItemMessage (hwnd, IDD_BUTTON_COMMAND, WM_SETTEXT, 0, (LPARAM) LpButtons->cAction [0]) ;

            FONT_Copy_Info (&ButFont, &hfFont [FONT_BUTTON]) ;
            
            nSel = 0 ;
            EnableWindow (GetDlgItem (hwnd, IDD_BUTTON_COMMAND), TOOLBOX_NeedCommandValue (LpButtons->nType [nSel])) ;

            TOOLBOX_CenterWindow (hwnd, GetWindow (hwnd, GW_OWNER)) ;
            return TRUE ;

        case WM_COMMAND:
            switch (LOWORD (wParam))
                {
                case IDD_BUTTON :
                    if (HIWORD (wParam) == LBN_SELCHANGE)
                        {
                        nI = SendDlgItemMessage (hwnd, IDD_BUTTON, LB_GETCURSEL, 0, 0) ;

                        SendDlgItemMessage (hwnd, IDD_BUTTON_LABEL, WM_GETTEXT, (WPARAM) MAX_BUTTON_TEXT, (LPARAM) cTmp) ;
                        strcpy (LpButtons->cText [nSel], cTmp) ;

                        nF = SendMessage (hFunction, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) ;
                        if (nF == CB_ERR)
                            {
                            LpButtons->nType [nSel] = 0 ;
                            }
                        else
                            {
                            LpButtons->nType [nSel] = nF ;
                            }

                        if (TOOLBOX_NeedCommandValue (LpButtons->nType [nSel]))
                            {
                            SendDlgItemMessage (hwnd, IDD_BUTTON_COMMAND, WM_GETTEXT, (WPARAM) MAX_BUTTON_ACTION - 1, (LPARAM) cTmp) ;
                            strcpy (LpButtons->cAction [nSel], cTmp) ;
                            }
                        else
                            {
                            strcpy (LpButtons->cAction [nSel], "") ;
                            }

                        strcpy (cTmp, FString [LpButtons->nType [nI]]) ;
                        if (SendMessage (hFunction, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) cTmp) == CB_ERR)
                            {
                            SendMessage (hFunction, CB_SETCURSEL, (WPARAM) -1, (LPARAM) 0) ;
                            SendMessage (hFunction, WM_SETTEXT, (WPARAM) 0, (LPARAM) cTmp) ;
                            }

                        SendDlgItemMessage (hwnd, IDD_BUTTON_LABEL,   WM_SETTEXT, 0, (LPARAM) LpButtons->cText   [nI]) ;
                        SendDlgItemMessage (hwnd, IDD_BUTTON_COMMAND, WM_SETTEXT, 0, (LPARAM) LpButtons->cAction [nI]) ;

                        nSel = nI ;
                        EnableWindow (GetDlgItem (hwnd, IDD_BUTTON_COMMAND), TOOLBOX_NeedCommandValue (LpButtons->nType [nSel])) ;
                        }
                    break ;

                case IDD_BUTTON_FUNCTION :
                    if (HIWORD (wParam) == CBN_SELCHANGE)
                        {
                        nF = SendMessage (hFunction, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) ;
                        if (nF == CB_ERR)
                            {
                            EnableWindow (GetDlgItem (hwnd, IDD_BUTTON_COMMAND), 0) ;
                            }
                        else
                            {
                            EnableWindow (GetDlgItem (hwnd, IDD_BUTTON_COMMAND), TOOLBOX_NeedCommandValue (nF)) ;
                            }
                        }
                    break ;

                case IDOK :
                    User.bExtendedButton = ((SendDlgItemMessage (hwnd, IDD_BUTTON_EXTENDED, BM_GETSTATE, 0, 0) == BST_CHECKED) ? 1 : 0) ;

                    SendDlgItemMessage (hwnd, IDD_BUTTON_LABEL, WM_GETTEXT, (WPARAM) MAX_BUTTON_TEXT, (LPARAM) cTmp) ;
                    strcpy (LpButtons->cText [nSel], cTmp) ;

                    nF = SendMessage (hFunction, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) ;
                    if (nF == CB_ERR)
                        {
                        LpButtons->nType [nSel] = 0 ;
                        }
                    else
                        {
                        LpButtons->nType [nSel] = nF ;
                        }

                    if (TOOLBOX_NeedCommandValue (LpButtons->nType [nSel]))
                        {
                        SendDlgItemMessage (hwnd, IDD_BUTTON_COMMAND, WM_GETTEXT, (WPARAM) MAX_BUTTON_ACTION - 1, (LPARAM) cTmp) ;
                        strcpy (LpButtons->cAction [nSel], cTmp) ;
                        }
                    else
                        {
                        strcpy (LpButtons->cAction [nSel], "") ;
                        }

                    for (nI = 0 ; nI < MAX_BUTTON ; nI++)
                        {
                        Buttons.nType [nI] = LpButtons->nType [nI] ;
                        strcpy (Buttons.cText   [nI], LpButtons->cText   [nI]) ;
                        strcpy (Buttons.cAction [nI], LpButtons->cAction [nI]) ;

                        TOOLBOX_AllTrim (Buttons.cText [nI]) ;

                        if (TOOLBOX_NeedCommandValue (Buttons.nType [nI]))
                            {
                            TOOLBOX_AllTrim (Buttons.cAction [nI]) ;
                            }
                        else
                            {
                            strcpy (Buttons.cAction [nI], "") ;
                            }
                        }

                    HeapFree (GetProcessHeap (), 0, LpButtons) ;

                    DeleteObject (ButFont.hfFont) ;

                    TOOLBOX_MakeFont (FONT_BUTTON) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SETFONT, (WPARAM) hfFont [FONT_BUTTON].hfFont, MAKELPARAM (TRUE, 0)) ;

                    BUTTON_Refresh () ;
                    BUTTON_RefreshLabel () ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    EndDialog (hwnd, TRUE) ;
                    STATE_LeaveDialogBox () ;
                    return TRUE ;

                case IDCANCEL :
                    HeapFree (GetProcessHeap (), 0, LpButtons) ;

                    FONT_Copy_Info (&hfFont [FONT_BUTTON], &ButFont) ;

                    DeleteObject (hfFont [FONT_BUTTON].hfFont) ;

                    TOOLBOX_MakeFont (FONT_BUTTON) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SETFONT, (WPARAM) hfFont [FONT_BUTTON].hfFont, MAKELPARAM (TRUE, 0)) ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    EndDialog (hwnd, FALSE) ;
                    STATE_LeaveDialogBox () ;
                    return TRUE ;

                case IDD_BUTTON_FONT :
                    if (FONT_ChooseFont (hwndWindow [HWND_BUTTON], FONT_BUTTON))
                        {
                        }
                    return TRUE ;

                case IDD_BUTTON_DEFAULT :
                    for (nI = 0 ; nI < MAX_BUTTON ; nI++)
                        {
                        LpButtons->nType [nI] = FUNCTION_COMMAND ;
                        strcpy (LpButtons->cText   [nI], "") ;
                        strcpy (LpButtons->cAction [nI], "") ;
                        }

                    strcpy (LpButtons->cText   [0], "Sit") ;
                    strcpy (LpButtons->cAction [0], "ptell Sit") ;

                    strcpy (LpButtons->cText   [1], "Go") ;
                    strcpy (LpButtons->cAction [1], "ptell Go") ;

                    strcpy (LpButtons->cText   [2], "Fast") ;
                    strcpy (LpButtons->cAction [2], "ptell Fast") ;

                    strcpy (LpButtons->cText   [3], "Time") ;
                    strcpy (LpButtons->cAction [3], "ptell Time") ;

                    strcpy (LpButtons->cText   [4], "++") ;
                    strcpy (LpButtons->cAction [4], "ptell ++") ;

                    strcpy (LpButtons->cText   [5], "----") ;
                    strcpy (LpButtons->cAction [5], "ptell ----") ;

                    strcpy (LpButtons->cText   [6], "+Q") ;
                    strcpy (LpButtons->cAction [6], "ptell +Q") ;

                    strcpy (LpButtons->cText   [7], "-Q") ;
                    strcpy (LpButtons->cAction [7], "ptell -Q") ;

                    strcpy (LpButtons->cText   [8], "+R") ;
                    strcpy (LpButtons->cAction [8], "ptell +R") ;

                    strcpy (LpButtons->cText   [9], "-R") ;
                    strcpy (LpButtons->cAction [9], "ptell -R") ;

                    strcpy (LpButtons->cText   [10], "+N") ;
                    strcpy (LpButtons->cAction [10], "ptell +N") ;

                    strcpy (LpButtons->cText   [11], "-N") ;
                    strcpy (LpButtons->cAction [11], "ptell -N") ;

                    strcpy (LpButtons->cText   [12], "+B") ;
                    strcpy (LpButtons->cAction [12], "ptell +B") ;

                    strcpy (LpButtons->cText   [13], "-B") ;
                    strcpy (LpButtons->cAction [13], "ptell -B") ;

                    strcpy (LpButtons->cText   [14], "+P") ;
                    strcpy (LpButtons->cAction [14], "ptell +P") ;

                    strcpy (LpButtons->cText   [15], "-P") ;
                    strcpy (LpButtons->cAction [15], "ptell -P") ;

                    strcpy (LpButtons->cText   [16], "OK") ;
                    strcpy (LpButtons->cAction [16], "ptell OK") ;

                    strcpy (LpButtons->cText   [17], "OK now") ;
                    strcpy (LpButtons->cAction [17], "ptell OK now") ;

                    strcpy (LpButtons->cText   [18], "Hard") ;
                    strcpy (LpButtons->cAction [18], "ptell Hard") ;

                    strcpy (LpButtons->cText   [19], "Coming") ;
                    strcpy (LpButtons->cAction [19], "ptell Coming") ;

                    strcpy (LpButtons->cText   [20], "Maybe") ;
                    strcpy (LpButtons->cAction [20], "ptell Maybe") ;

                    strcpy (LpButtons->cText   [21], "I sit") ;
                    strcpy (LpButtons->cAction [21], "ptell I sit") ;

                    strcpy (LpButtons->cText   [22], "Mates me") ;
                    strcpy (LpButtons->cAction [22], "ptell Mates me") ;

                    strcpy (LpButtons->cText   [23], "Mates him") ;
                    strcpy (LpButtons->cAction [23], "ptell Mates him") ;

                    strcpy (LpButtons->cText   [24], "I dead") ;
                    strcpy (LpButtons->cAction [24], "ptell I dead") ;

                    strcpy (LpButtons->cText   [25], "Opp dead") ;
                    strcpy (LpButtons->cAction [25], "ptell Opp dead") ;

                    strcpy (LpButtons->cText   [26], "Yes") ;
                    strcpy (LpButtons->cAction [26], "ptell Yes") ;

                    strcpy (LpButtons->cText   [27], "No") ;
                    strcpy (LpButtons->cAction [27], "ptell No") ;

                    strcpy (LpButtons->cText   [28], "Tell Me Go") ;
                    strcpy (LpButtons->cAction [28], "ptell Tell Me Go") ;

                    strcpy (LpButtons->cText   [29], "Tell U Go") ;
                    strcpy (LpButtons->cAction [29], "ptell Sit Until I Tell U Go") ;

                    strcpy (LpButtons->cText   [30], "Keep Check") ;
                    strcpy (LpButtons->cAction [30], "ptell Keep Checking") ;

                    strcpy (LpButtons->cText   [31], "Nevermind") ;
                    strcpy (LpButtons->cAction [31], "ptell Nevermind") ;

                    strcpy (LpButtons->cText   [32], "We up") ;
                    strcpy (LpButtons->cAction [32], "ptell We up") ;

                    strcpy (LpButtons->cText   [33], "We down") ;
                    strcpy (LpButtons->cAction [33], "ptell We down") ;

                    strcpy (LpButtons->cText   [34], "U get") ;
                    strcpy (LpButtons->cAction [34], "ptell U get") ;
                    
                    strcpy (LpButtons->cText   [35], "He gets") ;
                    strcpy (LpButtons->cAction [35], "ptell He gets") ;

                    strcpy (LpButtons->cText   [36], "Watchout") ;
                    strcpy (LpButtons->cAction [36], "ptell Watchout") ;

                    strcpy (LpButtons->cText   [37], "Feed me") ;
                    strcpy (LpButtons->cAction [37], "ptell Feed me") ;

                    strcpy (LpButtons->cText   [38], "Lag") ;
                    strcpy (LpButtons->cAction [38], "ptell Lag") ;

                    strcpy (LpButtons->cText   [39], "Lag cheat") ;
                    strcpy (LpButtons->cAction [39], "ptell Lag cheating fuckers") ;

                    strcpy (LpButtons->cText   [40], "Mother lag") ;
                    strcpy (LpButtons->cAction [40], "ptell Mother fucking lag") ;

                    strcpy (LpButtons->cText   [41], "Bitch dead") ;
                    strcpy (LpButtons->cAction [41], "ptell Bitch is dead") ;

                    strcpy (LpButtons->cText   [42], "Bite me") ;
                    strcpy (LpButtons->cAction [42], "ptell Bite me") ;

                    strcpy (LpButtons->cText   [43], "Cake") ;
                    strcpy (LpButtons->cAction [43], "ptell Cake") ;

                    strcpy (LpButtons->cText   [44], "Eat shit") ;
                    strcpy (LpButtons->cAction [44], "ptell Eat shit") ;

                    strcpy (LpButtons->cText   [45], "Fuck u") ;
                    strcpy (LpButtons->cAction [45], "ptell Fuck u") ;

                    strcpy (LpButtons->cText   [46], "I focked") ;
                    strcpy (LpButtons->cAction [46], "ptell I finger focked") ;

                    strcpy (LpButtons->cText   [47], "I focking") ;
                    strcpy (LpButtons->cAction [47], "ptell I finger focking") ;

                    strcpy (LpButtons->cText   [48], "Fast bitch") ;
                    strcpy (LpButtons->cAction [48], "ptell Move faster bitch") ;

                    strcpy (LpButtons->cText   [49], "Son bitch") ;
                    strcpy (LpButtons->cAction [49], "ptell Son of a bitch") ;

                    strcpy (LpButtons->cText   [50], "So easy") ;
                    strcpy (LpButtons->cAction [50], "ptell So easy") ;

                    strcpy (LpButtons->cText   [51], "She dead") ;
                    strcpy (LpButtons->cAction [51], "ptell She dead") ;

                    strcpy (LpButtons->cText   [52], "They suck") ;
                    strcpy (LpButtons->cAction [52], "ptell They suck") ;

                    LpButtons->nType [53] = FUNCTION_NONE ;
                    strcpy (LpButtons->cText   [53], "") ;
                    strcpy (LpButtons->cAction [53], "") ;

                    nI = SendDlgItemMessage (hwnd, IDD_BUTTON, LB_GETCURSEL, 0, 0) ;

                    SendDlgItemMessage (hwnd, IDD_BUTTON_LABEL, WM_SETTEXT, 0, (LPARAM) LpButtons->cText [nI]) ;

                    if (! TOOLBOX_NeedCommandValue (LpButtons->nType [nI]))
                        {
                        strcpy (LpButtons->cAction [nI], "") ;
                        }

                    strcpy (cTmp, FString [LpButtons->nType [nI]]) ;
                    if (SendMessage (hFunction, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) cTmp) == CB_ERR)
                        {
                        SendMessage (hFunction, CB_SETCURSEL, (WPARAM) -1, (LPARAM) 0) ;
                        SendMessage (hFunction, WM_SETTEXT, (WPARAM) 0, (LPARAM) cTmp) ;
                        }

                    SendDlgItemMessage (hwnd, IDD_BUTTON_COMMAND, WM_SETTEXT, 0, (LPARAM) LpButtons->cAction [nI]) ;

                    nSel = nI ;
                    EnableWindow (GetDlgItem (hwnd, IDD_BUTTON_COMMAND), TOOLBOX_NeedCommandValue (LpButtons->nType [nSel])) ;

                    FONT_Init_Button () ;
                    return TRUE ;

                case IDD_BUTTON_CHESS :
                    User.bExtendedButton = ((SendDlgItemMessage (hwnd, IDD_BUTTON_EXTENDED, BM_GETSTATE, 0, 0) == BST_CHECKED) ? 1 : 0) ;

                    HeapFree (GetProcessHeap (), 0, LpButtons) ;

                    FONT_Copy_Info (&hfFont [FONT_BUTTON], &ButFont) ;

                    DeleteObject (hfFont [FONT_BUTTON].hfFont) ;

                    TOOLBOX_MakeFont (FONT_BUTTON) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SETFONT, (WPARAM) hfFont [FONT_BUTTON].hfFont, MAKELPARAM (TRUE, 0)) ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    BUTTON_Chess () ;
                    BUTTON_Refresh () ;
                    BUTTON_RefreshLabel () ;

                    EAR_Destroy () ;
                    EAR_OldFICSEAR () ;
                    EAR_Load () ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    EndDialog (hwnd, FALSE) ;
                    STATE_LeaveDialogBox () ;
                    return TRUE ;

                case IDD_BUTTON_OLDFICS :
                    User.bExtendedButton = ((SendDlgItemMessage (hwnd, IDD_BUTTON_EXTENDED, BM_GETSTATE, 0, 0) == BST_CHECKED) ? 1 : 0) ;

                    HeapFree (GetProcessHeap (), 0, LpButtons) ;

                    FONT_Copy_Info (&hfFont [FONT_BUTTON], &ButFont) ;

                    DeleteObject (hfFont [FONT_BUTTON].hfFont) ;

                    TOOLBOX_MakeFont (FONT_BUTTON) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SETFONT, (WPARAM) hfFont [FONT_BUTTON].hfFont, MAKELPARAM (TRUE, 0)) ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    BUTTON_OldFICSEAR () ;
                    BUTTON_Refresh () ;
                    BUTTON_RefreshLabel () ;

                    EAR_Destroy () ;
                    EAR_OldFICSEAR () ;
                    EAR_Load () ;

                    EndDialog (hwnd, FALSE) ;
                    STATE_LeaveDialogBox () ;
                    return TRUE ;

                case IDD_BUTTON_NEWFICS :
                    User.bExtendedButton = ((SendDlgItemMessage (hwnd, IDD_BUTTON_EXTENDED, BM_GETSTATE, 0, 0) == BST_CHECKED) ? 1 : 0) ;

                    HeapFree (GetProcessHeap (), 0, LpButtons) ;

                    FONT_Copy_Info (&hfFont [FONT_BUTTON], &ButFont) ;

                    DeleteObject (hfFont [FONT_BUTTON].hfFont) ;

                    TOOLBOX_MakeFont (FONT_BUTTON) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SETFONT, (WPARAM) hfFont [FONT_BUTTON].hfFont, MAKELPARAM (TRUE, 0)) ;

                    GetClientRect (hwndWindow [HWND_BUTTON], &rc) ;

                    SendMessage (hwndWindow [HWND_BUTTON], WM_SIZE, SIZE_RESTORED, (LPARAM) ((rc.bottom << 16) | rc.right)) ;

                    BUTTON_NewFICSEAR () ;
                    BUTTON_Refresh () ;
                    BUTTON_RefreshLabel () ;

                    EAR_Destroy () ;
                    EAR_NewFICSEAR () ;
                    EAR_Load () ;

                    EndDialog (hwnd, FALSE) ;
                    STATE_LeaveDialogBox () ;
                    return TRUE ;
                }
            break ;
        }
    return FALSE ;
    }
