PHP WinBinder: Basic IO
|
UniServer 5-Nano PHP WinBinder. |
Basic Input and Output
Most applications are designed to provide some form of interaction with a user. This can be a simple change of text indicating something happened when a button is click. Maybe the result requires a more significant attention grabber such as an alert box.
This page shows how to create three changeable text areas, three buttons and an alert pop-up box.
Control Creation
The manual provides this function to create controls:
int wb_create_control (int parent, int ctlclass [, mixed caption [, int xpos [, int ypos [, int width [, int height [, int id [, int style [, int param [, int ntab]]]]]]]]]) |
On first encounter it looks intimidating, so I have reduced it to give this:
handle = wb_create_control(parent,ctlclass,caption,xpos,ypos,width,height,id) |
Parameters
parent | is a handle to the WinBinder object that is the parent window. |
ctlclass | is the class of the control or object to be created. |
caption | is either a string with the caption of the control, an array of captions, or an array of arrays with information, depending on the control class. |
xpos, | ypos, width and height describe the position and dimensions of the control when required. |
id | is an integer that identifies the control. It must be unique if the control is to be processed by a callback function. |
That’s all very well, but how does it all fit together?
Labels
Labels are areas on a window containing text.
They can be static (not changeable), however once given an ID and optionally grabbing their handle, you can have dynamic text areas, meaning dynamic feedback to a user.
Example 2 defines the following labels:
$label1 = wb_create_control($mainwin, Label, "Apache Port = ", 5, 20, 112, 20, 101); $label2 = wb_create_control($mainwin, Label, "MySQL Port = ", 5, 40, 112, 20, 102); $text = "This is test 3 on two lines\nThis is test 3 on two lines"; $label3 = wb_create_control($mainwin, Label, $text, 5, 60, 140, 40, 103); |
|
Parameters
parent | handle to the WinBinder object $mainwin |
ctlclass | is the class of the control or object to be created Label |
caption | A string (Test 1,Test 2) note for label3 uses variable $text |
xpos, | ypos, width and height Tweak the values to position text. Note if text is to large to fit in a box it is clipped. |
id | A unique integer identifies a control. Start from 100 to avoid clashes. Say labels will be 100 push buttons start at 200 etc |
OK! So where does this code go? Place it in section two of the template. Once the main window is created the components need to be created before they can be used.
Push Buttons
Strange as this may seem, push buttons are similar to labels in terms of creation. WinBinder has unified the creation process so there are only a few logical choices.
Example 2 defines the following push buttons:
$button1 = wb_create_control($mainwin, PushButton, "Apache", 20, 120, 80, 22, 201); $button2 = wb_create_control($mainwin, PushButton, "MySQL", 110, 120, 80, 22, 202); $button3 = wb_create_control($mainwin, PushButton, "Alert", 200, 120, 80, 22, 203); |
|
Parameters
parent | handle to the WinBinder object $mainwin |
ctlclass | is the class of the control or object to be created: PushButton |
caption | A string (Apache,MySQL and Alert) |
xpos, | ypos, width and height Tweak the values to position a button. Note: if a button is too small, text is clipped. |
id | A unique integer identifies a control. Start from 100 to avoid clashes. Say labels begin at 100, push buttons start at 200, etc. |
Where does this code go? Same place as the labels, in section two of the template.
Interaction
At this stage, running the script will produce a window as shown on the right. Although the buttons are active they appear to do nothing when clicked. However behind the scenes they call function process_main() with parameters $window and $id ($id = ID of the button). The function contains no supporting code hence just returns. Let's specify what the buttons will do and then add code to the function. Button Specification
|
Handler function - code
To resolve IDs, the handler function uses a switch statement.
This is not a strict requirement. You can use whatever method that suits your style of programming.
However in response to ID=IDCLOSE, you must run function wb_destroy_window($window)
Changing Label text
Our specification requires changing the label text. WinBinder provides the following general function:
bool wb_set_text (int wbobject, mixed text [, int item [, int subitem]]) |
A simpler description is: |
wb_set_text(handle,"some text"); |
When the labels are created their handles are captured in variables. These are global and can be used in the Handler Function.
If the handles were not assigned to variables they can be found using this function:
int wb_get_control (int wbobject [, int id]) Returns an integer handle that corresponds to the WinBinder object (control, toolbar item or menu item) wbobject that has the supplied id. This function is typically used to retrieve the handle of a child control in a dialog box or in a menu item. |
See example below:
Apache button
Handler code for Apache button:
case 201: // Apache button wb_set_text(wb_get_control($window, 101),"Apache Port = 80"); break; |
|
MySQL Button
Handler code for MySQL button:
case 202: // MySQL button global $label2; wb_set_text($label2,"MySQL Port = 3306"); break; |
|
Alert Button
case 203: // Alert button global $label1; global $label2; wb_set_text($label1,"Apache Port ="); wb_set_text($label2,"MySQL Port ="); wb_message_box($window, "Cannot run two servers on port 80.","ALERT Test 2"); break; |
|
Test 2 Script
The final script is:
<?php Include "../php/include/winbinder.php"; // Location Of Winbinder Library //=== 1) Create main window --------------------------------------------------- $mainwin = wb_create_window(NULL, AppWindow, "Test 2", 320, 240); //=== 2) Create controls for the main window ---------------------------------- $label1 = wb_create_control($mainwin, Label, "Apache Port = ", 5, 20, 112, 20, 101); $label2 = wb_create_control($mainwin, Label, "MySQL Port = ", 5, 40, 112, 20, 102); $text = "This is test 3 on two lines\nThis is test 3 on two lines"; $label3 = wb_create_control($mainwin, Label, $text, 5, 60, 140, 40, 103); $button1 = wb_create_control($mainwin, PushButton, "Apache", 20, 120, 80, 22, 201); $button2 = wb_create_control($mainwin, PushButton, "MySQL", 110, 120, 80, 22, 202); $button3 = wb_create_control($mainwin, PushButton, "Alert", 200, 120, 80, 22, 203); //=== 3) Assign handler function to the main window -------------------------- wb_set_handler($mainwin, "process_main"); //=== 5) Enter application loop ----------------------------------------------- wb_main_loop(); //=== 4) Handler Function ----------------------------------------------------- function process_main($window, $id) { global $label1; switch($id) { case 201: // Apache button wb_set_text(wb_get_control($window, 101),"Apache Port = 80"); break; case 202: // MySQL button global $label2; wb_set_text($label2,"MySQL Port = 3306"); break; case 203: // Alert button global $label1; global $label2; wb_set_text($label1,"Apache Port ="); wb_set_text($label2,"MySQL Port ="); wb_message_box($window, "Cannot run two servers on port 80.","ALERT Test 2"); break; case IDCLOSE: // Constant IDCLOSE (8) predefined wb_destroy_window($window); // Destroy the window break; } } ?> |
|
test_2.phpw Run: Navigate to folder UniServer\plugins\winbinder\examples Double click on test_2.bat |
Summary
This page introduced two basic controls: labels and push buttons. Even with this minimum set of controls: it is possible to produce useful applications.
What’s important is that you really do not require all the fancy bells and whistles as found on most windows applications. That said, WinBinder provides a vast array of controls you can use. These are fully described in the documentation.
The next page covers text input.