PHP WinBinder 3: Dialog pop-ups

From The Uniform Server Wiki
Jump to navigation Jump to search

 

UniServer 5-Nano
PHP WinBinder 3.

Dialog and message boxes

WinBinder provides a number of standard dialog boxes and message boxes. These can be run directly from a PHP CLI script or run within a Windows application.

Following examples demonstrate how to use them.

Message box

A message box has the following code format:

  • int wb_message_box (int parent, string message [, string title [, int style]])

A few examples of supported styles are listed below:

Style Value Display
WBC_OK (the default) Standard OK button.
WBC_INFO Information icon and a OK button.
WBC_OKCANCEL Question mark icon, OK and Cancel buttons.
WBC_YESNO Question mark icon, Yes and No buttons.
  • The default dialogue is neat very compact and is extremely useful for debugging code.
  • What may be confusing is you must state a parent! However if you put code in the INIT
    section you may think there is not parent window. Not strictly true every application
    has a default parent, to pick this up just use NULL.
  • Note: You can even use NULL in the Handler Function

Top

Example 1 (WBC_OK)

The following three lines are example code

  • wb_message_box (NULL, "Create Window", "TEST");
  • wb_message_box ($window, $name, "TEST");
  • wb_message_box (NULL, $pass, "TEST");

 

First line is placed in the INIT section. Clicking either OK or close has the same
effect because we are not using the returned value.

Next two lines are added to the Handler Function again the return value is not used.
However they do use text entered in the pop-up text box.


Edit file

  

Results

Edit file Pro_demo_wb.php add the above lines as shown below:

//=== INIT =========================================

  wb_message_box (NULL, "Create Window", "TEST"); 

//==================================== END INIT ====
     $pass  = trim($pass);

     wb_message_box ($window, $name, "TEST"); 
     wb_message_box (NULL, $pass, "TEST");

   break; 
  • Run the script (double click on file Pro_demo_wb.bat)
  • Result shown on the right
  • The first pop-up relates to the PHP CLI script no Window is generated until you click the OK button
  • Once clicked the window is created.
  • Processing is now confined to the Windows application.
  • Enter a name and password
  • Click Enter to start processing
  • Each text box is read
  • Followed by our two dialogue boxes

Function wb_message_box is a regular PHP function. You can use strings as in the first line for example
"Create Window" alternatively, use variables such as $name and $pass.

Important: If there is no obvious parent window use NULL

  

Demo 2.gif

Top

Example 2 (WBC_YESNO)

Message boxes are ideal for obtaining a user response to yes no questions.

The following line of code creates a message box with two buttons Yes and No. This is achieved using the style attribute WBC_YESNO

The following two lines are example code

  • $answer = wb_message_box (NULL, "Are you sure you want to create a window?", "TEST2",WBC_YESNO);
  • if($answer == FALSE) Exit;

 

Text displayed requires a yes or no response from a user.

If answer is No exit the script.

Clicking button Yes returns true while clicking button No returns false.

Edit file

Edit file Pro_demo_wb.php add the above lines as shown below:

//=== INIT =========================================

 wb_message_box (NULL, "Create Window", "TEST"); 

 $answer = wb_message_box (NULL, "Are you sure you want to create a window?", "TEST2",WBC_YESNO);
 if($answer == FALSE) Exit;

//==================================== END INIT ====
  • Run the script (double click on file Pro_demo_wb.bat)
  • Click OK
  • Click No
  • Result shown on the right

What’s important: When buttons are clicked in the
above sequence the Windows creation code is never executed.

It clearly shows you can use message boxes within pure PHP CLI code.

If you click Yes the Windows creation code is executed.

  

Demo 3.gif

Top

Coding style

Coding style is a personal choice. You will have noticed I assigned the returned value to a variable $answer and then used it in an if statement.

The following two examples give the same result

Replace

 $answer = wb_message_box (NULL, "Are you sure you want to create a window?", "TEST2",WBC_YESNO);
  if($answer == FALSE) Exit;

With

  if(!wb_message_box (NULL, "Are you sure you want to create a window?", "TEST2",WBC_YESNO)) Exit;

or

  if(!wb_message_box (NULL, "Are you sure you want to create a window?", "TEST2",WBC_YESNO)){
     Exit;
  }

My personal preference is the last style its easier to add additional code if required.

Top

Select Path dialog box

A select path dialog box has the following code format:

  • string wb_sys_dlg_path (int parent [, string title [, string path]])

Parameters:

  • parent We are using PHP CLI hence use NULL for this parameter
  • title A string to be displayed in the dialog box.
  • path Full path to a folder to start from (optional)

Returns full path of selected folder, or a blank string if the dialog box was canceled. Returns NULL if not successful.

The following lines are example code

  • $currentpath = dirname(__FILE__).'\www';
  • $path = wb_sys_dlg_path(NULL, 'Select a folder', $currentpath);
  • if($path){
  •  wb_message_box (NULL, $path, "TEST3");
  • }

 

First line sets the current path of the script. It is optional worth including if you know where to startfrom.

Second line creates the pop-up. Returned value is save in variable $path

If a path is returned it is displayed in a message box.


Edit file

Edit file Pro_demo_wb.php add the above lines as shown below:

  

Results

//=== INIT =========================================

  $currentpath = dirname(__FILE__).'\www';  
  $path = wb_sys_dlg_path(NULL, 'Select a folder', $currentpath);
  if($path){
     wb_message_box (NULL, $path, "TEST3"); 
  }

//==================================== END INIT ====
  • Run the script (double click on file Pro_demo_wb.bat)
  • Brows for Folder is displayed
  • Note folder www is pre-selected (we set this using $currentpath)
  • Select any folder (images shows folder images selected)
  • Click OK
  • Message box Test4 displays full path to selected folder.
  • Result shown on the right

In a real application you use the variable $path as required.

You would delete this code its only there for testing:

  if($path){
     wb_message_box (NULL, $path, "TEST3"); 
  }

  

Demo 4.gif


Top

Links

The following are usful links to the WinBinber reference manual:


Top

Summary

That concludes support material for the new Application plugin scripts.

If you followed the above link you will have noticed there are other dialogue functions such as Open dialog box and Save As dialog box you can use.

These will not work since our basic script does not support them this will be resolved later in the tutorial.

Our current implementation of the name-password pop-up is not independent of our script it an integral part. By this I mean it’s not a function like the above message boxes.

Next page shows how to convert the windows creation code into a function that can be used similar to a message box.

Top