Is this page loading slow? Try the mirror site!

Manuel Kiessling's Personal Home Page

Navigate:      Previous | Next | Home | Sitemap | News | Personal | Projects | Topics | Download | External Links | About this page

PHP-Gtk Tutorial

Listing des Quellcodes

Das komplette Listing ist im folgenden abgedruckt, es kann ausserdem hier heruntergeladen werden. Eine Schritt für Schritt Besprechung des Quellcodes folgt auf der nächsten Seite.

<?php

// Startup //////////////////////////////////////////////////////

if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
 dl('php_gtk.dll');
else
 dl('php_gtk.so');



// Init //////////////////////////////////////////////////////

mysql_connect("localhost", "root", "passwort");
mysql_select_db("test");

$verschiebung = 0;



// Support functions //////////////////////////////////////////////////////

function sonderzeichen($string)
{
 $string = str_replace("", "ae", $string);
 $string = str_replace("", "ue", $string);
 $string = str_replace("", "oe", $string);
 $string = str_replace("", "Ae", $string);
 $string = str_replace("", "Ue", $string);
 $string = str_replace("", "Oe", $string);
 $string = str_replace("", "ss", $string);
 $string = str_replace("", "", $string);
 return $string;
}



// GUI init //////////////////////////////////////////////////////

$window = &new GtkWindow();
$window->connect_object('destroy', array('gtk', 'main_quit'));
$window->connect('delete-event', 'delete_event');
$window->set_usize(800, 600);
$window->set_title('GPMNA');
$window->set_name('MainWindow');

$box1 = &new GtkVBox(false, 10);
$window->add($box1);

$table = &new GtkTable(4, 2, false);
$table->set_row_spacing(0,2);
$table->set_col_spacing(0,2);
$box1->pack_start($table, TRUE, TRUE, 0);

$text = &new GtkText (NULL, NULL);
$text->set_editable(FALSE);
$text->set_word_wrap(TRUE);
$table->attach($text, 0, 1, 0, 1,
               GTK_EXPAND | GTK_SHRINK | GTK_FILL,
               GTK_EXPAND | GTK_SHRINK | GTK_FILL,
               0, 0);
$vscrollbar = &new GtkVScrollbar($text->vadj);
$table->attach($vscrollbar, 1, 2, 0, 1,
               GTK_FILL,
               GTK_EXPAND | GTK_SHRINK | GTK_FILL,
               0, 0);

$table2 = &new GtkTable(1, 6, false);
$table2->set_col_spacing(0,0);
$table->attach($table2, 0, 1, 1, 2, 0, 0, 0);

$button = &new GtkButton('Beenden');
$button->connect_object('clicked', array('gtk', 'main_quit'));
$table2->attach($button, 0, 1, 0, 1, 0, 0, 0);

$prevday = &new GtkButton('Einen Tag zurueck');
$prevday->connect_object('clicked', 'lade_prevday');
$table2->attach($prevday, 1, 2, 0, 1, 0, 0, 0, 0);

$nextday = &new GtkButton('Einen Tag vor');
$nextday->connect_object('clicked', 'lade_nextday');
$table2->attach($nextday, 2, 3, 0, 1, 0, 0, 0, 0);

$prevweek = &new GtkButton('Eine Woche zurueck');
$prevweek->connect_object('clicked', 'lade_prevweek');
$table2->attach($prevweek, 3, 4, 0, 1, 0, 0, 0, 0);

$nextweek = &new GtkButton('Eine Woche vor');
$nextweek->connect_object('clicked', 'lade_nextweek');
$table2->attach($nextweek, 4, 5, 0, 1, 0, 0, 0, 0);

$nextweek = &new GtkButton('Loeschen');
$nextweek->connect_object('clicked', 'loesche_eintrag');
$table2->attach($nextweek, 5, 6, 0, 1, 0, 0, 0, 0);

$label = &new GtkLabel('');
$label->set_text("Bereit.");
$table->attach($label, 0, 1, 2, 3, 0, 0, 0);

$listtitles = array("ID", "Ueberschrift", "Rubrik");
$list = &new GtkCList(3, $listtitles);
$list->set_selection_mode(GTK_SELECTION_BROWSE);
$list->set_sort_column(0);
$list->set_auto_sort(TRUE);
$list->connect_object('select-row', 'list_change');
$list->set_column_width(0, 55);
$list->set_column_width(1, 570);
//$list->set_column_width(2, 100);

lade_day(0);

$scrolled_window = &new GtkScrolledWindow();
$scrolled_window->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
$scrolled_window->add_with_viewport($list);

$table->attach($scrolled_window, 0, 1, 3, 4,
               GTK_EXPAND | GTK_SHRINK | GTK_FILL,
               GTK_EXPAND | GTK_SHRINK | GTK_FILL,
               0, 0);


$window->show_all();

Gtk::main();



// Functions connected to Gtk objects //////////////////////////////////////////////////////

function delete_event()
{
 return false;
}

function loesche_eintrag()
{
 GLOBAL $list;
 GLOBAL $verschiebung;
 $row = $list->selection[0];
 $id = $list->get_text($row, 0);

 $result = mysql_query("DELETE from nachrichten WHERE id = '$id'");

 lade_day($verschiebung);

 return false;
}

function list_change()
{
 GLOBAL $list;
 GLOBAL $label;
 $row = $list->selection[0];
 $id = $list->get_text($row, 0);

 $result = mysql_query("SELECT text, datum, zeit, rubrik
                        from nachrichten WHERE id = '$id'");
 $a = mysql_fetch_array($result);
 $string = $a["text"];

 $string = sonderzeichen($string);

 GLOBAL $text;
 $length = $text->get_length();
 $text->backward_delete($length);
 $text->freeze();
 $text->insert(NULL, NULL, NULL, $string, -1);
 $text->thaw();
 $length = $text->get_length();
 $label->set_text("ID $id, Laenge $length, Datum ".$a["datum"].", ".
                   $a["zeit"]." Uhr, Rubrik ".$a["rubrik"]);

 return false;
}

function lade_day($verschiebung)
{
 GLOBAL $list;
 $tag = date("Y-m-d", mktime(0, 0, 0, date("m"),
                             date("d") + $verschiebung,
                             date("Y")));
 $list->clear();
 $list->freeze();
 $result = mysql_query("SELECT id, ueberschrift, rubrik from nachrichten
                        WHERE datum = '$tag'");
 while ($a = mysql_fetch_array($result))
 {
  $a["ueberschrift"] = sonderzeichen($a["ueberschrift"]);
  $thisarray = array($a["id"], $a["ueberschrift"], $a["rubrik"]);
  $list->append($thisarray);
 }
 $list->thaw();
 return false;
}

function lade_prevday()
{
 GLOBAL $verschiebung;
 $verschiebung--;
 lade_day($verschiebung);
 return false;
}

function lade_nextday()
{
 GLOBAL $verschiebung;
 $verschiebung++;
 lade_day($verschiebung);
 return false;
}

function lade_prevweek()
{
 GLOBAL $verschiebung;
 $verschiebung = $verschiebung - 7;
 lade_day($verschiebung);
 return false;
}

function lade_nextweek()
{
 GLOBAL $verschiebung;
 $verschiebung = $verschiebung + 7;
 lade_day($verschiebung);
 return false;
}


?>

Previous | Next

Last updated: $Date: 2001/07/16 12:15:24 $ by MK

Everything on this page is copyleft 2001 Manuel Kiessling unless otherwise stated. You may use it after you read the About section.