Sample Code 211: How to gray out an entry field?
Written by kksou   
Monday, 02 April 2007
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   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
56   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
<?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) {
  • 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
  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.

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved