211. How to gray out an entry field?

Problem

Suppose you would like to gray out some of the entry fields to make them inactive as shown below:

How to gray out an entry field?


Solution


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   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
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   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

$vbox->pack_start(new Title("Gray out an entry field", 30), 0);

$entry[0] = new GtkEntry();
$entry[1] = new GtkEntry();

$radiogrp = new RadioGroup (
    array('Square'=>0,
        'Radius'=>1),
    $entry);
$vbox->pack_start($radiogrp, 0);
$vbox->pack_start(new GtkLabel(), 0);

$vbox->pack_start($hbox = new GtkHBox(), 0);
$hbox->pack_start(new GtkLabel('Please enter width: '), 0);
$hbox->pack_start($entry[0], 0);

$vbox->pack_start($hbox = new GtkHBox(), 0);
$hbox->pack_start(new GtkLabel('Please enter radius: '), 0);
$hbox->pack_start($entry[1], 0);

$window->show_all();
Gtk::main();

class SpinButton extends GtkSpinButton {
    public function SpinButton() {
        parent::__construct();
        $this->set_editable(0);
    }
}

class RadioGroup_base extends GtkVBox {

    public function RadioGroup_base($options) {
        parent::__construct();
        $radio0 = null;
        $i = 0;
        foreach($options as $label=>$value) {
            $radio = new GtkRadioButton($radio0, $label);
            if ($i==0) $radio0 = $radio;
            $radio->connect('toggled', array(&$this, 'on_toggle'), $value);
            $this->pack_start($radio, 0);
            ++$i;
        }
        $radio0->toggled();
    }

    public function on_toggle($radio, $value) {}

}

class RadioGroup extends RadioGroup_base {

    public function RadioGroup($options, $entry) {
        $this->entry = $entry;
        parent::__construct($options);
    }

    public function on_toggle($radio, $value) {
        $label = $radio->child->get_label();
        $active = $radio->get_active();
        if ($active) {
            echo "radio button pressed: $label (value = $value)\n";

            if ($value==0) {
                $this->entry[0]->set_editable(1); // note 1
                $this->entry[0]->set_sensitive(1); // note 1
                $this->entry[1]->set_editable(0); // note 1
                $this->entry[1]->set_sensitive(0); // note 1
            } else {
                $this->entry[0]->set_editable(0); // note 2
                $this->entry[0]->set_sensitive(0); // note 2
                $this->entry[1]->set_editable(1); // note 2
                $this->entry[1]->set_sensitive(1); // note 2
            }
        }
    }
}

class Title extends GtkVBox {
    function Title($str, $height=40, $width=-1) {
        parent::__construct();
        $title = new GtkLabel($str);
        $title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
        $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
        $title->set_size_request($width, $height);
        $title->set_justify(Gtk::JUSTIFY_CENTER);
        $alignment = new GtkAlignment(0.5, 0, 0, 0);
        $alignment->add($title);
        $this->pack_start($alignment, 0, 0);
        $this->pack_start(new GtkLabel(), 0, 0);
    }
}

?>

Output

As shown above.

 

Explanation

  1. User has selected square. So we turn on width and turn off radius.
  2. User has selected circle. So we turn off width and turn on radius.

Add comment


Security code
Refresh