<?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)

 Code sample: Working with ListViews

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

//------------------------------------------------------------ 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_ITEMLIST",	101);
define("ID_UPDATE",		102);
define("ID_TITLES",		103);
define("ID_GAUGE",		104);
define("ID_LINES",		105);
define("ID_DELETE",		106);
define("ID_CLEAR",		107);
define("ID_RESET",		108);

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

$mainwin = wb_create_window(NULL, AppWindow, "Listview tests", WBC_CENTER, WBC_CENTER, 560, 360,
  WBC_NOTIFY, WBC_HEADERSEL);

// Create ListView and other controls

$list = wb_create_control($mainwin, ListView, "", 5, 30, 540, 200, ID_ITEMLIST,
   WBC_VISIBLE | WBC_ENABLED | WBC_SORT | WBC_LINES | WBC_CHECKBOXES);

wb_set_image($list, PATH_RES . "toolbar.bmp", GREEN, 0, 32);

$statusbar = wb_create_control($mainwin, StatusBar);
wb_create_control($mainwin, PushButton, "Update", 5, 240, 100, 22, ID_UPDATE);
wb_create_control($mainwin, PushButton, "Change widths", 114, 240, 100, 22, ID_TITLES);
$gauge = wb_create_control($mainwin, Gauge, "Update", 230, 245, 310, 15, ID_GAUGE);
wb_create_control($mainwin, CheckBox,   'Display grid lines',	5, 270, 130, 24, ID_LINES, 0, true);

// Create ListView header

wb_set_text($list, array(
   array("File", 	90),
   array(null, 		80),
   array("Name (right-aligned)",	140, WBC_RIGHT),
   array("Update",	200),
));

// Create toolbar

wb_create_control($mainwin, ToolBar, array(
    null,
    array(ID_CLEAR,		NULL, "Clear the list", 			0),
    array(ID_RESET,		NULL, "Reset the list", 			9),
    array(ID_DELETE,	NULL, "Delete the selected item",	12),
), 0, 0, 16, 15, 0, 0, PATH_RES . "toolbar.bmp");

$imax = 3;
wb_set_range($gauge, 0, $imax);
$files = array("File1", "File2", "File3");

reset_listview($list);

wb_set_handler($mainwin, "process_main");   // Must set up handler to sort the list
wb_main_loop();

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

function process_main($window, $id, $ctrl=0, $lparam1=0, $lparam2=0)
{
   global $imax, $files, $list, $gauge;
   global $statusbar;

	switch($id) {

		case ID_ITEMLIST:

			if($lparam1 == WBC_HEADERSEL) {
				wb_set_text($statusbar, "Column $lparam2 selected.");
				break;
			}

			// Show current selection and checked items

			$sel = wb_get_selected($ctrl);
			$sel = $sel ? implode(", ", $sel) : "none";

			$contents = wb_get_text($ctrl);
			$text = "";
			if($contents)
				foreach($contents as $row)
					$text .= $row ? "[" . implode(", ", $row) . "]  " : "";

			$checked = wb_get_value($list);
			$checked = $checked ? implode(", ", $checked) : "none";

			wb_set_text($statusbar,
			  "Selected lines: " . $sel .
			  " / checked: " . $checked .
			  " / contents: " . $text
			);
			break;

		case ID_UPDATE:

			// Update contents row by row

			for($i = 0; $i < $imax; $i++) {
				wb_set_value($gauge, $i + 1);
				$myfiles2 = array($files[$i] . ".bmp", "-->", null, "Updated");
				wb_set_text($list, $myfiles2, $i, 0);
				wb_set_item_image($list, $i + 11, $i, 0);
				wb_wait(null, 300);
			}
			for($i = 0; $i < $imax; $i++) {
				wb_set_value($gauge, $i + 1);
				for($j = 0; $j < 4; $j++) {
					wb_set_item_image($list, null, $i, $j);
				}
				wb_wait(null, 300);
			}
			for($i = 0; $i < $imax; $i++) {
				wb_set_value($gauge, $i + 1);
				wb_set_text($list, array("","","",""), $i);
				wb_wait(null, 300);
			}
			wb_set_value($gauge, 0);
			break;

		case ID_CLEAR:
            if(wb_message_box($window, "Are your sure you want to clear the ListView?",
              null, WBC_YESNO))
				wb_delete_items($list, null);
			break;

		case ID_RESET:
            if(wb_message_box($window, "Are your sure you want to reset the ListView?",
              null, WBC_YESNO))
              	reset_listview($list);
			break;

		case ID_DELETE:
			$sel = wb_get_selected($list);
			if(is_array($sel)) {
			rsort($sel);
			wb_delete_items($list, $sel);
			}
			break;

		case ID_TITLES:
			wb_set_size($list, array(200, null, 60, 60));
			break;

		case ID_LINES:
			wb_set_style($list, WBC_LINES, wb_get_value($ctrl));
			break;

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

function reset_listview($list)
{
	// Empty listview

	wb_delete_items($list, null);

	// Create listview items

	wb_create_items($list, array(
		array("1,000",	"Bob",		"John",		"Peter"),
		array(0,		"Sue",		"Paul",		"Mary"),
		array("200",	null,		300/2,		pi()),
	));

	// Set listview images

	wb_set_item_image($list, 5, 0, 1);
	wb_set_item_image($list, 2, 2, 3);
}

//------------------------------------------------------------------ END OF FILE

?>