Problem
You would like to set up a vertical volume control using GtkVScale as shown below:
Solution
- Create a vertical scale using GtkVScale::new_with_range (double min, double max, double step).
- Set GtkRange::set_inverted() to
true
so that the volume goes from soft at the bottom to loud at the top. - Register the signal format-value so that we can append the word "dB" in the callback function.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 21 22 23 24 25 30 32 33 36 37 38 39 40 44 47 48 49 | <?php $window = new GtkWindow(); $window->set_size_request(148, 240); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Set up Volume Control\n". "Part 1 - using GtkVScale"); $title->modify_font(new PangoFontDescription("Arial Narrow 8")); $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.5, 0, 0); $alignment->add($title); $vbox->pack_start($alignment, 0); $vol_adj = GtkVScale::new_with_range(-100, 80, 1); // note 1 $vbox->pack_start(new GtkLabel('Volume'), 0); $vbox->pack_start($vol_adj, 0); $vol_adj->set_size_request(10, 160); $vol_adj->set_inverted(1); // note 2 $vol_adj->connect('format-value', 'on_format_value'); // note 3 $window->show_all(); Gtk::main(); function on_format_value($scale, $value) { return $value."dB"; // note 4 } ?> |
Output
As shown above.
Explanation
- Create the vscale.
- Invert the scale so that the lower value is at the bottom.
- Register the signal
format-value
. - Append the word "dB" to the volume value.
Read more...