| View previous topic :: View next topic |
| Author |
Message |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Fri Jan 20, 2006 3:32 pm Post subject: Guidance required on php-gtk2 and glade application |
|
|
Hi Christian,
I need a bit of basic guidance, now that I have figured out how to get the various bits to work.
I so far have three files:
main.php
exp.class.php
glade/mainform.glade
1. In "exp.class.php" would you say this is the right to setup the window and functions associated with it?
2. I did start out with a separate logon form but kept getting memory errors on exit. Should the logon and main application be on the same window?
Thank you very much for all your help.
the code for these are:
main.php
| Code: | <?php
// main.php
if( !class_exists('gtk')) {
die('Please load the php-gtk2 module in your php.ini' . "\r\n");
}
require_once("exp.class.php");
//require_once("setup.php");
//require_once("setup_db.php");
//require_once("db.class.php");
//require_once("logon.class.php");
//require_once("main.class.php");
//require_once("item.class.php");
$exp = new offline_expenses();
gtk::main();
?> |
exp.class.php
| Code: |
<?php
// exp.class.php
class offline_expenses
{
protected $notebook;
protected $treeviewInfo;
protected $strLastClass = null;
protected static $strTitle = 'Expenses System';
public function offline_expenses()
{
$this->glade = & new GladeXML('glade/mainform.glade');
$this->buildGui();
$this->win->show_all();
}//public function __construct()
protected function buildGui()
{
$this->win = $this->glade->get_widget("window_main");
$this->win->set_title(offline_expenses::$strTitle);
$this->win->resize(600,500);
$this->win->set_position(3);
$this->win->connect_simple('destroy', array('gtk', 'main_quit'));
$this->quit = $this->glade->get_widget("button_quit");
$this->quit->connect_simple('clicked', array('gtk', 'main_quit'));
$this->add = $this->glade->get_widget("button_add");
$this->add->connect_simple('clicked', array($this, 'addItem'));
}
public function addItem()
{
echo ("hello\n");
}
}
?> |
glade/mainform.glade
| Code: | <?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="window_main">
<property name="border_width">10</property>
<property name="visible">True</property>
<property name="title" translatable="yes">Expenses</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_CENTER</property>
<property name="modal">False</property>
<property name="default_width">300</property>
<property name="default_height">200</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<signal name="destroy" handler="gtk_main_quit" last_modification_time="Thu, 12 Jan 2006 13:50:52 GMT"/>
<child>
<widget class="GtkHBox" id="hbox31">
<property name="width_request">245</property>
<property name="height_request">181</property>
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkVBox" id="vbox4">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkLabel" id="label74">
<property name="visible">True</property>
<property name="label" translatable="yes"><big><b>Expense Items</b></big></property>
<property name="use_underline">False</property>
<property name="use_markup">True</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkNotebook" id="notebook1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="show_tabs">True</property>
<property name="show_border">True</property>
<property name="tab_pos">GTK_POS_TOP</property>
<property name="scrollable">False</property>
<property name="enable_popup">False</property>
<child>
<widget class="GtkScrolledWindow" id="scrolledwindow3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
<property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
<property name="shadow_type">GTK_SHADOW_IN</property>
<property name="window_placement">GTK_CORNER_TOP_LEFT</property>
<child>
<widget class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="headers_visible">True</property>
<property name="rules_hint">False</property>
<property name="reorderable">False</property>
<property name="enable_search">True</property>
<property name="fixed_height_mode">False</property>
<property name="hover_selection">False</property>
<property name="hover_expand">False</property>
</widget>
</child>
</widget>
<packing>
<property name="tab_expand">False</property>
<property name="tab_fill">True</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label76">
<property name="visible">True</property>
<property name="label" translatable="yes">Unclaimed Items</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="type">tab</property>
</packing>
</child>
<child>
<widget class="GtkScrolledWindow" id="scrolledwindow2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
<property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
<property name="shadow_type">GTK_SHADOW_IN</property>
<property name="window_placement">GTK_CORNER_TOP_LEFT</property>
<child>
<placeholder/>
</child>
</widget>
<packing>
<property name="tab_expand">False</property>
<property name="tab_fill">True</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="label77">
<property name="visible">True</property>
<property name="label" translatable="yes">Claims</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="type">tab</property>
</packing>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<widget class="GtkHButtonBox" id="hbuttonbox4">
<property name="visible">True</property>
<property name="layout_style">GTK_BUTTONBOX_END</property>
<property name="spacing">5</property>
<child>
<widget class="GtkButton" id="button_add">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-add</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_add" after="yes" last_modification_time="Tue, 17 Jan 2006 13:51:28 GMT"/>
</widget>
</child>
<child>
<widget class="GtkButton" id="button_edit">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-edit</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_edit" after="yes" last_modification_time="Tue, 17 Jan 2006 14:14:34 GMT"/>
</widget>
</child>
<child>
<widget class="GtkButton" id="button_quit">
<property name="visible">True</property>
<property name="can_default">True</property>
<property name="can_focus">True</property>
<property name="label">gtk-quit</property>
<property name="use_stock">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_button_quit" last_modification_time="Thu, 12 Jan 2006 16:14:49 GMT"/>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkStatusbar" id="statusbar2">
<property name="visible">True</property>
<property name="has_resize_grip">True</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
|
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 1:25 pm Post subject: Re: Guidance required on php-gtk2 and glade application |
|
|
| kingrs wrote: | 1. In "exp.class.php" would you say this is the right to setup the window and functions associated with it?
2. I did start out with a separate logon form but kept getting memory errors on exit. Should the logon and main application be on the same window?
|
1. Why not? Having separate files for each class is a Good Thing. But remember that you should not use the ampersand "&" when instantiating anything. That was needed in PHP4, but not in PHP5. It's actually a bad thing using these with php5.
2. Why not separate? What memory errors? Maybe it's a bug in php-gtk2. Could you post them? And remember, you can have one single glade file with multiple windows in it AND can use the second (or third?) glade parameter to load only one from the file. That saves memory and you don't have problems with multiple windows occuring at once. |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 2:13 pm Post subject: |
|
|
Hi Christian,
Thanks for the feedback.
1. OK, I will not use these ampersands anymore.
2. I will post the code next time I get a memory error.
I did start out with one file with all the windows in that but I could not figure out how to show each window seperately.
I will try it again and post the code if I cannot get it to work.
Also I have decided to try and follow the coding guidlines set out by PEAR, this seems like good practice, its just a case of remembering all the upper-lower case conditions for functions, classes, variables etc.
Cheers
Rob |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 2:33 pm Post subject: |
|
|
If I use
| Code: | | $this->glade = new GladeXML('main.glade'); |
then is shows all windows.
If specify second parameter (root node):
| Code: | | $this->glade = new GladeXML('main.glade','window_main'); |
then only window_main is displayed.
Can I then use $this->glade to display the "window_logon" window which is in the same glade file?[/code] |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 2:46 pm Post subject: |
|
|
| kingrs wrote: | | Can I then use $this->glade to display the "window_logon" window which is in the same glade file? |
No. The $this->glade object contains only window_login and subwidgets. |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 2:54 pm Post subject: |
|
|
So are you saying if you use more than one window in one glade file you need to have a seperate GladeXML instance
example
| Code: |
$this->glade_main = new GladeXML('main.glade','window_main');
$this->glade_logon = new GladeXML('main.glade','window_logon');
|
Thats OK, but it would make more sense to have just one instance on the GladeXML. |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 3:13 pm Post subject: |
|
|
| kingrs wrote: | | So are you saying if you use more than one window in one glade file you need to have a seperate GladeXML instance. Thats OK, but it would make more sense to have just one instance on the GladeXML. |
You can use more windows with one single instance, but you have to set the windows to "hidden". If not, all will show when loading the file. Loading only a part of the file saves memory, if you don't need all of it. |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 6:01 pm Post subject: |
|
|
Yes that seems to work
| Code: |
$this->glade = new GladeXML('main.glade');
$this->win = $this->glade->get_widget('window_main');
$this->winlogon = $this->glade->get_widget("window_logon");
$this->winlogon->set_property('visible',false);
|
Next step is to get at the buttons.
Using the "window_main" widget I have arrived at this code to get the button, bit complex though:
| Code: |
$this->add = glade::get_widget_tree($this->win)->get_widget("button_add");
|
Then add the signal connect
| Code: |
$this->add->connect_simple('clicked', array($this, 'addItem'));
|
Now I can get at the button using GladeXML object and "get_widget" method but what happens if you have two buttons on different windows with the same name?
Answer:
Just tested it, the first widget it finds which could be the wrong one. |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 7:02 pm Post subject: |
|
|
| kingrs wrote: | | but what happens if you have two buttons on different windows with the same name? |
There may not be two widgets with the same name. It may work, but it's not a good thing. |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 8:57 pm Post subject: |
|
|
So given that I have the window widget called "$this->win", can this code be simplified?
| Code: |
$this->button_Add = glade::get_widget_tree($this->win)->get_widget("button_add");
|
Failing that I could assign a variable to the widget tree and then use that for all the get_widget methods for each widget I need to use.
Example:
| Code: |
$this->main_widget_tree = glade::get_widget_tree($this->win);
$this->button_Add = $this->main_widget_tree->get_widget("button_add");
$this->button_Edit = $this->main_widget_tree->get_widget("button_edit");
|
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 9:04 pm Post subject: |
|
|
| kingrs wrote: | | Failing that I could assign a variable to the widget tree and then use that for all the get_widget methods for each widget I need to use. |
You can't assign functions/methods to variables as you can do that in Javascript.
What I do is making an array with all widgets I want to assign to class variables and iterate over them via a foreach loop.
Another way to load widgets on demand would be to use PHP5's new magic __get method and try to load a widget with the given name. |
|
| Back to top |
|
 |
