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

 Test application

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

//------------------------------------------------------------ SYSTEM P

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("APPNAME", 			"Test application");

define("ID_APP_TIMER1",		6501);

define("ID_MENUTEST1",		3011);
define("ID_MENUTEST2",		3012);

define("ID_NEW", 			8601);
define("ID_ABOUT", 			8602);
define("ID_OPEN", 			8603);
define("ID_SAVE", 			8604);
define("ID_SAVEAS", 		8608);
define("ID_PRINT", 			8605);
define("ID_PREVIEW",		8606);
define("ID_CLOSE", 			8610);
define("ID_HELP", 			8620);
define("ID_STATUSBAR",		8621);
define("ID_WEBSITE",		8625);
define("ID_FIND",			8626);
define("ID_CUT",			8631);
define("ID_COPY",			8632);
define("ID_PASTE",			8633);
define("ID_UNDO",			8634);
define("ID_REDO",			8635);

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

create_main_window();
wb_main_loop();

//------------------------------------------------------------------------------
// MAIN WINDOW
//------------------------------------------------------------------------------

function create_main_window()
{
	global $mainwin, $statusbar, $mainmenu, $maintab;

	// Create main window (invisible) and controls

	// Remove the line below while not under development

	file_put_contents("manytests_main.rc.php", "<?php\n\n" . parse_rc(file_get_contents(PATH_DATA . "manytests_main.rc"), '$mainwin',
	  null, 'AppWindow', null, WBC_CENTER, WBC_CENTER, WBC_CENTER, WBC_CENTER,
	  WBC_INVISIBLE | WBC_TASKBAR | WBC_NOTIFY, WBC_GETFOCUS | WBC_KEYDOWN) . "\n?>");

	include("manytests_main.rc.php");

	// Import test modules (one for each tab)

	include("manytests_values.inc.php");
	include("manytests_listview.inc.php");
	include("manytests_static.inc.php");
	include("manytests_lowlevel.inc.php");

	$maintab = wb_get_control($mainwin, IDC_TABCONTROL1003);

	$str = "";
	$str .= parse_rc(file_get_contents(PATH_DATA . "manytests_listview.rc"),	'$maintab', '$maintab', 'TabControl') . "\n\n";
	$str .= parse_rc(file_get_contents(PATH_DATA . "manytests_values.rc"),		'$maintab', '$maintab', 'TabControl') . "\n\n";
	$str .= parse_rc(file_get_contents(PATH_DATA . "manytests_static.rc"),		'$maintab', '$maintab', 'TabControl');
	$str .= parse_rc(file_get_contents(PATH_DATA . "manytests_lowlevel.rc"),	'$maintab', '$maintab', 'TabControl');

	// Remove the line below while not under development

	file_put_contents("manytests_tabs.rc.php", "<?php\n\n$str\n?>");
	include("manytests_tabs.rc.php");

	// Initialize

	test_lists($maintab);
	test_values($maintab);

	// Create main menu

	$mainmenu = wb_create_control($mainwin, Menu, array(

		"&File",
			array(ID_NEW,		"&New\tCtrl+N",		"", PATH_RES . "menu_new.bmp",	"Ctrl+N"),
			array(ID_OPEN,		"&Open...\tCtrl+O", "", PATH_RES . "menu_open.bmp",	"Ctrl+O"),
			null,
			array(ID_SAVE,		"&Save\tCtrl+S",	"", PATH_RES . "menu_save.bmp",	"Ctrl+S"),
			array(ID_SAVEAS,	"Save &As..."),
			null,
			array(IDCLOSE,		"E&xit\tAlt+F4",	"", PATH_RES . "menu_exit.bmp"),

		"&Tests",
			array(ID_MENUTEST1,	"&Check this",		"",	""),
			array(ID_MENUTEST2,	"&Uncheck that",	"", ""),

		"&Help",
			array(ID_HELP,		"&Help topics\tF1",	"", PATH_RES . "menu_help.bmp",	"F1"),
			null,
			array(ID_WEBSITE,	"&Web site"),
			null,
			array(ID_ABOUT,		"&About...")
	));

	// Replace the accelerator table with another one

//	wb_create_control($mainwin, Accel, array(
//		array(ID_NEW,	"Ctrl+M"),
//		array(ID_SAVE,	"Ctrl+1"),
//		array(ID_OPEN,	"Ctrl+2")
//	));

	wb_set_image(wb_get_control($mainmenu, ID_ABOUT), PATH_RES . "menu_help.bmp");

	wb_set_enabled(wb_get_control($mainmenu, ID_SAVEAS), FALSE);
	wb_set_value(wb_get_control($mainmenu, ID_MENUTEST1), TRUE);

	// Create toolbar

	$toolbar = wb_create_control($mainwin, ToolBar, array(
	  	null,
	  	array(ID_NEW,		NULL, "New (Ctrl+N)",		0),
	  	array(ID_OPEN,		NULL, "Open (Ctrl+O)",		1),
	  	array(ID_SAVE,		NULL, "Save (Ctrl+S)",		2),
	  	null,
	  	array(ID_PRINT,		NULL, "Print (Ctrl+P)",		3),
	  	array(ID_PREVIEW,	NULL, "Print Preview",		4),
	  	null,
	  	array(ID_FIND,		NULL, "Find (F3)",			5),
	  	null,
	  	array(ID_CUT,		NULL, "Cut (Ctrl+X)",		6),
	  	array(ID_COPY,		NULL, "Copy (Ctrl+C)",		7),
	  	array(ID_PASTE,		NULL, "Paste (Ctrl+V)",		8),
	  	null,
	  	array(ID_UNDO,		NULL, "Undo (Ctrl+Z)", 		9),
	  	array(ID_REDO,		NULL, "Redo (Ctrl+Y)", 		10),
	  	null,
	  	array(ID_WEBSITE,	NULL, "Web site", 			11),
	  	array(ID_HELP,		NULL, "Help ", 				13)
	), 0, 0, 16, 15, 0, 0, PATH_RES . "toolbar.bmp");

	wb_set_enabled(wb_get_control($toolbar, ID_SAVE), FALSE);

	// Play with window handler

/*	function dummy($window, $id) { }		// New callback function
	echo wb_get_attrib($mainwin, "handler") . "\n";
	wb_set_handler($mainwin, "dummy");
	echo wb_get_attrib($mainwin, "handler") . "\n";
	wb_set_handler($mainwin, "process_main");
	echo wb_get_attrib($mainwin, "handler") . "\n";*/

	// Icon buttons

	wb_set_image(wb_get_control($mainwin, IDC_BUTTON1036), PATH_RES . 'hyper.ico');
//	$c = wb_get_control($mainwin, IDC_BUTTON1037);
//	wb_set_image($c, PATH_RES . 'up_arrow.bmp');

	$explorer = "explorer.exe";
	wb_set_image(wb_get_control($mainwin, IDC_BUTTON1039), $explorer, null, 3);
	wb_set_image(wb_get_control($mainwin, IDC_BUTTON1036), $explorer, null, 2);
	wb_set_image(wb_get_control($mainwin, IDC_BUTTON1037), $explorer, null, 6);

	$ctrl = wb_get_control($mainwin, IDC_BUTTON1035);
	$bmp = wb_load_image(PATH_RES . "up_arrow.bmp");
	wb_set_image($ctrl, $bmp, 0, GREEN);

	$ctrl = wb_get_control($mainwin, IDC_BUTTON1039);
//	$bmp = wb_load_image(PATH_RES . "dn_arrow.bmp");
	$bmp = wb_create_image(20, 20);
	wb_draw_rect($bmp, 0, 0, 10, 10, RED);
	wb_set_image($ctrl, $bmp, 0, GREEN);

/*
	$tab = wb_get_control($mainwin, IDC_TABCONTROL1003);
	wb_create_control($tab, GroupBox, "&Blah!",		10, 20,100, 70, 169, 0, 0);
	wb_create_control($tab, Label, "Left",		20, 40, 80, 14, 170, WBC_LEFT, 0);
	wb_create_control($tab, Label, "Center",		20, 55, 80, 14, 171, WBC_CENTER, 1);
	wb_create_control($tab, Label, "Right",		20, 80, 80, 14, 172, WBC_RIGHT, 2);
	wb_create_control($tab, PushButton, "&Test",  120, 50, 50, 18, 173, 0, 2);
	wb_create_control($tab, PushButton, "&Close",  120, 30, 50, 18, 174, 0, 2);
	wb_create_control($tab, CheckBox, "&Test",     20, 30,120, 18, 174, 0, 3);
*/
	// Enable / disable controls

	wb_set_enabled(wb_get_control($mainwin, IDC_BUTTON1035), 0);

/*

//	print_r(wb_get_size(wb_load_image(PATH_RES . "toolbar.bmp")));

	// Some tests for non-existent controls - Menus

	$s = wb_get_control($mainmenu, ID_OPEN);
	wb_get_enabled($s);
	echo (int)wb_get_enabled(wb_get_control($mainmenu, ID_OPEN));
	print_r(wb_get_control($mainmenu, 12));
	wb_set_enabled(wb_get_control($mainmenu, ID_SAVE), FALSE);
	print_r(wb_get_control($mainmenu, ID_SAVE));
	wb_set_enabled(array($mainmenu, ID_SAVE), FALSE);
	echo (int)wb_get_enabled($mainmenu, ID_OPEN);
	echo (int)wb_get_enabled(array($mainmenu, ID_OPEN));

	// Some tests for non-existent controls - Windows

	echo wb_get_control($mainwin, ID_OPEN);
	echo (int)wb_get_enabled(wb_get_control($mainwin, ID_OPEN));
*/

	// Almost done...

	$statusbar = wb_create_control($mainwin, StatusBar, "Hello, I'm the status bar.");
	wb_set_image($mainwin, PATH_RES . "hyper.ico");
	wb_set_handler($mainwin, "process_main");
	wb_set_selected($maintab, 1);

	// Sounds

//	wb_play_sound(WBC_OK);

	wb_create_timer($mainwin, ID_APP_TIMER1, 1000);
//	wb_create_timer($mainwin, ID_APP_TIMER2, 50);
	wb_set_text(wb_get_control($mainwin, ID_TIME), date("h:i:s A"));
	wb_set_value(wb_get_control($mainwin, IDC_PROGRESSBAR1051), null, 0, 60);
	wb_set_value(wb_get_control($mainwin, IDC_PROGRESSBAR1051), date("s"));

	// Fonts

	$font = wb_create_font("Arial", 12, null, FTA_BOLD);
	wb_set_font(wb_get_control($mainwin, ID_TIME), $font);
	wb_set_size(wb_get_control($mainwin, ID_TIME), 200, 22);

	// Show window

	wb_set_visible($mainwin, true);
}

