Application Template 01 - Multiple Modules in Multiple Windows

This application template provide you with a base for setting up an application with four modules, each running in its own window.

You can switch between the modules by clicking the button at the bottom of the window.

 

Note

Please take a look at How to run multiple applications in multiple windows - Part 1? which explains in detail the method we use in this template.

The Code - Part 1

The codes below produces exactly the same output as How to run multiple applications in multiple windows - Part 1?. The only difference is that it's re-written using classes.

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
41   
42   
43   
44   
45   
46   
47   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
79   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
<?php

class App {

    function App() {
        $this->modules = array("sales", "purchase", "inventory", "finance"); // names of the modules
    }

    function main() {
        foreach($this->modules as $module) {
            $this->apps[$module] = new $module($this);
        }
        $this->apps['sales']->dialog->show_all(); // show the first module
        $this->apps['sales']->dialog->run(); // let's go!
    }

    // process button click
    function on_clicked($button, $activated_module) {
        print "button_clicked: $activated_module\n";
        $this->apps[$activated_module]->dialog->show_all(); // show the activated module
        foreach($this->modules as $module) {
            if ($module!=$activated_module) $this->apps[$module]->dialog->hide_all(); // hide all others
        }
        $this->apps[$activated_module]->dialog->run();
    }
}

class base_module {

    function base_module($obj) {
        $this->main = $obj; // keep a copy of the module names
        $module = get_class($this);
        print "base_module::module = $module\n";
        $dialog = new GtkDialog($module, null, Gtk::DIALOG_MODAL);
        $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
        $dialog->set_size_request(400, 200);
        $top_area = $dialog->vbox;

        // run the setup for each module
        $this->setup($top_area);

        $top_area->pack_start(new GtkLabel()); // used to "force" the button to stay at bottom
        $this->show_buttons($top_area); // shows the buttons at bottom of windows
        $dialog->set_has_separator(false);
        $this->dialog = $dialog; // keep a copy of the dialog ID
    }

    // shows the buttons at bottom of windows
    function show_buttons($vbox) {
        global $modules;
        $hbox = new GtkHBox();
        $vbox->pack_start($hbox, 0, 0);
        $hbox->pack_start(new GtkLabel());
        foreach($this->main->modules as $module) {
            $button = new GtkButton(strtoupper(substr($module,0,1)).substr($module,1)); // cap 1st letter
            $button->set_size_request(80, 32); // makes all button the same size
            if ($module == get_class($this)) { // sets the color of the respective button
                $button->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#95DDFF"));
                $button->modify_bg(Gtk::STATE_ACTIVE, GdkColor::parse("#95DDFF"));
                $button->modify_bg(Gtk::STATE_PRELIGHT, GdkColor::parse("#95DDFF"));
            }
            $hbox->pack_start($button, 0, 0);
            $hbox->pack_start(new GtkLabel());
            $button->connect('clicked', array(&$this->main,'on_clicked'), $module); // event handler to handle button click
        }
    }
}

class sales extends base_module {
    function setup($vbox) {
        $vbox->pack_start(new GtkLabel(get_class($this)));
    }
}

class purchase extends base_module {
    function setup($vbox) {
        $vbox->pack_start(new GtkLabel(get_class($this)));
    }
}

class inventory extends base_module {
    function setup($vbox) {
        $vbox->pack_start(new GtkLabel(get_class($this)));
    }
}

class finance extends base_module {
    function setup($vbox) {
        $vbox->pack_start(new GtkLabel(get_class($this)));
    }
}


$app = new App();
$app->main();

?>
 

The Code - Part 2

The codes below is exactly the same as Part 1, except that we put some sample contents in each module.

For your reference, the contents for each module are directly taken from:

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
40   
41   
42   
43   
44   
45   
46   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
75   
76   
77   
78   
80   
81   
82   
83   
84   
85   
86   
87   
88   
89   
90   
91   
92   
93   
94   
95   
96   
97   
98   
99   
100   
101   
102   
103   
104   
105   
106   
107   
108   
109   
110   
111   
112   
113   
114   
115   
116   
117   
118   
119   
120   
121   
122   
123   
124   
125   
126   
127   
128   
129   
130   
131   
132   
133   
134   
135   
136   
137   
138   
139   
140   
141   
142   
143   
144   
145   
146   
147   
148   
149   
150   
151   
152   
153   
154   
155   
156   
157   
158   
159   
160   
161   
162   
163   
164   
165   
166   
167   
168   
169   
170   
171   
172   
173   
174   
175   
176   
177   
178   
179   
180   
181   
182   
183   
184   
185   
186   
187   
188   
189   
190   
191   
192   
193   
194   
195   
196   
197   
198   
199   
200   
201   
202   
203   
204   
205   
206   
207   
208   
209   
210   
211   
212   
213   
214   
215   
216   
217   
218   
219   
220   
221   
222   
223   
224   
225   
226   
227   
228   
229   
230   
231   
232   
233   
234   
235   
236   
237   
238   
239   
240   
241   
242   
243   
244   
245   
246   
247   
248   
249   
250   
251   
252   
253   
254   
255   
256   
257   
258   
259   
260   
261   
262   
263   
264   
265   
266   
267   
268   
269   
270   
271   
272   
273   
274   
275   
276   
277   
278   
279   
280   
281   
282   
283   
284   
285   
286   
287   
<?php