kingrs
Joined: 15 Jan 2006 Posts: 30 Location: Stafford, UK
|
Posted: Sun Jan 22, 2006 9:16 pm Post subject: |
|
|
Sorry I thought that the variable $this->main_widget_tree would contain the widget tree not a function.
So its back to using the get_widget using GladeXML variable?
| Code: | $this->glade = new GladeXML('main.glade');
$this->button_add = $this->glade->get_widget('button_add');
|
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jan 22, 2006 9:32 pm Post subject: |
|
|
| kingrs wrote: | | So its back to using the get_widget using GladeXML variable? |
Yep. |
|
| Back to top |
|
 |
beidlerj
Joined: 10 Feb 2006 Posts: 55 Location: Oregon, USA
|
Posted: Wed Feb 15, 2006 9:08 pm Post subject: |
|
|
Is there a way to get Glade to use the connect_simple method instead of connect() when you use GladeXML::signal_autoconnect()? If you don't need access to the widget and event, do you have to code all your connections by hand in your script, instead of specifying them in Glade?
Also, how do you pass parameters using Glade? I see spots for Signal and Handler, but no user_data, as appears in the definition. |
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Thu Feb 16, 2006 9:18 am Post subject: |
|
|
| beidlerj wrote: | | Is there a way to get Glade to use the connect_simple method instead of connect() when you use GladeXML::signal_autoconnect()? |
Not as far as I know.
| Quote: | | If you don't need access to the widget and event, do you have to code all your connections by hand in your script, instead of specifying them in Glade? |
Seems so.
| Quote: | | Also, how do you pass parameters using Glade? I see spots for Signal and Handler, but no user_data, as appears in the definition. |
As the parameters would be passed to all functions (what is not wished in most cases), it's not possible. |
|
| Back to top |
|
 |
|