<?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: Tricks with resizable windows / split window

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

//------------------------------------------------------------ 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_LIST",			100);
define("ID_LEVEL1",			102);
define("ID_AUTO",			103);

define("APPNAME",			"Resizable window and controls / split window");

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

// Create main window

init_dlls();

$mainwin = wb_create_window(NULL, ResizableWindow, APPNAME,
	200, 100, 410, 400, WBC_NOTIFY, WBC_RESIZE | WBC_MOUSEMOVE | WBC_MOUSEDOWN | WBC_MOUSEUP);
wb_set_area($mainwin, WBC_MINSIZE, 0, 0, 410, 400);
wb_set_area($mainwin, WBC_MAXSIZE, 0, 0, 410, 800);

// Create controls

$list = wb_create_control($mainwin,  ListView, '', 	   150,  20, 248, 214, ID_LIST, WBC_LINES);
$list2 = wb_create_control($mainwin, ListView, '', 	   150, 238, 248, 100, ID_LIST, WBC_LINES);
$list3 = wb_create_control($mainwin, ListView, '', 	    10,  50, 130, 265, ID_LIST, WBC_LINES);
wb_create_control($mainwin, PushButton, 'Do nothing',	10,  20, 130,  24, ID_LEVEL1);
wb_create_control($mainwin, CheckBox,   'Do nothing',	10, 320, 130,  24, ID_AUTO, 0, true);
$statusbar = wb_create_control($mainwin, StatusBar);
wb_set_text($statusbar, APPNAME);

// Start

DisableMaximizeButton($mainwin);
wb_set_handler($mainwin, "process_main");
wb_main_loop();

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

function process_main($window, $id, $ctrl=0, $lparam=0, $lparam2=0, $lparam3=0)
{
	global $list, $list2, $list3, $statusbar;
	static $lasty = 236;
	static $capturing = false;

	switch($id) {

		case IDDEFAULT:

			if(!$lparam & WBC_LBUTTON) {

				// Simulate a split window

				$x = $lparam2 & 0xFFFF;			// Mouse cursor position
				$y = ($lparam2 & 0xFFFF0000) >> 16;

				$pos = wb_get_position($list, true);
				$size = wb_get_size($list);

				if(($x >= $pos[0] - 4) && ($x <= $pos[0] + $size[0] - 4) &&
				   ($y >= $pos[1] + $size[1] - 30) && ($y <= $pos[1] + $size[1] + 10))
					wb_set_cursor(null, 'SIZENS');
				else
					wb_set_cursor(null, 'ARROW');

			} elseif($lparam & WBC_LBUTTON) {

				// Simulate a split window

				$x = $lparam2 & 0xFFFF;			// Mouse cursor position
				$y = ($lparam2 & 0xFFFF0000) >> 16;

				$pos = wb_get_position($list, true);
				$size = wb_get_size($list);

				if(!$capturing)
				  if(!(($x >= $pos[0] - 4) && ($x <= $pos[0] + $size[0] - 4) &&
				   ($y >= $pos[1] + $size[1] - 30) && ($y <= $pos[1] + $size[1] + 10)))
						break;

				if($lparam & WBC_MOUSEDOWN) {
					SetCapture($window);
					$capturing = true;
				} else if($lparam & WBC_MOUSEUP) {
					ReleaseCapture();
					$capturing = false;
				}

				if($capturing) {

					wb_set_cursor(null, 'SIZENS');

					$dim = wb_get_size($window, true);
					$winwidth = $dim[0];
					$winheight = $dim[1];

					// Set size of first control

					wb_set_position($list, $winwidth / 2 - 51, 20);
					wb_set_size($list, $winwidth - ($winwidth / 2 - 51) - 4,
					  min(max(40, $y - 22), ($winheight - 100)));

					// Set size/position of second control

					wb_set_position($list2, $winwidth / 2 - 51, $size[1] + 24);
					wb_set_size($list2, $winwidth - ($winwidth / 2 - 51) - 4,
						 ($winheight - 35) - ($size[1] + 24));
					$lasty = $y;
				}

			} else {

				// Just changes the mouse cursor

				$x = $lparam2 & 0xFFFF;			// Mouse cursor position
				$y = ($lparam2 & 0xFFFF0000) >> 16;

				$pos = wb_get_position($list, true);
				$size = wb_get_size($list);

				if(($x >= $pos[0] - 4) && ($x <= $pos[0] + $size[0] - 4) &&
				   ($y >= $pos[1] + $size[1] - 30) && ($y <= $pos[1] + $size[1] + 10))
					wb_set_cursor(null, 'SIZENS');

				if($lparam == WBC_RESIZE) {

					// Resize control

					$dim = wb_get_size($window, true);
					$winwidth = $dim[0];
					$winheight = $dim[1];

					// Set size/position of first control

					wb_set_position($list, $winwidth / 2 - 51, 20);
					wb_set_size($list, $winwidth - ($winwidth / 2 - 51) - 4,
						min(max(20, $lasty - 22), ($winheight - 80)));

					// Set size/position of second control

					$size = wb_get_size($list);
					wb_set_position($list2, $winwidth / 2 - 51, $size[1] + 24);
					wb_set_size($list2, $winwidth - ($winwidth / 2 - 51) - 4,
						 ($winheight - 35) - ($size[1] + 24));

					// Set size of third control

					$pos = wb_get_position($list, true);
					wb_set_size($list3, $pos[0] - 24, $winheight - 108);
					wb_set_position(wb_get_control($window, ID_AUTO), 10, $winheight - 53);

					// Show window size using $lparam

					$dim = wb_get_size($window);
					$winwidth = $dim[0];
					$winheight = $dim[1];
					wb_set_text($statusbar,
						"Window area: $winwidth x $winheight pixels -- " .
						"Client area: " . ($lparam3 & 0x0000FFFF) . " x " . (($lparam3 & 0xFFFF0000) / 0x10000) . " pixels\n");

				}
			}
			break;

		case IDCLOSE:
			wb_destroy_window($window);
	}
}

