<?

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

 WINBINDER - The native Windows binding for PHP for PHP

 Copyright © Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://winbinder.org/contact.php)

 Printing in WinBinder

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

//------------------------------------------------------------ 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

// Load printer extension

if(!extension_loaded('printer'))
	if(!@dl('php_printer.dll')) {
		trigger_error("Printer extension could not be loaded./n", E_USER_ERROR);
	}

define("APPNAME", 	"Printing in WinBinder");	// Application name

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

create_print_window(null);
wb_main_loop();

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

// End controls

function create_print_window($parent)
{
	global $winmain;

	include("print.form.php");

	// Fill out list of available printers

	$printer_list = printer_list(PRINTER_ENUM_LOCAL);
	$combo = wb_get_control($winmain, IDC_PRINTERLIST);
	wb_delete_items($combo);
	foreach($printer_list as $printer) {
		wb_create_items($combo, $printer['NAME']);
	}

	// Use some usual paper sizes

	$combo = wb_get_control($winmain, IDC_PAPERSIZE);
	static $formats = array('A4', 'US Letter', 'US Legal');
	foreach($formats as $format) {
		wb_create_items($combo, $format);
	}

	// Use some preset quality settings

	$combo = wb_get_control($winmain, IDC_QUALITY);
	static $dpis = array('Fast (100 dpi)', 'Standard (200 dpi)', 'Fine (300 dpi)');
	foreach($dpis as $dpi) {
		wb_create_items($combo, $dpi);
	}

	// Set up other default values

	wb_set_value(wb_get_control($winmain, IDC_PRINTALL), true);
	wb_set_text(wb_get_control($winmain, IDC_UNITS), 'mm');
	wb_set_value(wb_get_control($winmain, IDC_COPIES), 1);
	wb_set_enabled(wb_get_control($winmain, IDC_PRINTPAGES), false);
	wb_set_enabled(wb_get_control($winmain, IDC_STARTPAGE), false);
	wb_set_enabled(wb_get_control($winmain, IDC_ENDPAGE), false);

	// Almost ready

	wb_set_handler($winmain, 'process_print');
	wb_set_text($winmain, APPNAME);
	process_print($winmain, IDC_PAPERSIZE);		// Select first paper size
	wb_set_visible($winmain, true);
}

function process_print($window, $id, $ctrl=null)
{
	global $winmain;

	switch($id) {

		case IDC_PAPERSIZE:
			$size = wb_get_selected(wb_get_control($window, $id));
			switch($size) {
				case 0: $width = 297;	$height = 210; 		break;		// A4
				case 1: $width = 279.4; $height = 215.9;	break;		// Letter
				case 2: $width = 355.6;	$height = 215.9;	break;		// Legal
			}
			wb_set_text(wb_get_control($window, IDC_WIDTH),
				number_format($width, 1, '.', ','));
			wb_set_text(wb_get_control($window, IDC_HEIGHT),
				number_format($height, 1, '.', ','));
			break;

		case IDOK:

			// Open printer port

			$printer = wb_get_text(wb_get_control($winmain, IDC_PRINTERLIST));
			$prn = printer_open($printer);
			if(!$prn) {
				wb_message_box($window, "Could not open printer", APPNAME, WBC_WARNING);
				break;
			}

			// Set DPI

			$val = wb_get_selected(wb_get_control($winmain, IDC_QUALITY));
			printer_set_option($prn, PRINTER_RESOLUTION_X, ($val + 1) * 100);
			printer_set_option($prn, PRINTER_RESOLUTION_Y, ($val + 1) * 100);

			// Set paper size

			$val = wb_get_selected(wb_get_control($winmain, IDC_PAPERSIZE));
			switch($val) {
				case 1: printer_set_option($prn, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_A4); break;
				case 2: printer_set_option($prn, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_LETTER); break;
				case 3: printer_set_option($prn, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_LEGAL); break;
			}

			// Set other options

			printer_set_option($prn, PRINTER_COPIES, wb_get_value(wb_get_control($winmain, IDC_COPIES)));

			if(wb_get_value(wb_get_control($winmain, IDC_PORTRAIT)))
				printer_set_option($prn, PRINTER_ORIENTATION, PRINTER_ORIENTATION_PORTRAIT);
			else
				printer_set_option($prn, PRINTER_ORIENTATION, PRINTER_ORIENTATION_LANDSCAPE);

			// Start print job

			if(!printer_start_doc($prn, APPNAME)) {
				wb_message_box($window, "Could not start print job", APPNAME, WBC_WARNING);
				break;
			}
			printer_start_page($prn);

			// Print an image

			$bmpfile = '_temp.bmp';
			$bmp = wb_create_image(2000, 100);
			draw_on_window($bmp, 2000, 100);
			wb_save_image($bmp, $bmpfile);
			wb_destroy_image($bmp);
			printer_draw_bmp($prn, $bmpfile, 20, 20);
			printer_draw_bmp($prn, $bmpfile, 20, 500);

			// Print some text

			$font = printer_create_font("Tahoma", 148, 64, PRINTER_FW_LIGHT, false, false, false, 0);
			printer_select_font($prn, $font);
			printer_draw_text($prn, APPNAME, 20, 200);
			printer_delete_font($font);

			// End print job

			printer_end_page($prn);
			printer_end_doc($prn);
			printer_close($prn);
			unlink($bmpfile);
			break;

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

/* Draw main window image */

function draw_on_window($buffer, $width, $height)
{
	// Create a bitmap and draw on it

	wb_draw_rect($buffer, 0, 0, $width, $height, 0xD0A0A0);

	// Create a repeating pattern

	$pattern = wb_create_image(16, 16);
	wb_draw_rect($pattern, 0, 0, 16, 16, 0x704010);
	wb_draw_rect($pattern, 3, 3, 10, 10, 0xB08050);
	wb_draw_rect($pattern, 6, 6, 4, 4, 0x704010);
	for($i = 0; $i < $width; $i += 16) {
		wb_draw_image($buffer, $pattern, $i, 0);
	}
	wb_destroy_image($pattern);
}

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

?>