class App {

    function App() {
        $this->modules = array("sales", "purchase", "inventory", "finance"); // names of the modules
    }

    function main() {
        foreach($this->modules as $module) {
            $this->apps[$module] = new $module($this);
        }
        $this->apps['sales']->dialog->show_all(); // show the first module
        $this->apps['sales']->dialog->run(); // let's go!
    }

    // process button click
    function on_clicked($button, $activated_module) {
        print "button_clicked: $activated_module\n";
        $this->apps[$activated_module]->dialog->show_all(); // show the activated module
        foreach($this->modules as $module) {
            if ($module!=$activated_module) $this->apps[$module]->dialog->hide_all(); // hide all others
        }
        $this->apps[$activated_module]->dialog->run();
    }
}

class base_module {

    function base_module($obj) {
        $this->main = $obj; // keep a copy of the module names
        $module = get_class($this);
        print "base_module::module = $module\n";
        $dialog = new GtkDialog($module, null, Gtk::DIALOG_MODAL);
        $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
        $dialog->set_size_request(400, 250);
        $top_area = $dialog->vbox;

        // run the setup for each module
        $this->setup($top_area);

        $top_area->pack_start(new GtkLabel()); // used to "force" the button to stay at bottom
        $this->show_buttons($top_area); // shows the buttons at bottom of windows
        $dialog->set_has_separator(false);
        $this->dialog = $dialog; // keep a copy of the dialog ID
    }

    // shows the buttons at bottom of windows
    function show_buttons($vbox) {
        global $modules;
        $hbox = new GtkHBox();
        $vbox->pack_start($hbox, 0, 0);
        $hbox->pack_start(new GtkLabel());
        foreach($this->main->modules as $module) {
            $button = new GtkButton(strtoupper(substr($module,0,1)).substr($module,1)); // cap 1st letter
            $button->set_size_request(80, 32); // makes all button the same size
            if ($module == get_class($this)) { // sets the color of the respective button
                $button->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#95DDFF"));
                $button->modify_bg(Gtk::STATE_ACTIVE, GdkColor::parse("#95DDFF"));
                $button->modify_bg(Gtk::STATE_PRELIGHT, GdkColor::parse("#95DDFF"));
            }
            $hbox->pack_start($button, 0, 0);
            $hbox->pack_start(new GtkLabel());
            $button->connect('clicked', array(&$this->main,'on_clicked'), $module); // event handler to handle button click
        }
    }
}

class sales extends base_module {
    function setup($vbox) {
        // display title
        $title = new GtkLabel("Display 2D Array in GtkTreeView - Part 5\n                       get user selection");
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request(-1, 40);
        $vbox->pack_start($title, 0, 0);

        $vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap between the title and scroll_win

        // Set up a scroll window
        $scrolled_win = &new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
        $vbox->pack_start($scrolled_win);

        // the 2D table
        $data = array(
        array('row0', 'item 19', 2, 3.1),
        array('row1', 'item 16', 20, 6.21),
        array('row2', 'item 13', 8, 9.36),
        array('row3', 'item 10', 11, 12.4),
        array('row4', 'item 7', 5, 15.5),
        array('row5', 'item 4', 17, 18.6),
        array('row6', 'item 3', 20, 21.73));

        $this->display_table($scrolled_win, $data);
    }

    function display_table($scrolled_win, $data) {
        // Creates the list store
        $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
        Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
        $field_header = array('Row #', 'Description', 'Qty', 'Price');
        $field_justification = array(0, 0, 0.5, 1);

        // Creates the view to display the list store
        $view = new GtkTreeView($model);
        $scrolled_win->add($view);

        // Creates the columns
        for ($col=0; $col<count($field_header); ++$col) {
            $cell_renderer = new GtkCellRendererText();
            $cell_renderer->set_property("xalign", $field_justification[$col]);
            $column = new GtkTreeViewColumn($field_header[$col], $cell_renderer, 'text', $col);
            $column->set_alignment($field_justification[$col]);
            $column->set_sort_column_id($col);

            // set the header font and color
            $label = new GtkLabel($field_header[$col]);
            $label->modify_font(new PangoFontDescription("Arial Bold"));
            $label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
            $column->set_widget($label);
            $label->show();

            // setup self-defined function to display alternate row color
            $column->set_cell_data_func($cell_renderer, array(&$this, "format_col"), $col);
            $view->append_column($column);
        }

        // pupulates the data
        for ($row=0; $row<count($data); ++$row) {
            $values = array();
            for ($col=0; $col<count($data[$row]); ++$col) {
                $values[] = $data[$row][$col];
            }
            $model->append($values);
        }

        $selection = $view->get_selection();
        $selection->connect('changed', array(&$this, 'on_selection'));
    }

