Sample Code 195: How to set the background to original default color? |
|
Written by kksou
|
|
Sunday, 11 March 2007 |
|
Problem You have set up label 1 with a yellow background color as shown below:

You would like to set label 1 back to the original default background color at the click of a button as shown below. (Note: label 2 is for comparison.)

Solution
- We can get the original default background color before changing the color of label 1 with GtkWidget::get_style().
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->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,200); $window->add($vbox = new GtkVBox());
// display title
$title = new GtkLabel("Set Background to Original Default Color"); $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); $title->set_justify(Gtk::JUSTIFY_CENTER); $alignment = new GtkAlignment(0.5, 0, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0);
$window->realize(); // note 1
$org_bg = $window->get_style()->bg[Gtk::STATE_NORMAL]; // note 2
// create label 1 and 2
$label1 = new GtkLabel("This is label 1"); $eventbox = new GtkEventBox(); $eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00')); $eventbox->add($label1);
$hbox = new GtkHBox(); $hbox->set_size_request(400, 80); $hbox->pack_start($eventbox); $hbox->pack_start($label2 = new GtkLabel( "This is label 2\nin original\ndefault bg color")); $vbox->pack_start($hbox, 0);
// create buttons
$vbox->pack_start($button1 = new GtkButton('label in yellow'), 0); $button1->connect('clicked', 'on_click', 'yellow');
|
- 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
- Remember to realize the window before making a copy of the original background color. Try commenting this line out and you will find that you won't get the original color.
- Make a copy of the original background color.
- Set label 1 back to the original background color.
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |