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

 A calendar control

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

//------------------------------------------------------------ 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("DATE_FMT", 		"%A, %B %d, %Y");

define("ID_CALENDAR",	102);
define("ID_STRTOTIME",	103);
define("ID_UPDATE",		104);

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

// Create main window

$cpwindow = wb_create_window(NULL, PopupWindow, "Calendar test", WBC_CENTER, WBC_CENTER, 400, 300, 0);

// Create calendar control

$calendar = wb_create_control($cpwindow, Calendar, "", 95, 70, 200, 160, ID_CALENDAR, WBC_BORDER);

// Create other controls

$statusbar = wb_create_control($cpwindow, StatusBar);
$edit = wb_create_control($cpwindow, EditBox, "+2 week 2 days", 10, 10, 260, 20, ID_STRTOTIME);
$button = wb_create_control($cpwindow, PushButton, "Update calendar", 295, 8, 90, 24, ID_UPDATE, 0);
wb_create_control($cpwindow, Label, "Type a valid strtotime() string or leave the edit control blank.", 10, 40, 380, 20);

// Set handler and start application

wb_set_handler($cpwindow, "process_main");
wb_main_loop();

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

function process_main($window, $id, $ctrl=0, $lparam1=0, $lparam2=0)
{
	global $edit, $calendar, $statusbar;

	switch($id) {

		case ID_UPDATE:

			@$date = strtotime(wb_get_text($edit));

			if($date != -1) {
				wb_set_value($calendar, $date);
				$date = strftime(DATE_FMT, $date);
				wb_set_text($statusbar, "Ready");
			} else {
				wb_set_text($edit, "");
				wb_set_text($statusbar, "strtotime() valid date format or blank required");
			}
			break;

		case ID_CALENDAR:
			$date = strftime(DATE_FMT, wb_get_value($ctrl));
			//$date = strftime(DATE_FMT, $lparam1);				// Does the same thing
			wb_set_text($statusbar, "Ready");
			wb_set_text($edit, $date);
			break;

		case IDCLOSE:
			wb_destroy_window($window);
			break;

	}
}

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

?>