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

 Single instance demo

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

//------------------------------------------------------------ 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",	"Single Instance Application");

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

// If there is already an instance of this application,
// bring it to the front and quit

if(wb_get_instance(APPNAME, TRUE))
	die;

create_main_window();
wb_main_loop();

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

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

	// Position the window randomly on screen

	$dim = explode(" ", wb_get_system_info("screenarea"));
	$mainwin = wb_create_window(NULL, AppWindow, APPNAME,
		rand(0, $dim[2] - 320),
		rand(0, $dim[3] - 140),
		320, 140, WBC_INVISIBLE, 0);
	wb_set_handler($mainwin, "process_main");
	wb_create_control($mainwin, PushButton, "Close", 80, 30, 150, 50, IDCLOSE);

	// The application may change the caption of the main window. This will not
	// affect the results of wb_get_instance().

	wb_set_text($mainwin, "This caption was changed at runtime.");
	wb_set_visible($mainwin, true);
}

function process_main($window, $id)
{
	switch($id) {
		case IDCLOSE:
			wb_destroy_window($window);
			break;
	}
}

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

?>