mirror of
https://github.com/php/doc-gtk.git
synced 2026-03-25 01:22:22 +01:00
116 lines
3.5 KiB
Plaintext
116 lines
3.5 KiB
Plaintext
<?php
|
|
/**
|
|
* Here we create a login window.
|
|
* It has a username and a password field, and a
|
|
* Cancel and Login button. Some error checking
|
|
* is being done when the user clicks "Login".
|
|
*/
|
|
|
|
if (!class_exists('gtk')) {
|
|
die("Please load the php-gtk2 module in your php.ini\r\n");
|
|
}
|
|
|
|
/**
|
|
* This function gets called as soon as the user
|
|
* clicks on the Login button.
|
|
*
|
|
* @param GtkWindow $wnd The login window, needed to close it
|
|
* when all is ok
|
|
* @param GtkEntry $txtUsername The username text field, used to get
|
|
* the username
|
|
* @param GtkEntry $txtPassword The password widget to retrieve the
|
|
* password
|
|
*/
|
|
function login(GtkWindow $wnd, GtkEntry $txtUsername, GtkEntry $txtPassword)
|
|
{
|
|
//fetch the values from the widgets into variables
|
|
$strUsername = $txtUsername->get_text();
|
|
$strPassword = $txtPassword->get_text();
|
|
|
|
//Do some error checking
|
|
$errors = null;
|
|
if (strlen($strUsername) == 0) {
|
|
$errors .= "Username is missing.\r\n";
|
|
}
|
|
if (strlen($strPassword) == 0) {
|
|
$errors .= "No password given.\r\n";
|
|
}
|
|
|
|
if ($errors !== null) {
|
|
//There was at least one error.
|
|
//We show a message box with the errors
|
|
$dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL,
|
|
Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors);
|
|
$dialog->set_markup(
|
|
"The following errors occured:\r\n"
|
|
. "<span foreground='red'>" . $errors . "</span>"
|
|
);
|
|
$dialog->run();
|
|
$dialog->destroy();
|
|
} else {
|
|
//No error. You would need to hide the dialog now
|
|
//instead of destroying it (because when you destroy it,
|
|
//Gtk::main_quit() gets called) and show the main window
|
|
$wnd->destroy();
|
|
}
|
|
}
|
|
|
|
//Create the login window
|
|
$wnd = new GtkWindow();
|
|
$wnd->set_title('Login');
|
|
//Close the main loop when the window is destroyed
|
|
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
|
|
|
|
|
|
//Set up all the widgets we need
|
|
$lblCredit = new GtkLabel('Please provide your data');
|
|
//The second parameter says that the underscore should be parsed as underline
|
|
$lblUsername = new GtkLabel('_Username', true);
|
|
$lblPassword = new GtkLabel('_Password', true);
|
|
$txtUsername = new GtkEntry();
|
|
$txtPassword = new GtkEntry();
|
|
$btnLogin = new GtkButton('_Login');
|
|
$btnCancel = new GtkButton('_Cancel');
|
|
|
|
|
|
//Which widget should be activated when the
|
|
// mnemonic (Alt+U or Alt+P) is pressed?
|
|
$lblUsername->set_mnemonic_widget($txtUsername);
|
|
$lblPassword->set_mnemonic_widget($txtPassword);
|
|
//Hide the password
|
|
//$txtPassword->set_invisible_char('*');
|
|
|
|
//Destroy the window when the user clicks Cancel
|
|
$btnCancel->connect_simple('clicked', array($wnd, 'destroy'));
|
|
//Call the login function when the user clicks on Login
|
|
$btnLogin->connect_simple('clicked', 'login', $wnd, $txtUsername, $txtPassword);
|
|
|
|
|
|
//Lay out all the widgets in the table
|
|
$tbl = new GtkTable(3, 2);
|
|
$tbl->attach($lblCredit, 0, 2, 0, 1);
|
|
$tbl->attach($lblUsername, 0, 1, 1, 2);
|
|
$tbl->attach($txtUsername, 1, 2, 1, 2);
|
|
$tbl->attach($lblPassword, 0, 1, 2, 3);
|
|
$tbl->attach($txtPassword, 1, 2, 2, 3);
|
|
|
|
|
|
//Add the buttons to a button box
|
|
$bbox = new GtkHButtonBox();
|
|
$bbox->set_layout(Gtk::BUTTONBOX_EDGE);
|
|
$bbox->add($btnCancel);
|
|
$bbox->add($btnLogin);
|
|
|
|
|
|
//Add the table and the button box to a vbox
|
|
$vbox = new GtkVBox();
|
|
$vbox->pack_start($tbl);
|
|
$vbox->pack_start($bbox);
|
|
|
|
//Add the vbox to the window
|
|
$wnd->add($vbox);
|
|
//Show all widgets
|
|
$wnd->show_all();
|
|
//Start the main loop
|
|
Gtk::main();
|
|
?> |