PHP WinBinder: Basic IO

From The Uniform Server Wiki
Jump to: navigation, search

 

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?

Top

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

  • Each label has a unique ID 101,102,103
  • handles are captured in variables $label1,$label2 and $label3 these are optional
  • You can find a components handle using its ID, for example wb_get_control($window, 102)

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.

Top

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

  • Each push button has a unique ID: 201,202,203
  • No need to capture handles. Each button is traceable by its ID

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.

Top

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

  • Apache When clicked changes label1 to display “Apache Port = 80”
  • MySQL When clicked changes label2 to display “MySQL Port = 3306”
  • Alert When clicked produces a pop-up alert box with message “Cannot run two servers on port 80” and also changes both labels back to initial condition.

WinBinder 2.gif

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;
  • When Apache Button is clicked, case 201 (Button ID=201) is true.
  • The label text is updated with function wb_set_text(). The label handle is obtained using its ID (101)
  • break exits switch

MySQL Button

Handler code for MySQL button:

case 202:         // MySQL button
 global $label2; 
 wb_set_text($label2,"MySQL Port  = 3306");
 break;
  • When MySQL Button is clicked, case 202 (Button ID=202) is true.
  • global $label2; Gives access to label 2 handle
  • The label text is updated with function wb_set_text(). The label handle is directly used
  • break exits switch

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;


  • When Alert Button is clicked, case 203 (Button ID=203) is true.
  • global $label1; Gives access to label 1 handle
  • global $label2; Gives access to label 2 handle
  • Label text is restored to default values. Label handles are directly used
  • Function wb_message_box() produces a pop-up box. Text "Cannot run two servers on port 80." is displayed. The box has a title "ALERT Test 2"
  • break exits switch

Top

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; 
  }
}
?>

 


Script:

test_2.phpw

Run:

Navigate to folder

UniServer\plugins\winbinder\examples

Double click on test_2.bat

Top

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.

Top