Developing gui_php Foundation with PHP

Source: Internet
Author: User
Tags extend gtk php programming php source code

Environment: w2k+php4.3.1+php/gtk0.5.2
A simple Notepad (you can only open files for modification)
<?php
Set_time_limit (0); Set run time

if (!class_exists ("GTK"))//Determine if there is a GTK module
if (Strtoupper substr ($_server["OS", 0, 3)) = = "WIN")
DL ("Php_gtk.dll");
Else
DL ("php_gtk.so");

$window = &new Gtkwindow (); Build a Window
$window->set_uposition (100, 100); Where the window appears
$window->set_usize ((Gdk::screen_width () -200), (Gdk::screen_height ()-150)); Window size
$window->set_title ("WINDOWS"); Set window title
$window->connect_object (' Destroy ', Array (' GTK ', ' main_quit ')); Registering the window's events

$vbox = &new Gtkvbox ();
$hbox = &new Gtkhbox ();
$window->add ($vbox);


$menuBar = &new Gtkmenubar (); Create a Menu
$vbox->pack_start ($menuBar, False, False, 0);

$file = &new Gtkmenuitem ("file");
$menuBar->append ($file);

$fileMenu = &new Gtkmenu ();
$open = &new Gtkmenuitem ("open");
$save = &new Gtkmenuitem ("Save");
$line = &new Gtkmenuitem ();
$line->set_sensitive (TRUE);
$exit = &new Gtkmenuitem ("exit");
$fileMenu->append ($open);
$open->connect_object (' Activate ', ' showfileselection ');
$fileMenu->append ($save);
$save->connect_object (' Activate ', ' savefile ');
$fileMenu->append ($line);
$fileMenu->append ($exit);
$exit->connect_object (' Activate ', Array (' GTK ', ' main_quit '));

$file->set_submenu ($fileMenu);

$scroll = &new Gtkscrolledwindow ();
$scroll->set_border_width (8);
$scroll->set_policy (gtk_policy_automatic, gtk_policy_automatic);
$hbox->pack_start ($scroll, True, true, 0);
$vbox->pack_start ($hbox, True, true, 1);

$text = &new Gtktext ();
$text->set_editable (TRUE);
$text->set_word_wrap (TRUE);
$scroll->add ($text);

function showfileselection ()//File selection functions
{
$file = &new gtkfileselection ("File Selection");
$ok _button = $file->ok_button;
$ok _button->connect (' clicked ', ' OpenFile ', $file);
$ok _button->connect_object (' clicked ', Array ($file, ' destroy '));
$cancel _button = $file->cancel_button;
$cancel _button->connect_object (' clicked ', Array ($file, ' destroy '));
$file->show ();
}

$filePath = null;
function OpenFile ($button, $f)//Open file functions
{
GLOBAL $text, $save, $filePath;
$filePath = $f->get_filename ();
if (Is_file ($filePath))
{
$fp = fopen ($filePath, ' R ');
while (!feof ($FP))
$str. = Fgets ($fp, 1024);
$text->insert (null, NULL, NULL, $STR);
Fclose ($FP);
return $filePath;
}
}

function SaveFile ()//Saving file functions
{
GLOBAL $filePath, $text;
if (Is_file ($filePath))
{
$str = $text->get_chars (0,-1);
$fp = fopen ($filePath, ' w ');
Fwrite ($fp, $STR);
Fclose ($FP);
}
Return
}

$window->show_all (); Display all controls in the body of a window
Gtk::main (); The most important one, not less.
?>

Attached: PHP/GTK configuration

Actual combat PHP/GTK (reprint)

Diao Chan published in 2002-2-19 15:25 PHP programming ← Return layout

PHP was previously thought to be used only to write server-side CGI programs, if PHP can develop a GUI (graphical user interface) program under Windows, do you believe it? Recently, the PHP development team successfully developed PHP bundled with GTK +, so you can develop GUI programs under Windows.