function init_dlls()
{
	global $USER, $KERNEL, $GDI;

	// Most common Windows libraries

	$USER = wb_load_library("USER");
	$KERNEL = wb_load_library("KERNEL");
	$GDI = wb_load_library("GDI");

	// Declare constants related to Windows and WinBinder structures

	define("WBOBJ",			"Vhwnd/Vid/Vuclass/litem/lsubitem/Vstyle/Vparent/Vhandler/Vlparam/V8lparams/Vpbuffer");
	define("WBOBJ_RAW",		"V3l2V13");
	define("WBOBJ_SIZE",	72);

	define("SC_MAXIMIZE",	61488);

}

function SetCapture($hwnd)
{
	global $USER;
	static $pfn = null;

	$wbobj = unpack(WBOBJ, wb_peek($hwnd, WBOBJ_SIZE));

	if($pfn === null)
		$pfn = wb_get_function_address("SetCapture", $USER);
	return wb_call_function($pfn, array($wbobj["hwnd"]));
}

function ReleaseCapture()
{
	global $USER;
	static $pfn = null;

	if($pfn === null)
		$pfn = wb_get_function_address("ReleaseCapture", $USER);
	return wb_call_function($pfn);
}

function DisableMaximizeButton($hwnd)
{
	global $USER;
	static $gsm = null, $dmi;

	$wbobj = unpack(WBOBJ, wb_peek($hwnd, WBOBJ_SIZE));

	if($dmi === null)
		$dmi = wb_get_function_address("DeleteMenu", $USER);
	if($gsm === null)
		$gsm = wb_get_function_address("GetSystemMenu", $USER);

	$sysmenu = wb_call_function($gsm, array($wbobj["hwnd"], false));
	return wb_call_function($dmi, array($sysmenu, SC_MAXIMIZE, 0));
}

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

?>