Sample Code 42: How to display a popup alert for required fields - Part 2 - OK button centered? |
|
Written by kksou
|
|
Sunday, 01 October 2006 |
|
Problem You have displayed a popup alert box in Part 1.

However, note that the OK button is right aligned. You would like the OK button to be centered as shown below:

Solution The OK button can be centered by changing the layout style of the dialog action area with GtkButtonBox::set_layout(Gtk::BUTTONBOX_SPREAD)
Sample Code 1 2 3 4 5 6 7 8 9 10 11 12 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
| <?php $window = new GtkWindow(); $window->set_size_request(400, 150); $window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Display Alert - Part 2"); $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', 'on_activate');
// check user input
function on_activate($widget) { $input = $widget->get_text(); echo "name = $input\n"; if ($input=='') 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;
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
User reviews Average user ratings: 4.0 (from 4 users) Note: You have to be a registered member to leave a comment. Free registration here. |
February 08, 2007 2:39pm
Would be nice to have here the set_default_response technique shown
February 15, 2007 2:01am
As requested, here's an example on set_default_response:
How to set default button of GtkDialog - Part 1 - using set_default_response?
April 02, 2007 12:06am
while taking this coding to run,there is only entry only appear,button is disappear...
August 24, 2008 5:02am
nice code