function process_main($window, $id, $ctrl=0, $lparam1=0, $lparam2=0)
{
	global $statusbar, $mainwin, $mainmenu, $maintab;
	static $timer = true;

	static $file_filter = array(
		array("PHP source code",	"*.php?"),
		array("Web page",			"*.htm?"),
		array("Text document",		"*.txt"),
		array("All files",			"*.*")
	);

	// Don't process "get focus" messages, except if in Tests tab

	if(($ctrl != $mainwin) && ($lparam1 == WBC_GETFOCUS)) {
		process_test_values($window, $id, $ctrl, $lparam1, $lparam2);
		return false;
	}

	switch($id) {

		case ID_RESULT:
			if($lparam1 == WBC_KEYDOWN)
				wb_set_text($statusbar, "Char $lparam2: " . chr($lparam2));
			break;

		case IDC_BUTTON1037:

			wb_set_image($window, PATH_RES . "ttf.ico");

			// Minimize the window

			$wbobj = unpack(WBOBJ, wb_peek($window, WBOBJ_SIZE));
			$ShowWindow = wb_get_function_address("ShowWindow", wb_load_library("USER"));
			wb_call_function($ShowWindow, array($wbobj["hwnd"], 6));

			break;

		case IDC_TABCONTROL1003:
			wb_set_text(wb_get_control($window, ID_RESULT), "Tab $lparam1 (" .
			  wb_get_selected(wb_get_control($window, $id)) . ") selected.");
			break;

		case IDC_BUTTON1039:
			static $seltab = -1;
			if($seltab < 0) {
				$seltab = wb_get_selected(wb_get_control($window, IDC_TABCONTROL1003));
			}
			$seltab = ($seltab < 4) ? $seltab + 1 : 0;
			wb_set_selected($maintab, $seltab);
			break;

		case IDC_BUTTON1036:

			if($timer) {
				wb_destroy_timer($mainwin, ID_APP_TIMER1);
//				wb_destroy_timer($mainwin, ID_APP_TIMER2);
				$timer = false;
			} else {
				wb_create_timer($mainwin, ID_APP_TIMER1, 1000);
//				wb_create_timer($mainwin, ID_APP_TIMER2, 50);
				$timer = true;
			}
			break;

		case ID_APP_TIMER1:

			wb_set_text(wb_get_control($window, ID_TIME), date("h:i:s A"));
			$txt = wb_get_text(wb_get_focus());
			if(is_array($txt))
				$txt = "Array";
			wb_set_text(wb_get_control($window, ID_GDI),
				"GDI: " . wb_get_system_info("gdiobjects") .
				"  USER: " . wb_get_system_info("userobjects") .
				" >" . substr($txt, 0, 10));
			wb_set_value(wb_get_control($window, IDC_PROGRESSBAR1051), date("s"));
			break;

//		case ID_APP_TIMER2:
			//static $wow;
			//$wow = ($wow < 100) ? $wow + 2 : -$wow;
			//wb_set_value(wb_get_control($window, IDC_PROGRESSBAR1051), abs($wow));
//			wb_set_value(wb_get_control($window, IDC_PROGRESSBAR1051), (date("s") * 100) / 59);
//			break;

		case ID_NEW:
			wb_message_box($window, "New file.");
			break;

		case ID_HELP:
			wb_exec("../../winbinder.chm");
			break;

		case ID_WEBSITE:
			if(!wb_exec("http://winbinder.org/"))
				wb_message_box($window, "Problem opening web site.");
			break;

		case ID_MENUTEST1:
			wb_set_value(wb_get_control($mainmenu, ID_MENUTEST2),
				!wb_get_value(wb_get_control($mainmenu, ID_MENUTEST2)));
			break;

		case ID_MENUTEST2:
			wb_set_value(wb_get_control($mainmenu, ID_MENUTEST1),
				!wb_get_value(wb_get_control($mainmenu, ID_MENUTEST1)));
			break;

		case ID_FIND:
			wb_play_sound(WBC_BEEP);
			break;

		case ID_OPEN:
			$filename = wb_sys_dlg_open($window, "Get file name", $file_filter);
			if($filename)
				wb_set_text(wb_get_control($window, ID_RESULT), $filename);
			break;

		case ID_SAVE:
		case ID_SAVEAS:
			$filename = wb_get_text(wb_get_control($window, ID_RESULT));
			wb_sys_dlg_save($window, "Save file as...",
			  	"Arquivos Php (*.php)\0*.php\0All files (*.*)\0*.*" . "\0\0");
			break;

		case ID_PRINT:
			wb_set_text(wb_get_control($window, ID_RESULT),
			  wb_sys_dlg_path($window, "Choose a folder", "c:\\"));
			break;

		case ID_PREVIEW:

			echo "wb_get_pixel, wb_draw_point\n\n";

			$handle = wb_load_image(PATH_RES . "treeview.bmp");
			if(!$handle)
				die;
			for($y = 0; $y < 4; $y++)
				for($x = 0; $x < 4; $x++) {
					printf("(%d %d): %06X  ", $x, $y, wb_get_pixel($handle, $x, $y));
					wb_draw_point($handle, $x, $y, $x << 4 | $y << 12);
					printf("->  %06X\n", wb_get_pixel($handle, $x, $y));
				}

			$clr = wb_sys_dlg_color($window, "Choose a color", 0x008000);
			wb_set_text(wb_get_control($window, ID_RESULT), $clr === NULL ? "NULL" : sprintf("#%06X", $clr));
			break;

		case ID_ABOUT:
			create_about_box($window);
			break;

		case ID_EXIT:
		case IDCLOSE:
			if(wb_message_box($window, "Are your sure?", APPNAME, WBC_YESNO))
				wb_destroy_window($window);
			break;

		default:
//			echo $id . "\n";
			if(process_test_values($window, $id, $ctrl, $lparam1, $lparam2))
				break;
			if(process_test_lists($window, $id, $ctrl, $lparam1, $lparam2))
				break;
			if(process_test_static($window, $id, $ctrl, $lparam1, $lparam2))
				break;
			if(process_test_lowlevel($window, $id, $ctrl, $lparam1, $lparam2))
				break;
			break;
	}
}

//------------------------------------------------------------------------------
// 'ABOUT' DIALOG
//------------------------------------------------------------------------------

function create_about_box($parent)
{
	$about = wb_create_window($parent, ModalDialog, "About this program", 240, 180);
	wb_set_handler($about, "process_about");

	$text = APPNAME . "\r\nWinBinder version: " . WBC_VERSION . "\r\nPHP version: " . PHP_VERSION .
		"\r\n" . wb_get_system_info("osversion");

	wb_create_control($about, EditBox, $text, 20, 20, 200, 80, 0, WBC_MULTILINE | WBC_READONLY);
	wb_create_control($about, PushButton, "Close",  80, 120, 80, 22, IDCANCEL);
}

function process_about($window, $id)
{
	// No need to process anything else in an About dialog like this

	wb_destroy_window($window);
}

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

?>