    function format_col($column, $cell, $model, $iter, $col_num) {
        $path = $model->get_path($iter);
        $row_num = $path[0];
        if ($col_num==3) {
            $amt = $model->get_value($iter, 3);
            $cell->set_property('text', '$'.number_format($amt,2));
        }
        $row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
        $cell->set_property('cell-background', $row_color);
    }

    function on_selection($selection) {
        list($model, $iter) = $selection->get_selected();
        $desc = $model->get_value($iter, 1);
        $qty = $model->get_value($iter, 2);
        $price = $model->get_value($iter, 3);
        print "You have selected $desc: $qty ($price)\n";
    }
}

class purchase extends base_module {
    function setup($vbox) {
        // display title
        $title = new GtkLabel("Setup and read value from ComboBoxEntry");
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request(-1, 60);
        $vbox->pack_start($title, 0, 0);

        // the selection
        $list = array('item 1', 'item 2', 'item 3', 'item 4');

        $vbox->pack_start($hbox=new GtkHBox(), 0, 0);
        $hbox->pack_start(new GtkLabel('Select: '), 0, 0);

        // Create a new comboboxentry and populates it
        $combobox = &GtkComboBoxEntry::new_text();
        foreach($list as $choice) {
            $combobox->append_text($choice);
        }
        $combobox->get_child()->set_text('');
        $hbox->pack_start($combobox, 0, 0);

        // Set up the submit butotn
        $hbox->pack_start($button = new GtkLabel('  '), 0, 0);
        $hbox->pack_start($button = new GtkButton('Submit'), 0, 0);
        $button->set_size_request(60, 24);
        $button->connect('clicked', array(&$this, "on_button"), $combobox);
    }

    function on_button($button, $combobox) {
        $selection = $combobox->get_child()->get_text();
        print "You have selected: $selection\n";
    }
}

class inventory extends base_module {
    function setup($vbox) {
        // display title
        $title = new GtkLabel("Default Button Action");
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request(-1, 40);
        $vbox->pack_start($title, 0, 0);

        $hbox = new GtkHBox();
        $vbox->pack_start($hbox, 0, 0);
        $hbox->pack_start(new GtkLabel("Keyword: "), 0, 0);
        $hbox->pack_start($entry = new GtkEntry(), 0, 0);
        $hbox->pack_start($button = new GtkButton("Search"), 0, 0);

        $entry->connect('activate', array(&$this,'on_enter'), $button);
        $button->connect('clicked', array(&$this,'on_click'), $entry);
    }

    function on_enter($entry, $button) {
        $keyword = $entry->get_text();
        echo "Enter pressed. keyword = $keyword\n";
        $button->clicked();
    }

    function on_click($button, $entry) {
        $keyword = $entry->get_text();
        echo "button clicked. keyword = $keyword\n";
    }
}

class finance extends base_module {
    function setup($vbox) {
        // display title
        $title = new GtkLabel("Display Alert - Part 1");
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request(-1, 40);
        $vbox->pack_start($title, 0, 0);
        $vbox->pack_start(new GtkLabel(), 0, 0); // add a small gap

        // setup the entry field
        $hbox = new GtkHBox();
        $vbox->pack_start($hbox, 0, 0);
        $hbox->pack_start(new GtkLabel("Please enter your name:"), 0, 0);
        $name = new GtkEntry();
        $hbox->pack_start($name, 0, 0);
        $name->connect('activate', array(&$this,'on_activate'));
    }

    function on_activate($widget) {
        $input = $widget->get_text();
        echo "name = $input\n";
        if ($input=='') $this->alert("Please enter your name!");
        $widget->grab_focus();
    }

    // display popup alert box
    function alert($msg) {
        $dialog = new GtkDialog('Alert', null, Gtk::DIALOG_MODAL);
        $dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
        $top_area = $dialog->vbox;
        $top_area->pack_start($hbox = new GtkHBox());
        $stock = GtkImage::new_from_stock(Gtk::STOCK_DIALOG_WARNING,
        Gtk::ICON_SIZE_DIALOG);
        $hbox->pack_start($stock, 0, 0);
        $hbox->pack_start(new GtkLabel($msg));
        $dialog->add_button(Gtk::STOCK_OK, Gtk::RESPONSE_OK);
        $dialog->set_has_separator(false);
        $dialog->show_all();
        $dialog->run();
        $dialog->destroy();
    }

}


$app = new App();
$app->main();

?>

Note

Note that in this version, if you close the window in any of the four modules, the entire application will exit. We will fix this, add also add a main menu in Application Template 02 - multiple modules in multiple window + main menu.

Related Articles

Search This Site

Google
Web This Site

Search PHP-GTK2 Manual

Full-text search on php-gtk2 manual

Members Login

Username:
Password:
Key:
What is this?
  Forget Password?