From 69b8f446c414290ec0c3488dbee4a5c1476b68a5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 2 Feb 2012 22:18:50 -0500 Subject: [PATCH] Database connections now implemented with GtkListStore/GtkTreeView --- src/common/settings.php | 11 ++++++++ src/windows/main.php | 62 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/common/settings.php b/src/common/settings.php index 03bfdcd..fabd93b 100644 --- a/src/common/settings.php +++ b/src/common/settings.php @@ -127,5 +127,16 @@ class Settings { unset($this->current->dbs->{$name}); } + // -------------------------------------------------------------------------- + + /** + * Retreive all db connections + * + * @return array + */ + function get_dbs() + { + return $this->current->dbs; + } } // End of settings.php \ No newline at end of file diff --git a/src/windows/main.php b/src/windows/main.php index 8d34d7f..3d434b0 100644 --- a/src/windows/main.php +++ b/src/windows/main.php @@ -19,7 +19,7 @@ */ class Main extends GtkWindow { - private $main_vbox, $main_hbox; + private $settings; /** * Create and display the main window on startup @@ -31,6 +31,9 @@ class Main extends GtkWindow { //Resize to a sane size $this->resize(640, 480); + $this->set_position(Gtk::WIN_POS_CENTER); + $this->settings = new Settings(); + //Layout the interface $this->_main_layout(); } @@ -179,7 +182,6 @@ class Main extends GtkWindow { $dblabel = new GtkLabel('Database Connections'); $dblabel->set_alignment(0,0); - $add_button = new GtkButton(); $add_button->set_label("New Connnection"); $add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_ADD, Gtk::ICON_SIZE_SMALL_TOOLBAR)); @@ -188,12 +190,66 @@ class Main extends GtkWindow { $conn_vbox = new GtkVBox(); - $conn_vbox->pack_start($dblabel, FALSE); + // Treeview to show database connections + { + // Create a Storage object for connection list + $model = new GtkListStore(GObject::TYPE_PHP_VALUE, GObject::TYPE_STRING); + + // Add the existing connections to the model + $db_conns = $this->settings->get_dbs(); + if( ! empty($db_conns)) + { + foreach($db_conns as $name => $props) + { + $db = $props; + $db['name'] = $name; + + $iter = $model->append(); + $model->set($iter, 0, $db); + } + } + + // Initialize the treeview with the data + $treeview = new GtkTreeView($model); + + $cell_renderer = new GtkCellRendererText(); + $treeview->insert_column_with_data_func(-1, 'Database Connections', $cell_renderer, array(&$this, 'set_label')); + + $selection = $treeview->get_selection(); + $selection->set_mode(GTK::SELECTION_SINGLE); + } + + + $conn_vbox->pack_start($treeview); $conn_vbox->pack_start($add_button, FALSE); return $conn_vbox; } + /** + * Sets the label of the current db connection + * + * @param GtkTreeViewColumn $col + * @param GtkCellRenderer $cell + * @param GtkTreeModel $model + * @param GtkTreeIter $iter + */ + function set_label($col, $cell, $model, $iter) + { + $info = $model->get_value($iter, 0); + $cell->set_property('text', $info['name']); + } + + /** + * Redraws the data area based on the which connection is selected + * + * @param $selection + */ + private function _render_selected($selection) + { + + } + // -------------------------------------------------------------------------- /**