PHP WinBinder: Basic IO: Difference between revisions
(New page: {{Nav 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 somet...) |
(Proofreading and grammatical changes; some minor reformatting) |
||
Line 2: | Line 2: | ||
'''''Basic Input and Output''''' | '''''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. | 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 | 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: | The manual provides this function to create controls: | ||
{| | {| | ||
Line 15: | Line 15: | ||
</pre> | </pre> | ||
|} | |} | ||
On first encounter it looks intimidating | On first encounter it looks intimidating, so I have reduced it to give this: | ||
{| | {| | ||
|- | |- | ||
Line 35: | Line 35: | ||
|'''id'''|| is an integer that identifies the control. It must be unique if the control is to be processed by a callback function. | |'''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? | That’s all very well, but how does it all fit together? | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Labels == | == Labels == | ||
Labels are areas on a window containing text. | 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 | 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: | Example 2 defines the following labels: | ||
Line 59: | Line 58: | ||
* Each label has a unique ID 101,102,103 | * Each label has a unique ID 101,102,103 | ||
* handles are captured in variables $label1,$label2 and $label3 these are optional | * handles are captured in variables $label1,$label2 and $label3 these are optional | ||
* You can find a components handle using | * You can find a components handle using its ID, for example '''wb_get_control($window, 102)''' | ||
|} | |} | ||
Line 76: | Line 75: | ||
|} | |} | ||
OK! So where does this code go? Place it | 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 | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Push Buttons == | |||
== Push | 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. | ||
Strange as this may seem push buttons are similar to labels in terms of creation. WinBinder has unified the creation process | |||
Example 2 defines the following push buttons: | Example 2 defines the following push buttons: | ||
Line 95: | Line 93: | ||
</pre> | </pre> | ||
| | | | ||
* Each push button has a unique ID 201,202,203 | * Each push button has a unique ID: 201,202,203 | ||
* No need to capture handles. Each button is traceable by its ID | * No need to capture handles. Each button is traceable by its ID | ||
|} | |} | ||
Line 104: | Line 102: | ||
|'''parent'''|| handle to the WinBinder object '''$mainwin''' | |'''parent'''|| handle to the WinBinder object '''$mainwin''' | ||
|- | |- | ||
|'''ctlclass'''|| is the class of the control or object to be created '''PushButton''' | |'''ctlclass'''|| is the class of the control or object to be created: '''PushButton''' | ||
|- | |- | ||
|'''caption '''|| A string (Apache,MySQL and Alert) | |'''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. | |'''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 | |'''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 | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Interaction == | == Interaction == | ||
{| | {| | ||
|-valign="top" | |-valign="top" | ||
| | | | ||
At this stage running the script will produce a window as shown on the right. | 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. | 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. | 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''''' | '''''Button Specification''''' | ||
* '''Apache''' When clicked changes label1 to display “Apache Port = 80” | * '''Apache''' When clicked changes label1 to display “Apache Port = 80” | ||
* '''MySQL''' When clicked changes label2 to display “MySQL Port = 3306” | * '''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” also changes both labels back to initial condition. | * '''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. | ||
| | | | ||
[[Image:WinBinder_2.gif]] | [[Image:WinBinder_2.gif]] | ||
|} | |} | ||
=== Handler function - code === | === Handler function - code === | ||
To resolve | To resolve IDs, the handler function uses a switch statement. | ||
This is not a strict requirement | 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) | However in response to ID=IDCLOSE, you must run function wb_destroy_window($window) | ||
==== Changing Label text ==== | ==== Changing Label text ==== | ||
Our specification requires changing label text WinBinder provides the following general function: | Our specification requires changing the label text. WinBinder provides the following general function: | ||
{| | {| | ||
|- | |- | ||
Line 155: | Line 151: | ||
|- | |- | ||
| | | | ||
A simpler description | A simpler description is: | ||
| | | | ||
<pre> | <pre> | ||
Line 161: | Line 157: | ||
</pre> | </pre> | ||
|} | |} | ||
When the labels are created their handles are captured in variables | 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: | If the handles were not assigned to variables they can be found using this function: | ||
Line 176: | Line 172: | ||
|} | |} | ||
See example below: | See example below: | ||
==== Apache button ==== | ==== Apache button ==== | ||
Line 190: | Line 184: | ||
</pre> | </pre> | ||
| | | | ||
* When Apache Button is clicked case 201 (Button ID=201)is true. | * 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) | * The label text is updated with function wb_set_text(). The label handle is obtained using its ID (101) | ||
* break exits switch | * break exits switch | ||
|} | |} | ||
==== MySQL Button ==== | ==== MySQL Button ==== | ||
Handler code for MySQL button: | Handler code for MySQL button: | ||
Line 208: | Line 201: | ||
</pre> | </pre> | ||
| | | | ||
* When MySQL Button is clicked case 202 (Button ID=202)is true. | * When MySQL Button is clicked, case 202 (Button ID=202) is true. | ||
* global $label2; Gives access to label 2 handle | * global $label2; Gives access to label 2 handle | ||
* The label text is updated with function wb_set_text(). The label handle is directly used | * The label text is updated with function wb_set_text(). The label handle is directly used | ||
* break exits switch | * break exits switch | ||
|} | |} | ||
==== Alert Button ==== | ==== Alert Button ==== | ||
Line 234: | Line 225: | ||
</pre> | </pre> | ||
| | | | ||
* When Alert Button is clicked case 203 (Button ID=203)is true. | * When Alert Button is clicked, case 203 (Button ID=203) is true. | ||
* global $label1; Gives access to label 1 handle | * global $label1; Gives access to label 1 handle | ||
* global $label2; Gives access to label 2 handle | * global $label2; Gives access to label 2 handle | ||
Line 241: | Line 232: | ||
* break exits switch | * break exits switch | ||
|} | |} | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Test 2 Script == | == Test 2 Script == | ||
The final script is: | |||
{| | {| | ||
|-valign="top" | |-valign="top" | ||
Line 324: | Line 315: | ||
== Summary == | == Summary == | ||
This page introduced two basic controls | 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 you really do not require all the fancy bells and whistles as found on most windows 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 | That said, WinBinder provides a vast array of controls you can use. These are fully described in the documentation. | ||
The [[PHP WinBinder: Text input | '''next page''']] covers text input. | The [[PHP WinBinder: Text input | '''next page''']] covers text input. |
Latest revision as of 13:58, 29 January 2010
|
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.