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 16 17 18 19 20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 53 54 55 56 57 | <?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'); $vbox->pack_start($button2 = new GtkButton( 'label in original default bg color'), 0); $button2->connect('clicked', 'on_click', 'org_color'); $window->show_all(); Gtk::main(); function on_click($button, $color) { global $eventbox; if ($color=='yellow') { $eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00')); } else { global $org_bg; $eventbox->modify_bg(Gtk::STATE_NORMAL, $org_bg); // note 3 } } ?> |
Output
As shown above.
Read more...