Putting It All Together

   

So now you know how to make a label, a button (with a label in it), and a text entry field. With these three tools, you can begin to build an application!

First, we need to know how to get the text the user enters in the text entry widget. The GtkTextEntry widget has a method that meets our needs:

 $input = $text1->get_text();  

You can assign the user-entered data using the get_text() method on the $text1 object to a variable (for example $input), then do whatever you want with that text. The following script puts it all together and provides a short example of a function to handle the user-entered text. See Figure 12-6 for example output.

Script 12-4 text_submit.php
  1.  <?php  2.    if (!class_exists('gtk')) {  3.    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')  4.      dl('php_gtk.dll');  5.    else  6.      dl('php_gtk.so');  7.  }  8.  function destroy(){  9.    Gtk::main_quit(); 10.  } 11.  function submit(){ 12.    global $text1; 13.    $input = $text1->get_text(); 14.    echo $input . "\n"; 15.    $text1->set_text(''); 16.  } 17.  $window = &new GtkWindow(); 18.  $window->connect('destroy', 'destroy'); 19.  $window->set_border_width(5); 20.  $text1 = &new GtkEntry(); 21.  $label1 = &new GtkLabel("A Text Entry Field"); 22.  $button1 = &new GtkButton('Submit Text!'); 23.  $button1->connect('clicked', 'submit'); 24.  $box = &new GtkHBox(); 25.  $window->add($box); 26.  $box->pack_start($label1, false, false, 0); 27.  $box->pack_start($text1, false, false, 0); 28.  $box->pack_start($button1, false, false, 0); 29.  $window->show_all(); 30.  Gtk::main(); 31.  ?> 
Figure 12-6. text_submit.php

graphics/12fig06.jpg

Script 12-4. text_submit.php Line-by-Line Explanation

LINE

DESCRIPTION

2

If the GTK class has not been defined, then execute lines 3 7. If the GTK class has been defined, then the script needs to do nothing.

3

Check the first three characters of the PHP_OS environment variable.

4

If it is WIN, then the system is running Windows. Load the PHPGTK library for Windows.

5 7

If the PHP_OS environment variable is not WIN, then assume that PHP is running on a Linux system and load the Linux PHPGTK library.

8 10

Define a function, destroy(), that stops the script when called.

9

Execute the Gtk::main_quit() method to stop the script and close the GTK window.

10

End the function declaration.

11 16

Define a function, submit(), that echoes the user-entered text to STDOUT.

12

Allow the variable $text1 to be accessed by this function.

13

Assign the user-entered text to the $input variable by calling the get_text() method of the $text1 text object.

14

Echo the user-entered text to STDOUT along with a newline character

15

Clear the user-entered text from the text input by calling the set_text() method of the $text1 text object, assigning the new text to be blank ('').

16

End the function declaration.

17

Create a new window object called $window.

18

Connect the 'destroy' method of the window to the destroy() function that you defined earlier. The 'destroy' method is called when the user clicks the "X" on the top right of the window.

19

Set a five-pixel border around the window using the set_border_width method of the window object.

20

Create a new text-entry object called $text1.

21

Create a new label object called $label1 containing the text "A Text Entry Field".

22

Create a new button object called $button.

23

Connect the 'clicked' method of the button to the hello() function.

24

Create a new horizontal box object called $box.

25

Add the box to the window using the add() method.

26

Pack the label object into the box.

27

Pack the text-entry object into the box.

28

Pack the button object into the box.

29

Issue the show_all() function call to the $window object so that everything within the object is displayed.

30

Call the Gtk::main() method to run the script.

Dialog Boxes

Most GUI applications have the ability to display small windows notifying the user that something is wrong or to confirm that the user wants to take the desired action. For example, a dialog box appears asking "Are you sure…" in most word processing applications if you try to close the application without first saving the document on which you are working. PHPGTK also provides an easy way to create simple dialog boxes using the GtkDialog class.

The GtkDialog class is simply a window that contains a vbox, a separator, and an "action area." You place your text in the vbox and place your buttons in the "action area."

This next script shows an example of how to use a GtkDialog box to confirm that a user wishes to quit out of the application. When a user clicks the "Quit" button, or the "X" in the upper right corner of the main window, a dialog box is displayed, asking the users if they are sure they want to quit. If the user clicks "OK" in the dialog box, then the application exits. If the user clicks "Cancel" or the "X" in the upper right corner of the dialog box, then the dialog box closes, but the application remains open. See Figure 12-7 for example output.

Script 12-5 dialog.php
  1.  <?php  2.  if (!class_exists('gtk')) {  3.    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')  4.      dl('php_gtk.dll');  5.    else  6.      dl('php_gtk.so');  7.  }  8.  function destroy() {  9.    Gtk::main_quit(); 10.  } 11.  function close_dialog($button, $dialog){ 12.    $dialog->destroy(); 13.  } 14.  function delete_event(){ 15.    confirm(); 16.    return true; 17.  } 18.  function confirm(){ 19.    global $window; 20.    $dialog = &new GtkDialog; 21.    $dialog->set_title("Are You Sure?"); 22.    $dialog->set_default_size('100','100'); 23.    $dialog->set_modal(TRUE); 24.    $dialog->set_transient_for($window); 25.    $dialog->set_policy(0,0,0); 26.    $label = &new GtkLabel("Are You Sure You Want To Quit?"); 27.    $button_yes = &new GtkButton('OK'); 28.    $button_yes->connect('clicked', 'destroy'); 29.    $button_no = &new GtkButton('Cancel'); 30.    $button_no->connect('clicked', 'close_dialog', $dialog); 31.    $vbox1 = $dialog->vbox; 32.    $vbox2 = $dialog->action_area; 33.    $vbox1->pack_start($label); 34.    $vbox2->pack_start($button_yes); 35.    $vbox2->pack_start($button_no); 36.    $dialog->show_all(); 37.  } 38.  $window = &new GtkWindow(); 39.  $window->connect('delete_event', 'delete_event'); 40.  $button = &new GtkButton('Quit'); 41.  $button->connect('clicked', 'confirm'); 42.  $window->add($button); 43.  $window->show_all(); 44.  Gtk::main(); 45.  ?> 
Figure 12-7. dialog.php initial window and confirmation dialog box

graphics/12fig07.jpg

Script 12-5. dialog.php Line-by-Line Explanation

LINE

DESCRIPTION

2

If the GTK class has not been defined, then execute lines 3 7. If the GTK class has been defined, then the script needs to do nothing.

3

Check the first three characters of the PHP_OS environment variable.

4

If it is WIN, then the system is running Windows. Load the PHPGTK library for Windows.

5 7

If the PHP_OS environment variable is not WIN, then assume that PHP is running on a Linux system and load the Linux PHPGTK library.

8 10

Define a function, destroy(), that stops the script when called.

9

Execute the Gtk::main_quit() method to stop the script and close the GTK window.

10

End the function declaration.

11 13

Define a function, close_dialog(), that closes a dialog box. This function takes two arguments: $button and $dialog. See the description for line 30 for a description of the arguments.

12

Call the destroy() method of the $dialog object to close the dialog window.

13

End the function declaration.

14 17

Define a function, delete_event(), that catches when a user clicks on the "X" in the upper-right corner of the main application window and runs the confirm function(). The delete_event() is a special method that is run whenever a user clicks the "X" in the upper-right corner of the application window. "delete_event" is a valid connection point for a window, similar to the destroy connection point. If you do not use a delete_event connection point, then the window is automatically destroyed.

15

Execute the confirm() function to make certain the user wishes to exit the application.

16

Return true. If you return false (which is the default behavior of the delete_event when you don't assign a connection to it), then the window is destroyed.

17

End the function declaration.

18 37

Define a function, confirm(), to confirm that the user wishes to quit the application.

19

Allow the $window object to be used by this function.

20

Create a new dialog object called $dialog.

21

Set the title for the dialog object.

22

Set the default size of the dialog window to be 100 x 100 pixels.

23

Set the dialog window as a modal window. This means that the main application window cannot be accessed while this window exists.

24

Set the dialog window as a transient of the main window. A transient window does not cause an extra taskbar item to be created in the OS for the window. This is currently broken on Windows but works under Linux.

25

Set the resize policy for the window. The resize() method takes three boolean (true or false/0 or 1) arguments:

  • allow_shrink Allow the window to be smaller than its children (this should always be set to false).

  • allow_grow Allow the window to be made greater than its original size.

  • auto_shrink Allows the window to override its default size settings to shrink to the size of any child windows.

26

Create a new label object called $label.

27

Create a new button object called $button_yes. This is used as the "OK" button in the dialog box.

28

Use the connect() method to cause the destroy() function to be called when this button is clicked.

29

Create a new button called $button_no. This is used as the "Cancel" button in the dialog box.

30

Use the connect() method to cause the close_dialog() function to be called. Note that the connect method has an extra argument tagged on at the end. Normally, the connect() method has two arguments: the action (such as clicked) and the function that is called (such as close_dialog). You can add a third argument, which is an additional variable that is sent to the function. In this case, we are sending the dialog window object $dialog. Since we are sending two arguments, and the connected function usually takes only one by default (which is the object that is making the connection), you must define the default object ($button) and the additional object ($dialog) in the function for which you are creating the connection, in this case, close_dialog().

31

Since the GtkDialog object has built-in "boxes" for content, you need to assign a variable name for that object. Here you assign a variable name to the GtkDialog's vbox.

32

Assign a variable name to the GtkDialog's action_area.

33

Pack the label object into the vbox.

34 35

Pack the OK and Cancel buttons in the GtkDialog's action_area.

36

Show the contents of the dialog box.

37

End the function declaration.

38

Create a new window object called $window.

39

Connect the 'delete_event' method of the window to the delete_event() function that you defined earlier. The 'delete_event' method is called when the user clicks the "X" button in the upper right corner of the application. In the previous examples, we left this method out since we wanted to directly destroy the window.

40

Create a new button object called $button.

41

Connect the 'clicked' method of the button to the confirm function.

42

Add the button to the window using the add() method.

43

Issue the show_all() function call to the $window object so that everything within the object is displayed.

44

Call the Gtk::main() method to run the script.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net