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

 HTML 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("APPNAME", 	"WinBinder mini-browser");

define("HTML_CODE", "<html><body><center><p>" .
  "<br><br><br><br><br><br><br>\n" .
  "<font face='Verdana' size='2' color='maroon'>Welcome to the " . APPNAME . "! </font></p><br>" .
  "<img src='" . realpath("../res/cassini.jpg") . "' border='0'></center></body></html>");

define("PAGE_HOME",		"http://sourceforge.net/projects/winbinder/");
define("PAGE_SEARCH",	"http://google.com/");
define("PAGE_WEBSITE",	"http://winbinder.org/");
define("PAGE_FORUM",	"http://winbinder.org/forum/");

// Control identifiers

define("ID_BACK",		1001);
define("ID_FORWARD",	1002);
define("ID_HOME",		1003);
define("ID_SEARCH",		1004);
define("ID_REFRESH",	1005);
define("ID_STOP",		1006);

define("ID_GO",			201);
define("ID_OPEN", 		202);
define("ID_BLANK", 		203);
define("ID_ABOUT",		204);
define("ID_WEBSITE",	205);
define("ID_FORUM",		206);
define("ID_URL",		207);

define("ID_TIMER",		101);

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

create_main_window();
wb_main_loop();

function create_main_window()
{
	global $hwnd_html, $ctrl_url, $statusbar;

	$mainwin = wb_create_window(NULL, ResizableWindow,
	  APPNAME, WBC_CENTER, WBC_CENTER, 780, 560,
	  WBC_INVISIBLE | WBC_NOTIFY, WBC_RESIZE);

	// Create main menu

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

		"&File",
			null,
			array(ID_OPEN,		"&Open document...\tCtrl+O", "", PATH_RES . "menu_open.bmp",	"Ctrl+O"),
			null,
			array(IDCLOSE,		"E&xit\tAlt+F4",	"", PATH_RES . "menu_exit.bmp"),

		"&View",
			array(ID_GO,		"&Go to URL\tEnter","", "", "Enter"),
			null,
			array(ID_HOME,		"&Home page",		""),
			array(ID_BACK,		"Go &back",			""),
			array(ID_FORWARD,	"Go &forward", 		""),
			null,
			array(ID_BLANK,		"Blan&k page",		""),
			array(ID_STOP,		"&Stop", 			"",	"",	"Esc"),
			array(ID_REFRESH,	"&Refresh", 		""),

		"&Tools",
			array(ID_SEARCH,	"&Search",			""),

		"&Help",
			array(ID_WEBSITE,	"WinBinder &web site"),
			array(ID_FORUM,		"WinBinder &forum"),
			null,
			array(ID_ABOUT,		"&About...", "", PATH_RES . "menu_help.bmp", "")
	));

	// Create toolbar

	$toolbar = wb_create_control($mainwin, ToolBar, array(
		null,
		array(ID_BACK,		NULL, "Go Back",	0),
		array(ID_FORWARD,	NULL, "Go Forward",	1),
		array(ID_STOP,		NULL, "Stop",		5),
		array(ID_REFRESH,	NULL, "Refresh",	4),
		array(ID_HOME,		NULL, "Home",		2),
		null,
		array(ID_SEARCH,	NULL, "Search",		3),
		null,
		array(ID_GO,		NULL, "Go",			6),
	), 0, 0, 16, 16, 0, 0, PATH_RES . "browser.bmp");

	// Create HTML control and others

	$hwnd_html = wb_create_control($mainwin, HTMLControl, "", 0, 30, 765, 460);
	$ctrl_url = wb_create_control($toolbar, EditBox, PAGE_HOME, 190, 3, 580, 20, ID_URL);
	$statusbar = wb_create_control($mainwin, StatusBar, "");

	// Almost finished

	wb_set_handler($mainwin, "process_main");
	wb_set_image($mainwin, PATH_RES . 'hyper.ico');
	wb_set_visible($mainwin, TRUE);
	wb_create_timer($mainwin, ID_TIMER, 250);

	process_main($mainwin, ID_WEBSITE);
}

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

/* Process main window commands */

function process_main($window, $id, $ctrl=0, $lparam1=0, $lparam2=0)
{
	global $hwnd_html, $ctrl_url, $statusbar;
	static $file_filter = array(
		array("HTML files",			"*.htm?;*.mht"),
		array("Text files",			"*.txt"),
		array("Image files",		"*.gif;*.jpg;*.jpeg;*.png"),
		array("XML files",			"*.xml"),
		array("All files",			"*.*")
	);

    switch($id) {

		case IDDEFAULT:
			if($lparam1 == WBC_RESIZE) {
				// Resize HTML control to fill main window
				$dim = wb_get_size($window, false);
				wb_set_size($hwnd_html, $dim[0] - 15, $dim[1] - 100);
			}
			break;

		case ID_TIMER:
			if(wb_set_location($hwnd_html, "cmd:busy"))
				wb_set_text($statusbar, "Loading page, please wait...");
			else
				wb_set_text($statusbar, "Ready (" . (wb_get_system_info("appmemory") / 1024) . " kB)");
//				wb_set_text($statusbar, "Ready");
			break;

		case ID_BACK:
			wb_set_location($hwnd_html, "cmd:back");
			break;
		case ID_FORWARD:
			wb_set_location($hwnd_html, "cmd:forward");
			break;
		case ID_REFRESH:
			wb_set_location($hwnd_html, "cmd:refresh");
			break;
		case ID_STOP:
			wb_set_location($hwnd_html, "cmd:stop");
			break;

		case ID_OPEN:
			$filename = wb_sys_dlg_open($window, "Open document", $file_filter);
			if($filename)
				wb_set_text($hwnd_html, $filename);
			break;

		case ID_BLANK:
			wb_set_location($hwnd_html, "cmd:blank");
			wb_set_text($ctrl_url, "");
			break;

		case ID_SEARCH:			// Not using the browser function here
			wb_set_location($hwnd_html, PAGE_SEARCH);
			wb_set_text($ctrl_url, PAGE_SEARCH);
			break;

		case ID_HOME:			// Not using the browser function here
			wb_set_location($hwnd_html, PAGE_HOME);
			wb_set_text($ctrl_url, PAGE_HOME);
			break;

		case ID_WEBSITE:
			wb_set_location($hwnd_html, PAGE_WEBSITE);
			wb_set_text($ctrl_url, PAGE_WEBSITE);
			break;

		case ID_FORUM:
			wb_set_location($hwnd_html, PAGE_FORUM);
			wb_set_text($ctrl_url, PAGE_FORUM);
			break;

		case ID_GO:
			wb_set_location($hwnd_html, wb_get_text($ctrl_url));
			break;

		case ID_ABOUT:
			create_about_box($window);
			break;

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

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\n\r\nWinBinder version: " . WBC_VERSION . "\r\nPHP version: " . PHP_VERSION .
		"\r\n" . wb_get_system_info("osversion");

	wb_create_control($about, Label, $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 OF FILE

?>