// Implementation of MenuButtons


#ifndef GLOBAL_DECL

#include "global.h"     // Global data types and variables

#endif


#include <string.h>


buttons::buttons()                              // buttons constructor

{
    menu_level = 0;
    at_leaves = false;
    strcpy(szHeading, "Main Menu");
}

int buttons::MenuLevel()
{
    return menu_level;
}

bool buttons::AtLeaves()
{
    return at_leaves;
}

// Change to the chosen category

void buttons::PickCategory(int button_pressed, HWND hChecks[10], pglist* pagelist)
{
    int loop;                                       // loop variable


    if (strlen(szButtonValue[button_pressed]) > 0)
    {
        if (button_pressed < 9)
        {
            if (menu_level <= MAX_CAT_LEVELS)
            {
                menu_level++;

                if (menu_level > 1)
                    strcpy(menu_stack[menu_level], szButtonValue[button_pressed]);
                else if (menu_level == 1)
                    strcpy(menu_stack[menu_level], "Main");
            }
        }
        else
        {
            StoreCheckValues(hChecks, pagelist);
            menu_level--;
        }

        if (menu_level==0)
            strcpy(szHeading, "Main Menu");
        else if (menu_level==1)
            strcpy(szHeading, "Main Categories");
        else if (menu_level>1)
        {
            strcpy(szHeading, "Categories");
            for (loop=2; loop<=menu_level; loop++)
            {
                strcat(szHeading, ", ");
                strcat(szHeading, menu_stack[loop]);
            }
        }
    }
}

// Store the values of the check boxes when exiting a bottom-level menu

void buttons::StoreCheckValues(HWND hChecks[10], pglist* pagelist)
{
    data_item* data;
    int loop;

    for (loop=0; loop<10; loop++)
    {
        if (strlen(szButtonValue[loop]) != 0)   // Only check visible check boxes!

        {
            pagelist -> move_first();
            data = pagelist -> get_item();  // Get a page from the list


            while (data != NULL)
            {
                if (!strcmp(szCheckIDs[loop], data -> page_id))
                {
                    if (SendMessage(hChecks[loop], BM_GETCHECK, 0, 0) == BST_CHECKED)
                        data -> selected = true;
                    else
                        data -> selected = false;
                }
                data = pagelist -> get_item();
            }
        }
    }
}

// Work out the new values of the buttons (or check boxes)

