<?php

/*******************************************************************************

 WINBINDER - The native Windows binding for PHP for PHP

 Copyright © Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://winbinder.org/contact.php)

 Tests with combo boxes and list boxes

*******************************************************************************/

//------------------------------------------------------------ SYSTEM PARAMETERS

define("PATH_SCRIPT",	dirname(__FILE__) . "/");
define("PATH_DATA",		PATH_SCRIPT);
define("PATH_INC",		PATH_SCRIPT . "../include/");
define("PATH_RES",		PATH_SCRIPT . "../resources/");

//----------------------------------------------------------------- DEPENDENCIES

include PATH_INC . "winbinder.php";

//-------------------------------------------------------------------- CONSTANTS

define ("ID_COMBO1", 	1001);
define ("ID_COMBO2", 	1002);
define ("ID_LIST",   	1003);
define ("ID_ITEM",   	1004);
define ("ID_ITEMSP",   	1005);

//-------------------------------------------------------------- EXECUTABLE CODE

// Create main window

$mainwin = wb_create_window(NULL, PopupWindow, "Combo boxes and list boxes", WBC_CENTER, WBC_CENTER, 280, 280);

wb_create_control($mainwin, Label,		"Read-only combo:",	 10, 24,100, 20);
wb_create_control($mainwin, ComboBox,   "",             	120, 20,140,160,	ID_COMBO1, WBC_READONLY);
wb_create_control($mainwin, Label,		"Editable combo:",	 10, 49, 80, 20);
wb_create_control($mainwin, ComboBox,   "",             	120, 45,140,160,	ID_COMBO2);
wb_create_control($mainwin, ListBox,    "",             	120, 70,140,150,	ID_LIST);
wb_create_control($mainwin, PushButton, "Re-populate",  	 10,193, 90, 25,	IDOK);

wb_create_control($mainwin, Label, 		"Line:",			 14,117, 30, 20);
wb_create_control($mainwin, EditBox, 	"0",			  	 45,115, 36, 20,	ID_ITEM, WBC_NUMBER);
wb_create_control($mainwin, Spinner, 	"",				  	 83,115, 50, 20,	ID_ITEMSP, WBC_GROUP);

$statusbar = wb_create_control($mainwin, StatusBar);

process_main($mainwin, IDOK);           // Populate the combo box
process_main($mainwin, ID_ITEM);
wb_set_handler($mainwin, "process_main");
wb_main_loop();

//-------------------------------------------------------------------- FUNCTIONS

function process_main($window, $id, $ctrl=0)
{
	global $statusbar;

    switch($id) {

    	case ID_LIST:
    		$text = wb_get_text($ctrl) . "_set";
    		wb_set_text(wb_get_control($window, ID_COMBO2), $text);
    		// Fall-through
    	case ID_COMBO1:
    	case ID_COMBO2:
    		wb_set_text($statusbar, "Selection: " . wb_get_selected($ctrl) . " / value: " . wb_get_value($ctrl) . " / text: [" . wb_get_text($ctrl) . "]");
    		break;

    	case ID_ITEM:
			$list = wb_get_control($window, ID_LIST);
			$item = wb_get_value(wb_get_control($window, ID_ITEM));
    		wb_set_text($statusbar, "Item: $item / value: " . wb_get_value($list, $item) . " / text: [" . wb_get_text($list, $item) . "]");
    		break;

        case IDOK:

            $nitems = rand(2, 20);
            for($i = 0; $i < $nitems; $i++)
                $items[] = randstr();

			$combo1 = wb_get_control($window, ID_COMBO1);
			$combo2 = wb_get_control($window, ID_COMBO2);
			$list = wb_get_control($window, ID_LIST);
            wb_set_text($combo1, $items);
            wb_set_text($combo2, $items);
            wb_set_text($list, $items);

            for($i = 0; $i < $nitems; $i++) {
            	$val = rand(0, 1000);
				wb_set_value($combo1, $val, $i);
				wb_set_value($combo2, $val, $i);
				wb_set_value($list, $val, $i);
			}

			wb_set_range(wb_get_control($window, ID_ITEMSP), -1, $nitems - 1);
			wb_set_text($statusbar, "Controls populated with $nitems items each.");
			wb_set_value(wb_get_control($window, ID_ITEM), 0);

            break;

        case IDCLOSE:
            wb_destroy_window($window);
            break;
    }
}

function randstr()
{
    $str = "";
    for($i = 0; $i < rand(5, 15); $i++)
        $str .= chr(rand(ord("a"), ord("z")));
    return $str;
}

//-------------------------------------------------------------------------- END

?>