First, the establishment of PHP/GTK operating environment:
In fact, GUI programs and ordinary phpcgi program is no different, but PHP/GTK program by the GTK class to generate GUI interface, they are also open source code, rely on PHP to resolve the establishment of Windows. If you have set up the debugging environment of PHP, then the installation of PHP/GTK environment is relatively simple:
1, download php_gtk.dll (this DLL file to parse the PHP source code inside the GTK), download it to the extension of PHP (extension) directory;
2, download other PHP/GTK DLL files (altogether 6), unzip them to the Windows System32 directory;
3, open php.ini, in the Extended Settings section "Windows Extensions" Add the "Extension=php_gtk.dll" statement, we recommend that the first backup php.ini, to avoid the failure to modify the establishment of a good PHP operating environment void;
Now it's time to set up the PHP/GTK's operating environment. Of course, you can also add "DL (' Php_gtk.dll ')" to dynamically load GTK support instead of the second step in the first line of each PHP/GTK program source code.
If you haven't built a PHP run environment yet, it's easier to install:
1, download the entire PHP/GTK bundle support package, and then unzip them to the C-disk PHP4 directory below;
2, copy the DLL file in the Winnt directory to the Windows system32 directory, and copy the php.ini file to the Windows directory;
After the PHP/GTK is established, you can run a PHP/GTK program using command-line php: Enter "C:\php4\php-q Gtkprogrampath" in the runtime, where "c:\php4\php" is changed to PHP.EXE path, " Gtkprogrampath "is the path to the PHP/GTK program. For example, "C:\php4\php-q c:\php4\samples\hello.php" will run the example "Hello World" program that accompanies the PHP/GTK run package.

Second, compile the PHP/GTK program:
If you make a PHP/GTK application, you need the other computer to build PHP's operating environment and use the PHP command-line mode to parse before it can be executed, which is too complicated. So how can you compile a PHP/GTK program? After a search, the author found phpcompiler this software.
Phpcompiler by Www.deskcode.com Development (Http://www.deskcode.com/phpcompiler), PHP can be compiled into EXE executable file, built-in PHP support, if you want to compile a php/ GTK program, you must establish a PHP/GTK running environment (refer to the previous steps). The steps to compile a PHP/GTK program are as follows:
1, open Phpcompiler (as pictured), in the "Script to" select the PHP program to compile the source code, in the "destination" to choose the compiler after the successful EXE file path;
2, click the "Compile" button, will pop up a dialog box to ask the use of the compilation mode (if the PHP/GTK program on the choice of No, is a simple phpcgi program to choose Yes);
3, and then will pop up a dialog box asking whether to copy the Php4ts.dll file to the compiled EXE file directory, select Yes.
After the steps above, a PHP/GTK program compiles successfully, but there are several points to note for compiling the PHP/GTK program:
1, after compiling the executable file if necessary, the PC must have a GTK running environment required several DLL files (that is, the previous download of the several), if you need to make an application, You can copy several DLL files to the System32 directory at installation time (but this is not a "green software" ^_^).
2, Phpcompiler itself for the support of PHP is very good, but some people have tried to extend the support of the function, such as "Gzopen" and so on, before compiling a good, after the compilation is not used. In fact, after compiling the executable file, the equivalent of only the default PHP support (even GTK support is not available), so if you applied in the program any need to extend the support of the function, you must dynamically load the supporting function of the DLL file, such as "DL (' Php_gtk.dll ')", This will not make a mistake when the compilation is complete.
3, run a compiled executable file, will first pop up a DOS window, and then automatically shut down, because each GUI window is through the GTK to "draw" out, so must have that window.
4, perhaps the previous PHP can not be said to be the real OOP (object-oriented program) language, but to the PHP/GTK, any window by the GTK object to "draw", if there is no perfect OOP skills, it is difficult to write GUI programs.

Third, PHP/GTK resources:
1, the official website of Http://gtk.php.net:PHP/GTK, although it is the official website, but it is very simple, out of a FAQ and mailing list, can hardly find anything useful,
2, http://www.phpgtk.com: A Better Interface PHP/GTK website, has the latest version information.
3, Http://developer.gnome.org/doc/API/gtk/gtkobjects.html:PHP/GTK function and class of the encyclopedia/manual site, which has very rich php/gtk information.
4, http://www.phpuk.org/gtk/: Unofficial version of the GTK manual website, easy to understand.

If you think that E is more ugly, you can also come to zphp.com download the latest PHP/GTK operating environment and support packages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.