void buttons::Set_Buttons(HWND hButtons[10], HWND hChecks[10], pglist* pagelist)
{
    data_item*  data;                   // Item from linked list

    char        category_values[9][50]; // Current button category values

    char        new_val[50];            // New button value

    int         loop;                   // Loop variable

    bool        inserted;               // Flag to show wether value has been put onto a button

    bool        is_new;                 // Flag to show wether value has not been used before

    bool        is_valid;               // Flag to indicate whether item is valid for this menu


    // Main top level menu

    if (menu_level == 0)
    {
        strcpy(szButtonValue[0], "Instructions");
        strcpy(szButtonValue[1], "Select Categories");
        strcpy(szButtonValue[2], "Go To Sleep");
        strcpy(szButtonValue[3], "Get News Now");
        strcpy(szButtonValue[4], "View Compiled News File");
        strcpy(szButtonValue[5], "Get New Categories Now");
        strcpy(szButtonValue[6], "Change Settings");
        strcpy(szButtonValue[7], "About");
        strcpy(szButtonValue[8], "");
        strcpy(szButtonValue[9], "Quit");
        at_leaves = false;
    }

    // Categories

    else if (menu_level > 0)
    {
        for (loop=0; loop<9; loop++)
            category_values[loop][0] = NULL;

        pagelist -> move_first();

        do
        {
            data = pagelist -> get_item();  
            if (data != NULL)
            {
                strcpy(new_val, data -> category[menu_level-1]);

                is_valid = true;

                // Inly include the list items that are within the current category

                if (menu_level > 1)
                    for (loop=0; loop<=menu_level-2; loop++)
                        if (strcmp(data -> category[loop], menu_stack[loop+2]) != 0)
                            is_valid = false;
                // Don't include blank categories

                if (strlen(new_val)==0)
                    is_valid = false;

                is_new = true;
                inserted = false;

                // If valid, try and find an unused button to put it on

                if (is_valid)
                    for (loop=0; loop<9; loop++)
                        if (!inserted)
                        {
                            if (strcmp(new_val, category_values[loop]) == 0)
                                is_new = false;
                            else if ((strlen(category_values[loop]) == 0) && (is_new))
                            {
                                strcpy(category_values[loop], new_val);
                                inserted = true;
                            }
                        }
            }
        } while (data != NULL);

        // Check if at bottom of hierarchy (actual pages)

        at_leaves = true;
        for (loop=0; loop<9; loop++)
            if (strlen(category_values[loop]) > 0)
                at_leaves = false;

        // At the bottom of the hierarchy, so read page details

        if (at_leaves)
        {
            // Basically do the same as for categories, but for pages

            pagelist -> move_first();
            
            do
            {
                data = pagelist -> get_item();  
                if (data != NULL)
                {
                    strcpy(new_val, data -> description);

                    is_valid = true;

                    // Inly include the pages that are within the current category

                    if (menu_level > 1)
                        for (loop=0; loop<=menu_level-2; loop++)
                            if (strcmp(data -> category[loop], menu_stack[loop+2]) != 0)
                                is_valid = false;
                    // Don't include blank descriptions (there shouldn't be any!)

                    if (strlen(new_val)==0)
                        is_valid = false;

                    is_new = true;
                    inserted = false;

                    // If valid, try and find an unused button to put it on

                    if (is_valid)
                        for (loop=0; loop<9; loop++)
                            if (!inserted)
                            {
                                if (strcmp(new_val, category_values[loop]) == 0)
                                    is_new = false;
                                else if ((strlen(category_values[loop]) == 0) && (is_new))
                                {
                                    strcpy(category_values[loop], new_val);
                                    strcpy(szCheckIDs[loop], data -> page_id);
                                    // Set the value of the check box

                                    if (data -> selected)
                                        SendMessage(hChecks[loop], BM_SETCHECK, BST_CHECKED, 0);
                                    else
                                        SendMessage(hChecks[loop], BM_SETCHECK, BST_UNCHECKED, 0);
                                    inserted = true;
                                }
                            }
                }
            } while (data != NULL);
        }

        // Prepare new value for putting onto buttons

        for (loop=0; loop<9; loop++)
            strcpy(szButtonValue[loop], category_values[loop]);
        // Last button is always "Back"

        strcpy(szButtonValue[9], "Back");
    }
    
    Put_Text_On_Buttons(hButtons, hChecks);
}

// Put the text onto the actual buttons (or check boxes)

void buttons::Put_Text_On_Buttons(HWND hButtons[10], HWND hChecks[10])
{
    int     loop;                   // Loop variable

    char    szButValue[50];         // Button value

    char    blank[5];               // Temporary string for int to string conversion


    
    // First, deal with the Back button

    strcpy(szButValue, "ESC - ");
    strcat(szButValue, szButtonValue[9]);
    // Set the actual text on the button

    SetWindowText( hButtons[9], szButValue );

    // Now deal with the rest..

    if (!at_leaves)
    {
        // Set values of buttons

        for (loop=0; loop<9; loop++)
        {
            if (strlen(szButtonValue[loop]) > 0)
            {
                // Put value onto button with function key before it

                strcpy(szButValue, "F");
                strcat(szButValue, itoa(loop+1, blank, 10));
                strcat(szButValue, " - ");
                strcat(szButValue, szButtonValue[loop]);
            }
            else
            {
                // If there is no value on the button don't give it a fn key

                strcpy(szButValue, "");
            }
            // Set the actual text on the button

            SetWindowText( hButtons[loop], szButValue );
        }
    }
    else
    {
        // Set text for check boxes

        for (loop=0; loop<9; loop++)
        {
            if (strlen(szButtonValue[loop]) > 0)
            {
                // Put value onto button with function key before it

                strcpy(szButValue, "F");
                strcat(szButValue, itoa(loop+1, blank, 10));
                strcat(szButValue, " - ");
                strcat(szButValue, szButtonValue[loop]);
            }
            else
            {
                // If there is no value on the button don't give it a fn key

                strcpy(szButValue, "");
            }
            // Set the actual text on the button

            SetWindowText( hChecks[loop], szButValue );
        }
    }
}

syntax highlighted by Code2HTML, v. 0.8.11