PHPBuilder - HTML Toolbar Class



RSS Twitter
Snippets Html

HTML Toolbar Class

by: Brian Levine
|
May 16, 2003

Version: 1.0

Type: Class

Category: HTML

License: GNU General Public License

Description: Here's a neat class to do a toolbar that uses imagebuttons for an HTML form. Button clicks emulate a form submit, and can also call a user supplied Javascript function. eMail me at bjlevine@hotmail.com for a bunch of 3d button images to use.



<?php

class FormButton
{
	var $Name;	// name of button
	var $Cmd;	// command set by button click to $cmd 
	var $ImageUp;	// bitmap for up button
	var $ImageDown;	// button for down button
	var $ImageSel;	// image for selected button (mouseover)
	var $Form;	// form button applies to
	var $Text;	// tooltip text
	var $Def;	// html definition of button
	var $Width;	// width of button
	var $Height;	// height of button
	var $Visible;	// visible boolean
	var $Func;	// javascript function to run when button clicked

	// constructor
	function FormButton($nm, $cm, $frm, $txt, $up, $down, $sel, $func="", $w=32, $h=24)
		{
		$this->Name = $nm;
		$this->Cmd = $cm;
		$this->ImageUp = $up;
		$this->ImageDown = $down;
		$this->ImageSel = $sel;
		$this->Form = $frm;
		$this->Text = $txt;
		$this->Width = $w;
		$this->Height = $h;
		$this->Func = $func;
		$this->Visible = TRUE;

		// create html code for this button
		$this->Def = "<A HREF=\"javascript:document.$this->Form.submit()\" \n";
		$this->Def .= "onmouseover=\"document.$this->Form.$this->Name.src='$this->ImageSel'\" \n";
		$this->Def .= "onmouseout=\"document.$this->Form.$this->Name.src='$this->ImageUp'\" \n";
		$this->Def .= "onclick=\"$this->Func return formbutton_click('$this->Cmd', 'cmd', '$this->Name', '$this->ImageDown')\">\n";
		$this->Def .= "<IMG SRC=\"$this->ImageUp\" WIDTH=\"$this->Width\" HEIGHT=\"$this->Height\" BORDER=\"0\" ALT=\"$this->Text\" NAME=\"$this->Name\">\n";
		$this->Def .= "</A>";
		}

	// set a click handler function for this button
	function SetFunction($func="")
		{
		$this->Func = $func;
		$this->Def = "<A HREF=\"javascript:document.$this->Form.submit()\" \n";
		$this->Def .= "onmouseover=\"document.$this->Form.$this->Name.src='$this->ImageSel'\" \n";
		$this->Def .= "onmouseout=\"document.$this->Form.$this->Name.src='$this->ImageUp'\" \n";
		$this->Def .= "onclick=\"$this->Func return formbutton_click('$this->Cmd', 'cmd', '$this->Name', '$this->ImageDown')\">\n";
		$this->Def .= "<IMG SRC=\"$this->ImageUp\" WIDTH=\"$this->Width\" HEIGHT=\"$this->Height\" BORDER=\"0\" ALT=\"$this->Text\" NAME=\"$this->Name\">\n";
		$this->Def .= "</A>";
		}
}

class ToolBar
{
	var $Buttons;	array for FormButton objects

	// add button to toolbar
	function Add(&$btn)
		{
		$this->Buttons[$btn->Name] = $btn;
		}

	// display the toolbar
	function Display($usetable=TRUE)
		{
		if ($usertable)
			echo "<TABLE><TR>\n";
		foreach ($this->Buttons as $btn)
			{
			if ($btn->Visible)
				echo "<TD>$btn->Def</TD>\n";
			}
		if ($usetable)
			echo "</TR></TABLE>\n";
		}

	// hide a toolbar button
	function HideButton($name)
		{
		$btn = &$this->Buttons[$name];
		$btn->Visible = FALSE;
		}

	// show a toolbar button
	function ShowButton($name)
		{
		$btn = &$this->Buttons[$name];
		$btn->Visible = TRUE;
		}


	// set a function to run on button click
	function SetFunction($name, $func="")
		{
		$btn = &$this->Buttons[$name];
		$btn->SetFunction($func);
		}
}

?>

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

// function to set cmd value when toolbar button clicked
function formbutton_click(btn, cmd, nam, img)
{
	// redraw the button as clicked
	button = document.myform.elements[nam];
	button.src = img;

	// set the value
	command = document.myform.elements[cmd];
	command.value = btn;
	return true;
}

</SCRIPT>

</HEAD>

<BODY>

<?php

// load toolbar buttons
$btn_first = new FormButton("btn_first", "FIRST", "myform", "Go to First Record", 
	"./images/btn-first0.bmp", "./images/btn-first1.bmp", "./images/btn-first2.bmp");
$btn_prev = new FormButton("btn_prev", "PREV", "myform", "Go to Previous Record", 
	"./images/btn-prev0.bmp", "./images/btn-prev1.bmp", "./images/btn-prev2.bmp");
$btn_next = new FormButton("btn_next", "NEXT", "myform", "Go to NextRecord", 
	"./images/btn-next0.bmp", "./images/btn-next1.bmp", "./images/btn-next2.bmp");
$btn_last = new FormButton("btn_last", "LAST", "myform", "Go to Last Record", 
	"./images/btn-last0.bmp", "./images/btn-last1.bmp", "./images/btn-last2.bmp");
$btn_process = new FormButton("btn_process", "PROCESS", "myform", "Process Form Data", 
	"./images/btn-process0.bmp", "./images/btn-process1.bmp", "./images/btn-process2.bmp");

// create the toolbar
$toolbar = new ToolBar;
$toolbar->Add($btn_first);
$toolbar->Add($btn_prev);
$toolbar->Add($btn_next);
$toolbar->Add($btn_last);
$toolbar->Add($btn_process);

display the toolbar
$toolbar->Display();

// a sample form
// the hidden field '$cmd' contains the command of the button that was clicked
echo "<FORM NAME=\"myform\">\n";
echo "<TD><INPUT TYPE=\"hidden\" NAME=\"cmd\" VALUE=\"$cmd\"></TD>\n"; 
echo "</TR></TABLE>\n";
echo "</FORM>\n";

?>

</BODY>
</HTML>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Brian Levine

Comment:



Comment:

(Maximum characters: 1200). You have characters left.