home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 April / Chip_2003-04_cd1.bin / zkuste / phpgtk / download / samples / convert.php next >
Text File  |  2003-01-14  |  5KB  |  145 lines

  1. //**************************************
  2.     //     
  3.     // Name: Number Converter
  4.     // Description:This utility shows how to
  5.     //     convert numbers to different types (i.e.
  6.     //     hex, decimal, binary, octal).
  7.     // By: Josh Sherman
  8.     //
  9.     // Assumes:The code converts the current
  10.     //     value to a decimal, and then from decima
  11.     //     l to the selected type. I do this becaus
  12.     //     e PHP has built in functions to convert 
  13.     //     hexadecimal, octal, or binary to decimal
  14.     //     , and decimal to hexadecimal, octal, or 
  15.     //     binary, but doesn't including functions 
  16.     //     to convert octal to hexadecimal, binary 
  17.     //     to octal, or anything fancy like that. T
  18.     //     he code also makes use of the eval state
  19.     //     ment which helped reduce the length of t
  20.     //     he code considerably.
  21.     //
  22.     //This code is copyrighted and has    // limited warranties.Please see http://
  23.     //     www.Planet-Source-Code.com/xq/ASP/txtCod
  24.     //     eId.582/lngWId.8/qx/vb/scripts/ShowCode.
  25.     //     htm    //for details.    
  26.     //     
  27.     
  28.     <?
  29.     /*
  30.     * convert.php - Number conversion in PHP-GTK.
  31.     * 
  32.     * Author: Josh Sherman
  33.     * Purpose: Converts a number to a different type.
  34.     * Usage: php -q conversion.php
  35.     *
  36.     */
  37.     // Check to see if the PHP-GTK extension
  38.     //     is available.
  39.     dl( 'php_gtk.' . (strstr( PHP_OS, 'WIN') ? 'dll' : 'so'));
  40.     // Called when delete-event takes place,
  41.     //     tells it to proceed.
  42.     function delete_event()
  43.     {
  44.         return false;
  45.     }
  46.     // Called when the window is being destr
  47.     //     oyed, tells it to quit the main loop.
  48.     function destroy()
  49.     {
  50.         Gtk::main_quit();
  51.     }
  52.     // Called when a radio button is clicked
  53.     //     , converts the number to that format.
  54.     function convert($widget, $which)
  55.     {
  56.         global $current_type;
  57.         global $entry;
  58.         // Get the value from the entry field    
  59.         $number = $entry->get_text();
  60.         // Make sure they aren't clicking on an already active radio.
  61.         if ($current_type != $which) {
  62.             // Converts the number to decimal if it isn't already.
  63.             if ($current_type != "dec") {
  64.                 eval ("\$number = " . $current_type . "dec(\"$number\");");
  65.             }
  66.             // Converts the number to the desired format.
  67.             if ($which != "dec") {
  68.                 eval ("\$number = strtoupper(dec" . $which . "(\"$number\"));");
  69.             }
  70.             // Sets the entry box to the new value.
  71.             $entry->set_text($number);
  72.         }
  73.         // Set the new type as the current type.
  74.         $current_type = $which;
  75.     }
  76.     // Creates a new top-level window and co
  77.     //     nnect the signals to the appropriate fun
  78.     //     ctions.
  79.     $window = &new GtkWindow();
  80.     $window->connect('destroy', 'destroy');
  81.     $window->connect('delete-event', 'delete_event');
  82.     $window->set_title("Conversion Utility");
  83.     $window->set_border_width(5);
  84.     $window->set_policy(false, false, false);
  85.     // Creates a table to place our widgets,
  86.     //     and adds it to the table.
  87.     $table = &new GtkTable(2, 1);
  88.     $window->add($table);
  89.     // Creates an entry field, and places it
  90.     //     on our table.
  91.     $entry = &new GtkEntry();
  92.     $table->attach($entry, 0, 1, 0, 1);
  93.     // Creates another table, and places it 
  94.     //     on the existing table.
  95.     $types = &new GtkTable(1, 4);
  96.     $table->attach($types, 0, 1, 1, 2);
  97.     // Creates and groups radio buttons.
  98.     $hex = &new GtkRadioButton(null, 'Hex');
  99.     $dec = &new GtkRadioButton($hex, 'Dec');
  100.     $oct = &new GtkRadioButton($hex, 'Oct');
  101.     $bin = &new GtkRadioButton($hex, 'Bin');
  102.     // Set the 'Decimal' radio as active, an
  103.     //     d set the current type to decimal.
  104.     $dec->set_active(TRUE);
  105.     $current_type = "dec";
  106.     // Connect the radios to the convert fun
  107.     //     ction, and feeds the value to it.
  108.     $hex->connect('pressed', 'convert', 'hex');
  109.     $dec->connect('pressed', 'convert', 'dec');
  110.     $oct->connect('pressed', 'convert', 'oct');
  111.     $bin->connect('pressed', 'convert', 'bin');
  112.     // Place the radios on the table.
  113.     $types->attach($hex, 0, 1, 0, 1);
  114.     $types->attach($dec, 1, 2, 0, 1);
  115.     $types->attach($oct, 2, 3, 0, 1);
  116.     $types->attach($bin, 3, 4, 0, 1);
  117.     // Create tool tips for the widgets and 
  118.     //     enabled them.
  119.     $tthex = &new GtkTooltips();
  120.     $tthex->set_delay(200);
  121.     $tthex->set_tip($hex, 'Convert the number to Hexadecimal.', '');
  122.     $tthex->enable();
  123.     $ttdec = &new GtkTooltips();
  124.     $ttdec->set_delay(200);
  125.     $ttdec->set_tip($dec, 'Convert the number to Decimal.', '');
  126.     $ttdec->enable();
  127.     $ttoct = &new GtkTooltips();
  128.     $ttoct->set_delay(200);
  129.     $ttoct->set_tip($oct, 'Convert the number to Octal.', '');
  130.     $ttoct->enable();
  131.     $ttbin = &new GtkTooltips();
  132.     $ttbin->set_delay(200);
  133.     $ttbin->set_tip($bin, 'Convert the number to Binary.', '');
  134.     $ttbin->enable();
  135.     $ttentry = &new GtkTooltips();
  136.     $ttentry->set_delay(200);
  137.     $ttentry->set_tip($entry, 'Type the number you want to convert here.', '');
  138.     $ttentry->enable();
  139.     // Show the window and all of it's child
  140.     //     widgets.
  141.     $window->show_all();
  142.     // Run the main loop.
  143.     Gtk::main();
  144.     ?>
  145.